detail-echo.js 1.25 KB
/**
 * 详情回显 ACL — 解析 jsonContent 为 initialValues,与模版 schema 分离
 * @see packageDynamicForm/docs/ARCHITECTURE.md
 */
import { parseJsonContentDetailValues } from './submit-adapter.js'

/** 从详情解析引擎 initialValues(不写入 schema.list) */
export function parseDetailEchoValues(detail, schemaList = [], fallbackValues = {}) {
	if (!detail?.jsonContent || !schemaList?.length) {
		return fallbackValues || {}
	}
	return parseJsonContentDetailValues(detail.jsonContent, schemaList)
}

/** 剥离 schema 字段上的 value,保持模版定义 */
export function stripValuesFromSchemaRoot(schemaRoot = {}) {
	const next = JSON.parse(JSON.stringify(schemaRoot))
	function stripList(list = []) {
		return (list || []).map(field => {
			const copy = { ...field }
			delete copy.value
			if (copy.type === 'subform' && Array.isArray(copy.children)) {
				copy.children = copy.children.map(row =>
					Array.isArray(row) ? row.map(stripField) : row
				)
			}
			if (copy.type === 'group' && Array.isArray(copy.children)) {
				copy.children = copy.children.map(stripField)
			}
			return copy
		})
	}
	function stripField(field) {
		const copy = { ...field }
		delete copy.value
		return copy
	}
	next.list = stripList(next.list)
	return next
}