soma-core 1.4.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/core/build/blocks/block-compile.ts +31 -2
- package/core/build/blocks/block-container.ts +2 -11
- package/core/build/compiler/page-declaration-compiler.ts +30 -2
- package/core/build/compiler/soma-precompile.js +2053 -23
- package/core/build/component/component-graph.ts +208 -0
- package/core/build/component/package-loader.ts +32 -0
- package/core/build/eject/component-eject.ts +473 -0
- package/core/build/eject/component-upgrade.ts +359 -0
- package/core/build/layout/constraint-solver.ts +235 -0
- package/core/build/layout/css-emitter.ts +116 -0
- package/core/build/layout/page-layout.ts +85 -0
- package/core/build/pack/pack-init.ts +270 -0
- package/core/client/base/SomaHttpClient.ts +34 -1
- package/core/client/base/SomaPageRootBase.ts +61 -0
- package/core/client/component/ai-catalog.ts +121 -4
- package/core/client/component/component-defaults.ts +100 -0
- package/core/client/component/component-definition.types.ts +39 -0
- package/core/client/component/component-manager.ts +79 -4
- package/core/client/component/component-pack.ts +126 -2
- package/core/client/component/keyboard-matrix.ts +144 -0
- package/core/client/component/pack-styles.ts +59 -1
- package/core/client/component/style-features.ts +131 -0
- package/core/client/component/theme-component-tokens.ts +83 -0
- package/core/client/component/verify-pack.ts +120 -1
- package/core/client/container/SomaContainerAdapter.ts +6 -2
- package/core/client/data/data-object-resolver.ts +407 -4
- package/core/client/data/reaction-engine.ts +176 -0
- package/core/client/data/window-engine.ts +477 -0
- package/core/client/engine/event-engine.ts +98 -0
- package/core/client/engine/soma-bootstrap.ts +265 -5
- package/core/client/engine/step-runner.ts +134 -0
- package/core/client/infra/http/SomaHttpService.ts +2 -2
- package/core/client/parser/blocks/page-parser.ts +15 -1
- package/core/client/parser/blocks/use-parser.ts +5 -12
- package/core/client/parser/data-config-parser.ts +316 -8
- package/core/client/parser/duration.ts +16 -0
- package/core/client/parser/layout-parser.ts +157 -0
- package/core/client/parser/page-state-parser.ts +166 -0
- package/core/client/parser/pipeline-parser.ts +76 -1
- package/core/client/parser/reaction-parser.ts +459 -0
- package/core/client/parser/reaction-sugar.ts +47 -0
- package/core/client/parser/step-parser.ts +359 -0
- package/core/client/parser/ui-config-parser.ts +59 -0
- package/core/client/parser/window-parser.ts +294 -0
- package/core/client/theme/theme-engine.ts +102 -2
- package/core/index.ts +184 -0
- package/core/shared/block-errors.ts +11 -0
- package/core/shared/calc/coerce.ts +29 -0
- package/core/shared/calc/errors.ts +43 -0
- package/core/shared/calc/evaluator.ts +101 -0
- package/core/shared/calc/index.ts +12 -0
- package/core/shared/calc/lexer.ts +194 -0
- package/core/shared/calc/parser.ts +204 -0
- package/core/shared/calc/typer.ts +184 -0
- package/core/shared/component/derive-sample.ts +105 -0
- package/core/shared/component/field-lines.ts +473 -0
- package/core/shared/path-algebra.ts +169 -0
- package/core/shared/predicates/index.ts +527 -0
- package/core/shared/types/component-contract.types.ts +151 -9
- package/core/shared/types/data-config.types.ts +120 -1
- package/core/shared/types/domain.types.ts +13 -0
- package/core/shared/types/event.types.ts +22 -2
- package/core/shared/types/layout-constraint.types.ts +73 -0
- package/core/shared/types/page-state.types.ts +60 -0
- package/core/shared/types/reaction.types.ts +144 -0
- package/core/shared/types/step.types.ts +91 -0
- package/core/shared/types/ui-config.types.ts +18 -0
- package/core/shared/types/window.types.ts +177 -0
- package/dist/cli/soma.js +13 -1
- package/dist/core/index.cjs +5529 -631
- package/dist/types/build/blocks/block-container.d.ts +1 -6
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -0
- package/dist/types/build/component/component-graph.d.ts +85 -0
- package/dist/types/build/eject/component-eject.d.ts +121 -0
- package/dist/types/build/eject/component-upgrade.d.ts +43 -0
- package/dist/types/build/layout/constraint-solver.d.ts +36 -0
- package/dist/types/build/layout/css-emitter.d.ts +18 -0
- package/dist/types/build/layout/page-layout.d.ts +24 -0
- package/dist/types/build/pack/pack-init.d.ts +8 -0
- package/dist/types/client/base/SomaHttpClient.d.ts +8 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +28 -0
- package/dist/types/client/component/ai-catalog.d.ts +33 -1
- package/dist/types/client/component/component-defaults.d.ts +15 -0
- package/dist/types/client/component/component-definition.types.d.ts +38 -0
- package/dist/types/client/component/component-manager.d.ts +36 -0
- package/dist/types/client/component/component-pack.d.ts +32 -2
- package/dist/types/client/component/keyboard-matrix.d.ts +24 -0
- package/dist/types/client/component/pack-styles.d.ts +21 -0
- package/dist/types/client/component/style-features.d.ts +26 -0
- package/dist/types/client/component/theme-component-tokens.d.ts +14 -0
- package/dist/types/client/component/verify-pack.d.ts +10 -0
- package/dist/types/client/data/data-object-resolver.d.ts +106 -1
- package/dist/types/client/data/reaction-engine.d.ts +21 -0
- package/dist/types/client/data/window-engine.d.ts +27 -0
- package/dist/types/client/engine/event-engine.d.ts +15 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +70 -0
- package/dist/types/client/engine/step-runner.d.ts +24 -0
- package/dist/types/client/parser/blocks/use-parser.d.ts +2 -2
- package/dist/types/client/parser/duration.d.ts +1 -0
- package/dist/types/client/parser/layout-parser.d.ts +9 -0
- package/dist/types/client/parser/page-state-parser.d.ts +32 -0
- package/dist/types/client/parser/reaction-parser.d.ts +16 -0
- package/dist/types/client/parser/reaction-sugar.d.ts +13 -0
- package/dist/types/client/parser/step-parser.d.ts +23 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +5 -0
- package/dist/types/client/parser/window-parser.d.ts +25 -0
- package/dist/types/client/theme/theme-engine.d.ts +26 -0
- package/dist/types/index.d.ts +35 -2
- package/dist/types/shared/block-errors.d.ts +5 -0
- package/dist/types/shared/calc/coerce.d.ts +4 -0
- package/dist/types/shared/calc/errors.d.ts +14 -0
- package/dist/types/shared/calc/evaluator.d.ts +2 -0
- package/dist/types/shared/calc/index.d.ts +6 -0
- package/dist/types/shared/calc/lexer.d.ts +20 -0
- package/dist/types/shared/calc/parser.d.ts +49 -0
- package/dist/types/shared/calc/typer.d.ts +16 -0
- package/dist/types/shared/component/derive-sample.d.ts +23 -0
- package/dist/types/shared/component/field-lines.d.ts +19 -0
- package/dist/types/shared/path-algebra.d.ts +56 -0
- package/dist/types/shared/predicates/index.d.ts +79 -0
- package/dist/types/shared/types/component-contract.types.d.ts +134 -6
- package/dist/types/shared/types/data-config.types.d.ts +115 -1
- package/dist/types/shared/types/domain.types.d.ts +12 -0
- package/dist/types/shared/types/event.types.d.ts +8 -2
- package/dist/types/shared/types/layout-constraint.types.d.ts +52 -0
- package/dist/types/shared/types/page-state.types.d.ts +49 -0
- package/dist/types/shared/types/reaction.types.d.ts +115 -0
- package/dist/types/shared/types/step.types.d.ts +76 -0
- package/dist/types/shared/types/ui-config.types.d.ts +18 -0
- package/dist/types/shared/types/window.types.d.ts +171 -0
- package/package.json +3 -2
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// calc 类型推导(REQ-010 S1a)——本层是 calc 存在的最大理由。
|
|
2
|
+
//
|
|
3
|
+
// JsonLogic 的 `var` 路径是自由字符串、Formily 的表达式在 `new Function` 里跑、
|
|
4
|
+
// amis 的 `${}` 沿运行时数据链求值——三家都**无法在声明期知道一个表达式算出来是什么类型**,
|
|
5
|
+
// 于是 `[amount] - [note]`(金额减备注)这种错只会在页面上显示成 `NaN`,
|
|
6
|
+
// 而且显示 NaN 的那一刻离写错的地方已经隔了很远。
|
|
7
|
+
//
|
|
8
|
+
// SOMA 的字段类型是字典里的封闭枚举,所以这件事可以在解析期做完:
|
|
9
|
+
// 每个节点推出类型,与字段声明的 `type` 对账,不配就当场点名到列号。
|
|
10
|
+
//
|
|
11
|
+
// ── 一条刻意的收紧 ────────────────────────────────────────────────────
|
|
12
|
+
// `date - date → number`(毫秒差)是有用的(算时长),但 `date + date` 无意义,
|
|
13
|
+
// `date * 2` 更无意义——所以日期的算术只开了「差」与「加减一个数」两扇门。
|
|
14
|
+
|
|
15
|
+
import { SomaCalcTypeError } from './errors';
|
|
16
|
+
import type { CalcAst } from './parser';
|
|
17
|
+
|
|
18
|
+
export type CalcType = 'number' | 'text' | 'bool' | 'date';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 字段类型 → calc 类型。
|
|
22
|
+
*
|
|
23
|
+
* 表里没有的字段类型**不可被引用**:`richtext` 引用进算式没有意义(它是一段 HTML),
|
|
24
|
+
* `image / file` 是资源标识,`tags / tree / cascade` 是集合或路径。
|
|
25
|
+
* 与其让它们悄悄按 text 参与拼接,不如直接拒绝并让作者换个字段。
|
|
26
|
+
*/
|
|
27
|
+
const FIELD_TO_CALC: Readonly<Record<string, CalcType>> = {
|
|
28
|
+
int: 'number', float: 'number', money: 'number', percent: 'number', progress: 'number',
|
|
29
|
+
text: 'text', url: 'text', enum: 'text',
|
|
30
|
+
bool: 'bool',
|
|
31
|
+
date: 'date', datetime: 'date',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** calc 类型 → 允许声明的字段类型(用于结果与 `type` 对账) */
|
|
35
|
+
const CALC_TO_FIELD: Readonly<Record<CalcType, readonly string[]>> = {
|
|
36
|
+
number: ['int', 'float', 'money', 'percent', 'progress'],
|
|
37
|
+
text: ['text', 'url', 'enum'],
|
|
38
|
+
bool: ['bool'],
|
|
39
|
+
date: ['date', 'datetime'],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export interface CalcTypeContext {
|
|
43
|
+
/** 字段名 → 字典里的 `type`(缺省视为 text,与字典本身的缺省一致) */
|
|
44
|
+
fieldTypes: Readonly<Record<string, string | undefined>>;
|
|
45
|
+
/** 原始表达式串,仅用于错误定位 */
|
|
46
|
+
source: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function inferCalcType(ast: CalcAst, ctx: CalcTypeContext): CalcType {
|
|
50
|
+
return infer(ast, ctx);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function infer(node: CalcAst, ctx: CalcTypeContext): CalcType {
|
|
54
|
+
const bad: (message: string, expected: string, actual: string) => never = (message, expected, actual) => {
|
|
55
|
+
throw new SomaCalcTypeError(message, ctx.source, node.pos, expected, actual);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
switch (node.k) {
|
|
59
|
+
case 'num': return 'number';
|
|
60
|
+
case 'str': return 'text';
|
|
61
|
+
case 'bool': return 'bool';
|
|
62
|
+
case 'date': return 'date';
|
|
63
|
+
|
|
64
|
+
case 'ref': {
|
|
65
|
+
const declared = ctx.fieldTypes[node.name];
|
|
66
|
+
if (declared === undefined && !(node.name in ctx.fieldTypes)) {
|
|
67
|
+
bad(`引用了不存在的字段 "${node.name}"`, '字典里的字段', '未声明');
|
|
68
|
+
}
|
|
69
|
+
const kind = FIELD_TO_CALC[declared ?? 'text'];
|
|
70
|
+
if (!kind) {
|
|
71
|
+
bad(
|
|
72
|
+
`字段 "${node.name}" 是 ${declared} 类型,不能参与计算`
|
|
73
|
+
+ `(可引用的类型:${Object.keys(FIELD_TO_CALC).join(' / ')})`,
|
|
74
|
+
'可计算的字段类型', String(declared),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return kind;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
case 'un': {
|
|
81
|
+
const inner = infer(node.arg, ctx);
|
|
82
|
+
if (inner !== 'number') bad('取负只能作用在数值上', 'number', inner);
|
|
83
|
+
return 'number';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
case 'bin': {
|
|
87
|
+
const left = infer(node.left, ctx);
|
|
88
|
+
const right = infer(node.right, ctx);
|
|
89
|
+
switch (node.op) {
|
|
90
|
+
case '+': case '-': {
|
|
91
|
+
if (left === 'number' && right === 'number') return 'number';
|
|
92
|
+
if (node.op === '-' && left === 'date' && right === 'date') return 'number'; // 毫秒差
|
|
93
|
+
if (left === 'date' && right === 'number') return 'date'; // 日期 ± 天数(毫秒)
|
|
94
|
+
if (node.op === '+' && left === 'number' && right === 'date') return 'date';
|
|
95
|
+
// 建议要对得上运算符:对减法说「请用 CONCAT」是句错话,
|
|
96
|
+
// 作者照做只会得到第二个错误。只有 + 才可能是「想拼接」写错了。
|
|
97
|
+
if (node.op === '+' && (left === 'text' || right === 'text')) {
|
|
98
|
+
bad('文本不能用 + 拼接——请用 CONCAT(a, b)', 'number 或 date', `${left} 与 ${right}`);
|
|
99
|
+
}
|
|
100
|
+
return bad(`${node.op} 不能作用在 ${left} 与 ${right} 上`, 'number', `${left} 与 ${right}`);
|
|
101
|
+
}
|
|
102
|
+
case '*': case '/': {
|
|
103
|
+
if (left === 'number' && right === 'number') return 'number';
|
|
104
|
+
return bad(`${node.op} 只能作用在数值上`, 'number', `${left} 与 ${right}`);
|
|
105
|
+
}
|
|
106
|
+
case '>': case '>=': case '<': case '<=': {
|
|
107
|
+
if (left === right && (left === 'number' || left === 'date')) return 'bool';
|
|
108
|
+
return bad('比较大小只能在两个数值之间或两个日期之间', '同为 number 或同为 date', `${left} 与 ${right}`);
|
|
109
|
+
}
|
|
110
|
+
case '==': case '!=': {
|
|
111
|
+
if (left === right) return 'bool';
|
|
112
|
+
return bad('相等比较的两侧必须同型', left, right);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
case 'call': {
|
|
118
|
+
switch (node.fn) {
|
|
119
|
+
case 'IF': {
|
|
120
|
+
const cond = infer(node.args[0], ctx);
|
|
121
|
+
if (cond !== 'bool') bad('IF 的第一个参数必须是判断(比较或布尔字段)', 'bool', cond);
|
|
122
|
+
const a = infer(node.args[1], ctx);
|
|
123
|
+
const b = infer(node.args[2], ctx);
|
|
124
|
+
if (a !== b) bad('IF 的两个分支必须同型', a, b);
|
|
125
|
+
return a;
|
|
126
|
+
}
|
|
127
|
+
case 'MIN': case 'MAX': {
|
|
128
|
+
const kinds = node.args.map((arg) => infer(arg, ctx));
|
|
129
|
+
const first = kinds[0];
|
|
130
|
+
if (first !== 'number' && first !== 'date') {
|
|
131
|
+
bad(`${node.fn} 只能作用在数值或日期上`, 'number 或 date', first);
|
|
132
|
+
}
|
|
133
|
+
const off = kinds.find((kind) => kind !== first);
|
|
134
|
+
if (off) bad(`${node.fn} 的参数必须同型`, first, off);
|
|
135
|
+
return first;
|
|
136
|
+
}
|
|
137
|
+
case 'ROUND': {
|
|
138
|
+
const value = infer(node.args[0], ctx);
|
|
139
|
+
if (value !== 'number') bad('ROUND 只能作用在数值上', 'number', value);
|
|
140
|
+
return 'number';
|
|
141
|
+
}
|
|
142
|
+
case 'ABS': {
|
|
143
|
+
const value = infer(node.args[0], ctx);
|
|
144
|
+
if (value !== 'number') bad('ABS 只能作用在数值上', 'number', value);
|
|
145
|
+
return 'number';
|
|
146
|
+
}
|
|
147
|
+
case 'CONCAT': {
|
|
148
|
+
node.args.forEach((arg) => infer(arg, ctx)); // 任意型,逐个求值即可(错在里面照样点名)
|
|
149
|
+
return 'text';
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 与字段声明的 `type` 对账。
|
|
158
|
+
*
|
|
159
|
+
* 单独一个函数而不是塞进 `infer`:解析器可能被用在「只想知道类型」的场合
|
|
160
|
+
* (如 DW 的派生列预览),那时还没有目标字段。
|
|
161
|
+
*/
|
|
162
|
+
export function assertCalcResultType(
|
|
163
|
+
result: CalcType,
|
|
164
|
+
declaredFieldType: string | undefined,
|
|
165
|
+
ctx: CalcTypeContext,
|
|
166
|
+
ast: CalcAst,
|
|
167
|
+
): void {
|
|
168
|
+
const declared = declaredFieldType ?? 'text';
|
|
169
|
+
const allowed = CALC_TO_FIELD[result];
|
|
170
|
+
if (!allowed.includes(declared)) {
|
|
171
|
+
throw new SomaCalcTypeError(
|
|
172
|
+
`表达式算出来是 ${result},但字段声明的是 ${declared}`
|
|
173
|
+
+ `(${result} 可以声明为:${allowed.join(' / ')})`,
|
|
174
|
+
ctx.source, ast.pos, allowed.join('|'), declared,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
if (result === 'bool') {
|
|
178
|
+
throw new SomaCalcTypeError(
|
|
179
|
+
'派生列不能是判断结果——字段类型里没有「显示 true」这种业务语义;'
|
|
180
|
+
+ '想按条件出不同文案请用 IF(条件, "A", "B")',
|
|
181
|
+
ctx.source, ast.pos, 'number|text|date', 'bool',
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// 样张派生(REQ-012):契约字段的 example → 一份可直接渲染的 props。
|
|
2
|
+
//
|
|
3
|
+
// 为什么是派生而不是另写一份样张:另写一份就有两个真源,改了 prop 忘了改样张不会
|
|
4
|
+
// 报错——展示页与视觉矩阵会安静地拿旧形状渲染。示例值挂在字段上,样张是它的函数。
|
|
5
|
+
//
|
|
6
|
+
// 派生规则(对每个字段,递归):
|
|
7
|
+
// 1. example 有值 → 用它
|
|
8
|
+
// 2. type object → 逐 fields 递归
|
|
9
|
+
// 3. type array → 用 items 派生一个元素,组成单元素数组
|
|
10
|
+
// 4. 否则取 default
|
|
11
|
+
// 5. required 却以上都无 → 记一条缺口(由闸门报,不在这里抛)
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
SomaComponentContract,
|
|
15
|
+
SomaContractField,
|
|
16
|
+
} from '../types/component-contract.types';
|
|
17
|
+
|
|
18
|
+
export interface SampleGap {
|
|
19
|
+
/** 缺口路径,如 `order.items[].sku_id` */
|
|
20
|
+
path: string;
|
|
21
|
+
reason: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface DeriveSampleResult {
|
|
25
|
+
/** 可直接喂给组件的 props */
|
|
26
|
+
props: Record<string, unknown>;
|
|
27
|
+
/** 必填却既无 example 也无 default 的字段 */
|
|
28
|
+
gaps: SampleGap[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function deriveField(field: SomaContractField, path: string, gaps: SampleGap[]): unknown {
|
|
32
|
+
if (field.example !== undefined) return clone(field.example);
|
|
33
|
+
|
|
34
|
+
if (field.type === 'object') {
|
|
35
|
+
const fields = field.fields;
|
|
36
|
+
if (fields && Object.keys(fields).length > 0) {
|
|
37
|
+
const out: Record<string, unknown> = {};
|
|
38
|
+
for (const [name, child] of Object.entries(fields)) {
|
|
39
|
+
const value = deriveField(child, `${path}.${name}`, gaps);
|
|
40
|
+
if (value !== undefined) out[name] = value;
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (field.type === 'array') {
|
|
47
|
+
const items = field.items;
|
|
48
|
+
if (items) {
|
|
49
|
+
const element = deriveField(items, `${path}[]`, gaps);
|
|
50
|
+
if (element !== undefined) return [element];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (field.default !== undefined) return clone(field.default);
|
|
55
|
+
|
|
56
|
+
if (field.type === 'enum' && field.values && field.values.length > 0) {
|
|
57
|
+
// 枚举没写示例也没写默认时取第一个取值:比空串更接近真实形状,且不用作者多写一行
|
|
58
|
+
return field.values[0];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (field.required === true) {
|
|
62
|
+
gaps.push({
|
|
63
|
+
path,
|
|
64
|
+
reason: '必填字段既没有 示例 也没有 默认——样张渲染不出这一格',
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function clone<T>(value: T): T {
|
|
71
|
+
if (value === null || typeof value !== 'object') return value;
|
|
72
|
+
return JSON.parse(JSON.stringify(value)) as T;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 从契约派生一份样张 props */
|
|
76
|
+
export function deriveContractSample(contract: SomaComponentContract): DeriveSampleResult {
|
|
77
|
+
const gaps: SampleGap[] = [];
|
|
78
|
+
const props: Record<string, unknown> = {};
|
|
79
|
+
for (const [name, prop] of Object.entries(contract.props ?? {})) {
|
|
80
|
+
const value = deriveField(prop, name, gaps);
|
|
81
|
+
if (value !== undefined) props[name] = value;
|
|
82
|
+
}
|
|
83
|
+
return { props, gaps };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 单个字段的样张值(组件内部或工具按需调用) */
|
|
87
|
+
export function deriveFieldSample(field: SomaContractField): unknown {
|
|
88
|
+
return deriveField(field, '', []);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 这份契约够不够渲染一张有内容的样张。
|
|
93
|
+
*
|
|
94
|
+
* 判据是「至少有一个 prop 写了 example,或有嵌套字段写了 example」——
|
|
95
|
+
* 全靠 default 派生出来的是空态,展示页放上去只有一个空壳。
|
|
96
|
+
*/
|
|
97
|
+
export function hasRenderableSample(contract: SomaComponentContract): boolean {
|
|
98
|
+
const seen = (field: SomaContractField): boolean => {
|
|
99
|
+
if (field.example !== undefined) return true;
|
|
100
|
+
if (field.fields && Object.values(field.fields).some(seen)) return true;
|
|
101
|
+
if (field.items && seen(field.items)) return true;
|
|
102
|
+
return false;
|
|
103
|
+
};
|
|
104
|
+
return Object.values(contract.props ?? {}).some(seen);
|
|
105
|
+
}
|