soma-core 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/README.md +1 -1
  2. package/core/build/blocks/block-compile.ts +31 -2
  3. package/core/build/blocks/block-container.ts +2 -11
  4. package/core/build/compiler/page-declaration-compiler.ts +30 -2
  5. package/core/build/compiler/soma-precompile.js +2053 -23
  6. package/core/build/component/component-graph.ts +208 -0
  7. package/core/build/component/package-loader.ts +32 -0
  8. package/core/build/eject/component-eject.ts +473 -0
  9. package/core/build/eject/component-upgrade.ts +359 -0
  10. package/core/build/layout/constraint-solver.ts +235 -0
  11. package/core/build/layout/css-emitter.ts +116 -0
  12. package/core/build/layout/page-layout.ts +85 -0
  13. package/core/build/pack/pack-init.ts +270 -0
  14. package/core/client/base/SomaHttpClient.ts +34 -1
  15. package/core/client/base/SomaPageRootBase.ts +61 -0
  16. package/core/client/component/ai-catalog.ts +121 -4
  17. package/core/client/component/component-defaults.ts +100 -0
  18. package/core/client/component/component-definition.types.ts +39 -0
  19. package/core/client/component/component-manager.ts +79 -4
  20. package/core/client/component/component-pack.ts +126 -2
  21. package/core/client/component/keyboard-matrix.ts +144 -0
  22. package/core/client/component/pack-styles.ts +59 -1
  23. package/core/client/component/style-features.ts +131 -0
  24. package/core/client/component/theme-component-tokens.ts +83 -0
  25. package/core/client/component/verify-pack.ts +120 -1
  26. package/core/client/container/SomaContainerAdapter.ts +6 -2
  27. package/core/client/data/data-object-resolver.ts +407 -4
  28. package/core/client/data/reaction-engine.ts +176 -0
  29. package/core/client/data/window-engine.ts +477 -0
  30. package/core/client/engine/event-engine.ts +98 -0
  31. package/core/client/engine/soma-bootstrap.ts +265 -5
  32. package/core/client/engine/step-runner.ts +134 -0
  33. package/core/client/infra/http/SomaHttpService.ts +2 -2
  34. package/core/client/parser/blocks/page-parser.ts +15 -1
  35. package/core/client/parser/blocks/use-parser.ts +5 -12
  36. package/core/client/parser/data-config-parser.ts +316 -8
  37. package/core/client/parser/duration.ts +16 -0
  38. package/core/client/parser/layout-parser.ts +157 -0
  39. package/core/client/parser/page-state-parser.ts +166 -0
  40. package/core/client/parser/pipeline-parser.ts +76 -1
  41. package/core/client/parser/reaction-parser.ts +459 -0
  42. package/core/client/parser/reaction-sugar.ts +47 -0
  43. package/core/client/parser/step-parser.ts +359 -0
  44. package/core/client/parser/ui-config-parser.ts +59 -0
  45. package/core/client/parser/window-parser.ts +294 -0
  46. package/core/client/theme/theme-engine.ts +102 -2
  47. package/core/index.ts +184 -0
  48. package/core/shared/block-errors.ts +11 -0
  49. package/core/shared/calc/coerce.ts +29 -0
  50. package/core/shared/calc/errors.ts +43 -0
  51. package/core/shared/calc/evaluator.ts +101 -0
  52. package/core/shared/calc/index.ts +12 -0
  53. package/core/shared/calc/lexer.ts +194 -0
  54. package/core/shared/calc/parser.ts +204 -0
  55. package/core/shared/calc/typer.ts +184 -0
  56. package/core/shared/component/derive-sample.ts +105 -0
  57. package/core/shared/component/field-lines.ts +473 -0
  58. package/core/shared/path-algebra.ts +169 -0
  59. package/core/shared/predicates/index.ts +527 -0
  60. package/core/shared/types/component-contract.types.ts +151 -9
  61. package/core/shared/types/data-config.types.ts +120 -1
  62. package/core/shared/types/domain.types.ts +13 -0
  63. package/core/shared/types/event.types.ts +22 -2
  64. package/core/shared/types/layout-constraint.types.ts +73 -0
  65. package/core/shared/types/page-state.types.ts +60 -0
  66. package/core/shared/types/reaction.types.ts +144 -0
  67. package/core/shared/types/step.types.ts +91 -0
  68. package/core/shared/types/ui-config.types.ts +18 -0
  69. package/core/shared/types/window.types.ts +177 -0
  70. package/dist/cli/soma.js +13 -1
  71. package/dist/core/index.cjs +5529 -631
  72. package/dist/types/build/blocks/block-container.d.ts +1 -6
  73. package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -0
  74. package/dist/types/build/component/component-graph.d.ts +85 -0
  75. package/dist/types/build/eject/component-eject.d.ts +121 -0
  76. package/dist/types/build/eject/component-upgrade.d.ts +43 -0
  77. package/dist/types/build/layout/constraint-solver.d.ts +36 -0
  78. package/dist/types/build/layout/css-emitter.d.ts +18 -0
  79. package/dist/types/build/layout/page-layout.d.ts +24 -0
  80. package/dist/types/build/pack/pack-init.d.ts +8 -0
  81. package/dist/types/client/base/SomaHttpClient.d.ts +8 -0
  82. package/dist/types/client/base/SomaPageRootBase.d.ts +28 -0
  83. package/dist/types/client/component/ai-catalog.d.ts +33 -1
  84. package/dist/types/client/component/component-defaults.d.ts +15 -0
  85. package/dist/types/client/component/component-definition.types.d.ts +38 -0
  86. package/dist/types/client/component/component-manager.d.ts +36 -0
  87. package/dist/types/client/component/component-pack.d.ts +32 -2
  88. package/dist/types/client/component/keyboard-matrix.d.ts +24 -0
  89. package/dist/types/client/component/pack-styles.d.ts +21 -0
  90. package/dist/types/client/component/style-features.d.ts +26 -0
  91. package/dist/types/client/component/theme-component-tokens.d.ts +14 -0
  92. package/dist/types/client/component/verify-pack.d.ts +10 -0
  93. package/dist/types/client/data/data-object-resolver.d.ts +106 -1
  94. package/dist/types/client/data/reaction-engine.d.ts +21 -0
  95. package/dist/types/client/data/window-engine.d.ts +27 -0
  96. package/dist/types/client/engine/event-engine.d.ts +15 -0
  97. package/dist/types/client/engine/soma-bootstrap.d.ts +70 -0
  98. package/dist/types/client/engine/step-runner.d.ts +24 -0
  99. package/dist/types/client/parser/blocks/use-parser.d.ts +2 -2
  100. package/dist/types/client/parser/duration.d.ts +1 -0
  101. package/dist/types/client/parser/layout-parser.d.ts +9 -0
  102. package/dist/types/client/parser/page-state-parser.d.ts +32 -0
  103. package/dist/types/client/parser/reaction-parser.d.ts +16 -0
  104. package/dist/types/client/parser/reaction-sugar.d.ts +13 -0
  105. package/dist/types/client/parser/step-parser.d.ts +23 -0
  106. package/dist/types/client/parser/ui-config-parser.d.ts +5 -0
  107. package/dist/types/client/parser/window-parser.d.ts +25 -0
  108. package/dist/types/client/theme/theme-engine.d.ts +26 -0
  109. package/dist/types/index.d.ts +35 -2
  110. package/dist/types/shared/block-errors.d.ts +5 -0
  111. package/dist/types/shared/calc/coerce.d.ts +4 -0
  112. package/dist/types/shared/calc/errors.d.ts +14 -0
  113. package/dist/types/shared/calc/evaluator.d.ts +2 -0
  114. package/dist/types/shared/calc/index.d.ts +6 -0
  115. package/dist/types/shared/calc/lexer.d.ts +20 -0
  116. package/dist/types/shared/calc/parser.d.ts +49 -0
  117. package/dist/types/shared/calc/typer.d.ts +16 -0
  118. package/dist/types/shared/component/derive-sample.d.ts +23 -0
  119. package/dist/types/shared/component/field-lines.d.ts +19 -0
  120. package/dist/types/shared/path-algebra.d.ts +56 -0
  121. package/dist/types/shared/predicates/index.d.ts +79 -0
  122. package/dist/types/shared/types/component-contract.types.d.ts +134 -6
  123. package/dist/types/shared/types/data-config.types.d.ts +115 -1
  124. package/dist/types/shared/types/domain.types.d.ts +12 -0
  125. package/dist/types/shared/types/event.types.d.ts +8 -2
  126. package/dist/types/shared/types/layout-constraint.types.d.ts +52 -0
  127. package/dist/types/shared/types/page-state.types.d.ts +49 -0
  128. package/dist/types/shared/types/reaction.types.d.ts +115 -0
  129. package/dist/types/shared/types/step.types.d.ts +76 -0
  130. package/dist/types/shared/types/ui-config.types.d.ts +18 -0
  131. package/dist/types/shared/types/window.types.d.ts +171 -0
  132. package/package.json +3 -2
@@ -7,10 +7,16 @@
7
7
  // 纪律(D10):目录永远从契约生成,禁止手工维护第二份副本。
8
8
 
9
9
  import { deriveBlockExample } from './example-blocks';
10
- import type {
11
- SomaComponentContract,
12
- SomaGeometryFamily,
13
- SomaContractProp,
10
+ import { renderFieldLines } from '../../shared/component/field-lines';
11
+ import { deriveContractSample, hasRenderableSample } from '../../shared/component/derive-sample';
12
+ import { renderKeyboardMatrixLine } from './keyboard-matrix';
13
+ import {
14
+ componentTokenVar,
15
+ type SomaComponentContract,
16
+ type SomaContractKeyboard,
17
+ type SomaContractOverlay,
18
+ type SomaGeometryFamily,
19
+ type SomaContractProp,
14
20
  } from '../../shared/types/component-contract.types';
15
21
  import type { SomaComponentDefinition } from './component-definition.types';
16
22
 
@@ -36,12 +42,46 @@ export interface SomaAiCatalogEntry {
36
42
  desc: string;
37
43
  }>;
38
44
  slots: Array<{ name: string; required: boolean; desc: string }>;
45
+ /**
46
+ * 键盘契约(REQ-011 / IK-2)。
47
+ *
48
+ * 这里**只存契约**:逐键矩阵是 `deriveKeyboardMatrix(contract)` 的返回值,
49
+ * 那一行紧凑键盘表是 `renderKeyboardMatrixLine(contract)` 的返回值——两者都是渲染,
50
+ * 存进来等于把同一个真值抄成三份,而这一轮要治的正是这种抄写。
51
+ * 人与模型读到的键盘表在 markdown 那一侧(检索喂给模型的就是它);
52
+ * 需要逐键结构的程序自己调那个函数(与 soma-ui 接线、闸门读的是同一个)。
53
+ */
54
+ keyboard?: { contract: SomaContractKeyboard };
55
+ /** 浮层行为契约(声明了才有) */
56
+ overlay?: SomaContractOverlay;
57
+ /** 契约级依赖(F-46):装这个还要装什么 */
58
+ uses?: string[];
59
+ /** 组件级令牌(M-1):这个组件开放的可调旋钮及其 CSS 变量名 */
60
+ tokens?: Array<{ name: string; cssVar: string; default: string; desc: string }>;
39
61
  ai?: {
40
62
  whenToUse: string;
41
63
  whenNotToUse?: string;
42
64
  example: string;
43
65
  antiPatterns?: string[];
44
66
  };
67
+ /**
68
+ * 字段行原文(REQ-012):对象 / 数组 prop 的逐字段形状与示例。
69
+ *
70
+ * 只在契约真写了嵌套形状或示例时出现——否则它与上面的 props 表逐字重复。
71
+ * 目录直接打印它,不另生成第二张字段表:作者写的和读者读到的是同一份东西。
72
+ */
73
+ spec?: string;
74
+ /** 派生样张(REQ-012):展示页与视觉矩阵喂给组件的那份 props */
75
+ sample?: Record<string, unknown>;
76
+ }
77
+
78
+ /** 契约里是否写了值得单独打印的字段形状(嵌套或示例) */
79
+ function hasFieldSpec(contract: SomaComponentContract): boolean {
80
+ const seen = (field: SomaContractProp): boolean => {
81
+ if (field.fields !== undefined || field.items !== undefined || field.example !== undefined) return true;
82
+ return false;
83
+ };
84
+ return Object.values(contract.props ?? {}).some(seen);
45
85
  }
46
86
 
47
87
  /**
@@ -110,6 +150,22 @@ export function buildCatalogEntry(contract: SomaComponentContract): SomaAiCatalo
110
150
  required: slot.required === true,
111
151
  desc: slot.desc ?? '',
112
152
  })),
153
+ // 四个可选段:缺席时连键都不写——目录 JSON 的形状对存量消费者逐键不变
154
+ ...(contract.keyboard
155
+ ? { keyboard: { contract: contract.keyboard } }
156
+ : {}),
157
+ ...(contract.overlay ? { overlay: contract.overlay } : {}),
158
+ ...(contract.uses && contract.uses.length > 0 ? { uses: [...contract.uses] } : {}),
159
+ ...(contract.tokens && Object.keys(contract.tokens).length > 0
160
+ ? {
161
+ tokens: Object.entries(contract.tokens).map(([name, token]) => ({
162
+ name,
163
+ cssVar: componentTokenVar(contract.name, name),
164
+ default: token.default,
165
+ desc: token.desc ?? '',
166
+ })),
167
+ }
168
+ : {}),
113
169
  ai: contract.ai
114
170
  ? {
115
171
  whenToUse: contract.ai.when_to_use,
@@ -118,6 +174,11 @@ export function buildCatalogEntry(contract: SomaComponentContract): SomaAiCatalo
118
174
  antiPatterns: contract.ai.anti_patterns,
119
175
  }
120
176
  : undefined,
177
+ // 两个可选段(REQ-012):缺席时连键都不写,目录 JSON 的形状对存量消费者逐键不变
178
+ ...(hasFieldSpec(contract) ? { spec: renderFieldLines(contract.props ?? {}) } : {}),
179
+ ...(hasRenderableSample(contract)
180
+ ? { sample: deriveContractSample(contract).props }
181
+ : {}),
121
182
  };
122
183
  }
123
184
 
@@ -166,6 +227,25 @@ export function generateAiCatalog(
166
227
  };
167
228
  }
168
229
 
230
+ const OVERLAY_KIND_LABEL: Record<SomaContractOverlay['kind'], string> = {
231
+ popup: '弹出层', modal: '模态', non_modal: '非模态', toast: '提示条',
232
+ };
233
+
234
+ /** 浮层语义一行:只印契约里写了的字段——没写的按 kind 缺省,目录不替组件承诺 */
235
+ function renderOverlayLine(overlay: SomaContractOverlay): string {
236
+ const parts: string[] = [OVERLAY_KIND_LABEL[overlay.kind]];
237
+ if (overlay.escape_key_behavior !== undefined) {
238
+ parts.push({ close: 'Esc 关闭', none: 'Esc 不关', defer: 'Esc 让给外层' }[overlay.escape_key_behavior]);
239
+ }
240
+ if (overlay.outside_press_behavior !== undefined) {
241
+ parts.push(overlay.outside_press_behavior === 'close' ? '点外面关闭' : '点外面不关');
242
+ }
243
+ if (overlay.hide_outside !== undefined) parts.push(overlay.hide_outside ? '打开时隔离外部' : '不隔离外部');
244
+ if (overlay.restore_focus !== undefined) parts.push(overlay.restore_focus ? '关闭后回焦' : '关闭后不回焦');
245
+ if (overlay.presence !== undefined) parts.push(overlay.presence ? '退场动画放完再卸载' : '立即卸载');
246
+ return `浮层:${parts.join('·')}`;
247
+ }
248
+
169
249
  function propTypeLabel(prop: SomaContractProp): string {
170
250
  if (prop.type === 'enum' && prop.values?.length) {
171
251
  return prop.values.join(' | ');
@@ -223,6 +303,23 @@ function renderMarkdown(entries: SomaAiCatalogEntry[], conventions?: string): st
223
303
  }
224
304
  }
225
305
 
306
+ // 字段形状与样张(REQ-012):对象 / 数组 prop 的内部长什么样、真实数据是什么。
307
+ // 上面的 props 表只说得清标量;`{ id, name, phone? }` 那种花括号提示机器读不出来。
308
+ if (entry.spec) {
309
+ lines.push('');
310
+ lines.push('字段形状(名: 类型, 描述, 必填 或 默认 x, 示例 y;缩进即嵌套):');
311
+ lines.push('```');
312
+ lines.push(entry.spec);
313
+ lines.push('```');
314
+ }
315
+ if (entry.sample) {
316
+ lines.push('');
317
+ lines.push('样张(由示例值派生,展示页与视觉矩阵喂的就是它):');
318
+ lines.push('```json');
319
+ lines.push(JSON.stringify(entry.sample, null, 2));
320
+ lines.push('```');
321
+ }
322
+
226
323
  if (entry.emits.length) {
227
324
  lines.push('');
228
325
  lines.push('| emit | payload | 说明 |');
@@ -240,6 +337,26 @@ function renderMarkdown(entries: SomaAiCatalogEntry[], conventions?: string): st
240
337
  );
241
338
  }
242
339
 
340
+ // 键盘表 / 浮层语义 / 依赖 / 令牌(REQ-011):各一行,声明了才出现。
341
+ // 它们让"这个组件按键怎么走、Esc 关不关、还要装什么、能调什么"不必读源码——
342
+ // 对 AI 与业务开发者是同一份答案,而答案来自契约,不是另一份手写文档。
343
+ if (entry.keyboard) {
344
+ lines.push('');
345
+ lines.push(renderKeyboardMatrixLine(entry.keyboard.contract));
346
+ }
347
+ if (entry.overlay) {
348
+ lines.push('');
349
+ lines.push(renderOverlayLine(entry.overlay));
350
+ }
351
+ if (entry.uses?.length) {
352
+ lines.push('');
353
+ lines.push(`依赖组件:${entry.uses.join('、')}`);
354
+ }
355
+ if (entry.tokens?.length) {
356
+ lines.push('');
357
+ lines.push(`组件令牌:${entry.tokens.map((token) => `\`${token.cssVar}\`(默认 ${token.default})${token.desc}`).join(';')}`);
358
+ }
359
+
243
360
  if (entry.ai?.example) {
244
361
  lines.push('');
245
362
  lines.push('用法:');
@@ -0,0 +1,100 @@
1
+ // Host 级默认 props(REQ-009 M-2)。
2
+ //
3
+ // 解决的是一句反复出现的话:「本站所有表格都要 size: sm、zebra: true」。
4
+ // 今天只能每个页面写一遍——写漏一处就是一个视觉不一致的页面,而且没有任何东西会红。
5
+ //
6
+ // 刻意只做 **Host 级**,不做包级:两个包互相给对方的组件设默认时,
7
+ // 优先级无法裁决(谁先合成?谁更"特化"?),而无法裁决的优先级最后一定变成靠顺序猜。
8
+ // Host 的 bootstrap 是唯一一个「站点说了算」的地方,默认值就住在那里。
9
+ //
10
+ // 优先级:页面声明 > componentDefaults > 契约 default。
11
+ // 校验放在 bootstrap 期而不是实例化期:错误要在首屏之前暴露,与注册中心五道闸门同一哲学。
12
+
13
+ import type { SomaComponentDefinition } from './component-definition.types';
14
+
15
+ export class SomaComponentDefaultsError extends Error {
16
+ constructor(message: string) {
17
+ super(`[SomaComponentDefaults] ${message}`);
18
+ this.name = 'SomaComponentDefaultsError';
19
+ }
20
+ }
21
+
22
+ export type SomaComponentDefaults = Record<string, Record<string, unknown>>;
23
+
24
+ /**
25
+ * bootstrap 期校验 Host 默认 props。
26
+ *
27
+ * 四类拒绝,每一类都在拒绝"猜":
28
+ * 未知组件名 → 多半是拼错或组件改名,静默忽略会让人以为默认生效了;
29
+ * 未知 prop → 同上;
30
+ * 枚举外取值 → 组件按未知值渲染的行为没有定义;
31
+ * required prop → 必填项由页面负责,用默认值填等于把"忘了传"变成静默的错数据。
32
+ */
33
+ export function validateComponentDefaults(
34
+ defaults: SomaComponentDefaults | undefined,
35
+ components: Record<string, SomaComponentDefinition>,
36
+ ): void {
37
+ if (!defaults) return;
38
+ const issues: string[] = [];
39
+
40
+ for (const [componentName, propDefaults] of Object.entries(defaults)) {
41
+ const definition = components[componentName];
42
+ if (!definition) {
43
+ issues.push(`未知组件 "${componentName}"(componentDefaults 的键必须是已注册的组件名)`);
44
+ continue;
45
+ }
46
+ if (!propDefaults || typeof propDefaults !== 'object' || Array.isArray(propDefaults)) {
47
+ issues.push(`"${componentName}" 的默认值必须是 { prop: 值 } 对象`);
48
+ continue;
49
+ }
50
+ const contractProps = definition.contract.props ?? {};
51
+ for (const [propName, value] of Object.entries(propDefaults)) {
52
+ const prop = contractProps[propName];
53
+ if (!prop) {
54
+ issues.push(`"${componentName}" 没有名为 "${propName}" 的 prop`);
55
+ continue;
56
+ }
57
+ if (prop.required) {
58
+ issues.push(
59
+ `"${componentName}.${propName}" 是必填 prop,不能由 Host 默认值提供`
60
+ + '(必填项由页面负责;用默认值填等于把"忘了传"变成静默的错数据)',
61
+ );
62
+ continue;
63
+ }
64
+ if (prop.type === 'enum' && prop.values && !prop.values.includes(value as string | number)) {
65
+ issues.push(
66
+ `"${componentName}.${propName}" 的值 ${JSON.stringify(value)} 不在枚举 `
67
+ + `[${prop.values.join(' | ')}] 内`,
68
+ );
69
+ continue;
70
+ }
71
+ const typeIssue = checkPrimitiveType(prop.type, value);
72
+ if (typeIssue) issues.push(`"${componentName}.${propName}" ${typeIssue}`);
73
+ }
74
+ }
75
+
76
+ if (issues.length > 0) {
77
+ throw new SomaComponentDefaultsError(
78
+ `Host 默认 props 有 ${issues.length} 处问题:\n${issues.map((issue) => ` · ${issue}`).join('\n')}`,
79
+ );
80
+ }
81
+ }
82
+
83
+ function checkPrimitiveType(type: string, value: unknown): string | null {
84
+ switch (type) {
85
+ case 'string':
86
+ return typeof value === 'string' ? null : `应为 string,实际是 ${typeof value}`;
87
+ case 'number':
88
+ return typeof value === 'number' ? null : `应为 number,实际是 ${typeof value}`;
89
+ case 'boolean':
90
+ return typeof value === 'boolean' ? null : `应为 boolean,实际是 ${typeof value}`;
91
+ case 'array':
92
+ return Array.isArray(value) ? null : '应为数组';
93
+ case 'object':
94
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
95
+ ? null
96
+ : '应为对象';
97
+ default:
98
+ return null;
99
+ }
100
+ }
@@ -37,6 +37,39 @@ export type SomaComponentControllerFactory = (
37
37
  ctx: SomaComponentControllerContext,
38
38
  ) => Record<string, unknown>;
39
39
 
40
+ /**
41
+ * 特性分片(REQ-009 M-3):按 prop 装配的 CSS。
42
+ *
43
+ * 既有的 `styles.components` 是「按组件」装配——组件一实例化就付全部 CSS。
44
+ * DataTable 的核心分片已经贴着 7,600 上限,而排队的表格特性有五个
45
+ * (单元格类型 / 批量动作条 / 列筛选 / 展开行 / 列设置),每个 600~1,500 字节。
46
+ * 抬上限只装得下一个,下一个又要重开辩论;正确的杠杆是再细一档:
47
+ * **用到这个特性的页面才为它付钱**。
48
+ *
49
+ * 注入顺序恒定「组件分片 → 特性分片(声明序)」。这与「单组件提共享是注入顺序炸弹」
50
+ * 不冲突:特性分片只属于一个组件,永远排在它自己的分片之后,无跨组件顺序依赖。
51
+ */
52
+ /**
53
+ * 特性开关。两种写法:
54
+ * · `when: 'close_guard'` —— boolean 真值 / array 非空即启用;
55
+ * · `when: { prop: 'list_type', equals: ['picture', 'picture-card'] }` ——
56
+ * enum 取到指定值之一才启用。
57
+ *
58
+ * 第二种是 REQ-009 施工中补的:首批两个消费者(Upload 的 `list_type`、
59
+ * DataTable 的单元格类型)都卡在同一处——真正决定"要不要这段样式"的是一个**枚举值**,
60
+ * 而不是某个布尔开关。只有布尔一种写法时,它们只能退而按一个恒为真的数组 prop 开门,
61
+ * 结果是所有页面都付这笔钱,机制等于白建。
62
+ */
63
+ export type SomaStyleFeatureWhen =
64
+ | string
65
+ | { prop: string; equals: readonly (string | number | boolean)[] };
66
+
67
+ export interface SomaComponentStyleFeature {
68
+ css: string;
69
+ /** 开关:prop 名(boolean/array),或 { prop, equals } 的枚举取值门控 */
70
+ when: SomaStyleFeatureWhen;
71
+ }
72
+
40
73
  export interface SomaComponentDefinition {
41
74
  contract: SomaComponentContract;
42
75
  /**
@@ -59,6 +92,12 @@ export interface SomaComponentDefinition {
59
92
  * 未声明的服务不注入。不声明本字段 = 非自治组件,ctx.services 为 undefined。
60
93
  */
61
94
  services?: string[];
95
+ /**
96
+ * 按 prop 装配的样式分片(M-3)。键为特性名,注入 key 为 `feature:<组件>#<特性>`。
97
+ * `when` 指向的 prop 首次为真值(数组非空)时注入一次,不回收——
98
+ * CSS 回收会让「关掉再打开」出现无样式帧,代价大于收益。
99
+ */
100
+ styleFeatures?: Record<string, SomaComponentStyleFeature>;
62
101
  }
63
102
 
64
103
  /** 保留键:实例作用域根上暴露控制器的字段名,契约禁止同名 prop */
@@ -36,6 +36,9 @@ import {
36
36
  SOMA_COMPONENT_READY_ATTR,
37
37
  } from './component-expander';
38
38
  import { SomaScopedContainerAdapter, SOMA_SCOPE_ATTR } from './scoped-adapter';
39
+ import type { SomaComponentDefaults } from './component-defaults';
40
+ import { ensureStyleFeatures } from './style-features';
41
+ import type { PackStyleOptions } from './pack-styles';
39
42
 
40
43
  const MAX_EXPANSION_ROUNDS = 8;
41
44
 
@@ -56,6 +59,14 @@ export interface SomaComponentManagerOptions {
56
59
  diagnostics?: SomaDiagnostics;
57
60
  /** 格式化管道注册表(实例内部绑定同样可用管道) */
58
61
  pipeRegistry?: import('../pipes/pipe-registry').SomaPipeRegistry;
62
+ /**
63
+ * Host 级默认 props(M-2):组件名 → { prop: 默认值 }。
64
+ * 优先级:页面声明 > 本表 > 契约 default。合法性由 bootstrap 期的
65
+ * validateComponentDefaults 保证,这里只做取值。
66
+ */
67
+ componentDefaults?: SomaComponentDefaults;
68
+ /** 特性分片注入用的 CSP nonce(M-3) */
69
+ styleOptions?: PackStyleOptions;
59
70
  }
60
71
 
61
72
  /** 作用域读取器:绑定路径求值 + var 判定(页面作用域 / 行作用域两种实现) */
@@ -76,6 +87,8 @@ export class SomaComponentManager {
76
87
  private readonly scheduler?: SomaScheduler;
77
88
  private readonly diagnostics?: SomaDiagnostics;
78
89
  private readonly pipeRegistry?: import('../pipes/pipe-registry').SomaPipeRegistry;
90
+ private readonly componentDefaults?: SomaComponentDefaults;
91
+ private readonly styleOptions?: PackStyleOptions;
79
92
  private readonly expander = new SomaComponentExpander();
80
93
 
81
94
  private staticDecls: StaticInstanceDecl[] = [];
@@ -90,6 +103,8 @@ export class SomaComponentManager {
90
103
  this.scheduler = options.scheduler;
91
104
  this.diagnostics = options.diagnostics;
92
105
  this.pipeRegistry = options.pipeRegistry;
106
+ this.componentDefaults = options.componentDefaults;
107
+ this.styleOptions = options.styleOptions;
93
108
  }
94
109
 
95
110
  /**
@@ -160,6 +175,22 @@ export class SomaComponentManager {
160
175
  page.renderEngine.render();
161
176
  }
162
177
 
178
+ /**
179
+ * 让全部存活实例重算一遍派生视图。
180
+ *
181
+ * 为什么需要它:数据对象层是**异步**取数的(首屏不阻塞),而自治组件在取数完成前
182
+ * 就已经挂载。页面侧的 `renderEngine.render()` 重渲的是页面模板,进不到组件实例
183
+ * 自己的渲染引擎——于是一个 `data_object` 形态的 FormConsole 在数据到达后
184
+ * 仍然显示着它挂载那一刻看到的空字典:一张空表单,没有任何报错。
185
+ *
186
+ * 组件的 ctrl 派生 getter 本来就是幂等的纯读,重算一遍是安全的。
187
+ */
188
+ refreshAll(): void {
189
+ for (const instance of this.instances) {
190
+ instance.refresh();
191
+ }
192
+ }
193
+
163
194
  destroy(): void {
164
195
  for (const dispose of this.watchDisposers) {
165
196
  dispose();
@@ -228,7 +259,7 @@ export class SomaComponentManager {
228
259
  const reader = this.createRowReader(this.page, rowData);
229
260
  const existing = this.hostInstances.get(element);
230
261
  if (existing) {
231
- existing.setProps(resolveProps(propsDecl, reader, existing.def));
262
+ existing.setProps(resolveProps(propsDecl, reader, existing.def, this.defaultsFor(existing.def)));
232
263
  return;
233
264
  }
234
265
 
@@ -254,10 +285,11 @@ export class SomaComponentManager {
254
285
  host,
255
286
  instanceId,
256
287
  def,
257
- initialProps: resolveProps(propsDecl, reader, def),
288
+ initialProps: resolveProps(propsDecl, reader, def, this.defaultsFor(def)),
258
289
  scheduler: this.scheduler,
259
290
  diagnostics: this.diagnostics,
260
291
  pipeRegistry: this.pipeRegistry,
292
+ styleOptions: this.styleOptions,
261
293
  services: this.filterServices(def),
262
294
  dispatchEmit: (emitName, payload) => this.dispatchEmit(instanceId, host, emitName, payload),
263
295
  registry: this.registry,
@@ -288,7 +320,7 @@ export class SomaComponentManager {
288
320
  const reader = this.createChildReader(parent, rowData);
289
321
  const existing = this.hostInstances.get(element);
290
322
  if (existing) {
291
- existing.setProps(resolveProps(propsDecl, reader, existing.def));
323
+ existing.setProps(resolveProps(propsDecl, reader, existing.def, this.defaultsFor(existing.def)));
292
324
  return;
293
325
  }
294
326
 
@@ -298,10 +330,11 @@ export class SomaComponentManager {
298
330
  host: element,
299
331
  instanceId: childId,
300
332
  def,
301
- initialProps: resolveProps(propsDecl, reader, def),
333
+ initialProps: resolveProps(propsDecl, reader, def, this.defaultsFor(def)),
302
334
  scheduler: this.scheduler,
303
335
  diagnostics: this.diagnostics,
304
336
  pipeRegistry: this.pipeRegistry,
337
+ styleOptions: this.styleOptions,
305
338
  services: this.filterServices(def),
306
339
  registry: this.registry,
307
340
  expandNested: (root) => this.expandNestedTemplates(root),
@@ -430,6 +463,11 @@ export class SomaComponentManager {
430
463
  }
431
464
 
432
465
  /** 行作用域读取:与 dom-renderer.resolveShowValue 同语义 */
466
+ /** 该组件的 Host 级默认 props(M-2);没有就返回 undefined,走原路径 */
467
+ private defaultsFor(def: SomaComponentDefinition): Record<string, unknown> | undefined {
468
+ return this.componentDefaults?.[def.contract.name];
469
+ }
470
+
433
471
  private createRowReader(page: SomaComponentPageRuntime, rowData: unknown): ScopeReader {
434
472
  return {
435
473
  read: (path: string) => {
@@ -478,6 +516,8 @@ interface SomaComponentInstanceOptions {
478
516
  scheduler?: SomaScheduler;
479
517
  diagnostics?: SomaDiagnostics;
480
518
  pipeRegistry?: import('../pipes/pipe-registry').SomaPipeRegistry;
519
+ /** 特性分片注入选项(M-3) */
520
+ styleOptions?: PackStyleOptions;
481
521
  services?: Record<string, unknown>;
482
522
  dispatchEmit: (emitName: string, payload: unknown) => void;
483
523
  registry: SomaComponentRegistry;
@@ -517,12 +557,17 @@ export class SomaComponentInstance {
517
557
  propsDecl: Record<string, unknown>;
518
558
  }> = [];
519
559
  private readonly mountChild: SomaComponentInstanceOptions['mountChild'];
560
+ private readonly styleOptions?: PackStyleOptions;
520
561
 
521
562
  constructor(options: SomaComponentInstanceOptions) {
522
563
  this.def = options.def;
523
564
  this.mountChild = options.mountChild;
565
+ this.styleOptions = options.styleOptions;
524
566
  this.scopeRoot = { ...options.initialProps };
525
567
 
568
+ // 特性分片(M-3):必须在控制器与渲染之前注入,否则首帧是无样式的。
569
+ ensureStyleFeatures(options.def, this.scopeRoot, this.styleOptions);
570
+
526
571
  if (options.def.controllerFactory) {
527
572
  this.scopeRoot[SOMA_COMPONENT_CTRL_KEY] = options.def.controllerFactory({
528
573
  props: this.scopeRoot,
@@ -644,6 +689,25 @@ export class SomaComponentInstance {
644
689
  * 分开写迟早会有人新加一处 `renderEngine.render()` 却忘了刷子组件——
645
690
  * 那种漏法不报错、只是子组件停在旧值,是最难查的一类。
646
691
  */
692
+ /**
693
+ * 外部触发的重算(数据对象取数完成等):与 props 下行走同一条重渲路径,
694
+ * 另外通知 ctrl「你读的那份外部数据换了」。
695
+ *
696
+ * 光重渲不够:重渲只让派生 getter 重算,而回填这类**副作用**(把刚到的记录摊进
697
+ * 表单模型)没有任何 getter 会做。缺这一声通知,表单的字段会出现、值却是空的。
698
+ */
699
+ refresh(): void {
700
+ if (this.destroyed) return;
701
+ this.rerender();
702
+ const ctrl = this.scopeRoot[SOMA_COMPONENT_CTRL_KEY] as
703
+ { onDataObjectsChange?: () => void } | undefined;
704
+ try {
705
+ ctrl?.onDataObjectsChange?.();
706
+ } catch (error) {
707
+ console.error('[soma-core][组件] onDataObjectsChange 抛错:', error);
708
+ }
709
+ }
710
+
647
711
  private rerender(): void {
648
712
  this.renderEngine.render();
649
713
  if (this.staticChildren.length === 0) return;
@@ -663,6 +727,9 @@ export class SomaComponentInstance {
663
727
  }
664
728
  this.scopeRoot[key] = value;
665
729
  }
730
+ // 特性开关可能是这一帧才翻真的(页面把 batch_actions 从空数组换成两条)。
731
+ // 注入是幂等的,重复调用只是一次 Set 查询。
732
+ ensureStyleFeatures(this.def, this.scopeRoot, this.styleOptions);
666
733
  this.rerender();
667
734
  // props 变更钩子:浮层类组件需要在"打开/关闭"翻转时做副作用
668
735
  // (焦点陷阱移入与归还、滚动锁定…),这类事情模板绑定表达不了。
@@ -815,6 +882,7 @@ function resolveProps(
815
882
  propsDecl: Record<string, unknown>,
816
883
  reader: ScopeReader,
817
884
  def: SomaComponentDefinition,
885
+ hostDefaults?: Record<string, unknown>,
818
886
  ): Record<string, unknown> {
819
887
  const result: Record<string, unknown> = {};
820
888
 
@@ -825,6 +893,13 @@ function resolveProps(
825
893
  }
826
894
  }
827
895
 
896
+ // Host 级默认(M-2):压在契约默认之上、页面声明之下。
897
+ // 同样要逐实例隔离——一张 Host 默认表被所有页面的所有实例共享,
898
+ // 不隔离的话一个组件写回默认数组会污染整站。
899
+ for (const [propName, value] of Object.entries(hostDefaults ?? {})) {
900
+ result[propName] = isolateContractDefault(value);
901
+ }
902
+
828
903
  for (const [propName, declValue] of Object.entries(propsDecl)) {
829
904
  result[propName] = resolvePropValue(declValue, reader);
830
905
  }