11 小时以前 35722562e9e13f0504acc15b740d042ecb810199
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * 质量属性 ↔ 画布 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;
}