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
|
@@ -6,11 +6,15 @@
|
|
|
6
6
|
//
|
|
7
7
|
// 纪律(D10):目录永远从契约生成,禁止手工维护第二份副本。
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
import { deriveBlockExample } from './example-blocks';
|
|
10
|
+
import { renderKeyboardMatrixLine } from './keyboard-matrix';
|
|
11
|
+
import {
|
|
12
|
+
componentTokenVar,
|
|
13
|
+
type SomaComponentContract,
|
|
14
|
+
type SomaContractKeyboard,
|
|
15
|
+
type SomaContractOverlay,
|
|
16
|
+
type SomaGeometryFamily,
|
|
17
|
+
type SomaContractProp,
|
|
14
18
|
} from '../../shared/types/component-contract.types';
|
|
15
19
|
import type { SomaComponentDefinition } from './component-definition.types';
|
|
16
20
|
|
|
@@ -36,6 +40,22 @@ export interface SomaAiCatalogEntry {
|
|
|
36
40
|
desc: string;
|
|
37
41
|
}>;
|
|
38
42
|
slots: Array<{ name: string; required: boolean; desc: string }>;
|
|
43
|
+
/**
|
|
44
|
+
* 键盘契约(REQ-011 / IK-2)。
|
|
45
|
+
*
|
|
46
|
+
* 这里**只存契约**:逐键矩阵是 `deriveKeyboardMatrix(contract)` 的返回值,
|
|
47
|
+
* 那一行紧凑键盘表是 `renderKeyboardMatrixLine(contract)` 的返回值——两者都是渲染,
|
|
48
|
+
* 存进来等于把同一个真值抄成三份,而这一轮要治的正是这种抄写。
|
|
49
|
+
* 人与模型读到的键盘表在 markdown 那一侧(检索喂给模型的就是它);
|
|
50
|
+
* 需要逐键结构的程序自己调那个函数(与 soma-ui 接线、闸门读的是同一个)。
|
|
51
|
+
*/
|
|
52
|
+
keyboard?: { contract: SomaContractKeyboard };
|
|
53
|
+
/** 浮层行为契约(声明了才有) */
|
|
54
|
+
overlay?: SomaContractOverlay;
|
|
55
|
+
/** 契约级依赖(F-46):装这个还要装什么 */
|
|
56
|
+
uses?: string[];
|
|
57
|
+
/** 组件级令牌(M-1):这个组件开放的可调旋钮及其 CSS 变量名 */
|
|
58
|
+
tokens?: Array<{ name: string; cssVar: string; default: string; desc: string }>;
|
|
39
59
|
ai?: {
|
|
40
60
|
whenToUse: string;
|
|
41
61
|
whenNotToUse?: string;
|
|
@@ -110,6 +130,22 @@ export function buildCatalogEntry(contract: SomaComponentContract): SomaAiCatalo
|
|
|
110
130
|
required: slot.required === true,
|
|
111
131
|
desc: slot.desc ?? '',
|
|
112
132
|
})),
|
|
133
|
+
// 四个可选段:缺席时连键都不写——目录 JSON 的形状对存量消费者逐键不变
|
|
134
|
+
...(contract.keyboard
|
|
135
|
+
? { keyboard: { contract: contract.keyboard } }
|
|
136
|
+
: {}),
|
|
137
|
+
...(contract.overlay ? { overlay: contract.overlay } : {}),
|
|
138
|
+
...(contract.uses && contract.uses.length > 0 ? { uses: [...contract.uses] } : {}),
|
|
139
|
+
...(contract.tokens && Object.keys(contract.tokens).length > 0
|
|
140
|
+
? {
|
|
141
|
+
tokens: Object.entries(contract.tokens).map(([name, token]) => ({
|
|
142
|
+
name,
|
|
143
|
+
cssVar: componentTokenVar(contract.name, name),
|
|
144
|
+
default: token.default,
|
|
145
|
+
desc: token.desc ?? '',
|
|
146
|
+
})),
|
|
147
|
+
}
|
|
148
|
+
: {}),
|
|
113
149
|
ai: contract.ai
|
|
114
150
|
? {
|
|
115
151
|
whenToUse: contract.ai.when_to_use,
|
|
@@ -166,6 +202,25 @@ export function generateAiCatalog(
|
|
|
166
202
|
};
|
|
167
203
|
}
|
|
168
204
|
|
|
205
|
+
const OVERLAY_KIND_LABEL: Record<SomaContractOverlay['kind'], string> = {
|
|
206
|
+
popup: '弹出层', modal: '模态', non_modal: '非模态', toast: '提示条',
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/** 浮层语义一行:只印契约里写了的字段——没写的按 kind 缺省,目录不替组件承诺 */
|
|
210
|
+
function renderOverlayLine(overlay: SomaContractOverlay): string {
|
|
211
|
+
const parts: string[] = [OVERLAY_KIND_LABEL[overlay.kind]];
|
|
212
|
+
if (overlay.escape_key_behavior !== undefined) {
|
|
213
|
+
parts.push({ close: 'Esc 关闭', none: 'Esc 不关', defer: 'Esc 让给外层' }[overlay.escape_key_behavior]);
|
|
214
|
+
}
|
|
215
|
+
if (overlay.outside_press_behavior !== undefined) {
|
|
216
|
+
parts.push(overlay.outside_press_behavior === 'close' ? '点外面关闭' : '点外面不关');
|
|
217
|
+
}
|
|
218
|
+
if (overlay.hide_outside !== undefined) parts.push(overlay.hide_outside ? '打开时隔离外部' : '不隔离外部');
|
|
219
|
+
if (overlay.restore_focus !== undefined) parts.push(overlay.restore_focus ? '关闭后回焦' : '关闭后不回焦');
|
|
220
|
+
if (overlay.presence !== undefined) parts.push(overlay.presence ? '退场动画放完再卸载' : '立即卸载');
|
|
221
|
+
return `浮层:${parts.join('·')}`;
|
|
222
|
+
}
|
|
223
|
+
|
|
169
224
|
function propTypeLabel(prop: SomaContractProp): string {
|
|
170
225
|
if (prop.type === 'enum' && prop.values?.length) {
|
|
171
226
|
return prop.values.join(' | ');
|
|
@@ -240,6 +295,26 @@ function renderMarkdown(entries: SomaAiCatalogEntry[], conventions?: string): st
|
|
|
240
295
|
);
|
|
241
296
|
}
|
|
242
297
|
|
|
298
|
+
// 键盘表 / 浮层语义 / 依赖 / 令牌(REQ-011):各一行,声明了才出现。
|
|
299
|
+
// 它们让"这个组件按键怎么走、Esc 关不关、还要装什么、能调什么"不必读源码——
|
|
300
|
+
// 对 AI 与业务开发者是同一份答案,而答案来自契约,不是另一份手写文档。
|
|
301
|
+
if (entry.keyboard) {
|
|
302
|
+
lines.push('');
|
|
303
|
+
lines.push(renderKeyboardMatrixLine(entry.keyboard.contract));
|
|
304
|
+
}
|
|
305
|
+
if (entry.overlay) {
|
|
306
|
+
lines.push('');
|
|
307
|
+
lines.push(renderOverlayLine(entry.overlay));
|
|
308
|
+
}
|
|
309
|
+
if (entry.uses?.length) {
|
|
310
|
+
lines.push('');
|
|
311
|
+
lines.push(`依赖组件:${entry.uses.join('、')}`);
|
|
312
|
+
}
|
|
313
|
+
if (entry.tokens?.length) {
|
|
314
|
+
lines.push('');
|
|
315
|
+
lines.push(`组件令牌:${entry.tokens.map((token) => `\`${token.cssVar}\`(默认 ${token.default})${token.desc}`).join(';')}`);
|
|
316
|
+
}
|
|
317
|
+
|
|
243
318
|
if (entry.ai?.example) {
|
|
244
319
|
lines.push('');
|
|
245
320
|
lines.push('用法:');
|
|
@@ -247,15 +322,15 @@ function renderMarkdown(entries: SomaAiCatalogEntry[], conventions?: string): st
|
|
|
247
322
|
lines.push(entry.ai.example.trimEnd());
|
|
248
323
|
lines.push('```');
|
|
249
324
|
|
|
250
|
-
//
|
|
325
|
+
// 块写法(由平铺示例机械派生,见 example-blocks.ts)。
|
|
251
326
|
// 派生而不是在 315 份契约里各写一份:契约仍是单一真源,
|
|
252
327
|
// 两份示例不可能不一致——因为后者是前者的函数
|
|
253
|
-
const v2 =
|
|
328
|
+
const v2 = deriveBlockExample(entry.name, entry.ai.example);
|
|
254
329
|
if (v2) {
|
|
255
330
|
lines.push('');
|
|
256
331
|
// 目录会被压成裸文本喂给模型(围栏与多余空行都剥掉),
|
|
257
332
|
// 所以这行标注必须自带上下文:不能靠代码块的视觉分隔来区分两种写法
|
|
258
|
-
lines.push('
|
|
333
|
+
lines.push('块写法(html 同上,_ui.yaml 改为块声明):');
|
|
259
334
|
lines.push('```');
|
|
260
335
|
lines.push(v2);
|
|
261
336
|
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
|
}
|
|
@@ -26,6 +26,7 @@ import type { SomaComponentDefinition } from './component-definition.types';
|
|
|
26
26
|
import { generateAiCatalog, type SomaAiCatalog } from './ai-catalog';
|
|
27
27
|
import { attachPackStyles } from './pack-styles';
|
|
28
28
|
import { checkOverrideCompat } from './contract-compat';
|
|
29
|
+
import { componentTokenPrefix } from '../../shared/types/component-contract.types';
|
|
29
30
|
|
|
30
31
|
/** 包标识:小写字母/数字/连字符——冲突报错与来源登记都用它 */
|
|
31
32
|
export const PACK_NAME_PATTERN = /^[a-z][a-z0-9-]*$/;
|
|
@@ -39,6 +40,20 @@ export interface SomaComponentPackStyles {
|
|
|
39
40
|
deps?: Record<string, string[]>;
|
|
40
41
|
/** 包级基座 CSS(恒定注入;克制使用——它是每个页面首屏都要付的钱) */
|
|
41
42
|
base?: string;
|
|
43
|
+
/**
|
|
44
|
+
* 样式补丁(REQ-009 M-4):给**别的包**的组件追加 CSS。
|
|
45
|
+
*
|
|
46
|
+
* 解决的是「官方组件 90% 合适、改 10%」——此前这类诉求只有整组件覆盖一条路,
|
|
47
|
+
* 为了挪一处间距就要接手整个组件的维护。补丁在目标组件分片与其特性分片**之后**注入。
|
|
48
|
+
*
|
|
49
|
+
* 四条硬规矩(compose 期判定,见 collectPatches):
|
|
50
|
+
* ① 目标必须属于别的包(本包组件直接改分片,不需要补丁);
|
|
51
|
+
* ② 每条规则的首选择器必须以 `.soma-<组件名 kebab>` 开头——
|
|
52
|
+
* 补丁只能改目标组件内部,不能顺手改页面;
|
|
53
|
+
* ③ ≤ 1,200 字节(超了说明该 eject 或该覆盖,不该是补丁);
|
|
54
|
+
* ④ 能用组件令牌调的(圆角/高度/颜色)走令牌,不给补丁。
|
|
55
|
+
*/
|
|
56
|
+
patches?: Record<string, string>;
|
|
42
57
|
}
|
|
43
58
|
|
|
44
59
|
export interface SomaComponentPack {
|
|
@@ -103,8 +118,8 @@ export interface ComposedComponentPacks {
|
|
|
103
118
|
components: Record<string, SomaComponentDefinition>;
|
|
104
119
|
/** 组件名 → 来源包名。排障、目录展示、闸门报错都用它 */
|
|
105
120
|
provenance: Record<string, string>;
|
|
106
|
-
/** 合成后的按需装配样式(键已按包做过冲突检查) */
|
|
107
|
-
styles: Required<Omit<SomaComponentPackStyles, 'base'>> & { base: string[] };
|
|
121
|
+
/** 合成后的按需装配样式(键已按包做过冲突检查)。补丁不在这里,它有自己的顶层字段 */
|
|
122
|
+
styles: Required<Omit<SomaComponentPackStyles, 'base' | 'patches'>> & { base: string[] };
|
|
108
123
|
/** 参与合成的包(顺序保留;目录生成用) */
|
|
109
124
|
packs: SomaComponentPack[];
|
|
110
125
|
/** 被显式覆盖的组件名 → { 胜出包, 被盖掉的包, 覆盖来源 } */
|
|
@@ -113,8 +128,13 @@ export interface ComposedComponentPacks {
|
|
|
113
128
|
shadowed: string[];
|
|
114
129
|
source: 'contract' | 'allowOverride';
|
|
115
130
|
}>;
|
|
131
|
+
/** 生效的样式补丁(M-4):谁给谁打了什么。目录条目据此标注「被 X 包打了补丁」 */
|
|
132
|
+
patches: Array<{ target: string; patchedBy: string; css: string }>;
|
|
116
133
|
}
|
|
117
134
|
|
|
135
|
+
/** 单条补丁的字节上限:超了说明该 eject 或该整包覆盖,不该是补丁 */
|
|
136
|
+
export const PACK_PATCH_BYTE_LIMIT = 1200;
|
|
137
|
+
|
|
118
138
|
function assertPack(pack: SomaComponentPack): void {
|
|
119
139
|
if (!pack || typeof pack !== 'object') {
|
|
120
140
|
throw new ComponentPackError('组件包必须是对象');
|
|
@@ -228,6 +248,7 @@ export function composeComponentPacks(
|
|
|
228
248
|
styles: composeStyles(packs, provenance),
|
|
229
249
|
packs: [...packs],
|
|
230
250
|
overrides,
|
|
251
|
+
patches: collectPatches(packs, provenance),
|
|
231
252
|
};
|
|
232
253
|
if (options.lazyStyles === false) return composed;
|
|
233
254
|
return {
|
|
@@ -406,6 +427,109 @@ function composeStyles(
|
|
|
406
427
|
return { components, shared, deps, base };
|
|
407
428
|
}
|
|
408
429
|
|
|
430
|
+
/**
|
|
431
|
+
* 收集样式补丁(M-4)并逐条过闸门。
|
|
432
|
+
*
|
|
433
|
+
* 为什么全部在合成期判定而不是注入期:注入发生在浏览器里、在某个页面第一次用到
|
|
434
|
+
* 那个组件的时候——那时报错既晚又难归因。合成期是包与包相遇的唯一时刻,
|
|
435
|
+
* 也是「谁能给谁打补丁」唯一说得清的时刻。
|
|
436
|
+
*/
|
|
437
|
+
function collectPatches(
|
|
438
|
+
packs: readonly SomaComponentPack[],
|
|
439
|
+
provenance: Record<string, string>,
|
|
440
|
+
): ComposedComponentPacks['patches'] {
|
|
441
|
+
const patches: ComposedComponentPacks['patches'] = [];
|
|
442
|
+
|
|
443
|
+
for (const pack of packs) {
|
|
444
|
+
for (const [target, css] of Object.entries(pack.styles?.patches ?? {})) {
|
|
445
|
+
const owner = provenance[target];
|
|
446
|
+
if (owner === undefined) {
|
|
447
|
+
throw new ComponentPackError(
|
|
448
|
+
`包 "${pack.name}" 的样式补丁指向组件 "${target}",但本次合成里没有这个组件。`
|
|
449
|
+
+ '\n 补丁指向不存在的组件时永远不会生效,而页面只是"看起来没改动"——所以这里拒绝',
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
if (owner === pack.name) {
|
|
453
|
+
throw new ComponentPackError(
|
|
454
|
+
`包 "${pack.name}" 给自己的组件 "${target}" 打补丁——`
|
|
455
|
+
+ '本包组件请直接改它的样式分片,补丁通道是给跨包微调用的',
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
const bytes = byteLengthOf(css);
|
|
459
|
+
if (bytes > PACK_PATCH_BYTE_LIMIT) {
|
|
460
|
+
throw new ComponentPackError(
|
|
461
|
+
`包 "${pack.name}" 给 "${target}" 的补丁有 ${bytes} 字节,超过上限 ${PACK_PATCH_BYTE_LIMIT}。`
|
|
462
|
+
+ '\n 补丁是"改 10%"的通道;要改的比这更多,正确的路是 soma component eject 或整包覆盖',
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
const stray = findStraySelector(css, target);
|
|
466
|
+
if (stray) {
|
|
467
|
+
throw new ComponentPackError(
|
|
468
|
+
`包 "${pack.name}" 给 "${target}" 的补丁里有越界选择器:${stray}。`
|
|
469
|
+
+ `\n 每条规则的首选择器必须以 ".soma-${componentTokenPrefix(target)}" 开头——`
|
|
470
|
+
+ '补丁只能改目标组件内部,不能顺手改页面上别的东西',
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
patches.push({ target, patchedBy: pack.name, css });
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return patches;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function byteLengthOf(text: string): number {
|
|
480
|
+
return typeof TextEncoder === 'undefined'
|
|
481
|
+
? Buffer.byteLength(text, 'utf8')
|
|
482
|
+
: new TextEncoder().encode(text).length;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* 找出补丁里第一条不以目标组件根类开头的规则。
|
|
487
|
+
*
|
|
488
|
+
* 只做静态扫描(去注释 → 按顶层 `}` 切规则 → 取选择器列表),不解析完整 CSS:
|
|
489
|
+
* 目的是拦住"顺手改页面"这类越界,不是实现一个 CSS parser。
|
|
490
|
+
* `@media` / `@supports` 这类块级 at 规则递归进去看里面的规则。
|
|
491
|
+
*/
|
|
492
|
+
export function findStraySelector(css: string, target: string): string | null {
|
|
493
|
+
const prefix = `.soma-${componentTokenPrefix(target)}`;
|
|
494
|
+
const cleaned = css.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
495
|
+
let depth = 0;
|
|
496
|
+
let buffer = '';
|
|
497
|
+
for (const char of cleaned) {
|
|
498
|
+
if (char === '{') {
|
|
499
|
+
depth += 1;
|
|
500
|
+
if (depth === 1) {
|
|
501
|
+
const selectorText = buffer.trim();
|
|
502
|
+
buffer = '';
|
|
503
|
+
if (!selectorText) continue;
|
|
504
|
+
if (selectorText.startsWith('@')) {
|
|
505
|
+
// 块级 at 规则:里面的规则继续受同一条约束,交给下一轮 depth 计数处理
|
|
506
|
+
if (/^@(media|supports|container|layer)\b/.test(selectorText)) {
|
|
507
|
+
depth -= 1;
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
// @keyframes 之类:整块跳过(内部是百分比选择器,不是组件锚点)
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
for (const selector of selectorText.split(',')) {
|
|
514
|
+
const trimmed = selector.trim();
|
|
515
|
+
if (!trimmed) continue;
|
|
516
|
+
if (!trimmed.startsWith(prefix)) return trimmed;
|
|
517
|
+
}
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
buffer = '';
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
if (char === '}') {
|
|
524
|
+
depth = Math.max(0, depth - 1);
|
|
525
|
+
buffer = '';
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
if (depth === 0) buffer += char;
|
|
529
|
+
}
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
|
|
409
533
|
/**
|
|
410
534
|
* 生成带来源标注的组件目录。
|
|
411
535
|
*
|