soma-core 1.3.5 → 1.4.1
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 +20 -7
- package/app/SomaPageClient.tsx +4 -4
- package/app/components/SomaHostPage.tsx +5 -5
- package/app/components/SomaHydrationShell.tsx +4 -7
- package/app/globals.css +126 -3
- package/core/build/{v2 → blocks}/block-compile.ts +47 -18
- package/core/build/{v2/soma-file-loader.ts → blocks/block-container.ts} +28 -37
- package/core/build/{v2 → blocks}/block-loader.ts +1 -1
- package/core/build/{v2 → blocks}/html-index.ts +1 -1
- package/core/build/{v2/migrate-v1.ts → blocks/migrate-flat.ts} +27 -27
- package/core/build/compiler/host-config-sync.cjs +2 -9
- package/core/build/compiler/page-declaration-compiler.ts +32 -4
- package/core/build/compiler/soma-manifest-webpack-plugin.js +1 -1
- package/core/build/compiler/soma-precompile.js +2057 -35
- package/core/build/compiler/soma-precompile.ts +2 -2
- package/core/build/component/component-graph.ts +208 -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 +265 -0
- package/core/client/base/SomaPageRootBase.ts +61 -0
- package/core/client/component/ai-catalog.ts +83 -8
- 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/contract-compat.ts +32 -3
- package/core/client/component/{example-v2.ts → example-blocks.ts} +19 -19
- 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/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 +286 -7
- package/core/client/engine/step-runner.ts +134 -0
- package/core/client/engine/style-binder.ts +1 -1
- package/core/client/infra/notify/DomNotifyService.ts +2 -1
- package/core/client/infra/notify/INotifyTheme.ts +1 -1
- package/core/client/parser/{v2 → blocks}/namespace.ts +1 -1
- package/core/client/parser/{v2 → blocks}/page-parser.ts +30 -16
- package/core/client/parser/{v2 → blocks}/use-parser.ts +24 -25
- 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/style-config-parser.ts +7 -7
- package/core/client/parser/ui-config-parser.ts +59 -0
- package/core/client/parser/value-lang.ts +1 -1
- package/core/client/parser/window-parser.ts +294 -0
- package/core/client/registry/index.ts +8 -0
- package/core/client/theme/theme-engine.ts +144 -16
- package/core/client/typography/typography-base.ts +1 -1
- package/core/index.ts +188 -13
- package/core/server/host/csp-nonce-metadata.ts +1 -1
- package/core/server/host/published-page-assets.ts +3 -8
- package/core/server/host/soma-page-compile.ts +1 -1
- package/core/server/index.ts +2 -2
- 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/path-algebra.ts +169 -0
- package/core/shared/predicates/index.ts +527 -0
- package/core/shared/published-page-route.ts +61 -0
- package/core/shared/types/adaptive.types.ts +1 -1
- package/core/shared/types/component-contract.types.ts +128 -2
- package/core/shared/types/data-config.types.ts +149 -2
- 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/scope-manifest.types.ts +11 -15
- package/core/shared/types/step.types.ts +91 -0
- package/core/shared/types/style-config.types.ts +4 -4
- package/core/shared/types/ui-config.types.ts +18 -0
- package/core/shared/types/window.types.ts +177 -0
- package/dist/cli/soma.js +28 -3
- package/dist/core/index.cjs +4829 -405
- package/dist/types/build/{v2 → blocks}/block-compile.d.ts +7 -7
- package/dist/types/build/{v2/soma-file-loader.d.ts → blocks/block-container.d.ts} +6 -11
- package/dist/types/build/{v2/migrate-v1.d.ts → blocks/migrate-flat.d.ts} +6 -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/SomaPageRootBase.d.ts +28 -0
- package/dist/types/client/component/ai-catalog.d.ts +24 -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/contract-compat.d.ts +2 -2
- package/dist/types/client/component/example-blocks.d.ts +9 -0
- 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/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/infra/notify/DomNotifyService.d.ts +2 -1
- package/dist/types/client/infra/notify/INotifyTheme.d.ts +1 -1
- package/dist/types/client/parser/{v2 → blocks}/page-parser.d.ts +2 -2
- package/dist/types/client/parser/{v2 → blocks}/use-parser.d.ts +3 -3
- 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/style-config-parser.d.ts +1 -1
- 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/registry/index.d.ts +1 -0
- package/dist/types/client/theme/theme-engine.d.ts +37 -0
- package/dist/types/index.d.ts +40 -9
- package/dist/types/server/host/published-page-assets.d.ts +3 -2
- package/dist/types/server/index.d.ts +1 -1
- 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/path-algebra.d.ts +56 -0
- package/dist/types/shared/predicates/index.d.ts +79 -0
- package/dist/types/shared/published-page-route.d.ts +21 -0
- package/dist/types/shared/types/component-contract.types.d.ts +112 -0
- package/dist/types/shared/types/data-config.types.d.ts +132 -2
- 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/scope-manifest.types.d.ts +11 -11
- package/dist/types/shared/types/step.types.d.ts +76 -0
- package/dist/types/shared/types/style-config.types.d.ts +4 -4
- 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 +2 -6
- package/dist/types/client/component/example-v2.d.ts +0 -9
- package/postcss.config.js +0 -6
- package/tailwind.config.ts +0 -43
- /package/dist/types/build/{v2 → blocks}/block-loader.d.ts +0 -0
- /package/dist/types/build/{v2 → blocks}/html-index.d.ts +0 -0
- /package/dist/types/client/parser/{v2 → blocks}/namespace.d.ts +0 -0
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
// props 缺失/类型变 → 页面照常传值,组件读不到,白屏或空块;
|
|
14
14
|
// 可选变必填 → 存量页面没传那个 prop,覆盖后变成缺必填;
|
|
15
15
|
// default 变化 → 最阴的一种:结构完全兼容,但存量页面没传该 prop 的地方
|
|
16
|
-
// 行为静默改变(分页大小从 20 变 50
|
|
16
|
+
// 行为静默改变(分页大小从 20 变 50 这种),任何结构检查都抓不到;
|
|
17
|
+
// 插槽缺失/收紧 → 唯一会把问题推迟成**页面级崩溃**的一类:
|
|
18
|
+
// 投影内容在挂载时被判为未知槽名,直接抛 SomaComponentExpandError。
|
|
17
19
|
|
|
18
20
|
import type {
|
|
19
21
|
SomaComponentContract,
|
|
@@ -25,11 +27,13 @@ export type ContractCompatIssueKind =
|
|
|
25
27
|
| 'prop-type-changed'
|
|
26
28
|
| 'prop-required-tightened'
|
|
27
29
|
| 'prop-default-diverged'
|
|
28
|
-
| 'emit-missing'
|
|
30
|
+
| 'emit-missing'
|
|
31
|
+
| 'slot-missing'
|
|
32
|
+
| 'slot-required-tightened';
|
|
29
33
|
|
|
30
34
|
export interface ContractCompatIssue {
|
|
31
35
|
kind: ContractCompatIssueKind;
|
|
32
|
-
/** prop
|
|
36
|
+
/** prop / emit / slot 名 */
|
|
33
37
|
member: string;
|
|
34
38
|
/** 点名到具体差异内容——报错信息是接入者读得最认真的文档 */
|
|
35
39
|
detail: string;
|
|
@@ -99,6 +103,31 @@ export function checkOverrideCompat(
|
|
|
99
103
|
}
|
|
100
104
|
}
|
|
101
105
|
|
|
106
|
+
// 插槽(REQ-008 D-5 补洞):此前闸门只查 props + emits,插槽整个漏在管辖外。
|
|
107
|
+
// 漏掉的代价比另两类更重——页面投影进插槽的内容不会消失,它会在**运行时**
|
|
108
|
+
// 被 component-expander 判为「声明了组件不认识的槽名」而抛 SomaComponentExpandError,
|
|
109
|
+
// 整页崩。也就是说:本该在合成期挡下的问题,被推迟成了页面级崩溃。
|
|
110
|
+
const winnerSlots = winner.slots ?? {};
|
|
111
|
+
for (const [name, baseSlot] of Object.entries(base.slots ?? {})) {
|
|
112
|
+
const winnerSlot = winnerSlots[name];
|
|
113
|
+
if (!winnerSlot) {
|
|
114
|
+
issues.push({
|
|
115
|
+
kind: 'slot-missing',
|
|
116
|
+
member: name,
|
|
117
|
+
detail: `缺少插槽 "${name}"——存量页面仍在往这个槽投影内容,`
|
|
118
|
+
+ '覆盖后不是少一块内容,而是挂载时直接抛 SomaComponentExpandError(整页崩)',
|
|
119
|
+
});
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (winnerSlot.required === true && baseSlot.required !== true) {
|
|
123
|
+
issues.push({
|
|
124
|
+
kind: 'slot-required-tightened',
|
|
125
|
+
member: name,
|
|
126
|
+
detail: `插槽 "${name}" 从可选收紧为必填——存量页面没给这个槽的地方会在挂载时抛错`,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
102
131
|
return issues;
|
|
103
132
|
}
|
|
104
133
|
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 契约示例的块写法派生(REQ-005 F8.4 生态再生)。
|
|
2
2
|
//
|
|
3
|
-
// 315 份契约的 `ai.example`
|
|
4
|
-
// AI
|
|
3
|
+
// 315 份契约的 `ai.example` 是平铺三行式(html / ui / event)。块声明落地后
|
|
4
|
+
// AI 需要看到块写法,否则它学到的永远是旧格式——而"生成质量的知识沉在
|
|
5
5
|
// 通用资产里"这条工程铁律,说的就是这种地方。
|
|
6
6
|
//
|
|
7
7
|
// ── 为什么是派生而不是改 315 个源文件 ──────────────────────────────────
|
|
8
|
-
// 改源文件要动 315
|
|
9
|
-
//
|
|
8
|
+
// 改源文件要动 315 处,且从此两份示例并存、各自演化、必然漂移。
|
|
9
|
+
// 派生只有一处实现:契约仍是单一真源,块写法示例由平铺示例机械换算而来,
|
|
10
10
|
// 两者不可能不一致——因为后者是前者的函数。
|
|
11
11
|
//
|
|
12
|
-
// ──
|
|
13
|
-
// 1.
|
|
14
|
-
//
|
|
12
|
+
// ── 换算规则(块写法的两处结构差异)─────────────────────────────────────
|
|
13
|
+
// 1. 组件实例**自成顶层块**(块声明第一公理:"组件是声明的第一公民"):
|
|
14
|
+
// 平铺的 `锚点: { props: {…} }` → 块写法的 `锚点:` 块 + `component:` + `ui:`;
|
|
15
15
|
// 2. 实例的 emit 路由并进块内的 `event.on`,不再写在页面级事件文件里。
|
|
16
16
|
//
|
|
17
|
-
//
|
|
17
|
+
// 换不出来的(示例本身不是标准三行式)就不产块写法行——宁可少给一份示例,
|
|
18
18
|
// 也不要给一份**看着对、抄过去装载不进去**的示例。
|
|
19
19
|
|
|
20
|
-
/**
|
|
20
|
+
/** 平铺示例的三行前缀 */
|
|
21
21
|
const HTML_PREFIX = 'html:';
|
|
22
22
|
const UI_PREFIX = 'ui:';
|
|
23
23
|
const EVENT_PREFIX = 'event:';
|
|
@@ -56,14 +56,14 @@ function lineAfter(lines: readonly string[], prefix: string): string | null {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
59
|
+
* 解析平铺示例的 ui / event 两行。
|
|
60
60
|
*
|
|
61
61
|
* 只认最规整的那一种形状:`锚点: { props: { … } }`。
|
|
62
62
|
* 示例写得更花的(多锚点、嵌套 show、列表作用域)一律返回 null——
|
|
63
|
-
*
|
|
63
|
+
* 那些形状换算成块写法需要真的理解作用域,不是字符串搬运;
|
|
64
64
|
* 猜错了产出的是能骗过肉眼、装载器却拒收的示例。
|
|
65
65
|
*/
|
|
66
|
-
function
|
|
66
|
+
function parseFlatExample(example: string): ParsedExample | null {
|
|
67
67
|
const lines = example.split('\n');
|
|
68
68
|
const ui = lineAfter(lines, UI_PREFIX);
|
|
69
69
|
if (!ui) return null;
|
|
@@ -99,16 +99,16 @@ function parseV1Example(example: string): ParsedExample | null {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
|
-
*
|
|
102
|
+
* 由平铺示例派生一行块写法;换算不出来时返回 null。
|
|
103
103
|
*
|
|
104
104
|
* 产出的是**块声明**片段(写进 `_ui.yaml` 的 `{{ … }}` 之内),
|
|
105
|
-
* html
|
|
105
|
+
* html 那一行两种写法通用,不重复。
|
|
106
106
|
*/
|
|
107
|
-
export function
|
|
108
|
-
const parsed =
|
|
107
|
+
export function deriveBlockExample(componentName: string, example: string): string | null {
|
|
108
|
+
const parsed = parseFlatExample(example);
|
|
109
109
|
if (!parsed) return null;
|
|
110
110
|
|
|
111
|
-
// props
|
|
111
|
+
// props 逐字与平铺的 ui 行相同——**不重复贴一遍**,写成"同上"。
|
|
112
112
|
//
|
|
113
113
|
// 目录是喂给模型的,字节即成本(体积预算闸门盯着最大单分片)。
|
|
114
114
|
// 重贴一份 props 对 AI 没有增量信息:它要学的是**结构差异**
|
|
@@ -126,7 +126,7 @@ export function deriveV2Example(componentName: string, example: string): string
|
|
|
126
126
|
return lines.join('\n');
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
/** 示例里的 html
|
|
129
|
+
/** 示例里的 html 行(两种写法通用),供块写法段复用而不重复贴一遍 */
|
|
130
130
|
export function exampleHtmlLine(example: string): string | null {
|
|
131
131
|
return lineAfter(example.split('\n'), HTML_PREFIX);
|
|
132
132
|
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// 键盘矩阵(REQ-011 / IK-2)——契约 `keyboard:` 段 → 「按哪个键做什么」的唯一答案。
|
|
2
|
+
//
|
|
3
|
+
// ── 为什么它必须是一个函数而不是一张手写表 ──────────────────────────────
|
|
4
|
+
// 「声明 → 接线 → 文档 → 测试」四者同源,靠的是三个消费者读**同一个函数**:
|
|
5
|
+
// · soma-ui 的 keyboard-wiring 按它派发按键(接线);
|
|
6
|
+
// · ai-catalog 按它渲染目录里的键盘表(文档);
|
|
7
|
+
// · 闸门按它逐键真按并断言落点(测试)。
|
|
8
|
+
// 任何一处改成手抄,就会出现"目录说 Home 到首项、实际按了没反应"——而且没人会发现,
|
|
9
|
+
// 因为目录与实现各自看起来都对。React Aria 新站删掉了全部键盘表、键位真相只在源码里,
|
|
10
|
+
// 正是同一种漂移的终点。
|
|
11
|
+
|
|
12
|
+
import type { SomaContractKeyboard } from '../../shared/types/component-contract.types';
|
|
13
|
+
|
|
14
|
+
/** 引擎能兑现的意图闭集;矩阵里的每个键都落在这里面 */
|
|
15
|
+
export const SOMA_KEYBOARD_INTENTS = [
|
|
16
|
+
'next', 'previous', 'left', 'right',
|
|
17
|
+
'first', 'last', 'page_next', 'page_previous',
|
|
18
|
+
'expand', 'collapse',
|
|
19
|
+
'activate', 'toggle_select', 'search',
|
|
20
|
+
] as const;
|
|
21
|
+
export type SomaKeyboardIntent = (typeof SOMA_KEYBOARD_INTENTS)[number];
|
|
22
|
+
|
|
23
|
+
export interface SomaKeyboardMatrixRow {
|
|
24
|
+
/** 触发这个意图的按键(`KeyboardEvent.key` 值);多个键同义时并列 */
|
|
25
|
+
keys: string[];
|
|
26
|
+
intent: SomaKeyboardIntent;
|
|
27
|
+
/** 给目录看的一句话 */
|
|
28
|
+
desc: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const INTENT_DESC: Record<SomaKeyboardIntent, string> = {
|
|
32
|
+
next: '下一项',
|
|
33
|
+
previous: '上一项',
|
|
34
|
+
left: '左一格',
|
|
35
|
+
right: '右一格',
|
|
36
|
+
first: '首项',
|
|
37
|
+
last: '末项',
|
|
38
|
+
page_next: '下翻一页',
|
|
39
|
+
page_previous: '上翻一页',
|
|
40
|
+
expand: '展开(已展开则进入第一个子级)',
|
|
41
|
+
collapse: '收起(已收起则回到父级)',
|
|
42
|
+
activate: '激活当前项',
|
|
43
|
+
toggle_select: '勾选 / 取消当前项',
|
|
44
|
+
search: '首字母跳转',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 由契约推出矩阵。纯函数、无 DOM:目录构建(Node)与浏览器接线读同一份。
|
|
49
|
+
*
|
|
50
|
+
* `layout: none` 出空表——这是"我不接管键盘"的显式声明,不是缺省。
|
|
51
|
+
*/
|
|
52
|
+
export function deriveKeyboardMatrix(keyboard: SomaContractKeyboard): SomaKeyboardMatrixRow[] {
|
|
53
|
+
if (keyboard.layout === 'none') return [];
|
|
54
|
+
const rows: SomaKeyboardMatrixRow[] = [];
|
|
55
|
+
const horizontal = keyboard.layout !== 'grid' && keyboard.orientation === 'horizontal';
|
|
56
|
+
|
|
57
|
+
if (keyboard.layout === 'grid') {
|
|
58
|
+
rows.push({ keys: ['ArrowDown'], intent: 'next', desc: '下一行同列' });
|
|
59
|
+
rows.push({ keys: ['ArrowUp'], intent: 'previous', desc: '上一行同列' });
|
|
60
|
+
rows.push({ keys: ['ArrowRight'], intent: 'right', desc: INTENT_DESC.right });
|
|
61
|
+
rows.push({ keys: ['ArrowLeft'], intent: 'left', desc: INTENT_DESC.left });
|
|
62
|
+
} else if (horizontal) {
|
|
63
|
+
rows.push({ keys: ['ArrowRight'], intent: 'next', desc: `${INTENT_DESC.next}(RTL 下换向)` });
|
|
64
|
+
rows.push({ keys: ['ArrowLeft'], intent: 'previous', desc: `${INTENT_DESC.previous}(RTL 下换向)` });
|
|
65
|
+
} else if (keyboard.layout !== 'tree' && keyboard.orientation === 'both') {
|
|
66
|
+
rows.push({ keys: ['ArrowDown', 'ArrowRight'], intent: 'next', desc: `${INTENT_DESC.next}(→ 在 RTL 下换向)` });
|
|
67
|
+
rows.push({ keys: ['ArrowUp', 'ArrowLeft'], intent: 'previous', desc: `${INTENT_DESC.previous}(← 在 RTL 下换向)` });
|
|
68
|
+
} else {
|
|
69
|
+
rows.push({ keys: ['ArrowDown'], intent: 'next', desc: INTENT_DESC.next });
|
|
70
|
+
rows.push({ keys: ['ArrowUp'], intent: 'previous', desc: INTENT_DESC.previous });
|
|
71
|
+
if (keyboard.layout === 'tree') {
|
|
72
|
+
rows.push({ keys: ['ArrowRight'], intent: 'expand', desc: `${INTENT_DESC.expand}(RTL 下换向)` });
|
|
73
|
+
rows.push({ keys: ['ArrowLeft'], intent: 'collapse', desc: `${INTENT_DESC.collapse}(RTL 下换向)` });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
rows.push({ keys: ['Home'], intent: 'first', desc: INTENT_DESC.first });
|
|
78
|
+
rows.push({ keys: ['End'], intent: 'last', desc: INTENT_DESC.last });
|
|
79
|
+
// 横向列表不翻页:一行选项卡没有"页"可翻,把 PageDown 吞掉只会挡住页面滚动
|
|
80
|
+
if (!horizontal) {
|
|
81
|
+
rows.push({ keys: ['PageDown'], intent: 'page_next', desc: INTENT_DESC.page_next });
|
|
82
|
+
rows.push({ keys: ['PageUp'], intent: 'page_previous', desc: INTENT_DESC.page_previous });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (keyboard.space === 'toggle_select') {
|
|
86
|
+
rows.push({ keys: ['Enter'], intent: 'activate', desc: INTENT_DESC.activate });
|
|
87
|
+
rows.push({ keys: [' '], intent: 'toggle_select', desc: INTENT_DESC.toggle_select });
|
|
88
|
+
} else if (keyboard.space === 'none') {
|
|
89
|
+
// 空格不进表:它归触发器(原生 button 的"按下")。印上去等于替组件承诺一件它不做的事
|
|
90
|
+
rows.push({ keys: ['Enter'], intent: 'activate', desc: INTENT_DESC.activate });
|
|
91
|
+
} else {
|
|
92
|
+
rows.push({ keys: ['Enter', ' '], intent: 'activate', desc: INTENT_DESC.activate });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (keyboard.typeahead === true) {
|
|
96
|
+
rows.push({ keys: ['<字符>'], intent: 'search', desc: `${INTENT_DESC.search}(1 秒内连打为前缀)` });
|
|
97
|
+
}
|
|
98
|
+
return rows;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const LAYOUT_LABEL: Record<SomaContractKeyboard['layout'], string> = {
|
|
102
|
+
list: '列表', grid: '网格', tree: '树', none: '不接管',
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
function keyLabel(key: string): string {
|
|
106
|
+
switch (key) {
|
|
107
|
+
case 'ArrowDown': return '↓';
|
|
108
|
+
case 'ArrowUp': return '↑';
|
|
109
|
+
case 'ArrowLeft': return '←';
|
|
110
|
+
case 'ArrowRight': return '→';
|
|
111
|
+
case ' ': return 'Space';
|
|
112
|
+
default: return key;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 目录用的一行紧凑键盘表。
|
|
118
|
+
*
|
|
119
|
+
* 一行而不是一张表:目录会被压成裸文本喂给模型,且每个分片有字节预算;
|
|
120
|
+
* 键盘表是"查得到"的信息,不是"读一遍"的信息。
|
|
121
|
+
*/
|
|
122
|
+
export function renderKeyboardMatrixLine(keyboard: SomaContractKeyboard): string {
|
|
123
|
+
const rows = deriveKeyboardMatrix(keyboard);
|
|
124
|
+
const traits: string[] = [LAYOUT_LABEL[keyboard.layout]];
|
|
125
|
+
if (keyboard.layout !== 'none') {
|
|
126
|
+
if (keyboard.layout !== 'grid') {
|
|
127
|
+
traits.push(keyboard.orientation === 'horizontal' ? '横向' : keyboard.orientation === 'both' ? '双向' : '纵向');
|
|
128
|
+
}
|
|
129
|
+
traits.push(keyboard.wrap === true ? '到边回绕' : '到边停住');
|
|
130
|
+
if (keyboard.selection_follows_focus === true) traits.push('选中随焦点');
|
|
131
|
+
if (keyboard.focus_model === 'activedescendant') traits.push('aria-activedescendant');
|
|
132
|
+
}
|
|
133
|
+
// 组件特有键:与矩阵某一键同名时**替换**那一行的说明(Enter 在穿梭框里是"搬运"),
|
|
134
|
+
// 否则追加。同一个键印两行说明,读的人不知道该信哪一行。
|
|
135
|
+
const extra = new Map(Object.entries(keyboard.extra ?? {}));
|
|
136
|
+
const parts = rows.map((row) => {
|
|
137
|
+
const label = row.keys.map(keyLabel).join('/');
|
|
138
|
+
const override = extra.get(label);
|
|
139
|
+
if (override !== undefined) extra.delete(label);
|
|
140
|
+
return `${label} ${override ?? row.desc}`;
|
|
141
|
+
});
|
|
142
|
+
for (const [key, desc] of extra) parts.push(`${key} ${desc}`);
|
|
143
|
+
return `键盘(${traits.join('·')}):${parts.join(';')}`;
|
|
144
|
+
}
|
|
@@ -15,10 +15,28 @@
|
|
|
15
15
|
// 这样热更新重复执行也不会堆出十几张同样的 <style>)。
|
|
16
16
|
|
|
17
17
|
import type { SomaComponentDefinition } from './component-definition.types';
|
|
18
|
+
import type { SomaComponentContract } from '../../shared/types/component-contract.types';
|
|
19
|
+
import { componentTokenPrefix, componentTokenVar } from '../../shared/types/component-contract.types';
|
|
18
20
|
import type { ComposedComponentPacks } from './component-pack';
|
|
19
21
|
|
|
20
22
|
const injected = new Set<string>();
|
|
21
23
|
|
|
24
|
+
/**
|
|
25
|
+
* 组件令牌规则(M-1):把契约声明的可调旋钮写成一条挂在组件根类上的规则。
|
|
26
|
+
*
|
|
27
|
+
* 为什么是规则不是内联样式:一个组件一条,与实例数无关;
|
|
28
|
+
* 且 `ThemeScope` 或主题 `components` 段写出的 `--soma-<comp>-*` 层级更高,
|
|
29
|
+
* 靠 CSS 级联天然覆盖,不需要引擎逐实例改写。
|
|
30
|
+
*/
|
|
31
|
+
export function renderComponentTokenRule(contract: SomaComponentContract): string {
|
|
32
|
+
const tokens = contract.tokens;
|
|
33
|
+
if (!tokens || Object.keys(tokens).length === 0) return '';
|
|
34
|
+
const decls = Object.entries(tokens)
|
|
35
|
+
.map(([key, token]) => `${componentTokenVar(contract.name, key)}:${token.default};`)
|
|
36
|
+
.join('');
|
|
37
|
+
return `.soma-${componentTokenPrefix(contract.name)}{${decls}}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
22
40
|
export interface PackStyleOptions {
|
|
23
41
|
/** CSP nonce;宿主启用严格 CSP 时必须给 */
|
|
24
42
|
nonce?: string | null;
|
|
@@ -29,6 +47,14 @@ export function resetPackStyleInjection(): void {
|
|
|
29
47
|
injected.clear();
|
|
30
48
|
}
|
|
31
49
|
|
|
50
|
+
/**
|
|
51
|
+
* 幂等注入一段 CSS(组件分片、特性分片与页面级布局产物共用同一个入口)。
|
|
52
|
+
* 同一个 key 只落一张 <style>;热更新重复执行不会堆出十几张同样的样式表。
|
|
53
|
+
*/
|
|
54
|
+
export function injectStyleOnce(key: string, css: string, options: PackStyleOptions): void {
|
|
55
|
+
injectOnce(key, css, options);
|
|
56
|
+
}
|
|
57
|
+
|
|
32
58
|
function injectOnce(key: string, css: string, options: PackStyleOptions): void {
|
|
33
59
|
if (!css.trim() || injected.has(key)) return;
|
|
34
60
|
if (typeof document === 'undefined') return;
|
|
@@ -56,10 +82,19 @@ export function attachPackStyles(
|
|
|
56
82
|
const { components, styles } = composed;
|
|
57
83
|
const result: Record<string, SomaComponentDefinition> = {};
|
|
58
84
|
|
|
85
|
+
const patchesByTarget = new Map<string, Array<{ patchedBy: string; css: string }>>();
|
|
86
|
+
for (const patch of composed.patches ?? []) {
|
|
87
|
+
const list = patchesByTarget.get(patch.target) ?? [];
|
|
88
|
+
list.push({ patchedBy: patch.patchedBy, css: patch.css });
|
|
89
|
+
patchesByTarget.set(patch.target, list);
|
|
90
|
+
}
|
|
91
|
+
|
|
59
92
|
for (const [name, definition] of Object.entries(components) as Array<[string, SomaComponentDefinition]>) {
|
|
60
93
|
const own = styles.components[name];
|
|
61
94
|
const deps = styles.deps[name] ?? [];
|
|
62
|
-
|
|
95
|
+
const tokenRule = renderComponentTokenRule(definition.contract);
|
|
96
|
+
const patches = patchesByTarget.get(name) ?? [];
|
|
97
|
+
if (!own && deps.length === 0 && !tokenRule && patches.length === 0) {
|
|
63
98
|
result[name] = definition;
|
|
64
99
|
continue;
|
|
65
100
|
}
|
|
@@ -71,7 +106,15 @@ export function attachPackStyles(
|
|
|
71
106
|
const shared = styles.shared[dep];
|
|
72
107
|
if (shared) injectOnce(`shared:${dep}`, shared, options);
|
|
73
108
|
}
|
|
109
|
+
// 令牌规则必须排在组件分片**之前**:分片里的声明要能覆盖旋钮的缺省值,
|
|
110
|
+
// 反过来注入会让组件自己的特化失效。
|
|
111
|
+
if (tokenRule) injectOnce(`tokens:${name}`, tokenRule, options);
|
|
74
112
|
if (own) injectOnce(`component:${name}`, own, options);
|
|
113
|
+
// 补丁最后:它的全部意义就是覆盖官方分片(M-4)。
|
|
114
|
+
// 特性分片在这两者之间,由 component-manager 按 prop 注入。
|
|
115
|
+
for (const patch of patches) {
|
|
116
|
+
injectOnce(`patch:${patch.patchedBy}→${name}`, patch.css, options);
|
|
117
|
+
}
|
|
75
118
|
return inner ? inner(ctx) : {};
|
|
76
119
|
},
|
|
77
120
|
};
|
|
@@ -92,3 +135,18 @@ export function ensurePackBaseStyles(
|
|
|
92
135
|
injectOnce(`base:${index}`, css, options);
|
|
93
136
|
});
|
|
94
137
|
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 注入一片特性分片(M-3)。由 component-manager 在 prop 首次为真值时调用。
|
|
141
|
+
*
|
|
142
|
+
* key 用 `feature:<组件>#<特性>`,与 `component:<组件>` 同一个 injected 集合,
|
|
143
|
+
* 所以「注入一次」的保证是同一套,不需要组件自己记账。
|
|
144
|
+
*/
|
|
145
|
+
export function injectStyleFeature(
|
|
146
|
+
componentName: string,
|
|
147
|
+
featureName: string,
|
|
148
|
+
css: string,
|
|
149
|
+
options: PackStyleOptions = {},
|
|
150
|
+
): void {
|
|
151
|
+
injectOnce(`feature:${componentName}#${featureName}`, css, options);
|
|
152
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// 特性分片的装配(REQ-009 M-3)。
|
|
2
|
+
//
|
|
3
|
+
// 「按需装配」此前只细到组件一档:组件一实例化就付它全部 CSS。
|
|
4
|
+
// 这一档在 DataTable 上不够用——核心分片贴着 7,600 上限,而排队的表格特性有五个。
|
|
5
|
+
// 抬上限只装得下一个;正确的杠杆是再细一档:**用到这个特性的页面才为它付钱**。
|
|
6
|
+
//
|
|
7
|
+
// 判定刻意做得很窄:`when` 只能指向契约里的 **prop**,不能指向控制器的派生态。
|
|
8
|
+
// 派生态要等实例跑起来才知道,而"要不要注入 CSS"必须在渲染第一帧之前定下来——
|
|
9
|
+
// 否则用户会看到一帧无样式的内容。
|
|
10
|
+
//
|
|
11
|
+
// 两种开关形态:boolean/array 的真值门控,以及 enum 的按值门控
|
|
12
|
+
// (`{ prop, equals }`)。后者是施工中补的:首批两个消费者都卡在同一处——
|
|
13
|
+
// 真正决定"要不要这段样式"的是一个枚举值而不是布尔开关,只有布尔一种写法时
|
|
14
|
+
// 它们只能退而按一个恒为真的数组 prop 开门,于是所有页面都付这笔钱,机制等于白建。
|
|
15
|
+
|
|
16
|
+
import type {
|
|
17
|
+
SomaComponentDefinition,
|
|
18
|
+
SomaStyleFeatureWhen,
|
|
19
|
+
} from './component-definition.types';
|
|
20
|
+
import { injectStyleFeature, type PackStyleOptions } from './pack-styles';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 真值门控:`true`、非空数组,或**非空字符串**。`0` / `''` / `[]` 一律不算启用。
|
|
24
|
+
*
|
|
25
|
+
* 字符串这一路是后补的:`detail_api` / `submit_api` 这类"声明了接口就要这段样式"的
|
|
26
|
+
* prop 是自治组件里最常见的开关形态——给了路径就意味着这段界面会出现。
|
|
27
|
+
* 不认它的话,这类特性只能退回"所有页面都付这笔钱",机制等于白建
|
|
28
|
+
* (与枚举门控当初的处境完全一样)。
|
|
29
|
+
*/
|
|
30
|
+
export function isFeatureActive(value: unknown): boolean {
|
|
31
|
+
if (Array.isArray(value)) return value.length > 0;
|
|
32
|
+
if (typeof value === 'string') return value.trim() !== '';
|
|
33
|
+
return value === true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 开关名(两种形态共用的取 prop 名) */
|
|
37
|
+
export function featurePropOf(when: SomaStyleFeatureWhen): string {
|
|
38
|
+
return typeof when === 'string' ? when : when.prop;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 按当前 props 判定某个特性是否启用 */
|
|
42
|
+
export function isFeatureEnabled(when: SomaStyleFeatureWhen, props: Record<string, unknown>): boolean {
|
|
43
|
+
if (typeof when === 'string') return isFeatureActive(props[when]);
|
|
44
|
+
const value = props[when.prop];
|
|
45
|
+
// 逐值比较用 Object.is 而不是 ==:枚举值可能是数字,'2' 与 2 不该互相命中
|
|
46
|
+
return when.equals.some((candidate) => Object.is(candidate, value));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 装配期校验:`when` 必须指向契约里真实存在的 prop,且形态要对得上——
|
|
51
|
+
* 真值门控只认 boolean / array,按值门控只认 enum 且取值必须在枚举内。
|
|
52
|
+
* 返回问题清单(空数组 = 通过),由 bootstrap 汇总后抛。
|
|
53
|
+
*/
|
|
54
|
+
export function validateStyleFeatures(definition: SomaComponentDefinition): string[] {
|
|
55
|
+
const features = definition.styleFeatures;
|
|
56
|
+
if (!features) return [];
|
|
57
|
+
const issues: string[] = [];
|
|
58
|
+
const props = definition.contract.props ?? {};
|
|
59
|
+
const componentName = definition.contract.name;
|
|
60
|
+
|
|
61
|
+
for (const [featureName, feature] of Object.entries(features)) {
|
|
62
|
+
if (!feature || typeof feature.css !== 'string' || !feature.css.trim()) {
|
|
63
|
+
issues.push(`"${componentName}" 的特性分片 "${featureName}" 缺少 css`);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const propName = featurePropOf(feature.when);
|
|
67
|
+
const prop = props[propName];
|
|
68
|
+
if (!prop) {
|
|
69
|
+
issues.push(
|
|
70
|
+
`"${componentName}" 的特性分片 "${featureName}" 的 when 指向 "${propName}",`
|
|
71
|
+
+ '但契约里没有这个 prop(when 只能指向契约 prop,不能指向控制器派生态——'
|
|
72
|
+
+ '派生态要等实例跑起来才知道,而"要不要注入 CSS"必须在第一帧之前定下来)',
|
|
73
|
+
);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (typeof feature.when === 'string') {
|
|
78
|
+
if (prop.type !== 'boolean' && prop.type !== 'array' && prop.type !== 'string') {
|
|
79
|
+
issues.push(
|
|
80
|
+
`"${componentName}" 的特性分片 "${featureName}" 的 when 指向 "${propName}",`
|
|
81
|
+
+ `但它是 ${prop.type} 类型——真值开关只能是 boolean / array / string`
|
|
82
|
+
+ '(string 按"非空即启用"判,用于 detail_api 这类"声明了就要这段样式"的口子)。'
|
|
83
|
+
+ `若想按取值开门,写成 { prop: '${propName}', equals: [...] }`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (prop.type !== 'enum') {
|
|
90
|
+
issues.push(
|
|
91
|
+
`"${componentName}" 的特性分片 "${featureName}" 用了按值门控,`
|
|
92
|
+
+ `但 "${propName}" 是 ${prop.type} 类型——{ prop, equals } 只对 enum 有意义`,
|
|
93
|
+
);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (!Array.isArray(feature.when.equals) || feature.when.equals.length === 0) {
|
|
97
|
+
issues.push(`"${componentName}" 的特性分片 "${featureName}" 的 equals 必须是非空数组`);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
// 取值不在枚举内 = 这个特性永远不会被注入。它不报错、只是"设了没用",
|
|
101
|
+
// 所以必须在装配期拦下——运行期没有任何迹象能让人发现它。
|
|
102
|
+
const allowed = new Set<unknown>(prop.values ?? []);
|
|
103
|
+
const stray = feature.when.equals.filter((value) => !allowed.has(value));
|
|
104
|
+
if (allowed.size > 0 && stray.length > 0) {
|
|
105
|
+
issues.push(
|
|
106
|
+
`"${componentName}" 的特性分片 "${featureName}" 的 equals 含枚举外取值 `
|
|
107
|
+
+ `${stray.map((v) => JSON.stringify(v)).join(' / ')}——`
|
|
108
|
+
+ `"${propName}" 的合法取值是 [${(prop.values ?? []).join(' | ')}]。`
|
|
109
|
+
+ '取不到的值等于这段样式永远不注入,而运行期没有任何迹象能发现它',
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return issues;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 按当前 props 注入该启用的特性分片。实例创建与每次 setProps 后调用。
|
|
118
|
+
* 注入是幂等的(pack-styles 的 injected 集合负责),所以这里不用自己记账。
|
|
119
|
+
*/
|
|
120
|
+
export function ensureStyleFeatures(
|
|
121
|
+
definition: SomaComponentDefinition,
|
|
122
|
+
props: Record<string, unknown>,
|
|
123
|
+
options: PackStyleOptions = {},
|
|
124
|
+
): void {
|
|
125
|
+
const features = definition.styleFeatures;
|
|
126
|
+
if (!features) return;
|
|
127
|
+
for (const [featureName, feature] of Object.entries(features)) {
|
|
128
|
+
if (!isFeatureEnabled(feature.when, props)) continue;
|
|
129
|
+
injectStyleFeature(definition.contract.name, featureName, feature.css, options);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// 主题输入里的组件令牌 × 契约声明的对账(REQ-009 M-1)。
|
|
2
|
+
//
|
|
3
|
+
// 分工的理由:主题引擎不认识组件(它只认令牌名,且刻意不可插拔),
|
|
4
|
+
// 而契约住在组件包里。两者相遇的唯一地点是装配层——也就是这里。
|
|
5
|
+
//
|
|
6
|
+
// 为什么必须对账而不是随便写:`components: { Buton: { radius: … } }` 这样一个拼写错误,
|
|
7
|
+
// 在没有对账时的表现是「改了没反应」——变量确实写进了 :root,只是没有任何组件读它。
|
|
8
|
+
// 这类错误的排查成本远高于报错。
|
|
9
|
+
|
|
10
|
+
import type { SomaComponentDefinition } from './component-definition.types';
|
|
11
|
+
import { COMPONENT_TOKEN_KEY_PATTERN } from '../../shared/types/component-contract.types';
|
|
12
|
+
|
|
13
|
+
export class SomaThemeComponentTokenError extends Error {
|
|
14
|
+
constructor(message: string) {
|
|
15
|
+
super(`[SomaThemeComponentTokens] ${message}`);
|
|
16
|
+
this.name = 'SomaThemeComponentTokenError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 校验主题输入的 `components` / `darkComponents` 段是否与契约对得上。
|
|
22
|
+
* Host 在调 `applyTheme` 之前调用;不给组件表时(纯预览场景)跳过。
|
|
23
|
+
*/
|
|
24
|
+
export function validateThemeComponentTokens(
|
|
25
|
+
input: {
|
|
26
|
+
components?: Record<string, Record<string, string>>;
|
|
27
|
+
darkComponents?: Record<string, Record<string, string>>;
|
|
28
|
+
},
|
|
29
|
+
components: Record<string, SomaComponentDefinition>,
|
|
30
|
+
): void {
|
|
31
|
+
const issues: string[] = [];
|
|
32
|
+
for (const segment of [input.components, input.darkComponents]) {
|
|
33
|
+
if (!segment) continue;
|
|
34
|
+
for (const [componentName, tokens] of Object.entries(segment)) {
|
|
35
|
+
const definition = components[componentName];
|
|
36
|
+
if (!definition) {
|
|
37
|
+
issues.push(`未知组件 "${componentName}"`);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const declared = definition.contract.tokens ?? {};
|
|
41
|
+
for (const key of Object.keys(tokens ?? {})) {
|
|
42
|
+
if (!(key in declared)) {
|
|
43
|
+
const available = Object.keys(declared);
|
|
44
|
+
issues.push(
|
|
45
|
+
`"${componentName}" 没有声明可调令牌 "${key}"`
|
|
46
|
+
+ (available.length > 0
|
|
47
|
+
? `(它声明的是:${available.join(' / ')})`
|
|
48
|
+
: '(它一个可调令牌都没声明——组件令牌是组件自己的承诺,'
|
|
49
|
+
+ '不能由外面凭空指定)'),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (issues.length > 0) {
|
|
56
|
+
throw new SomaThemeComponentTokenError(
|
|
57
|
+
`主题的组件令牌段有 ${issues.length} 处问题:\n${issues.map((i) => ` · ${i}`).join('\n')}`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 契约自检:令牌键名格式 + default 必须引用语义令牌。返回问题清单 */
|
|
63
|
+
export function validateContractTokens(definition: SomaComponentDefinition): string[] {
|
|
64
|
+
const tokens = definition.contract.tokens;
|
|
65
|
+
if (!tokens) return [];
|
|
66
|
+
const issues: string[] = [];
|
|
67
|
+
const name = definition.contract.name;
|
|
68
|
+
for (const [key, token] of Object.entries(tokens)) {
|
|
69
|
+
if (!COMPONENT_TOKEN_KEY_PATTERN.test(key)) {
|
|
70
|
+
issues.push(`"${name}" 的令牌键 "${key}" 必须是 kebab-case`);
|
|
71
|
+
}
|
|
72
|
+
if (!token || typeof token.default !== 'string' || !token.default.includes('var(--soma-')) {
|
|
73
|
+
issues.push(
|
|
74
|
+
`"${name}.${key}" 的 default 必须引用语义令牌(形如 var(--soma-…))——`
|
|
75
|
+
+ '组件令牌是语义令牌的别名与收窄,不是新的真源',
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (!token?.desc?.trim()) {
|
|
79
|
+
issues.push(`"${name}.${key}" 缺少 desc(可调令牌要告诉使用者它调的是什么)`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return issues;
|
|
83
|
+
}
|