soma-core 1.3.3 → 1.3.4
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 +7 -6
- package/app/SomaPageClient.tsx +2 -72
- package/bin/soma-lsp.cjs +1 -4
- package/core/build/compiler/full-manifest-builder.ts +17 -26
- package/core/build/compiler/html-include-expander.ts +56 -0
- package/core/build/compiler/page-assets.ts +26 -0
- package/core/build/compiler/page-declaration-compiler.ts +85 -0
- package/core/build/compiler/rollup-plugin-soma.ts +4 -19
- package/core/build/compiler/route-builder.ts +0 -2
- package/core/build/compiler/rspack-plugin-soma.js +1 -10
- package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -13
- package/core/build/compiler/soma-precompile.js +74 -0
- package/core/build/compiler/soma-precompile.ts +13 -0
- package/core/build/compiler/soma-rspack-yaml-loader.js +5 -11
- package/core/build/compiler/soma-webpack-loader.js +6 -21
- package/core/build/compiler/vite-plugin-soma.ts +9 -31
- package/core/build/component/pack-from-dir.ts +58 -0
- package/core/build/component/package-loader.ts +158 -12
- package/core/build/eject/plan-eject.ts +15 -21
- package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
- package/core/build/lsp/extra-completion.cjs +14 -8
- package/core/build/lsp/lsp-navigation.cjs +50 -43
- package/core/build/lsp/soma-declaration.cjs +85 -0
- package/core/build/lsp/ui-yaml-completion.cjs +98 -104
- package/core/build/v2/block-compile.ts +200 -0
- package/core/build/v2/block-loader.ts +33 -0
- package/core/build/v2/html-index.ts +120 -0
- package/core/build/v2/migrate-v1.ts +370 -0
- package/core/build/v2/soma-file-loader.ts +216 -0
- package/core/client/component/ai-catalog.ts +15 -0
- package/core/client/component/component-pack.ts +205 -31
- package/core/client/component/contract-compat.ts +132 -0
- package/core/client/component/example-v2.ts +132 -0
- package/core/client/engine/dom-renderer.ts +14 -8
- package/core/client/engine/reactive-proxy.ts +8 -7
- package/core/client/engine/soma-bootstrap.ts +23 -5
- package/core/client/parser/v2/namespace.ts +182 -0
- package/core/client/parser/v2/page-parser.ts +359 -0
- package/core/client/parser/v2/use-parser.ts +412 -0
- package/core/client/parser/value-lang.ts +340 -0
- package/core/client/pipes/pipe-registry.ts +1 -1
- package/core/client/registry/declaration-renderer.ts +17 -15
- package/core/client/theme/harmony.ts +307 -0
- package/core/client/theme/scene-tokens.ts +134 -0
- package/core/client/theme/theme-axes.ts +228 -0
- package/core/client/theme/theme-engine.ts +333 -73
- package/core/client/theme/tone-solver.ts +118 -0
- package/core/index.ts +74 -2
- package/core/server/host/server-page-assets.ts +33 -16
- package/core/server/host/soma-page-compile.ts +33 -0
- package/core/server/index.ts +15 -0
- package/core/server/registry/declaration-precompiler.ts +24 -54
- package/core/server/registry/page-registry-store.ts +38 -9
- package/core/server/registry/services/pages/Get.ts +19 -9
- package/core/server/registry/validation/compile-gate.ts +14 -31
- package/core/server/registry/validation/cross-check.ts +7 -17
- package/core/server/registry/validation/declaration-safety-scan.ts +2 -2
- package/core/shared/types/component-contract.types.ts +10 -0
- package/dist/core/index.cjs +2138 -154
- package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -4
- package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
- package/dist/types/build/compiler/page-assets.d.ts +3 -0
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
- package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -3
- package/dist/types/build/compiler/route-builder.d.ts +0 -1
- package/dist/types/build/compiler/soma-precompile.d.ts +8 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -10
- package/dist/types/build/component/pack-from-dir.d.ts +17 -0
- package/dist/types/build/component/package-loader.d.ts +33 -2
- package/dist/types/build/v2/block-compile.d.ts +28 -0
- package/dist/types/build/v2/block-loader.d.ts +9 -0
- package/dist/types/build/v2/html-index.d.ts +37 -0
- package/dist/types/build/v2/migrate-v1.d.ts +32 -0
- package/dist/types/build/v2/soma-file-loader.d.ts +45 -0
- package/dist/types/client/component/component-pack.d.ts +23 -3
- package/dist/types/client/component/contract-compat.d.ts +16 -0
- package/dist/types/client/component/example-v2.d.ts +9 -0
- package/dist/types/client/engine/dom-renderer.d.ts +2 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +3 -1
- package/dist/types/client/parser/v2/namespace.d.ts +52 -0
- package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
- package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
- package/dist/types/client/parser/value-lang.d.ts +63 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +1 -2
- package/dist/types/client/theme/harmony.d.ts +77 -0
- package/dist/types/client/theme/scene-tokens.d.ts +25 -0
- package/dist/types/client/theme/theme-axes.d.ts +81 -0
- package/dist/types/client/theme/theme-engine.d.ts +29 -1
- package/dist/types/client/theme/tone-solver.d.ts +47 -0
- package/dist/types/index.d.ts +13 -2
- package/dist/types/server/host/server-page-assets.d.ts +1 -4
- package/dist/types/server/host/soma-page-compile.d.ts +7 -0
- package/dist/types/server/index.d.ts +3 -1
- package/dist/types/server/registry/declaration-precompiler.d.ts +3 -3
- package/dist/types/server/registry/page-registry-store.d.ts +4 -4
- package/dist/types/server/registry/validation/compile-gate.d.ts +2 -0
- package/dist/types/server/registry/validation/cross-check.d.ts +4 -4
- package/dist/types/shared/types/component-contract.types.d.ts +10 -0
- package/editor/vscode/package.json +1 -24
- package/global.d.ts +1 -11
- package/next.config.js +32 -50
- package/package.json +3 -2
- package/tailwind.config.ts +0 -1
- package/core/build/lsp/event-yaml-completion.cjs +0 -106
- package/core/client/host-bridge/empty-host/data/README.md +0 -17
- package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
// v2 解析 · 组件块(REQ-005 F3)。
|
|
2
|
+
//
|
|
3
|
+
// 一个实例 = 一个块,块体是该组件的**全部**:data(三通道 + 字段字典)、
|
|
4
|
+
// ui(props / Row 体系)、event(actions 定义 + on 路由)。
|
|
5
|
+
// 不自包含就是耦合——复杂场景一来三处拼装,复杂度没有地方搬进业务组件。
|
|
6
|
+
//
|
|
7
|
+
// 产出仍是 v1 等价物:props 进 UiConfig.show 的 ExtendedShowValue.props,
|
|
8
|
+
// emit 路由进事件清单。soma-core 运行时对"块"一无所知,也不需要知道。
|
|
9
|
+
|
|
10
|
+
import type { ExtendedShowValue } from '../../../shared/types/ui-config.types';
|
|
11
|
+
import { SomaV2ParseError } from '../../../build/v2/soma-file-loader';
|
|
12
|
+
import {
|
|
13
|
+
buildBlockScope,
|
|
14
|
+
expandPath,
|
|
15
|
+
resolveApiRef,
|
|
16
|
+
type BlockScope,
|
|
17
|
+
} from './namespace';
|
|
18
|
+
|
|
19
|
+
const BLOCK_KEYS = new Set(['component', 'alias', 'when', 'data', 'ui', 'event']);
|
|
20
|
+
const DATA_OBJECT_KEYS = new Set(['from', 'save', 'refresh', 'reload_on', 'fields']);
|
|
21
|
+
const EVENT_KEYS = new Set(['actions', 'on']);
|
|
22
|
+
const ACTION_KEYS = new Set([
|
|
23
|
+
'label', 'handler', 'change', 'confirm', 'permission', 'tone', 'when', 'disabled_reason',
|
|
24
|
+
]);
|
|
25
|
+
const FIELD_KEYS = new Set(['name', 'type', 'values', 'permission', 'rules']);
|
|
26
|
+
const FIELD_TYPES = new Set([
|
|
27
|
+
'text', 'int', 'number', 'money', 'date', 'enum', 'bool', 'percent',
|
|
28
|
+
// progress 与 percent 同数据两种读法:前者出进度条,后者出文字(见 soma-ui 类型渲染表)
|
|
29
|
+
'progress',
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
export interface DataObjectDecl {
|
|
33
|
+
localName: string;
|
|
34
|
+
/** 全局身份:块id.名 */
|
|
35
|
+
fullName: string;
|
|
36
|
+
from?: { api: string; params: Record<string, string> };
|
|
37
|
+
save?: {
|
|
38
|
+
api: string; method: string; key?: string; trigger: string; fields?: string[];
|
|
39
|
+
};
|
|
40
|
+
refreshMs?: number;
|
|
41
|
+
reloadOn?: string[];
|
|
42
|
+
fields?: Record<string, FieldDecl>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface FieldDecl {
|
|
46
|
+
name?: string;
|
|
47
|
+
type: string;
|
|
48
|
+
values?: Record<string, string>;
|
|
49
|
+
permission: 'r' | 'w';
|
|
50
|
+
rules?: Array<Record<string, unknown>>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ActionDecl {
|
|
54
|
+
id: string;
|
|
55
|
+
label: string;
|
|
56
|
+
handler?: string;
|
|
57
|
+
change?: string;
|
|
58
|
+
confirm?: string;
|
|
59
|
+
permission?: string;
|
|
60
|
+
tone?: string;
|
|
61
|
+
when?: string;
|
|
62
|
+
disabled_reason?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface UseBlockAst {
|
|
66
|
+
blockId: string;
|
|
67
|
+
/** 声明的期望组件名(与 HTML 对账后的结果) */
|
|
68
|
+
component?: string;
|
|
69
|
+
/** 整块显隐 */
|
|
70
|
+
when?: string;
|
|
71
|
+
/** 数据对象(声明与引入) */
|
|
72
|
+
dataObjects: DataObjectDecl[];
|
|
73
|
+
dataImports: Array<{ localName: string; targetPath: string }>;
|
|
74
|
+
/** ui 段原文(Row 编译在 soma-ui 词汇层,core 只做路径还原与结构校验) */
|
|
75
|
+
uiRaw: Record<string, unknown>;
|
|
76
|
+
/** 动作定义表 */
|
|
77
|
+
actions: Map<string, ActionDecl>;
|
|
78
|
+
/** emit 路由:emit 名 → Pipeline 表达式 */
|
|
79
|
+
emitRoutes: Record<string, string>;
|
|
80
|
+
scope: BlockScope;
|
|
81
|
+
/** 供 v1 编译管线消费的 props 形态 */
|
|
82
|
+
toShowValue(): ExtendedShowValue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface ParseUseInput {
|
|
86
|
+
blockId: string;
|
|
87
|
+
body: Record<string, unknown>;
|
|
88
|
+
/** HTML 中该实例的组件名(对账用) */
|
|
89
|
+
htmlComponent?: string;
|
|
90
|
+
/** 该实例可见的行变量(由 HTML 位置决定) */
|
|
91
|
+
rowVars?: Iterable<string>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function parseUseBlock(input: ParseUseInput): UseBlockAst {
|
|
95
|
+
const { blockId, body } = input;
|
|
96
|
+
const fail = (message: string): never => {
|
|
97
|
+
throw new SomaV2ParseError(message, blockId);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
for (const key of Object.keys(body)) {
|
|
101
|
+
if (!BLOCK_KEYS.has(key)) {
|
|
102
|
+
fail(`组件块不支持键 "${key}"(可用:${[...BLOCK_KEYS].join('/')})`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ── component 对账:声明与 HTML 漂移即红 ────────────────────────────────
|
|
107
|
+
const component = body.component === undefined ? undefined : String(body.component);
|
|
108
|
+
if (component && input.htmlComponent && component !== input.htmlComponent) {
|
|
109
|
+
fail(
|
|
110
|
+
`component 声明为 "${component}",但 HTML 里是 "${input.htmlComponent}"`
|
|
111
|
+
+ '——两处必须一致(改了一处忘了另一处时,页面渲染的是哪个只能靠猜)',
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ── 作用域:data 名 + alias + 行变量 ────────────────────────────────────
|
|
116
|
+
const dataRaw = (body.data as Record<string, unknown>) ?? {};
|
|
117
|
+
if (typeof dataRaw !== 'object' || Array.isArray(dataRaw)) fail('data 段必须是映射');
|
|
118
|
+
|
|
119
|
+
const scope = buildBlockScope({
|
|
120
|
+
blockId,
|
|
121
|
+
dataNames: Object.keys(dataRaw),
|
|
122
|
+
aliasRaw: body.alias as Record<string, unknown> | undefined,
|
|
123
|
+
rowVars: input.rowVars,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// ── event 段:先解析(data.save.trigger 要校验动作存在) ────────────────
|
|
127
|
+
const { actions, emitRoutes } = parseEventSection(body.event, blockId, scope, fail);
|
|
128
|
+
|
|
129
|
+
// ── data 段:声明与引入 ─────────────────────────────────────────────────
|
|
130
|
+
const dataObjects: DataObjectDecl[] = [];
|
|
131
|
+
const dataImports: Array<{ localName: string; targetPath: string }> = [];
|
|
132
|
+
for (const [localName, raw] of Object.entries(dataRaw)) {
|
|
133
|
+
if (typeof raw === 'string') {
|
|
134
|
+
dataImports.push({ localName, targetPath: expandPath(raw, { ...scope, dataNames: new Set() }) });
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
138
|
+
fail(`data.${localName} 必须是映射(声明)或字符串路径(引入)`);
|
|
139
|
+
}
|
|
140
|
+
dataObjects.push(parseDataObject(localName, raw as Record<string, unknown>, blockId, scope, actions, fail));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ── ui 段 ──────────────────────────────────────────────────────────────
|
|
144
|
+
const uiRaw = (body.ui as Record<string, unknown>) ?? {};
|
|
145
|
+
if (typeof uiRaw !== 'object' || Array.isArray(uiRaw)) fail('ui 段必须是映射');
|
|
146
|
+
|
|
147
|
+
const when = body.when === undefined ? undefined : expandCondition(String(body.when), scope);
|
|
148
|
+
|
|
149
|
+
const ast: UseBlockAst = {
|
|
150
|
+
blockId,
|
|
151
|
+
...(component ? { component } : {}),
|
|
152
|
+
...(when ? { when } : {}),
|
|
153
|
+
dataObjects,
|
|
154
|
+
dataImports,
|
|
155
|
+
uiRaw: expandUiPaths(uiRaw, scope),
|
|
156
|
+
actions,
|
|
157
|
+
emitRoutes,
|
|
158
|
+
scope,
|
|
159
|
+
toShowValue(): ExtendedShowValue {
|
|
160
|
+
const value: ExtendedShowValue = { props: this.uiRaw };
|
|
161
|
+
if (this.when) value.when = this.when;
|
|
162
|
+
return value;
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
return ast;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function parseDataObject(
|
|
169
|
+
localName: string,
|
|
170
|
+
raw: Record<string, unknown>,
|
|
171
|
+
blockId: string,
|
|
172
|
+
scope: BlockScope,
|
|
173
|
+
actions: Map<string, ActionDecl>,
|
|
174
|
+
fail: (m: string) => never,
|
|
175
|
+
): DataObjectDecl {
|
|
176
|
+
for (const key of Object.keys(raw)) {
|
|
177
|
+
if (!DATA_OBJECT_KEYS.has(key)) {
|
|
178
|
+
fail(`data.${localName} 不支持键 "${key}"(可用:${[...DATA_OBJECT_KEYS].join('/')})`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const decl: DataObjectDecl = { localName, fullName: `${blockId}.${localName}` };
|
|
183
|
+
|
|
184
|
+
const from = raw.from as Record<string, unknown> | undefined;
|
|
185
|
+
if (from !== undefined) {
|
|
186
|
+
if (typeof from !== 'object' || Array.isArray(from)) fail(`data.${localName}.from 必须是映射`);
|
|
187
|
+
const api = from.api;
|
|
188
|
+
if (typeof api !== 'string' || api.trim() === '') fail(`data.${localName}.from 缺少 api`);
|
|
189
|
+
const params: Record<string, string> = {};
|
|
190
|
+
for (const [key, value] of Object.entries((from.params as Record<string, unknown>) ?? {})) {
|
|
191
|
+
params[key] = expandPath(String(value), scope);
|
|
192
|
+
}
|
|
193
|
+
decl.from = { api: resolveApiRef(String(api), scope), params };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const save = raw.save as Record<string, unknown> | undefined;
|
|
197
|
+
if (save !== undefined) {
|
|
198
|
+
if (typeof save !== 'object' || Array.isArray(save)) fail(`data.${localName}.save 必须是映射`);
|
|
199
|
+
const api = save.api;
|
|
200
|
+
const trigger = save.trigger;
|
|
201
|
+
if (typeof api !== 'string' || api.trim() === '') fail(`data.${localName}.save 缺少 api`);
|
|
202
|
+
if (typeof trigger !== 'string' || trigger.trim() === '') {
|
|
203
|
+
fail(`data.${localName}.save 缺少 trigger(由哪个动作触发回写)`);
|
|
204
|
+
}
|
|
205
|
+
if (!actions.has(String(trigger))) {
|
|
206
|
+
const known = [...actions.keys()].join('/') || '(本块未定义任何动作)';
|
|
207
|
+
fail(
|
|
208
|
+
`data.${localName}.save.trigger 指向未定义的动作 "${trigger}"`
|
|
209
|
+
+ `——请在 event.actions 里定义它(现有:${known})`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
decl.save = {
|
|
213
|
+
api: resolveApiRef(String(api), scope),
|
|
214
|
+
method: String(save.method ?? 'POST').toUpperCase(),
|
|
215
|
+
trigger: String(trigger),
|
|
216
|
+
...(save.key !== undefined ? { key: String(save.key) } : {}),
|
|
217
|
+
...(Array.isArray(save.fields) ? { fields: (save.fields as unknown[]).map(String) } : {}),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (raw.refresh !== undefined) decl.refreshMs = parseDuration(String(raw.refresh), localName, fail);
|
|
222
|
+
|
|
223
|
+
if (raw.reload_on !== undefined) {
|
|
224
|
+
if (!Array.isArray(raw.reload_on)) fail(`data.${localName}.reload_on 必须是数组`);
|
|
225
|
+
decl.reloadOn = (raw.reload_on as unknown[]).map((item) => expandPath(String(item), scope));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (raw.fields !== undefined) {
|
|
229
|
+
const fieldsRaw = raw.fields as Record<string, unknown>;
|
|
230
|
+
if (typeof fieldsRaw !== 'object' || Array.isArray(fieldsRaw)) {
|
|
231
|
+
fail(`data.${localName}.fields 必须是映射`);
|
|
232
|
+
}
|
|
233
|
+
const fields: Record<string, FieldDecl> = {};
|
|
234
|
+
for (const [fieldName, fieldRaw] of Object.entries(fieldsRaw)) {
|
|
235
|
+
fields[fieldName] = parseField(localName, fieldName, fieldRaw, fail);
|
|
236
|
+
}
|
|
237
|
+
decl.fields = fields;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return decl;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function parseField(
|
|
244
|
+
objectName: string,
|
|
245
|
+
fieldName: string,
|
|
246
|
+
raw: unknown,
|
|
247
|
+
fail: (m: string) => never,
|
|
248
|
+
): FieldDecl {
|
|
249
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
250
|
+
fail(`字段字典 ${objectName}.${fieldName} 必须是映射`);
|
|
251
|
+
}
|
|
252
|
+
const field = raw as Record<string, unknown>;
|
|
253
|
+
for (const key of Object.keys(field)) {
|
|
254
|
+
if (!FIELD_KEYS.has(key)) {
|
|
255
|
+
fail(`字段 ${objectName}.${fieldName} 不支持键 "${key}"(可用:${[...FIELD_KEYS].join('/')})`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const type = String(field.type ?? '');
|
|
259
|
+
if (!FIELD_TYPES.has(type)) {
|
|
260
|
+
fail(
|
|
261
|
+
`字段 ${objectName}.${fieldName} 的 type "${type}" 非法`
|
|
262
|
+
+ `(封闭集:${[...FIELD_TYPES].join('/')})`,
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
if (type === 'enum') {
|
|
266
|
+
const values = field.values;
|
|
267
|
+
if (values === null || typeof values !== 'object' || Array.isArray(values)
|
|
268
|
+
|| Object.keys(values as object).length === 0) {
|
|
269
|
+
fail(`字段 ${objectName}.${fieldName} 是 enum,必须提供非空 values 映射(它同时是翻译表与选项表)`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const permission = field.permission === undefined ? 'r' : String(field.permission);
|
|
273
|
+
if (permission !== 'r' && permission !== 'w') {
|
|
274
|
+
fail(`字段 ${objectName}.${fieldName} 的 permission 只能是 r 或 w`);
|
|
275
|
+
}
|
|
276
|
+
if (field.rules !== undefined && !Array.isArray(field.rules)) {
|
|
277
|
+
fail(`字段 ${objectName}.${fieldName} 的 rules 必须是数组`);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
type,
|
|
282
|
+
permission,
|
|
283
|
+
...(field.name !== undefined ? { name: String(field.name) } : {}),
|
|
284
|
+
...(field.values ? { values: field.values as Record<string, string> } : {}),
|
|
285
|
+
...(field.rules ? { rules: field.rules as Array<Record<string, unknown>> } : {}),
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** `30s` → 30000;单位封闭 ms/s/m,无单位报错(不猜) */
|
|
290
|
+
export function parseDuration(raw: string, where: string, fail: (m: string) => never): number {
|
|
291
|
+
const match = /^(\d+(?:\.\d+)?)(ms|s|m)$/.exec(raw.trim());
|
|
292
|
+
if (!match) {
|
|
293
|
+
fail(`${where} 的时长 "${raw}" 非法——形如 500ms / 30s / 5m(单位必填,不猜)`);
|
|
294
|
+
}
|
|
295
|
+
const value = Number(match![1]);
|
|
296
|
+
const unit = match![2];
|
|
297
|
+
return unit === 'ms' ? value : unit === 's' ? value * 1000 : value * 60000;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function parseEventSection(
|
|
301
|
+
raw: unknown,
|
|
302
|
+
blockId: string,
|
|
303
|
+
scope: BlockScope,
|
|
304
|
+
fail: (m: string) => never,
|
|
305
|
+
): { actions: Map<string, ActionDecl>; emitRoutes: Record<string, string> } {
|
|
306
|
+
const actions = new Map<string, ActionDecl>();
|
|
307
|
+
const emitRoutes: Record<string, string> = {};
|
|
308
|
+
if (raw === undefined) return { actions, emitRoutes };
|
|
309
|
+
|
|
310
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
311
|
+
fail('event 段必须是映射(子键 actions / on)');
|
|
312
|
+
}
|
|
313
|
+
const section = raw as Record<string, unknown>;
|
|
314
|
+
for (const key of Object.keys(section)) {
|
|
315
|
+
if (!EVENT_KEYS.has(key)) {
|
|
316
|
+
fail(`event 段不支持子键 "${key}"(可用:actions / on)——定义与路由分开住,不靠值形状猜`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
for (const [id, actionRaw] of Object.entries((section.actions as Record<string, unknown>) ?? {})) {
|
|
321
|
+
if (actionRaw === null || typeof actionRaw !== 'object' || Array.isArray(actionRaw)) {
|
|
322
|
+
fail(`event.actions.${id} 必须是映射`);
|
|
323
|
+
}
|
|
324
|
+
const action = actionRaw as Record<string, unknown>;
|
|
325
|
+
for (const key of Object.keys(action)) {
|
|
326
|
+
if (!ACTION_KEYS.has(key)) {
|
|
327
|
+
fail(`动作 ${id} 不支持字段 "${key}"(可用:${[...ACTION_KEYS].join('/')})`);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (action.label === undefined) fail(`动作 ${id} 缺少 label`);
|
|
331
|
+
actions.set(id, {
|
|
332
|
+
id,
|
|
333
|
+
label: String(action.label),
|
|
334
|
+
...(action.handler !== undefined ? { handler: withPageDefault(String(action.handler)) } : {}),
|
|
335
|
+
...(action.change !== undefined ? { change: withPageDefault(String(action.change)) } : {}),
|
|
336
|
+
...(action.confirm !== undefined ? { confirm: String(action.confirm) } : {}),
|
|
337
|
+
...(action.permission !== undefined ? { permission: String(action.permission) } : {}),
|
|
338
|
+
...(action.tone !== undefined ? { tone: String(action.tone) } : {}),
|
|
339
|
+
...(action.when !== undefined ? { when: expandCondition(String(action.when), scope) } : {}),
|
|
340
|
+
...(action.disabled_reason !== undefined
|
|
341
|
+
? { disabled_reason: expandPath(String(action.disabled_reason), scope) }
|
|
342
|
+
: {}),
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
for (const [emitName, expr] of Object.entries((section.on as Record<string, unknown>) ?? {})) {
|
|
347
|
+
if (emitName.includes('.')) {
|
|
348
|
+
fail(
|
|
349
|
+
`event.on 的键 "${emitName}" 含点号——组件块只订阅本实例的契约 emit(直接写 emit 名),`
|
|
350
|
+
+ 'DOM 事件写在 page 块的 event.on',
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
emitRoutes[emitName] = withPageDefaultInPipeline(String(expr));
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return { actions, emitRoutes };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** funcPath 不含点 → 自动补 page.(v1 onChange 既有先例) */
|
|
360
|
+
function withPageDefault(funcPath: string): string {
|
|
361
|
+
return funcPath.includes('.') ? funcPath : `page.${funcPath}`;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Pipeline 表达式里的处理器段同样享受免前缀 */
|
|
365
|
+
function withPageDefaultInPipeline(expr: string): string {
|
|
366
|
+
const parts = expr.split('|').map((part) => part.trim());
|
|
367
|
+
const verb = parts[0];
|
|
368
|
+
// UI_INTERACT 的第 2 槽是 DOM 目标,第 3 槽才是可选处理器;
|
|
369
|
+
// NAV_STATIC 只有路由目标,不存在处理器。不能把裸锚点误改成 page.<锚点>。
|
|
370
|
+
const handlerIndex = verb === 'UI_INTERACT'
|
|
371
|
+
? 2
|
|
372
|
+
: verb === 'NAV_STATIC'
|
|
373
|
+
? -1
|
|
374
|
+
: ['UI_REFRESH', 'NAV_ACTION'].includes(verb)
|
|
375
|
+
? 1
|
|
376
|
+
: 0;
|
|
377
|
+
if (handlerIndex >= 0 && parts[handlerIndex] && /^[A-Za-z_][A-Za-z0-9_]*$/.test(parts[handlerIndex])) {
|
|
378
|
+
parts[handlerIndex] = `page.${parts[handlerIndex]}`;
|
|
379
|
+
}
|
|
380
|
+
return parts.reduce(
|
|
381
|
+
(result, part, index) => index === 0 ? part : `${result} |${part ? ` ${part}` : ''}`,
|
|
382
|
+
'',
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function expandCondition(raw: string, scope: BlockScope): string {
|
|
387
|
+
const text = raw.trim();
|
|
388
|
+
const negated = text.startsWith('!');
|
|
389
|
+
const bare = negated ? text.slice(1).trim() : text;
|
|
390
|
+
return `${negated ? '!' : ''}${expandPath(bare, scope)}`;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** 递归把 ui 段里的绑定路径还原为全名(字面量与结构原样保留) */
|
|
394
|
+
function expandUiPaths(value: unknown, scope: BlockScope): Record<string, unknown> {
|
|
395
|
+
const walk = (node: unknown): unknown => {
|
|
396
|
+
if (typeof node === 'string') {
|
|
397
|
+
const segments = node.split(/\s+\|\s+/);
|
|
398
|
+
segments[0] = expandPath(segments[0], scope);
|
|
399
|
+
return segments.join(' | ');
|
|
400
|
+
}
|
|
401
|
+
if (Array.isArray(node)) return node.map(walk);
|
|
402
|
+
if (node !== null && typeof node === 'object') {
|
|
403
|
+
const out: Record<string, unknown> = {};
|
|
404
|
+
for (const [key, child] of Object.entries(node as Record<string, unknown>)) {
|
|
405
|
+
out[key] = walk(child);
|
|
406
|
+
}
|
|
407
|
+
return out;
|
|
408
|
+
}
|
|
409
|
+
return node;
|
|
410
|
+
};
|
|
411
|
+
return walk(value) as Record<string, unknown>;
|
|
412
|
+
}
|