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.
Files changed (169) hide show
  1. package/README.md +20 -7
  2. package/app/SomaPageClient.tsx +4 -4
  3. package/app/components/SomaHostPage.tsx +5 -5
  4. package/app/components/SomaHydrationShell.tsx +4 -7
  5. package/app/globals.css +126 -3
  6. package/core/build/{v2 → blocks}/block-compile.ts +47 -18
  7. package/core/build/{v2/soma-file-loader.ts → blocks/block-container.ts} +28 -37
  8. package/core/build/{v2 → blocks}/block-loader.ts +1 -1
  9. package/core/build/{v2 → blocks}/html-index.ts +1 -1
  10. package/core/build/{v2/migrate-v1.ts → blocks/migrate-flat.ts} +27 -27
  11. package/core/build/compiler/host-config-sync.cjs +2 -9
  12. package/core/build/compiler/page-declaration-compiler.ts +32 -4
  13. package/core/build/compiler/soma-manifest-webpack-plugin.js +1 -1
  14. package/core/build/compiler/soma-precompile.js +2057 -35
  15. package/core/build/compiler/soma-precompile.ts +2 -2
  16. package/core/build/component/component-graph.ts +208 -0
  17. package/core/build/eject/component-eject.ts +473 -0
  18. package/core/build/eject/component-upgrade.ts +359 -0
  19. package/core/build/layout/constraint-solver.ts +235 -0
  20. package/core/build/layout/css-emitter.ts +116 -0
  21. package/core/build/layout/page-layout.ts +85 -0
  22. package/core/build/pack/pack-init.ts +265 -0
  23. package/core/client/base/SomaPageRootBase.ts +61 -0
  24. package/core/client/component/ai-catalog.ts +83 -8
  25. package/core/client/component/component-defaults.ts +100 -0
  26. package/core/client/component/component-definition.types.ts +39 -0
  27. package/core/client/component/component-manager.ts +79 -4
  28. package/core/client/component/component-pack.ts +126 -2
  29. package/core/client/component/contract-compat.ts +32 -3
  30. package/core/client/component/{example-v2.ts → example-blocks.ts} +19 -19
  31. package/core/client/component/keyboard-matrix.ts +144 -0
  32. package/core/client/component/pack-styles.ts +59 -1
  33. package/core/client/component/style-features.ts +131 -0
  34. package/core/client/component/theme-component-tokens.ts +83 -0
  35. package/core/client/data/data-object-resolver.ts +407 -4
  36. package/core/client/data/reaction-engine.ts +176 -0
  37. package/core/client/data/window-engine.ts +477 -0
  38. package/core/client/engine/event-engine.ts +98 -0
  39. package/core/client/engine/soma-bootstrap.ts +286 -7
  40. package/core/client/engine/step-runner.ts +134 -0
  41. package/core/client/engine/style-binder.ts +1 -1
  42. package/core/client/infra/notify/DomNotifyService.ts +2 -1
  43. package/core/client/infra/notify/INotifyTheme.ts +1 -1
  44. package/core/client/parser/{v2 → blocks}/namespace.ts +1 -1
  45. package/core/client/parser/{v2 → blocks}/page-parser.ts +30 -16
  46. package/core/client/parser/{v2 → blocks}/use-parser.ts +24 -25
  47. package/core/client/parser/data-config-parser.ts +316 -8
  48. package/core/client/parser/duration.ts +16 -0
  49. package/core/client/parser/layout-parser.ts +157 -0
  50. package/core/client/parser/page-state-parser.ts +166 -0
  51. package/core/client/parser/pipeline-parser.ts +76 -1
  52. package/core/client/parser/reaction-parser.ts +459 -0
  53. package/core/client/parser/reaction-sugar.ts +47 -0
  54. package/core/client/parser/step-parser.ts +359 -0
  55. package/core/client/parser/style-config-parser.ts +7 -7
  56. package/core/client/parser/ui-config-parser.ts +59 -0
  57. package/core/client/parser/value-lang.ts +1 -1
  58. package/core/client/parser/window-parser.ts +294 -0
  59. package/core/client/registry/index.ts +8 -0
  60. package/core/client/theme/theme-engine.ts +144 -16
  61. package/core/client/typography/typography-base.ts +1 -1
  62. package/core/index.ts +188 -13
  63. package/core/server/host/csp-nonce-metadata.ts +1 -1
  64. package/core/server/host/published-page-assets.ts +3 -8
  65. package/core/server/host/soma-page-compile.ts +1 -1
  66. package/core/server/index.ts +2 -2
  67. package/core/shared/block-errors.ts +11 -0
  68. package/core/shared/calc/coerce.ts +29 -0
  69. package/core/shared/calc/errors.ts +43 -0
  70. package/core/shared/calc/evaluator.ts +101 -0
  71. package/core/shared/calc/index.ts +12 -0
  72. package/core/shared/calc/lexer.ts +194 -0
  73. package/core/shared/calc/parser.ts +204 -0
  74. package/core/shared/calc/typer.ts +184 -0
  75. package/core/shared/path-algebra.ts +169 -0
  76. package/core/shared/predicates/index.ts +527 -0
  77. package/core/shared/published-page-route.ts +61 -0
  78. package/core/shared/types/adaptive.types.ts +1 -1
  79. package/core/shared/types/component-contract.types.ts +128 -2
  80. package/core/shared/types/data-config.types.ts +149 -2
  81. package/core/shared/types/domain.types.ts +13 -0
  82. package/core/shared/types/event.types.ts +22 -2
  83. package/core/shared/types/layout-constraint.types.ts +73 -0
  84. package/core/shared/types/page-state.types.ts +60 -0
  85. package/core/shared/types/reaction.types.ts +144 -0
  86. package/core/shared/types/scope-manifest.types.ts +11 -15
  87. package/core/shared/types/step.types.ts +91 -0
  88. package/core/shared/types/style-config.types.ts +4 -4
  89. package/core/shared/types/ui-config.types.ts +18 -0
  90. package/core/shared/types/window.types.ts +177 -0
  91. package/dist/cli/soma.js +28 -3
  92. package/dist/core/index.cjs +4829 -405
  93. package/dist/types/build/{v2 → blocks}/block-compile.d.ts +7 -7
  94. package/dist/types/build/{v2/soma-file-loader.d.ts → blocks/block-container.d.ts} +6 -11
  95. package/dist/types/build/{v2/migrate-v1.d.ts → blocks/migrate-flat.d.ts} +6 -6
  96. package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -0
  97. package/dist/types/build/component/component-graph.d.ts +85 -0
  98. package/dist/types/build/eject/component-eject.d.ts +121 -0
  99. package/dist/types/build/eject/component-upgrade.d.ts +43 -0
  100. package/dist/types/build/layout/constraint-solver.d.ts +36 -0
  101. package/dist/types/build/layout/css-emitter.d.ts +18 -0
  102. package/dist/types/build/layout/page-layout.d.ts +24 -0
  103. package/dist/types/build/pack/pack-init.d.ts +8 -0
  104. package/dist/types/client/base/SomaPageRootBase.d.ts +28 -0
  105. package/dist/types/client/component/ai-catalog.d.ts +24 -1
  106. package/dist/types/client/component/component-defaults.d.ts +15 -0
  107. package/dist/types/client/component/component-definition.types.d.ts +38 -0
  108. package/dist/types/client/component/component-manager.d.ts +36 -0
  109. package/dist/types/client/component/component-pack.d.ts +32 -2
  110. package/dist/types/client/component/contract-compat.d.ts +2 -2
  111. package/dist/types/client/component/example-blocks.d.ts +9 -0
  112. package/dist/types/client/component/keyboard-matrix.d.ts +24 -0
  113. package/dist/types/client/component/pack-styles.d.ts +21 -0
  114. package/dist/types/client/component/style-features.d.ts +26 -0
  115. package/dist/types/client/component/theme-component-tokens.d.ts +14 -0
  116. package/dist/types/client/data/data-object-resolver.d.ts +106 -1
  117. package/dist/types/client/data/reaction-engine.d.ts +21 -0
  118. package/dist/types/client/data/window-engine.d.ts +27 -0
  119. package/dist/types/client/engine/event-engine.d.ts +15 -0
  120. package/dist/types/client/engine/soma-bootstrap.d.ts +70 -0
  121. package/dist/types/client/engine/step-runner.d.ts +24 -0
  122. package/dist/types/client/infra/notify/DomNotifyService.d.ts +2 -1
  123. package/dist/types/client/infra/notify/INotifyTheme.d.ts +1 -1
  124. package/dist/types/client/parser/{v2 → blocks}/page-parser.d.ts +2 -2
  125. package/dist/types/client/parser/{v2 → blocks}/use-parser.d.ts +3 -3
  126. package/dist/types/client/parser/duration.d.ts +1 -0
  127. package/dist/types/client/parser/layout-parser.d.ts +9 -0
  128. package/dist/types/client/parser/page-state-parser.d.ts +32 -0
  129. package/dist/types/client/parser/reaction-parser.d.ts +16 -0
  130. package/dist/types/client/parser/reaction-sugar.d.ts +13 -0
  131. package/dist/types/client/parser/step-parser.d.ts +23 -0
  132. package/dist/types/client/parser/style-config-parser.d.ts +1 -1
  133. package/dist/types/client/parser/ui-config-parser.d.ts +5 -0
  134. package/dist/types/client/parser/window-parser.d.ts +25 -0
  135. package/dist/types/client/registry/index.d.ts +1 -0
  136. package/dist/types/client/theme/theme-engine.d.ts +37 -0
  137. package/dist/types/index.d.ts +40 -9
  138. package/dist/types/server/host/published-page-assets.d.ts +3 -2
  139. package/dist/types/server/index.d.ts +1 -1
  140. package/dist/types/shared/block-errors.d.ts +5 -0
  141. package/dist/types/shared/calc/coerce.d.ts +4 -0
  142. package/dist/types/shared/calc/errors.d.ts +14 -0
  143. package/dist/types/shared/calc/evaluator.d.ts +2 -0
  144. package/dist/types/shared/calc/index.d.ts +6 -0
  145. package/dist/types/shared/calc/lexer.d.ts +20 -0
  146. package/dist/types/shared/calc/parser.d.ts +49 -0
  147. package/dist/types/shared/calc/typer.d.ts +16 -0
  148. package/dist/types/shared/path-algebra.d.ts +56 -0
  149. package/dist/types/shared/predicates/index.d.ts +79 -0
  150. package/dist/types/shared/published-page-route.d.ts +21 -0
  151. package/dist/types/shared/types/component-contract.types.d.ts +112 -0
  152. package/dist/types/shared/types/data-config.types.d.ts +132 -2
  153. package/dist/types/shared/types/domain.types.d.ts +12 -0
  154. package/dist/types/shared/types/event.types.d.ts +8 -2
  155. package/dist/types/shared/types/layout-constraint.types.d.ts +52 -0
  156. package/dist/types/shared/types/page-state.types.d.ts +49 -0
  157. package/dist/types/shared/types/reaction.types.d.ts +115 -0
  158. package/dist/types/shared/types/scope-manifest.types.d.ts +11 -11
  159. package/dist/types/shared/types/step.types.d.ts +76 -0
  160. package/dist/types/shared/types/style-config.types.d.ts +4 -4
  161. package/dist/types/shared/types/ui-config.types.d.ts +18 -0
  162. package/dist/types/shared/types/window.types.d.ts +171 -0
  163. package/package.json +2 -6
  164. package/dist/types/client/component/example-v2.d.ts +0 -9
  165. package/postcss.config.js +0 -6
  166. package/tailwind.config.ts +0 -43
  167. /package/dist/types/build/{v2 → blocks}/block-loader.d.ts +0 -0
  168. /package/dist/types/build/{v2 → blocks}/html-index.d.ts +0 -0
  169. /package/dist/types/client/parser/{v2 → blocks}/namespace.d.ts +0 -0
@@ -0,0 +1,157 @@
1
+ // `layout:` 段解析(REQ-010 S3)——约束布局的声明入口。
2
+ //
3
+ // 词汇是闭集(sizing 三档、priority 四档、wrap 三档、align 七档),
4
+ // 所以「这个布局解不解得出」在**声明期**就有答案:求解器对八档容器宽各解一次,
5
+ // 任一档不可满足就当场指出是哪两条声明打架。
6
+ //
7
+ // 对比手写容器查询:作者要在四五段查询里各写一遍 flex 值,段与段之间的一致性靠人维护,
8
+ // 而它们坏起来是"某一档下侧栏比 min 还窄"——只有把窗口拖到那一档才看得见。
9
+
10
+ import {
11
+ SOMA_LAYOUT_ALIGNS,
12
+ SOMA_LAYOUT_PRIORITIES,
13
+ type SomaAlign,
14
+ type SomaLayoutConstraint,
15
+ type SomaLayoutNodeConstraint,
16
+ type SomaPriority,
17
+ type SomaSizing,
18
+ type SomaWrap,
19
+ } from '../../shared/types/layout-constraint.types';
20
+
21
+ export interface LayoutParseInput {
22
+ raw: unknown;
23
+ path: string;
24
+ }
25
+
26
+ export interface LayoutParseDeps {
27
+ fail(message: string, path: string): never;
28
+ }
29
+
30
+ const SIZINGS = new Set<string>(['fixed', 'hug', 'fill']);
31
+ const WRAPS = new Set<string>(['none', 'below', 'reel']);
32
+ const NODE_KEYS = new Set(['sizing', 'width', 'min', 'max', 'priority']);
33
+ const SECTION_KEYS = new Set(['gap', 'wrap', 'align', 'container_name', 'nodes']);
34
+ const NAME_PATTERN = /^[a-z][a-z0-9_]*$/;
35
+
36
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
37
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
38
+ }
39
+
40
+ export function parseLayoutConstraint(
41
+ input: LayoutParseInput,
42
+ deps: LayoutParseDeps,
43
+ ): SomaLayoutConstraint {
44
+ const { raw, path } = input;
45
+ const fail: (message: string, at: string) => never = deps.fail;
46
+
47
+ if (!isPlainObject(raw)) fail('layout 必须是映射', path);
48
+
49
+ // 两种写法:显式 `nodes: { … }`,或把子项直接摊在段上(更常见也更短)。
50
+ // 摊平写法要把已知的段级键排除掉——否则 `gap` 会被当成一个叫 gap 的子项,
51
+ // 而那个"子项"没有 sizing,报错会指向一个作者根本没写的东西。
52
+ const explicit = isPlainObject(raw.nodes) ? (raw.nodes as Record<string, unknown>) : null;
53
+ const source = explicit ?? Object.fromEntries(
54
+ Object.entries(raw).filter(([key]) => !SECTION_KEYS.has(key)),
55
+ );
56
+
57
+ if (explicit === null) {
58
+ for (const key of Object.keys(raw)) {
59
+ if (SECTION_KEYS.has(key)) continue;
60
+ if (!NAME_PATTERN.test(key)) {
61
+ fail(
62
+ `${path}.${key} 既不是段级键(${[...SECTION_KEYS].join(' / ')}),`
63
+ + '也不是合法子项名(小写字母开头、下划线分隔)',
64
+ `${path}.${key}`,
65
+ );
66
+ }
67
+ }
68
+ }
69
+
70
+ const nodes: Record<string, SomaLayoutNodeConstraint> = {};
71
+ for (const [name, rawNode] of Object.entries(source)) {
72
+ const at = `${path}.${name}`;
73
+ if (!isPlainObject(rawNode)) fail(`${at} 必须是映射`, at);
74
+ for (const key of Object.keys(rawNode)) {
75
+ if (!NODE_KEYS.has(key)) {
76
+ fail(`${at} 有未知键 "${key}"(支持: ${[...NODE_KEYS].join(' / ')})`, `${at}.${key}`);
77
+ }
78
+ }
79
+ const sizing = String(rawNode.sizing ?? '');
80
+ if (!SIZINGS.has(sizing)) {
81
+ fail(
82
+ `${at}.sizing 只能是 ${[...SIZINGS].join(' / ')}(收到 ${JSON.stringify(rawNode.sizing)})`
83
+ + '——fixed 是"就这么宽",hug 是"内容多宽我多宽",fill 是"剩下的给我"',
84
+ `${at}.sizing`,
85
+ );
86
+ }
87
+ const node: SomaLayoutNodeConstraint = { sizing: sizing as SomaSizing };
88
+ for (const key of ['width', 'min', 'max'] as const) {
89
+ if (rawNode[key] === undefined) continue;
90
+ const literal = String(rawNode[key]);
91
+ if (!/^\d+(?:\.\d+)?(rem|%|cqi)$/.test(literal)) {
92
+ fail(
93
+ `${at}.${key} 的长度 "${literal}" 非法——只认 rem / % / cqi(单位必填,不猜)`,
94
+ `${at}.${key}`,
95
+ );
96
+ }
97
+ node[key] = literal;
98
+ }
99
+ if (rawNode.priority !== undefined) {
100
+ const priority = Number(rawNode.priority);
101
+ if (!(SOMA_LAYOUT_PRIORITIES as readonly number[]).includes(priority)) {
102
+ fail(
103
+ `${at}.priority 只能是 ${SOMA_LAYOUT_PRIORITIES.join(' / ')}(收到 ${String(rawNode.priority)})`
104
+ + '——250 是"我愿意被拉大",750 是"我不愿意被压小",1000 是不可违反',
105
+ `${at}.priority`,
106
+ );
107
+ }
108
+ node.priority = priority as SomaPriority;
109
+ }
110
+ nodes[name] = node;
111
+ }
112
+
113
+ if (Object.keys(nodes).length === 0) {
114
+ fail(`${path} 至少要有一个子项`, path);
115
+ }
116
+
117
+ const constraint: SomaLayoutConstraint = { nodes };
118
+
119
+ if (raw.gap !== undefined) {
120
+ const gap = String(raw.gap);
121
+ if (!/^\d+(?:\.\d+)?(rem|%|cqi)$/.test(gap)) {
122
+ fail(`${path}.gap 的长度 "${gap}" 非法——只认 rem / % / cqi`, `${path}.gap`);
123
+ }
124
+ constraint.gap = gap;
125
+ }
126
+ if (raw.wrap !== undefined) {
127
+ const wrap = String(raw.wrap);
128
+ if (!WRAPS.has(wrap)) {
129
+ fail(`${path}.wrap 只能是 ${[...WRAPS].join(' / ')}(收到 ${JSON.stringify(raw.wrap)})`, `${path}.wrap`);
130
+ }
131
+ constraint.wrap = wrap as SomaWrap;
132
+ }
133
+ if (raw.align !== undefined) {
134
+ const align = String(raw.align);
135
+ if (!(SOMA_LAYOUT_ALIGNS as readonly string[]).includes(align)) {
136
+ fail(
137
+ `${path}.align 只能是 ${SOMA_LAYOUT_ALIGNS.join(' / ')}(收到 ${JSON.stringify(raw.align)})`,
138
+ `${path}.align`,
139
+ );
140
+ }
141
+ constraint.align = align as SomaAlign;
142
+ }
143
+ if (raw.container_name !== undefined) {
144
+ const containerName = String(raw.container_name);
145
+ // 具名容器要能被子孙的 `@container <name> (…)` 引用,所以它必须是一个合法的 CSS 标识符
146
+ if (!/^[a-z][a-z0-9-]*$/.test(containerName)) {
147
+ fail(
148
+ `${path}.container_name "${containerName}" 不是合法的容器名`
149
+ + '(小写字母开头、连字符分隔;它要出现在 CSS 的 container-name 上)',
150
+ `${path}.container_name`,
151
+ );
152
+ }
153
+ constraint.container_name = containerName;
154
+ }
155
+
156
+ return constraint;
157
+ }
@@ -0,0 +1,166 @@
1
+ // `state:` 段解析(REQ-010 F-37)。
2
+ //
3
+ // 这一段的价值全在**校验**上:切片名要指向真实存在的数据对象与表格锚点。
4
+ // 不校验的话,一个拼错的窗口名会让「恢复状态」静默少恢复一块——
5
+ // 用户打开分享链接,看到的是一屏"差不多但不完全对"的东西,而没有任何提示。
6
+
7
+ import type { SomaPageState, SomaPageStateConfig } from '../../shared/types/page-state.types';
8
+
9
+ export interface PageStateParseInput {
10
+ raw: unknown;
11
+ /** 已声明的数据对象名(`window` 切片只能指向它们) */
12
+ objectNames: readonly string[];
13
+ /** 页面上的锚点 id(`columns` 切片只能指向它们);不给就跳过这一层校验 */
14
+ anchorIds?: readonly string[];
15
+ path: string;
16
+ }
17
+
18
+ export interface PageStateParseDeps {
19
+ fail(message: string, path: string): never;
20
+ }
21
+
22
+ const CONFIG_KEYS = new Set(['version', 'window', 'columns', 'density', 'expanded', 'initial_state', 'url']);
23
+ const DENSITIES = new Set(['sm', 'md', 'lg']);
24
+
25
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
26
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
27
+ }
28
+
29
+ export function parsePageState(
30
+ input: PageStateParseInput,
31
+ deps: PageStateParseDeps,
32
+ ): SomaPageStateConfig {
33
+ const { raw, objectNames, anchorIds, path } = input;
34
+ const fail: (message: string, at: string) => never = deps.fail;
35
+
36
+ if (!isPlainObject(raw)) fail('state 必须是映射', path);
37
+ for (const key of Object.keys(raw)) {
38
+ if (!CONFIG_KEYS.has(key)) {
39
+ fail(`未知的 state 字段 "${key}"(支持: ${[...CONFIG_KEYS].join(' / ')})`, `${path}.${key}`);
40
+ }
41
+ }
42
+
43
+ // version 必填:恢复一份旧结构的状态会让界面进入一个谁都没设计过的样子,
44
+ // 而那种坏法是静默的(列还在、但顺序是乱的)。没有版本就没法判断该不该丢弃。
45
+ const version = raw.version;
46
+ if (!Number.isInteger(version) || (version as number) < 1) {
47
+ fail('state.version 必填,且必须是 ≥ 1 的整数(它决定一份旧状态该不该被丢弃)', `${path}.version`);
48
+ }
49
+
50
+ const names = (value: unknown, at: string, allowed: readonly string[], what: string): string[] => {
51
+ if (value === undefined) return [];
52
+ if (!Array.isArray(value)) fail(`${at} 必须是名字数组`, at);
53
+ return (value as unknown[]).map((entry) => {
54
+ const name = String(entry);
55
+ if (!allowed.includes(name)) {
56
+ fail(`${at} 里的 "${name}" 不是${what}(可选:${allowed.join(' / ') || '无'})`, at);
57
+ }
58
+ return name;
59
+ });
60
+ };
61
+
62
+ const windows = names(raw.window, `${path}.window`, objectNames, '本页声明过的数据对象');
63
+ const columns = anchorIds === undefined
64
+ ? (Array.isArray(raw.columns) ? (raw.columns as unknown[]).map(String) : [])
65
+ : names(raw.columns, `${path}.columns`, anchorIds, '本页的锚点');
66
+
67
+ const config: SomaPageStateConfig = { version: version as number };
68
+ if (windows.length > 0) config.window = windows;
69
+ if (columns.length > 0) config.columns = columns;
70
+ if (raw.density !== undefined) config.density = raw.density === true;
71
+ if (raw.expanded !== undefined) config.expanded = raw.expanded === true;
72
+ if (raw.url !== undefined) config.url = raw.url === true;
73
+
74
+ if (raw.initial_state !== undefined) {
75
+ config.initial_state = parseStateValue(raw.initial_state, `${path}.initial_state`, config, fail);
76
+ }
77
+ return config;
78
+ }
79
+
80
+ /**
81
+ * 校验一份具体的状态值(`initial_state`、URL 还原、`setState` 入参共用)。
82
+ *
83
+ * 越界的切片**丢弃并点名**,而不是整份拒收:一份来自旧版本 URL 的状态里多半只有
84
+ * 一两块对不上,整份丢掉等于让分享链接彻底失效——而它本来还能恢复大半。
85
+ */
86
+ export function parseStateValue(
87
+ raw: unknown,
88
+ path: string,
89
+ config: SomaPageStateConfig,
90
+ fail: (message: string, at: string) => never,
91
+ ): SomaPageState {
92
+ if (!isPlainObject(raw)) fail(`${path} 必须是映射`, path);
93
+ const state: SomaPageState = { version: config.version };
94
+
95
+ if (raw.version !== undefined && raw.version !== config.version) {
96
+ // 版本对不上:这里不抛(`initial_state` 是作者自己写的,抛得有道理;
97
+ // 但同一个函数也服务于 URL 还原,那里抛就等于让一条旧链接打不开页面)。
98
+ // 交给调用方按来源决定,函数只负责如实标注。
99
+ state.version = raw.version as number;
100
+ return state;
101
+ }
102
+
103
+ if (isPlainObject(raw.window)) {
104
+ const allowed = config.window ?? [];
105
+ const windows: Record<string, unknown> = {};
106
+ for (const [name, slice] of Object.entries(raw.window)) {
107
+ if (!allowed.includes(name)) continue; // 越界切片丢弃:见上文
108
+ windows[name] = slice;
109
+ }
110
+ if (Object.keys(windows).length > 0) {
111
+ state.window = windows as unknown as SomaPageState['window'];
112
+ }
113
+ }
114
+
115
+ if (isPlainObject(raw.columns)) {
116
+ const allowed = config.columns ?? [];
117
+ const columns: Record<string, unknown> = {};
118
+ for (const [name, slice] of Object.entries(raw.columns)) {
119
+ if (!allowed.includes(name)) continue;
120
+ columns[name] = slice;
121
+ }
122
+ if (Object.keys(columns).length > 0) {
123
+ state.columns = columns as unknown as SomaPageState['columns'];
124
+ }
125
+ }
126
+
127
+ if (config.density && typeof raw.density === 'string' && DENSITIES.has(raw.density)) {
128
+ state.density = raw.density as SomaPageState['density'];
129
+ }
130
+ if (config.expanded && Array.isArray(raw.expanded)) {
131
+ state.expanded = (raw.expanded as unknown[]).map(String);
132
+ }
133
+ return state;
134
+ }
135
+
136
+ /**
137
+ * 页面态 ⇄ URL 的紧凑序列化。
138
+ *
139
+ * 用 base64url 包一层 JSON 而不是把每个切片摊成 query 参数:状态是树形的
140
+ * (`window.orders.filter` 是一棵递归 AST),摊平会得到一串 `window.orders.filter.conditions[0].op=in`
141
+ * 这样的东西——既难读,也会撞上 URL 长度与转义的一堆边角。
142
+ *
143
+ * 不用 `btoa`:它只吃 Latin-1,中文过滤值会直接抛。
144
+ */
145
+ export function encodePageState(state: SomaPageState): string {
146
+ const json = JSON.stringify(state);
147
+ const bytes = new TextEncoder().encode(json);
148
+ let binary = '';
149
+ for (const byte of bytes) binary += String.fromCharCode(byte);
150
+ return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
151
+ }
152
+
153
+ /** URL → 页面态;坏串返回 null(一条被截断的链接不该让页面打不开) */
154
+ export function decodePageState(encoded: string): SomaPageState | null {
155
+ try {
156
+ const padded = encoded.replace(/-/g, '+').replace(/_/g, '/');
157
+ const binary = atob(padded + '='.repeat((4 - (padded.length % 4)) % 4));
158
+ const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
159
+ const parsed = JSON.parse(new TextDecoder().decode(bytes));
160
+ return isPlainObject(parsed) && typeof parsed.version === 'number'
161
+ ? (parsed as unknown as SomaPageState)
162
+ : null;
163
+ } catch {
164
+ return null;
165
+ }
166
+ }
@@ -1,6 +1,7 @@
1
1
  // core/client/parser/pipeline-parser.ts — SOMA Pipeline DSL 解析器
2
2
  // 负责解析 _event.yaml 中的 Pipeline 表达式,将其转换为类型安全的 ParsedEvent 结构
3
3
 
4
+ import { parseSteps } from './step-parser';
4
5
  import type {
5
6
  ParsedEvent,
6
7
  EventType,
@@ -32,6 +33,7 @@ const VALID_TYPES = new Set<EventType>([
32
33
  'NAV_ACTION',
33
34
  'UI_REFRESH',
34
35
  'UI_INTERACT',
36
+ 'ACTION',
35
37
  ]);
36
38
 
37
39
  /** 支持的 DOM 事件 */
@@ -42,7 +44,10 @@ const VALID_DOM_EVENTS = new Set<string>([
42
44
  ]);
43
45
 
44
46
  /** 支持的 UI 交互动作 */
45
- const VALID_ACTIONS = new Set<InteractAction>(['open', 'close', 'toggle']);
47
+ const VALID_ACTIONS = new Set<InteractAction>([
48
+ 'open', 'close', 'toggle',
49
+ 'focus', 'scroll_to', 'select',
50
+ ]);
46
51
 
47
52
  /** 组件契约 emit 名(两段式触发器放行;运行时由组件管理器标记路由) */
48
53
  const EMIT_NAME_PATTERN = /^[a-z][a-z0-9_]*$/;
@@ -52,6 +57,39 @@ export function isKnownDomEvent(name: string): boolean {
52
57
  return VALID_DOM_EVENTS.has(name);
53
58
  }
54
59
 
60
+ /** `{ steps: [...] }` 形态识别:值是对象且恰好带一个数组型 `steps` 键 */
61
+ function isStepsDeclaration(value: unknown): value is { steps: unknown[] } {
62
+ return value !== null
63
+ && typeof value === 'object'
64
+ && !Array.isArray(value)
65
+ && Array.isArray((value as { steps?: unknown }).steps);
66
+ }
67
+
68
+ /**
69
+ * 从步骤声明里收集 `when` 引用到的根名,当作载荷字段的作用域。
70
+ *
71
+ * 事件载荷的字段在编译期不可知——它来自组件契约的 emit payload,而 `_event.yaml`
72
+ * 手上没有契约。给空作用域的话,每一条 `when` 都会被判成"引用了不存在的字段",
73
+ * 编排事实上就写不出来了。所以这里放行载荷引用,只保留**槽的前向引用**这条真正
74
+ * 只靠步骤本身就能判的规则;载荷字段名对不对,归组件契约那一侧管。
75
+ */
76
+ function collectWhenRoots(steps: unknown): string[] {
77
+ const roots = new Set<string>();
78
+ const walk = (node: unknown): void => {
79
+ if (Array.isArray(node)) {
80
+ for (const item of node) walk(item);
81
+ return;
82
+ }
83
+ if (node === null || typeof node !== 'object') return;
84
+ for (const [key, value] of Object.entries(node)) {
85
+ if (key === 'field' && typeof value === 'string') roots.add(value.split('.')[0]);
86
+ else walk(value);
87
+ }
88
+ };
89
+ walk(steps);
90
+ return [...roots];
91
+ }
92
+
55
93
  /** Pipeline optional confirm segment prefix */
56
94
  const CONFIRM_PREFIX = 'confirm:';
57
95
 
@@ -249,6 +287,19 @@ export function parsePipelineBody(
249
287
  result.funcPath = parts[1];
250
288
  break;
251
289
 
290
+ case 'ACTION':
291
+ // `ACTION | <名字>`:名字存不存在由 bootstrap 的注册表判(解析器手上没有注册表)。
292
+ // 这里只保证它是个合法标识符——空名字或带管道符的名字往下传,
293
+ // 报出来的会是「未注册的动作 ""」,那条信息帮不上任何忙。
294
+ if (!parts[1] || !/^[a-z][a-zA-Z0-9_]*$/.test(parts[1])) {
295
+ throw new SomaEventParseError(
296
+ `ACTION 缺少合法的动作名(小写字母开头的标识符),收到 "${parts[1] ?? ''}"`,
297
+ expression,
298
+ );
299
+ }
300
+ result.funcPath = parts[1];
301
+ break;
302
+
252
303
  case 'UI_INTERACT':
253
304
  result.targetId = parts[1] || undefined;
254
305
  result.funcPath = parts[2] || null;
@@ -344,6 +395,30 @@ export function parseEventConfig(
344
395
  }
345
396
  throw error;
346
397
  }
398
+ } else if (isStepsDeclaration(value)) {
399
+ // 编排事件(REQ-010 F-13):`{ steps: [...] }`。
400
+ // 它长得像"嵌套层级",所以必须先于递归分支判掉——否则 `steps` 会被当成
401
+ // 一个子锚点名递归下去,而数组值又不是字符串,于是整条声明被静默丢弃。
402
+ const { somaId, domEvent, keyModifier } = parseSelector(key);
403
+ const graph = parseSteps(
404
+ {
405
+ raw: (value as { steps: unknown }).steps,
406
+ // 事件载荷的字段在编译期不可知(它来自组件契约的 emit payload)。
407
+ // 给空作用域会把所有 `when` 都判成越界,所以这里放行载荷引用、
408
+ // 只校验槽的前向引用——载荷字段名的校验归组件契约那一侧。
409
+ payloadFields: collectWhenRoots((value as { steps: unknown }).steps),
410
+ path: `${[...parentPath, key].join(' > ')}.steps`,
411
+ },
412
+ {
413
+ fail(message: string): never {
414
+ throw new SomaEventParseError(message, key);
415
+ },
416
+ parsePipeline: parsePipelineBody,
417
+ },
418
+ );
419
+ const event: ParsedEvent = { somaId, domEvent, type: 'STEPS', steps: graph };
420
+ if (keyModifier) event.keyModifier = keyModifier;
421
+ events.push(event);
347
422
  } else if (typeof value === 'object' && value !== null) {
348
423
  // 嵌套层级:递归解析
349
424
  events.push(