soma-core 1.4.2 → 1.5.0
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/bin/component.cjs +240 -0
- package/bin/eject.cjs +174 -0
- package/bin/migrate.cjs +362 -0
- package/core/build/blocks/block-compile.ts +143 -17
- package/core/build/blocks/migrate-flat.ts +118 -25
- package/core/build/compiler/page-declaration-compiler.ts +149 -5
- package/core/build/compiler/route-builder.ts +0 -4
- package/core/build/compiler/soma-precompile.js +431 -26
- package/core/build/component/component-graph.ts +28 -105
- package/core/build/component/package-loader.ts +18 -4
- package/core/build/eject/component-eject.ts +8 -2
- package/core/build/layout/constraint-solver.ts +131 -16
- package/core/build/layout/css-emitter.ts +15 -9
- package/core/build/ooui/artifact/artifact.types.ts +275 -0
- package/core/build/ooui/artifact/emit-artifact.ts +269 -0
- package/core/build/ooui/artifact/emit-html.ts +204 -0
- package/core/build/ooui/artifact/index.ts +53 -0
- package/core/build/ooui/artifact/spatial-tree.ts +268 -0
- package/core/build/ooui/artifact/utility-class.ts +83 -0
- package/core/build/ooui/composition/candidate.types.ts +323 -0
- package/core/build/ooui/composition/control-model.ts +114 -0
- package/core/build/ooui/composition/registry.ts +520 -0
- package/core/build/ooui/composition/select.ts +232 -0
- package/core/build/ooui/diagnostics/codes.ts +230 -0
- package/core/build/ooui/diagnostics/error.ts +235 -0
- package/core/build/ooui/diagnostics/format.ts +106 -0
- package/core/build/ooui/diagnostics/index.ts +62 -0
- package/core/build/ooui/diagnostics/issue.types.ts +112 -0
- package/core/build/ooui/engine-slot.ts +184 -0
- package/core/build/ooui/index.ts +1427 -0
- package/core/build/ooui/intent/derive-intent.ts +450 -0
- package/core/build/ooui/intent/field-rules.ts +330 -0
- package/core/build/ooui/intent/intent.types.ts +476 -0
- package/core/build/ooui/intent/verb-events.ts +285 -0
- package/core/build/ooui/page/budget.ts +503 -0
- package/core/build/ooui/page/compose-page.ts +736 -0
- package/core/build/ooui/page/index.ts +66 -0
- package/core/build/ooui/page/page.types.ts +402 -0
- package/core/build/ooui/page/region-row.ts +241 -0
- package/core/build/ooui/semantic/expand-page-surface.ts +180 -0
- package/core/build/ooui/semantic/kernel.types.ts +187 -0
- package/core/build/ooui/semantic/normalize.ts +784 -0
- package/core/build/ooui/semantic/origin.types.ts +211 -0
- package/core/build/ooui/semantic/role-alias.ts +154 -0
- package/core/build/ooui/semantic/shorthand.ts +233 -0
- package/core/build/ooui/semantic/surface.types.ts +78 -0
- package/core/build/ooui/semantic/validate-request.ts +267 -0
- package/core/build/ooui/spatial/emit-utility.ts +480 -0
- package/core/build/ooui/spatial/ir.types.ts +443 -0
- package/core/build/ooui/spatial/registry.ts +565 -0
- package/core/build/ooui/spatial/solve.ts +769 -0
- package/core/build/ooui/spatial/utility.types.ts +242 -0
- package/core/build/ooui/trace/index.ts +40 -0
- package/core/build/ooui/trace/render.ts +190 -0
- package/core/build/ooui/trace/source.types.ts +69 -0
- package/core/build/ooui/trace/trace.types.ts +326 -0
- package/core/client/component/example-blocks.ts +1 -7
- package/core/client/component/theme-component-tokens.ts +12 -15
- package/core/client/data/data-object-resolver.ts +27 -8
- package/core/client/diagnostics/telemetry-context.ts +0 -4
- package/core/client/engine/asset-resolver.ts +0 -1
- package/core/client/engine/event-engine.ts +7 -3
- package/core/client/engine/soma-bootstrap.ts +292 -34
- package/core/client/engine/style-binder.ts +0 -1
- package/core/client/infra/http/IHttpClient.ts +0 -6
- package/core/client/infra/http/SomaHttpService.ts +4 -1
- package/core/client/infra/notify/DomNotifyService.ts +1 -1
- package/core/client/infra/router/router-path.ts +0 -6
- package/core/client/parser/annotation-extractor.ts +1 -1
- package/core/client/parser/blocks/namespace.ts +30 -5
- package/core/client/parser/blocks/page-parser.ts +44 -1
- package/core/client/parser/blocks/use-parser.ts +266 -46
- package/core/client/parser/data-config-parser.ts +71 -3
- package/core/client/parser/layout-parser.ts +4 -3
- package/core/client/parser/reaction-parser.ts +0 -1
- package/core/client/parser/ui-config-parser.ts +212 -74
- package/core/client/theme/scene-tokens.ts +9 -3
- package/core/client/theme/theme-axes.ts +74 -16
- package/core/client/theme/theme-engine.ts +280 -69
- package/core/client/theme/tone-solver.ts +9 -8
- package/core/index.ts +18 -4
- package/core/server/api/load-host-service.ts +8 -2
- package/core/server/declaration-safety.ts +11 -0
- package/core/server/host/page-response-request.ts +0 -2
- package/core/server/host/published-page-artifact-cache.ts +244 -0
- package/core/server/host/published-page-assets.ts +44 -5
- package/core/server/mock/node/cli.mjs +0 -1
- package/core/server/mock/node/schema-generator.mjs +0 -1
- package/core/server/ooui.ts +34 -0
- package/core/server/page-assets.ts +8 -0
- package/core/server/page-declaration.ts +18 -0
- package/core/server/registry/asset-store.ts +327 -0
- package/core/server/registry/declaration-precompiler.ts +49 -6
- package/core/server/registry/index.ts +47 -1
- package/core/server/registry/page-registry-repository.ts +6 -2
- package/core/server/registry/page-registry-store.ts +133 -19
- package/core/server/registry/publish-asset-pin.ts +557 -0
- package/core/server/registry/services/pages/List.ts +15 -3
- package/core/server/registry/validation/compile-gate.ts +180 -8
- package/core/server/registry/validation/declaration-safety-scan.ts +113 -23
- package/core/shared/component/derive-sample.ts +201 -16
- package/core/shared/component/field-lines.ts +98 -8
- package/core/shared/container-breakpoints.ts +46 -0
- package/core/shared/object-semantics.ts +240 -0
- package/core/shared/ooui-declaration.ts +235 -0
- package/core/shared/types/component-contract.types.ts +7 -6
- package/core/shared/types/data-config.types.ts +95 -0
- package/core/shared/types/domain.types.ts +0 -24
- package/core/shared/types/engine.types.ts +0 -5
- package/core/shared/types/layout-constraint.types.ts +16 -0
- package/core/shared/types/ui-config.types.ts +163 -13
- package/dist/cli/soma.js +24 -11
- package/dist/core/index.cjs +1563 -800
- package/dist/types/build/blocks/block-compile.d.ts +16 -1
- package/dist/types/build/blocks/migrate-flat.d.ts +17 -0
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +46 -0
- package/dist/types/build/compiler/route-builder.d.ts +0 -1
- package/dist/types/build/component/component-graph.d.ts +17 -53
- package/dist/types/build/component/package-loader.d.ts +8 -1
- package/dist/types/build/layout/constraint-solver.d.ts +46 -4
- package/dist/types/build/ooui/artifact/artifact.types.d.ts +180 -0
- package/dist/types/build/ooui/artifact/emit-artifact.d.ts +58 -0
- package/dist/types/build/ooui/artifact/emit-html.d.ts +53 -0
- package/dist/types/build/ooui/artifact/index.d.ts +5 -0
- package/dist/types/build/ooui/artifact/spatial-tree.d.ts +48 -0
- package/dist/types/build/ooui/artifact/utility-class.d.ts +46 -0
- package/dist/types/build/ooui/composition/candidate.types.d.ts +206 -0
- package/dist/types/build/ooui/composition/control-model.d.ts +37 -0
- package/dist/types/build/ooui/composition/registry.d.ts +66 -0
- package/dist/types/build/ooui/composition/select.d.ts +63 -0
- package/dist/types/build/ooui/diagnostics/codes.d.ts +28 -0
- package/dist/types/build/ooui/diagnostics/error.d.ts +135 -0
- package/dist/types/build/ooui/diagnostics/format.d.ts +51 -0
- package/dist/types/build/ooui/diagnostics/index.d.ts +25 -0
- package/dist/types/build/ooui/diagnostics/issue.types.d.ts +56 -0
- package/dist/types/build/ooui/engine-slot.d.ts +41 -0
- package/dist/types/build/ooui/index.d.ts +286 -0
- package/dist/types/build/ooui/intent/derive-intent.d.ts +32 -0
- package/dist/types/build/ooui/intent/field-rules.d.ts +99 -0
- package/dist/types/build/ooui/intent/intent.types.d.ts +318 -0
- package/dist/types/build/ooui/intent/verb-events.d.ts +70 -0
- package/dist/types/build/ooui/page/budget.d.ts +28 -0
- package/dist/types/build/ooui/page/compose-page.d.ts +41 -0
- package/dist/types/build/ooui/page/index.d.ts +4 -0
- package/dist/types/build/ooui/page/page.types.d.ts +264 -0
- package/dist/types/build/ooui/page/region-row.d.ts +16 -0
- package/dist/types/build/ooui/semantic/expand-page-surface.d.ts +33 -0
- package/dist/types/build/ooui/semantic/kernel.types.d.ts +125 -0
- package/dist/types/build/ooui/semantic/normalize.d.ts +45 -0
- package/dist/types/build/ooui/semantic/origin.types.d.ts +95 -0
- package/dist/types/build/ooui/semantic/role-alias.d.ts +35 -0
- package/dist/types/build/ooui/semantic/shorthand.d.ts +60 -0
- package/dist/types/build/ooui/semantic/surface.types.d.ts +16 -0
- package/dist/types/build/ooui/semantic/validate-request.d.ts +19 -0
- package/dist/types/build/ooui/spatial/emit-utility.d.ts +43 -0
- package/dist/types/build/ooui/spatial/ir.types.d.ts +280 -0
- package/dist/types/build/ooui/spatial/registry.d.ts +110 -0
- package/dist/types/build/ooui/spatial/solve.d.ts +42 -0
- package/dist/types/build/ooui/spatial/utility.types.d.ts +191 -0
- package/dist/types/build/ooui/trace/index.d.ts +3 -0
- package/dist/types/build/ooui/trace/render.d.ts +42 -0
- package/dist/types/build/ooui/trace/source.types.d.ts +25 -0
- package/dist/types/build/ooui/trace/trace.types.d.ts +143 -0
- package/dist/types/client/component/example-blocks.d.ts +0 -2
- package/dist/types/client/component/theme-component-tokens.d.ts +8 -5
- package/dist/types/client/data/data-object-resolver.d.ts +10 -2
- package/dist/types/client/diagnostics/telemetry-context.d.ts +0 -1
- package/dist/types/client/engine/soma-bootstrap.d.ts +123 -10
- package/dist/types/client/infra/http/IHttpClient.d.ts +0 -5
- package/dist/types/client/infra/http/SomaHttpService.d.ts +1 -1
- package/dist/types/client/infra/router/router-path.d.ts +0 -2
- package/dist/types/client/parser/blocks/namespace.d.ts +20 -0
- package/dist/types/client/parser/blocks/page-parser.d.ts +9 -0
- package/dist/types/client/parser/blocks/use-parser.d.ts +38 -4
- package/dist/types/client/parser/data-config-parser.d.ts +13 -0
- package/dist/types/client/parser/ui-config-parser.d.ts +1 -11
- package/dist/types/client/theme/scene-tokens.d.ts +9 -3
- package/dist/types/client/theme/theme-axes.d.ts +64 -1
- package/dist/types/client/theme/theme-engine.d.ts +13 -8
- package/dist/types/client/theme/tone-solver.d.ts +9 -5
- package/dist/types/index.d.ts +4 -5
- package/dist/types/server/declaration-safety.d.ts +7 -0
- package/dist/types/server/host/page-response-request.d.ts +0 -1
- package/dist/types/server/host/published-page-artifact-cache.d.ts +64 -0
- package/dist/types/server/ooui.d.ts +18 -0
- package/dist/types/server/page-assets.d.ts +7 -0
- package/dist/types/server/page-declaration.d.ts +14 -0
- package/dist/types/server/registry/asset-store.d.ts +112 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +23 -3
- package/dist/types/server/registry/index.d.ts +3 -1
- package/dist/types/server/registry/page-registry-store.d.ts +57 -4
- package/dist/types/server/registry/publish-asset-pin.d.ts +166 -0
- package/dist/types/server/registry/services/pages/List.d.ts +9 -3
- package/dist/types/server/registry/validation/compile-gate.d.ts +63 -0
- package/dist/types/shared/component/derive-sample.d.ts +14 -0
- package/dist/types/shared/container-breakpoints.d.ts +42 -0
- package/dist/types/shared/object-semantics.d.ts +42 -0
- package/dist/types/shared/ooui-declaration.d.ts +135 -0
- package/dist/types/shared/types/component-contract.types.d.ts +7 -6
- package/dist/types/shared/types/data-config.types.d.ts +86 -0
- package/dist/types/shared/types/domain.types.d.ts +0 -21
- package/dist/types/shared/types/engine.types.d.ts +0 -4
- package/dist/types/shared/types/layout-constraint.types.d.ts +1 -0
- package/dist/types/shared/types/ui-config.types.d.ts +157 -12
- package/next.config.js +106 -13
- package/package.json +28 -2
- package/core/client/host-bridge/index.ts +0 -27
- package/core/client/parser/yaml-loader.ts +0 -36
- package/core/shared/manifest/manifest-contract.ts +0 -12
- package/dist/types/client/host-bridge/index.d.ts +0 -9
- package/dist/types/client/parser/yaml-loader.d.ts +0 -21
- package/dist/types/shared/manifest/manifest-contract.d.ts +0 -4
|
@@ -235,9 +235,56 @@ function parseType(raw: string, line: number): ParsedType {
|
|
|
235
235
|
|
|
236
236
|
// ── 字段行 ─────────────────────────────────────────────────────────────────
|
|
237
237
|
|
|
238
|
-
/**
|
|
238
|
+
/**
|
|
239
|
+
* 值是否在本段内自洽:引号开头要以同一种引号收尾;括号开头要配对归零(引号里的括号不算)。
|
|
240
|
+
* 裸值(不以引号或括号开头)恒自洽。
|
|
241
|
+
*/
|
|
242
|
+
function isValueClosed(value: string): boolean {
|
|
243
|
+
const text = value.trim();
|
|
244
|
+
const head = text[0];
|
|
245
|
+
if (head === '"' || head === "'") return text.length >= 2 && text.endsWith(head);
|
|
246
|
+
if (head !== '[' && head !== '{') return true;
|
|
247
|
+
let depth = 0;
|
|
248
|
+
let quote: string | null = null;
|
|
249
|
+
for (const ch of text) {
|
|
250
|
+
if (quote) {
|
|
251
|
+
if (ch === quote) quote = null;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (ch === '"' || ch === "'") quote = ch;
|
|
255
|
+
else if (ch === '[' || ch === '{') depth += 1;
|
|
256
|
+
else if (ch === ']' || ch === '}') depth -= 1;
|
|
257
|
+
}
|
|
258
|
+
return depth <= 0 && quote === null;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* 切分 `类型, 描述, 修饰…`;`示例 ` 之后整段归示例(可含逗号冒号空格)。
|
|
263
|
+
*
|
|
264
|
+
* `默认 ` 的值以引号或括号开头时,一直取到它配对闭合为止——值里的 `, ` 不切
|
|
265
|
+
* (`默认 "甲, 乙"`、`默认 [a, b]`)。渲染端(`renderFieldLines`)正是靠这一条让
|
|
266
|
+
* 默认值原样往返;取到行尾都闭合不了的,按原样切,不猜。
|
|
267
|
+
* 这一步排在找 `示例` 之前:默认值里恰好含 `, 示例 ` 时,它仍归默认值。
|
|
268
|
+
*/
|
|
239
269
|
function splitSegments(rest: string, line: number): string[] {
|
|
240
|
-
const
|
|
270
|
+
const raw = rest.split(', ');
|
|
271
|
+
const parts: string[] = [];
|
|
272
|
+
for (let index = 0; index < raw.length; index += 1) {
|
|
273
|
+
let part = raw[index];
|
|
274
|
+
const trimmed = part.trimStart();
|
|
275
|
+
if (trimmed.startsWith('默认 ') && !isValueClosed(trimmed.slice('默认 '.length))) {
|
|
276
|
+
let merged = part;
|
|
277
|
+
for (let next = index + 1; next < raw.length; next += 1) {
|
|
278
|
+
merged = `${merged}, ${raw[next]}`;
|
|
279
|
+
if (isValueClosed(merged.trimStart().slice('默认 '.length))) {
|
|
280
|
+
part = merged;
|
|
281
|
+
index = next;
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
parts.push(part);
|
|
287
|
+
}
|
|
241
288
|
const exampleAt = parts.findIndex((part) => part.startsWith('示例 ') || part === '示例');
|
|
242
289
|
if (exampleAt < 0) return parts.map((part) => part.trim());
|
|
243
290
|
const head = parts.slice(0, exampleAt).map((part) => part.trim());
|
|
@@ -375,15 +422,56 @@ export function parseSlotLines(text: string): Record<string, SomaContractSlot> {
|
|
|
375
422
|
// ── 反向渲染:契约 → 字段行原文 ────────────────────────────────────────────
|
|
376
423
|
//
|
|
377
424
|
// 目录直接打印这段文本,不再另生成一张字段表:作者写的和读者读到的是同一份东西。
|
|
425
|
+
// AI 目录的 `spec` 也是这段文本——所以它必须能**原样解析回来**(render → parse 往返),
|
|
426
|
+
// 否则字符串 "12" 印成 `示例 12`,读的人(和模型)会把它当数字。
|
|
427
|
+
//
|
|
428
|
+
// 值文法没有转义,往返靠「需要时加引号」。需要的情形:
|
|
429
|
+
// · 会被读成别的类型:空串、`12` / `-3.5`、`true` / `false`、`null` / `~`;
|
|
430
|
+
// · 以引号或括号开头(会被读成引号串或流式集合);首尾带空白(解析时会被 trim);
|
|
431
|
+
// · `默认` 值里含 ASCII 的 `, `(分段按它切;引号里的由 splitSegments 合回来);
|
|
432
|
+
// · 流式集合的元素里含 `,` `[` `]` `{` `}` 或引号(splitFlow 按它们切分与配对)。
|
|
433
|
+
// `示例` 不必为 `, ` 加引号:它排在最后、吞掉整行余下部分。
|
|
434
|
+
// 引号取值里没有的那一种(先双后单);两种都有时尽力而为——这套文法表达不了。
|
|
435
|
+
|
|
436
|
+
/** 值所在的位置:决定哪些字符会打乱解析 */
|
|
437
|
+
type ValueSite = 'default' | 'example' | 'flow';
|
|
438
|
+
|
|
439
|
+
/** 裸写会被读成数字 / 布尔 / null 的串(与 parseValue 的判据同一套) */
|
|
440
|
+
const SCALAR_LOOKALIKE = /^(?:null|~|true|false|-?\d+(?:\.\d+)?)$/;
|
|
441
|
+
|
|
442
|
+
/** 在流式集合里有语法含义的字符(splitFlow 的切分与配对依据) */
|
|
443
|
+
const FLOW_SYNTAX = /[,[\]{}"']/;
|
|
444
|
+
|
|
445
|
+
function quoteText(text: string): string {
|
|
446
|
+
const mark = !text.includes('"') ? '"' : !text.includes("'") ? "'" : '"';
|
|
447
|
+
return `${mark}${text}${mark}`;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function renderString(text: string, site: ValueSite): string {
|
|
451
|
+
const needsQuote = text === ''
|
|
452
|
+
|| text !== text.trim()
|
|
453
|
+
|| SCALAR_LOOKALIKE.test(text)
|
|
454
|
+
|| /^["'[{]/.test(text)
|
|
455
|
+
|| (site === 'default' && text.includes(', '))
|
|
456
|
+
|| (site === 'flow' && FLOW_SYNTAX.test(text));
|
|
457
|
+
return needsQuote ? quoteText(text) : text;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** 流式对象的键:键恒为字符串,只在会打乱切分时加引号(键里带 `:` 这套文法表达不了) */
|
|
461
|
+
function renderFlowKey(key: string): string {
|
|
462
|
+
return key === '' || key !== key.trim() || FLOW_SYNTAX.test(key) ? quoteText(key) : key;
|
|
463
|
+
}
|
|
378
464
|
|
|
379
|
-
function renderValue(value: unknown): string {
|
|
465
|
+
function renderValue(value: unknown, site: ValueSite): string {
|
|
380
466
|
if (value === null) return 'null';
|
|
381
|
-
if (typeof value === 'string') return value
|
|
382
|
-
if (Array.isArray(value)) return `[${value.map(renderValue).join(', ')}]`;
|
|
467
|
+
if (typeof value === 'string') return renderString(value, site);
|
|
468
|
+
if (Array.isArray(value)) return `[${value.map((item) => renderValue(item, 'flow')).join(', ')}]`;
|
|
383
469
|
if (typeof value === 'object') {
|
|
384
470
|
const entries = Object.entries(value as Record<string, unknown>);
|
|
385
471
|
if (entries.length === 0) return '{}';
|
|
386
|
-
return `{ ${entries
|
|
472
|
+
return `{ ${entries
|
|
473
|
+
.map(([key, item]) => `${renderFlowKey(key)}: ${renderValue(item, 'flow')}`)
|
|
474
|
+
.join(', ')} }`;
|
|
387
475
|
}
|
|
388
476
|
return String(value);
|
|
389
477
|
}
|
|
@@ -401,9 +489,11 @@ function renderFieldNode(name: string, field: SomaContractField, depth: number,
|
|
|
401
489
|
const segments: string[] = [renderTypeLabel(field)];
|
|
402
490
|
if (field.desc) segments.push(field.desc);
|
|
403
491
|
if (field.required === true) segments.push('必填');
|
|
404
|
-
else if (field.default !== undefined) segments.push(`默认 ${renderValue(field.default)}`);
|
|
405
|
-
if (field.example !== undefined) segments.push(`示例 ${renderValue(field.example)}`);
|
|
492
|
+
else if (field.default !== undefined) segments.push(`默认 ${renderValue(field.default, 'default')}`);
|
|
406
493
|
if ((field as SomaContractProp).mode === 'model') segments.push('双向');
|
|
494
|
+
// 示例必须排最后:它从 `示例 ` 起吞掉整行余下部分——曾经 `双向` 排在它后面,
|
|
495
|
+
// 往返一次就变成示例值的尾巴(`示例 甲, 双向` → 示例 "甲, 双向"、双向丢失)。
|
|
496
|
+
if (field.example !== undefined) segments.push(`示例 ${renderValue(field.example, 'example')}`);
|
|
407
497
|
|
|
408
498
|
out.push(`${' '.repeat(depth * INDENT_UNIT)}${name}: ${segments.join(', ')}`);
|
|
409
499
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 容器断点:全体系唯一真源。
|
|
3
|
+
*
|
|
4
|
+
* ── 为什么单独一个模块 ─────────────────────────────────────────────
|
|
5
|
+
* 此前同一组值有**三份**副本:构建期求解器的元组表、soma-ui 的对象表、
|
|
6
|
+
* 视觉矩阵脚本里的硬编码数组。三处靠注释"同源同值"与人工同步维系,
|
|
7
|
+
* 而它们坏起来是**零报错**的——改一处漏一处,表现为构建期求解出的
|
|
8
|
+
* CSS 与组件 CSS 在不同宽度上折断,只有把窗口拖到那一档才看得见。
|
|
9
|
+
*
|
|
10
|
+
* ── 为什么不直接从求解器导出 ───────────────────────────────────────
|
|
11
|
+
* `build/layout/constraint-solver.ts` 带着 `SomaLayoutConstraintError`
|
|
12
|
+
* 与整个求解算法。soma-ui 若 import 它,等于把**构建期求解器拖进客户端
|
|
13
|
+
* 依赖图**,并会撞 `verify:client-boundary` / `verify:client-dep-graph`
|
|
14
|
+
* 的 build-only 规则。
|
|
15
|
+
*
|
|
16
|
+
* 因此本模块是**纯字面量**:不 import 任何东西,也不牵连任何运行时资产,
|
|
17
|
+
* 可以安全地被 soma-ui、生成链与构建期求解器同时消费——与
|
|
18
|
+
* `"./adaptive-vocabulary"` 子路径同一形态。
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 容器断点八档(rem;16px 根字号下为 320/384/448/576/640/768/896/1024px)。
|
|
23
|
+
*
|
|
24
|
+
* 键序即档序,从窄到宽。`CONTAINER_STEPS` 依赖这个顺序。
|
|
25
|
+
*/
|
|
26
|
+
export const CONTAINER_BREAKPOINTS = {
|
|
27
|
+
cqXs: 20,
|
|
28
|
+
cqSm: 24,
|
|
29
|
+
cqMd: 28,
|
|
30
|
+
cqLg: 36,
|
|
31
|
+
cqXl: 40,
|
|
32
|
+
cq2xl: 48,
|
|
33
|
+
cq3xl: 56,
|
|
34
|
+
cq4xl: 64,
|
|
35
|
+
} as const;
|
|
36
|
+
|
|
37
|
+
export type ContainerBreakpoint = keyof typeof CONTAINER_BREAKPOINTS;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 元组形态:构建期求解器按档序遍历时用(`solveLayout` 逐档各解一次,
|
|
41
|
+
* `css-emitter` 按 index 取 rem 值发 `@container (min-width: Nrem)`)。
|
|
42
|
+
*
|
|
43
|
+
* **由上表派生,不另写一份**——这正是本模块要消灭的那种双写。
|
|
44
|
+
*/
|
|
45
|
+
export const CONTAINER_STEPS: ReadonlyArray<readonly [ContainerBreakpoint, number]> =
|
|
46
|
+
Object.entries(CONTAINER_BREAKPOINTS) as ReadonlyArray<readonly [ContainerBreakpoint, number]>;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// core/shared/object-semantics.ts — 对象级语义(roles / relations / modifies)的**唯一**校验实现。
|
|
2
|
+
//
|
|
3
|
+
// ── 为什么这三样的失败模式是静默的 ─────────────────────────────────
|
|
4
|
+
// 这三样都不影响取数、不影响渲染、不抛异常,坏掉的只是"这一页看起来对不对":
|
|
5
|
+
//
|
|
6
|
+
// · `roles.title` 指向一个不存在的字段 → 标题栏空白。页面照常出来,
|
|
7
|
+
// 接口照常返回 200,控制台干净。表现是"这页没标题",而这句话在验收时
|
|
8
|
+
// 多半被当成"设计就这样"。
|
|
9
|
+
// · `relations.to` 指向一个没声明的对象 → `navigate` / `compare` 拿不到
|
|
10
|
+
// 目标对象的字典,演绎出来的是一张**空的**关联区,而不是一条报错。
|
|
11
|
+
// · `modifies` 指向不存在的字段(或自己指自己) → 修饰字段没有被并入,
|
|
12
|
+
// 于是它自己占一格,页面上多出一排空的 `status_tone: —`(§15.3 缺口 1)。
|
|
13
|
+
//
|
|
14
|
+
// 三条的共同点是**都在"能跑"的那一侧**。所以它们必须在声明期被拦住,
|
|
15
|
+
// 而不是留到"有人觉得这页怪怪的"那天。
|
|
16
|
+
//
|
|
17
|
+
// ── 为什么这份校验住在 shared,而不是各写一遍 ──────────────────────
|
|
18
|
+
// SOMA 有两条书写路径:块声明(`core/client/parser/blocks/use-parser.ts`)
|
|
19
|
+
// 与平铺 `_data.yaml`(`core/client/parser/data-config-parser.ts`)。
|
|
20
|
+
// 两条路径各写一遍同一个判据,是本仓库已经付过学费的错法——
|
|
21
|
+
// 字段类型表曾经被手抄成四份,结果 `type: url` 在一条路上合法、在另一条路上被拒收,
|
|
22
|
+
// 且"哪一条对"没有答案(见 `types/data-config.types.ts` 头注释)。
|
|
23
|
+
// 判据只准有一份,两个解析器各自把自己的错误类套在返回的 issue 上。
|
|
24
|
+
//
|
|
25
|
+
// 返回 `string[]` 而不是抛:照 `validateNamedPredicates` 的先例——
|
|
26
|
+
// 让调用方决定措辞、定位路径与错误类(`SomaDataConfigError` vs `SomaBlockParseError`)。
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
SOMA_DATA_OBJECT_ROLES,
|
|
30
|
+
SOMA_DATA_RELATION_CARDINALITIES,
|
|
31
|
+
type SomaDataObjectRelation,
|
|
32
|
+
type SomaDataObjectRoles,
|
|
33
|
+
} from './types/data-config.types';
|
|
34
|
+
|
|
35
|
+
/** 对象名 / 关系名 / 字段名的合法标识符(与 data-config-parser 同一条) */
|
|
36
|
+
const IDENTIFIER = /^[a-zA-Z_]\w*$/;
|
|
37
|
+
|
|
38
|
+
/** relations 条目的封闭键集 */
|
|
39
|
+
const RELATION_KEYS: readonly string[] = ['name', 'to', 'cardinality', 'via'];
|
|
40
|
+
|
|
41
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
42
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function fieldList(fieldKeys: readonly string[]): string {
|
|
46
|
+
return fieldKeys.length > 0 ? fieldKeys.join(' / ') : '(空)';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 校验 `roles:` 段。返回空数组即合法。
|
|
51
|
+
*
|
|
52
|
+
* `fieldKeys` 是本对象字段字典的键。**字典为空时直接拒收整段**:
|
|
53
|
+
* 角色的值就是字段名,没有字典就没有任何东西能对账,
|
|
54
|
+
* 放行等于把"指错字段"这类错推迟到渲染期,而渲染期它只会表现为一处空白。
|
|
55
|
+
*/
|
|
56
|
+
export function validateObjectRoles(
|
|
57
|
+
raw: unknown,
|
|
58
|
+
fieldKeys: readonly string[],
|
|
59
|
+
): string[] {
|
|
60
|
+
if (raw === undefined || raw === null) return [];
|
|
61
|
+
if (!isPlainObject(raw)) {
|
|
62
|
+
return ['roles 必须是映射(角色名 → 字段名),如 `roles: { title: no, status: state }`'];
|
|
63
|
+
}
|
|
64
|
+
const entries = Object.entries(raw);
|
|
65
|
+
if (entries.length === 0) {
|
|
66
|
+
return ['roles 是空映射——不声明与声明成空是两回事,删掉整段或至少给一个角色'];
|
|
67
|
+
}
|
|
68
|
+
if (fieldKeys.length === 0) {
|
|
69
|
+
return [
|
|
70
|
+
'roles 的值是字段名,但本对象没有声明 fields,任何角色都无从对账'
|
|
71
|
+
+ '——请先补 `fields:` 字段字典,再声明 roles',
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const issues: string[] = [];
|
|
76
|
+
const known = SOMA_DATA_OBJECT_ROLES as readonly string[];
|
|
77
|
+
for (const [role, target] of entries) {
|
|
78
|
+
if (!known.includes(role)) {
|
|
79
|
+
issues.push(
|
|
80
|
+
`未知的角色 "${role}"(封闭集:${known.join(' / ')})`
|
|
81
|
+
+ '——角色表是封闭集,加一个角色是一次体系决策,不在声明里临时扩',
|
|
82
|
+
);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (typeof target !== 'string' || target.trim() === '') {
|
|
86
|
+
issues.push(`角色 "${role}" 的值必须是非空字段名,当前值: ${JSON.stringify(target)}`);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (!fieldKeys.includes(target)) {
|
|
90
|
+
issues.push(
|
|
91
|
+
`角色 "${role}" 指向字段 "${target}",但字典里没有它(本对象字典:${fieldList(fieldKeys)})`
|
|
92
|
+
+ '——指错的后果是那一位静默留空,页面照常渲染',
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return issues;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** `validateObjectRelations` 的上下文 */
|
|
100
|
+
export interface ObjectRelationsContext {
|
|
101
|
+
/** 本对象字段字典的键(校验 `via`) */
|
|
102
|
+
fieldKeys: readonly string[];
|
|
103
|
+
/**
|
|
104
|
+
* 本次声明里全部已知的对象名(校验 `to`)。
|
|
105
|
+
*
|
|
106
|
+
* 传 `undefined` 表示**调用方拿不到这份名单**(块声明路径就是如此:块内私有对象
|
|
107
|
+
* 的全局身份要等 block-compile 合并之后才成形)。这时跳过 `to` 的存在性检查,
|
|
108
|
+
* 但**仍校验它是不是合法标识符**——放行未知名字与放行乱码是两件事。
|
|
109
|
+
*/
|
|
110
|
+
objectNames?: readonly string[];
|
|
111
|
+
/** 本对象名(`to` 指向自己是合法的:树形自关联) */
|
|
112
|
+
selfName?: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 校验 `relations:` 段。返回空数组即合法。
|
|
117
|
+
*/
|
|
118
|
+
export function validateObjectRelations(
|
|
119
|
+
raw: unknown,
|
|
120
|
+
ctx: ObjectRelationsContext,
|
|
121
|
+
): string[] {
|
|
122
|
+
if (raw === undefined || raw === null) return [];
|
|
123
|
+
if (!Array.isArray(raw)) {
|
|
124
|
+
return [
|
|
125
|
+
'relations 必须是数组,如 '
|
|
126
|
+
+ '`relations: [{ name: items, to: order_item, cardinality: many, via: order_no }]`',
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
if (raw.length === 0) {
|
|
130
|
+
return ['relations 是空数组——不声明与声明成空是两回事,删掉整段或至少给一条关系'];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const issues: string[] = [];
|
|
134
|
+
const seen = new Set<string>();
|
|
135
|
+
const cardinalities = SOMA_DATA_RELATION_CARDINALITIES as readonly string[];
|
|
136
|
+
|
|
137
|
+
raw.forEach((entry, index) => {
|
|
138
|
+
const at = `relations[${index}]`;
|
|
139
|
+
if (!isPlainObject(entry)) {
|
|
140
|
+
issues.push(`${at} 必须是对象(name / to / cardinality / via)`);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
for (const key of Object.keys(entry)) {
|
|
144
|
+
if (!RELATION_KEYS.includes(key)) {
|
|
145
|
+
issues.push(`${at} 不支持键 "${key}"(可用:${RELATION_KEYS.join(' / ')})`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const { name, to, cardinality, via } = entry as Partial<SomaDataObjectRelation>;
|
|
150
|
+
|
|
151
|
+
if (typeof name !== 'string' || !IDENTIFIER.test(name)) {
|
|
152
|
+
issues.push(`${at}.name 必须是标识符(字母/下划线开头),当前值: ${JSON.stringify(name)}`);
|
|
153
|
+
} else if (seen.has(name)) {
|
|
154
|
+
issues.push(
|
|
155
|
+
`${at}.name "${name}" 与前面的关系重名`
|
|
156
|
+
+ '——`through` 按名字引用关系,重名等于让它指向两个地方',
|
|
157
|
+
);
|
|
158
|
+
} else {
|
|
159
|
+
seen.add(name);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (typeof to !== 'string' || !IDENTIFIER.test(to)) {
|
|
163
|
+
issues.push(`${at}.to 必须是数据对象名(标识符),当前值: ${JSON.stringify(to)}`);
|
|
164
|
+
} else if (ctx.objectNames !== undefined && !ctx.objectNames.includes(to)) {
|
|
165
|
+
issues.push(
|
|
166
|
+
`${at}.to 指向未声明的数据对象 "${to}"`
|
|
167
|
+
+ `(本次声明里有:${ctx.objectNames.length > 0 ? ctx.objectNames.join(' / ') : '(空)'})`
|
|
168
|
+
+ '——把目标对象也声明进 data,或删掉这条关系;'
|
|
169
|
+
+ '留着它会让 navigate / compare 取不到目标字典,演绎出一片空白而不是报错',
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (typeof cardinality !== 'string' || !cardinalities.includes(cardinality)) {
|
|
174
|
+
issues.push(
|
|
175
|
+
`${at}.cardinality 必须是 ${cardinalities.join(' / ')},当前值: ${JSON.stringify(cardinality)}`
|
|
176
|
+
+ '——基数决定的是"演绎成一格还是一列",猜不得',
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (via !== undefined) {
|
|
181
|
+
if (typeof via !== 'string' || via.trim() === '') {
|
|
182
|
+
issues.push(`${at}.via 必须是非空字段名,当前值: ${JSON.stringify(via)}`);
|
|
183
|
+
} else if (!ctx.fieldKeys.includes(via)) {
|
|
184
|
+
issues.push(
|
|
185
|
+
`${at}.via 指向字段 "${via}",但字典里没有它(本对象字典:${fieldList(ctx.fieldKeys)})`,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
return issues;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 校验字段字典里的 `modifies:` 修饰关系。返回空数组即合法。
|
|
196
|
+
*
|
|
197
|
+
* 三条判据,都拦"能跑但看着不对"的错法:
|
|
198
|
+
* ① 目标必须在同一本字典里——指错则修饰字段并不进被修饰者,自己占一格;
|
|
199
|
+
* ② 不能指自己——自指的语义是"我修饰我",没有可执行含义;
|
|
200
|
+
* ③ 目标自己不能再带 `modifies`——**修饰不可级联**。级联时"谁是被呈现的那个"
|
|
201
|
+
* 没有唯一答案,而这种歧义只会表现为"少显示了一格"。
|
|
202
|
+
*/
|
|
203
|
+
export function validateFieldModifies(
|
|
204
|
+
fields: Readonly<Record<string, { modifies?: string }>>,
|
|
205
|
+
): string[] {
|
|
206
|
+
const issues: string[] = [];
|
|
207
|
+
const keys = Object.keys(fields);
|
|
208
|
+
for (const key of keys) {
|
|
209
|
+
const target = fields[key]?.modifies;
|
|
210
|
+
if (target === undefined) continue;
|
|
211
|
+
if (typeof target !== 'string' || target.trim() === '') {
|
|
212
|
+
issues.push(`字段 "${key}" 的 modifies 必须是非空字段名,当前值: ${JSON.stringify(target)}`);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (target === key) {
|
|
216
|
+
issues.push(
|
|
217
|
+
`字段 "${key}" 的 modifies 指向它自己`
|
|
218
|
+
+ '——修饰关系说的是"我是别人的语气/单位/图标",自指没有可执行含义',
|
|
219
|
+
);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (!(target in fields)) {
|
|
223
|
+
issues.push(
|
|
224
|
+
`字段 "${key}" 的 modifies 指向 "${target}",但字典里没有它(本对象字典:${fieldList(keys)})`
|
|
225
|
+
+ '——指错则本字段不会被并入被修饰者,而是自己占一格,页面上多出一行空值',
|
|
226
|
+
);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (fields[target].modifies !== undefined) {
|
|
230
|
+
issues.push(
|
|
231
|
+
`字段 "${key}" 修饰 "${target}",而 "${target}" 自己也带 modifies——修饰不可级联`
|
|
232
|
+
+ `(请让 "${key}" 直接指向最终被修饰的那个字段)`,
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return issues;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** 供解析器复用的类型别名出口,避免消费方再从两处 import */
|
|
240
|
+
export type { SomaDataObjectRelation, SomaDataObjectRoles };
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OOUI 声明与产物的共享字面量:全体系唯一真源(REQ-014)。
|
|
3
|
+
*
|
|
4
|
+
* ── 为什么单独一个模块 ─────────────────────────────────────────────
|
|
5
|
+
* 这里的每一样东西都被**隔着依赖闸门的几方**同时消费:
|
|
6
|
+
*
|
|
7
|
+
* · 运行时解析器 `client/parser/ui-config-parser.ts`——client 不许 import `core/build`
|
|
8
|
+
* (`verify:client-dep-graph`);
|
|
9
|
+
* · 构建期引擎 `build/ooui/**` 的编排层、页面组合层、空间层与产物层——
|
|
10
|
+
* 层与层之间只许单向看(`verify-ooui-dep-graph`),产物层够不着构件层,
|
|
11
|
+
* 空间层与产物层谁都够不着页面组合层。
|
|
12
|
+
*
|
|
13
|
+
* 此前的结果是同一件事在各处各抄一份,且抄本宽严不一:锚点文法抄成三份
|
|
14
|
+
* (`1list` 过得了页面组合、到产物层才红;`_list` 过得了解析器、到页面组合才红),
|
|
15
|
+
* 演绎请求的键闭集抄成两份(解析器那份收下 `bound_to` / `layout` 却不搬进声明),
|
|
16
|
+
* `'#text'` 在构件层是常量、在产物层是两处字面量。每一份单看都对,
|
|
17
|
+
* 只是报错总落在比写错那一行晚一两层的地方——或者干脆不报。
|
|
18
|
+
*
|
|
19
|
+
* `core/shared/**` 是唯一几方都 import 得到的位置(与 `container-breakpoints.ts` 同一形态):
|
|
20
|
+
* 纯字面量与纯函数,除同在 `core/shared` 的纯字面量闭集(`SOMA_LAYOUT_SIZINGS`)外只 import 类型,
|
|
21
|
+
* 不牵连任何运行时模块。
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
SOMA_LAYOUT_SIZINGS,
|
|
26
|
+
type SomaLayoutNodeConstraint,
|
|
27
|
+
type SomaSizing,
|
|
28
|
+
} from './types/layout-constraint.types';
|
|
29
|
+
import type { SomaOouiRequestDeclaration } from './types/ui-config.types';
|
|
30
|
+
|
|
31
|
+
// ─────────────────────────────────────────────────────────────
|
|
32
|
+
// 锚点文法
|
|
33
|
+
// ─────────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* `ooui:` 的锚点名 = 页面组合的区域名 = 空间 IR 的节点 id。**三处是同一个名字,所以是同一条文法。**
|
|
37
|
+
*
|
|
38
|
+
* 锚点名就是区域名(`compilePageOoui` 拿锚点当区域名),区域名就是 IR 叶子的 id
|
|
39
|
+
* (`solvePageBudget` 原样放进去),产物层再按它派生 `<锚点>-shell` / `<锚点>-root`。
|
|
40
|
+
* 它会原样进 DOM 属性与 CSS 属性选择器:放开引号、括号、空格等于给选择器注入留门,
|
|
41
|
+
* 而注入成功的表现是页面照常渲染。
|
|
42
|
+
*
|
|
43
|
+
* 取「字母或下划线开头」:与运行时解析器一直收的、`layout:` 段锚点键用的是同一条
|
|
44
|
+
* (SOMA 标识符的通行写法);数字开头不收——`data-soma-id="1"` 是合法 HTML,
|
|
45
|
+
* 但读不出它指的是什么。
|
|
46
|
+
*/
|
|
47
|
+
export const SOMA_OOUI_ANCHOR_PATTERN = /^[A-Za-z_][A-Za-z0-9_-]*$/;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 同一条文法的人话版本。错误消息只引用它,不各写一句——
|
|
51
|
+
* 此前三份消息分别写着「字母开头」「字母或数字开头」,与各自的正则一起漂开。
|
|
52
|
+
*/
|
|
53
|
+
export const SOMA_OOUI_ANCHOR_GRAMMAR = '字母或下划线开头,其余可含字母、数字、- 与 _';
|
|
54
|
+
|
|
55
|
+
// ─────────────────────────────────────────────────────────────
|
|
56
|
+
// 演绎请求(`ooui:` 段的一条)的形状
|
|
57
|
+
// ─────────────────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 键闭集。以 `SomaOouiRequestDeclaration` 为准做**编译期对账**(`satisfies`):
|
|
61
|
+
* 声明类型加了字段而这里没跟上,typecheck 当场红——不会再出现"类型里有、闭集里没有"
|
|
62
|
+
* 或反过来"闭集收下了、没人搬"的那一种。
|
|
63
|
+
*/
|
|
64
|
+
const REQUEST_KEY_SET = {
|
|
65
|
+
object: true,
|
|
66
|
+
verb: true,
|
|
67
|
+
focus: true,
|
|
68
|
+
through: true,
|
|
69
|
+
form: true,
|
|
70
|
+
bound_to: true,
|
|
71
|
+
layout: true,
|
|
72
|
+
} as const satisfies Record<keyof SomaOouiRequestDeclaration, true>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 一条演绎请求允许的键——闭集,写错一个字母必须当场红。
|
|
76
|
+
*
|
|
77
|
+
* 运行时解析器与构建期编译器各校一次形状(前者服务直接喂源 YAML 的路径,
|
|
78
|
+
* 后者是发布主路径),两处用的**是这一份**。顺序即书写顺序,错误消息按它列出可用键。
|
|
79
|
+
*/
|
|
80
|
+
export const SOMA_OOUI_REQUEST_KEYS = Object.keys(REQUEST_KEY_SET) as readonly (keyof SomaOouiRequestDeclaration)[];
|
|
81
|
+
|
|
82
|
+
/** 声明里 `layout` 的形状(区域的空间约束逃生门) */
|
|
83
|
+
export type SomaOouiLayoutDeclaration = NonNullable<SomaOouiRequestDeclaration['layout']>;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* `layout` 的键 → 值的原始类型。键集同时对着声明类型与空间层的节点约束对账:
|
|
87
|
+
* 两边任一加了键,这里不跟上就是 typecheck 红。
|
|
88
|
+
*/
|
|
89
|
+
const LAYOUT_VALUE_TYPES = {
|
|
90
|
+
sizing: 'string',
|
|
91
|
+
width: 'string',
|
|
92
|
+
min: 'string',
|
|
93
|
+
max: 'string',
|
|
94
|
+
priority: 'number',
|
|
95
|
+
} as const satisfies Record<keyof SomaOouiLayoutDeclaration, 'string' | 'number'>
|
|
96
|
+
& Record<keyof SomaLayoutNodeConstraint, 'string' | 'number'>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* `layout` 允许的键——**白名单**。
|
|
100
|
+
*
|
|
101
|
+
* 为什么必须是白名单而不是"原样摊进去":区域的约束会被摊进空间 IR 的叶子节点,
|
|
102
|
+
* 而叶子节点上还有 `id` / `kind` / `anchor` / `classes` 这些**结构键**。
|
|
103
|
+
* 放行一个 `layout: { id: x }`,叶子就被改了名,求解结果里再也找不到这个区域——
|
|
104
|
+
* 此前那里读成 0rem,于是这个区域静默降到最窄的候选,页面照常渲染。
|
|
105
|
+
*/
|
|
106
|
+
export const SOMA_OOUI_LAYOUT_KEYS = Object.keys(LAYOUT_VALUE_TYPES) as readonly (keyof SomaOouiLayoutDeclaration)[];
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 尺寸意图三档(`fixed` 就这么宽 / `hug` 内容多宽我多宽 / `fill` 剩下的给我)。顺序即文档书写序。
|
|
110
|
+
*
|
|
111
|
+
* 此前没有任何一层核 `ooui:` 区域的 sizing——解析器、编译器、页面组合层都只核"是不是字符串",
|
|
112
|
+
* 空间层求解器拿到什么就摊进约束。写成 `sizing: fil` 的区域既不是 fixed 也不是 fill,
|
|
113
|
+
* 求解器按"从 min 起步、不参与分余量"处理,于是它**静默缩到下界**(没写 min 就是 0rem),
|
|
114
|
+
* 一路喂进构件选型——这一块降到最窄的候选,页面照常渲染。
|
|
115
|
+
*
|
|
116
|
+
* 三方都要用它:运行时解析器(client 不许 import `core/build`)、编译器与页面组合层
|
|
117
|
+
* (声明期就指着写错的那一行报)、空间层求解器(IR 树的最后一道)。
|
|
118
|
+
* **它不是一份抄本,是 `SOMA_LAYOUT_SIZINGS` 本身**(`shared/types/layout-constraint.types.ts`,
|
|
119
|
+
* 在那里以 `SomaSizing` 做 `satisfies` 对账):`layout:` 段的解析器认的也是那一份。
|
|
120
|
+
* 此前这里自持一份、解析器又自持一份,两份逐字相同,漂开的那天没有断言会红。
|
|
121
|
+
*/
|
|
122
|
+
export const SOMA_OOUI_SIZINGS: readonly SomaSizing[] = SOMA_LAYOUT_SIZINGS;
|
|
123
|
+
|
|
124
|
+
/** 值是不是三档之一。非字符串一律不是——`sizing: 1` 不该被 `String()` 顶成 `'1'` 再比 */
|
|
125
|
+
export function isOouiSizing(value: unknown): value is SomaSizing {
|
|
126
|
+
return typeof value === 'string' && (SOMA_OOUI_SIZINGS as readonly string[]).includes(value);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** `layout` 里第一处不合形状的键:闭集之外的键、值的原始类型不对,或 sizing 不在三档里 */
|
|
130
|
+
export interface SomaOouiLayoutProblem {
|
|
131
|
+
readonly key: string;
|
|
132
|
+
readonly kind: 'unknown-key' | 'value-type' | 'sizing-unknown';
|
|
133
|
+
/** `value-type` 时:这个键要的原始类型 */
|
|
134
|
+
readonly expected?: 'string' | 'number';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 映射(非 null、非数组的对象)。YAML 里 `~` / 列表 / 标量都不是 */
|
|
138
|
+
export function isOouiMap(value: unknown): value is Record<string, unknown> {
|
|
139
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 非空白字符串:`object` / `verb` / `through` / `form` 与 `bound_to` 两项的形状判据 */
|
|
143
|
+
export function isOouiText(value: unknown): value is string {
|
|
144
|
+
return typeof value === 'string' && value.trim() !== '';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* `focus`:非空、且每一项都是非空白字段名。
|
|
149
|
+
*
|
|
150
|
+
* 空数组单独拦:`focus: []` 读起来像"不限",实际演绎出一个没有字段的界面——
|
|
151
|
+
* 框架在、标题在、空白在,是最像成功的失败。"全部字段"的写法是省略 focus。
|
|
152
|
+
*/
|
|
153
|
+
export function isOouiFocus(value: unknown): value is string[] {
|
|
154
|
+
return Array.isArray(value) && value.length > 0 && value.every((item) => isOouiText(item));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 声明里 `bound_to` 的形状(区域间联动的来源) */
|
|
158
|
+
export type SomaOouiBindingDeclaration = NonNullable<SomaOouiRequestDeclaration['bound_to']>;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* `bound_to` 的键闭集。以声明类型做**编译期对账**(`satisfies`),理由同 `REQUEST_KEY_SET`。
|
|
162
|
+
*
|
|
163
|
+
* 此前解析器与编译器都只取 `region` / `as` 两项、其余键**静默丢弃**:写成
|
|
164
|
+
* `bound_to: { region: list, as: select, event: row_click }` 的作者以为自己把组件的 emit 也钉住了,
|
|
165
|
+
* 实际那一项从解析那一刻起就不存在——"写了没生效"与"这个能力不存在"分不开。
|
|
166
|
+
*/
|
|
167
|
+
const BINDING_KEY_SET = {
|
|
168
|
+
region: true,
|
|
169
|
+
as: true,
|
|
170
|
+
} as const satisfies Record<keyof SomaOouiBindingDeclaration, true>;
|
|
171
|
+
|
|
172
|
+
/** `bound_to` 允许的键。解析器与编译器的"未知键"错误都按它列出可用键 */
|
|
173
|
+
export const SOMA_OOUI_BINDING_KEYS = Object.keys(BINDING_KEY_SET) as readonly (keyof SomaOouiBindingDeclaration)[];
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* `bound_to` 里第一个闭集之外的键(按书写序);没有则 undefined。
|
|
177
|
+
*
|
|
178
|
+
* 收 `object` 而不是映射类型:页面组合层拿到的是已经带类型的 `SomaRegionBinding`,
|
|
179
|
+
* 那里多出来的键同样要报,而接口类型不能直接当 `Record<string, unknown>` 用。
|
|
180
|
+
*/
|
|
181
|
+
export function findOouiBindingUnknownKey(binding: object): string | undefined {
|
|
182
|
+
return Object.keys(binding).find((key) => !(SOMA_OOUI_BINDING_KEYS as readonly string[]).includes(key));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* `bound_to`:恰好 `{ region, as }` 两项、且都是非空白字符串的映射。
|
|
187
|
+
*
|
|
188
|
+
* 多一个键也不算合形状——判据若只看两项齐不齐,多出来的键就会一路被当成"合法、只是没用上"。
|
|
189
|
+
* 调用方要报**哪个键**多了时先调 `findOouiBindingUnknownKey`(与 `findOouiLayoutProblem` 同一形态)。
|
|
190
|
+
*/
|
|
191
|
+
export function isOouiBinding(value: unknown): value is SomaOouiBindingDeclaration {
|
|
192
|
+
return isOouiMap(value)
|
|
193
|
+
&& findOouiBindingUnknownKey(value) === undefined
|
|
194
|
+
&& isOouiText(value.region)
|
|
195
|
+
&& isOouiText(value.as);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 找 `layout` 里第一处不合形状的键。合形状返回 undefined。
|
|
200
|
+
*
|
|
201
|
+
* 核三件:**键**在白名单里、**值的原始类型**对、**sizing 在三档里**。
|
|
202
|
+
* priority 的四档与长度单位仍由空间层的求解器核(长度要拿到容器宽才折算得出来)。
|
|
203
|
+
*
|
|
204
|
+
* sizing 的闭集此前刻意不在这里核("闭集归空间层"),结果是**哪一层都没核**:
|
|
205
|
+
* 空间层求解器也只是把它摊进约束。现在它是 `SOMA_OOUI_SIZINGS` 这一份——类型的运行时镜像;
|
|
206
|
+
* 解析器、编译器、页面组合层都经本函数判,空间层求解器按同一份常量兜 IR 树的最后一道。
|
|
207
|
+
*/
|
|
208
|
+
export function findOouiLayoutProblem(layout: Readonly<Record<string, unknown>>): SomaOouiLayoutProblem | undefined {
|
|
209
|
+
for (const [key, value] of Object.entries(layout)) {
|
|
210
|
+
if (!(SOMA_OOUI_LAYOUT_KEYS as readonly string[]).includes(key)) return { key, kind: 'unknown-key' };
|
|
211
|
+
const expected = LAYOUT_VALUE_TYPES[key as keyof typeof LAYOUT_VALUE_TYPES];
|
|
212
|
+
if (typeof value !== expected) return { key, kind: 'value-type', expected };
|
|
213
|
+
if (key === 'sizing' && !isOouiSizing(value)) return { key, kind: 'sizing-unknown' };
|
|
214
|
+
}
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** `layout` 合形状:映射、键全在白名单里、值的原始类型对、sizing 在三档里 */
|
|
219
|
+
export function isOouiLayout(value: unknown): value is SomaOouiLayoutDeclaration {
|
|
220
|
+
return isOouiMap(value) && findOouiLayoutProblem(value) === undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// ─────────────────────────────────────────────────────────────
|
|
224
|
+
// 产物节点
|
|
225
|
+
// ─────────────────────────────────────────────────────────────
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 纯文本节点的组件名占位:**不是组件**(`SomaOouiNode.component` 的一个取值)。
|
|
229
|
+
*
|
|
230
|
+
* 构件层选型时产出它,产物层发射时按它分支(落成 `<span>`、不写 `data-soma-component`)。
|
|
231
|
+
* 两层之间隔着依赖闸门(产物层回看构件层判 `SKIP-BACK`),所以常量住在两边都够得着的这里。
|
|
232
|
+
* 各写一个 `'#text'` 字面量的话,改名那天只改了一边,另一边的判断静默失配——
|
|
233
|
+
* 纯文本字段被当成一个叫 `#text` 的组件去挂载,查不到契约就跳过,那一格永远空着。
|
|
234
|
+
*/
|
|
235
|
+
export const SOMA_TEXT_NODE = '#text';
|