/**
|
* 质量属性 ↔ 画布 attributes 的双向转换
|
* <p>
|
* 业务属性统一落在画布节点的 data-qc-<key> 上,画布数据(grapes)本身即自描述,
|
* 保存、回显、渲染都不需要再额外维护一份映射表。
|
*/
|
import type {
|
QualityComponentDefinition,
|
QualityProps,
|
} from './types';
|
|
import { isQualityPropAttr, propAttrName, propKeyFromAttr } from './attrs';
|
import { coerceFieldValue } from './validator';
|
|
/** 业务属性 → 画布 attributes(全部字符串化,DOM 属性只能是字符串) */
|
export function propsToAttributes(
|
definition: QualityComponentDefinition,
|
props: QualityProps,
|
): Record<string, string> {
|
const attributes: Record<string, string> = {};
|
definition.propertySchema.forEach((field) => {
|
const value = props[field.key];
|
if (value === undefined) {
|
return;
|
}
|
attributes[propAttrName(field.key)] = String(value);
|
});
|
return attributes;
|
}
|
|
/** 画布 attributes → 业务属性(按字段声明还原类型) */
|
export function attributesToProps(
|
definition: QualityComponentDefinition,
|
attributes: Record<string, string>,
|
): QualityProps {
|
const props: QualityProps = {};
|
definition.propertySchema.forEach((field) => {
|
const raw = attributes[propAttrName(field.key)];
|
if (raw === undefined) {
|
return;
|
}
|
props[field.key] = coerceFieldValue(field, raw);
|
});
|
return props;
|
}
|
|
/** 取出节点上所有业务属性(不认识定义时也能读,用于诊断与迁移) */
|
export function extractRawProps(attributes: Record<string, string>): QualityProps {
|
const props: QualityProps = {};
|
Object.entries(attributes).forEach(([name, value]) => {
|
if (isQualityPropAttr(name)) {
|
props[propKeyFromAttr(name)] = value;
|
}
|
});
|
return props;
|
}
|