soma-core 1.6.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/core/build/compiler/soma-precompile.js +182 -15
  2. package/core/build/eject/component-eject.ts +1 -0
  3. package/core/client/adaptive/observer-pool.ts +122 -8
  4. package/core/client/compiler/manifest-compiler.ts +46 -5
  5. package/core/client/component/component-defaults.ts +1 -1
  6. package/core/client/component/component-expander.ts +1 -1
  7. package/core/client/component/component-manager.ts +784 -35
  8. package/core/client/component/component-pack.ts +1 -1
  9. package/core/client/component/component-registry.ts +1 -1
  10. package/core/client/container/SomaContainerAdapter.ts +103 -2
  11. package/core/client/data/data-object-resolver.ts +45 -1
  12. package/core/client/engine/dom-renderer.ts +432 -29
  13. package/core/client/engine/event-engine.ts +301 -13
  14. package/core/client/engine/property-watcher.ts +164 -18
  15. package/core/client/engine/reactive-proxy.ts +194 -23
  16. package/core/client/engine/render-engine.ts +118 -11
  17. package/core/client/engine/scope-resolver.ts +3 -3
  18. package/core/client/engine/soma-bootstrap.ts +31 -17
  19. package/core/client/parser/adaptive-parser.ts +1 -1
  20. package/core/client/parser/annotation-extractor.ts +1 -1
  21. package/core/client/parser/blocks/page-parser.ts +44 -1
  22. package/core/client/parser/blocks/use-parser.ts +21 -6
  23. package/core/client/parser/data-config-parser.ts +1 -1
  24. package/core/client/parser/pipeline-parser.ts +252 -15
  25. package/core/client/parser/style-config-parser.ts +1 -1
  26. package/core/client/parser/timer-parser.ts +1 -1
  27. package/core/client/parser/trigger-parser.ts +1 -1
  28. package/core/client/parser/ui-config-parser.ts +1 -1
  29. package/core/client/pipes/pipe-registry.ts +45 -4
  30. package/core/client/runtime/SomaSnapshotStore.ts +1 -1
  31. package/core/client/scheduler/SomaScheduler.ts +1 -1
  32. package/core/client/security/attribute-presence.ts +3 -0
  33. package/core/client/theme/harmony.ts +135 -13
  34. package/core/client/theme/scene-tokens.ts +17 -2
  35. package/core/client/theme/theme-axes.ts +43 -6
  36. package/core/client/theme/theme-engine.ts +921 -126
  37. package/core/client/validation/contract-validator.ts +1 -1
  38. package/core/server/mock/node/cli.mjs +3 -3
  39. package/core/shared/path-algebra.ts +1 -1
  40. package/core/shared/types/event.types.ts +8 -4
  41. package/dist/core/index.cjs +5411 -3507
  42. package/dist/lsp/component-tree.cjs +701 -511
  43. package/dist/types/client/adaptive/observer-pool.d.ts +9 -2
  44. package/dist/types/client/compiler/manifest-compiler.d.ts +11 -2
  45. package/dist/types/client/component/component-manager.d.ts +86 -0
  46. package/dist/types/client/container/SomaContainerAdapter.d.ts +8 -0
  47. package/dist/types/client/data/data-object-resolver.d.ts +19 -0
  48. package/dist/types/client/engine/dom-renderer.d.ts +110 -2
  49. package/dist/types/client/engine/event-engine.d.ts +61 -3
  50. package/dist/types/client/engine/property-watcher.d.ts +41 -1
  51. package/dist/types/client/engine/reactive-proxy.d.ts +42 -2
  52. package/dist/types/client/engine/render-engine.d.ts +54 -3
  53. package/dist/types/client/parser/blocks/use-parser.d.ts +18 -0
  54. package/dist/types/client/parser/pipeline-parser.d.ts +12 -4
  55. package/dist/types/client/pipes/pipe-registry.d.ts +39 -1
  56. package/dist/types/client/theme/harmony.d.ts +44 -2
  57. package/dist/types/client/theme/scene-tokens.d.ts +11 -1
  58. package/dist/types/client/theme/theme-axes.d.ts +36 -6
  59. package/dist/types/shared/types/event.types.d.ts +8 -4
  60. package/package.json +4 -4
  61. package/dist/preview-export.cjs +0 -3401
@@ -44,7 +44,250 @@ function pageRowBudget(element: HTMLElement): Map<SomaRowScope, HTMLElement> {
44
44
  return budget;
45
45
  }
46
46
 
47
- export type ModelWriteBack = (path: string, value: string) => void;
47
+ /** 模型值随控件形态而定:checkbox 为布尔,radio、file 与文本类为字符串,number / range 为数值(空为 null),select[multiple] 为字符串数组 */
48
+ export type ModelWriteBack = (path: string, value: unknown) => void;
49
+
50
+ /**
51
+ * model 绑定的控件形态,决定读写哪个属性、听哪些事件:
52
+ * - checkbox:读写 `.checked`,模型值为布尔(按真值勾选,与 when / class 绑定同一口径);
53
+ * - radio:模型值是「这一组当前选中的值」——`.checked = (模型值 === 本控件的 value)`,
54
+ * 按字符串比较,模型值为 null / undefined 时不选;本控件被选中时回写它的 value;
55
+ * - multi-select(select[multiple]):模型值为选中项 value 组成的数组;
56
+ * - file(input[type=file]):读 `.value`(浏览器给出的 "C:\fakepath\文件名",没选文件为空串),与文本类同为字符串;
57
+ * 写只认清空:模型值为空(null / undefined / 空串)时清掉已选文件,非空一律不写——file 的 value 只能由用户
58
+ * 选文件改变,脚本写非空值会抛 InvalidStateError。要拿文件本身走 change 事件读 `event.target.files`(Upload 组件同此);
59
+ * - number(input[type=number|range]):模型值为数值,输入框为空时为 null(与 NumberInput / FormConsole 的数值通道同口径);
60
+ * - text:其余控件(文本类 input / textarea / 单选 select),经适配器 getValue / setValue 读写 `.value`。
61
+ * checkbox / radio / multi-select / file 只听 change:它们的状态只随勾选、选择改变。blur 时再回写没有新信息,
62
+ * 而数组每次读出来都是新引用,会让下游误以为数据变了。
63
+ * 读写时现取形态(`@type` 绑定可能在首帧之后才把 type 写上);听哪些事件在附着监听时按当时的形态定下。
64
+ */
65
+ type ModelControlKind = 'checkbox' | 'radio' | 'multi-select' | 'file' | 'number' | 'text';
66
+
67
+ function modelControlKind(element: HTMLElement): ModelControlKind {
68
+ const tag = element.localName;
69
+ if (tag === 'input') {
70
+ const type = (element as HTMLInputElement).type;
71
+ if (type === 'checkbox' || type === 'radio' || type === 'file') return type;
72
+ return type === 'number' || type === 'range' ? 'number' : 'text';
73
+ }
74
+ return tag === 'select' && (element as HTMLSelectElement).multiple ? 'multi-select' : 'text';
75
+ }
76
+
77
+ /** 控件当前的模型值;未选中的 radio 不代表这一组的值,返回 `{ skip: true }`,不回写 */
78
+ function readModelValue(
79
+ adapter: IContainerAdapter,
80
+ element: HTMLElement,
81
+ ): { skip: true } | { skip: false; value: unknown } {
82
+ switch (modelControlKind(element)) {
83
+ case 'checkbox':
84
+ return { skip: false, value: (element as HTMLInputElement).checked };
85
+ case 'radio': {
86
+ const radio = element as HTMLInputElement;
87
+ return radio.checked ? { skip: false, value: radio.value } : { skip: true };
88
+ }
89
+ case 'multi-select':
90
+ return {
91
+ skip: false,
92
+ value: Array.from((element as HTMLSelectElement).options)
93
+ .filter((option) => option.selected)
94
+ .map((option) => option.value),
95
+ };
96
+ case 'number': {
97
+ const raw = adapter.getValue(element);
98
+ const number = Number(raw);
99
+ return { skip: false, value: raw === '' || !Number.isFinite(number) ? null : number };
100
+ }
101
+ default:
102
+ return { skip: false, value: adapter.getValue(element) };
103
+ }
104
+ }
105
+
106
+ /**
107
+ * 控件读出来的值与模型现值是否算同一个:多选每次读出来都是新数组(引用必不相同),按元素比。
108
+ * 用于回写前去重。代理的 set 只对基本类型去重(对象与数组一律算变了,见 reactive-proxy 的判据),
109
+ * 多选这种每次都是新数组的值到不了那一层,只能在这里按元素比;不去重的话文本控件的
110
+ * input / change / blur 与处理器执行前的整表同步会把同一个值连发好几遍。
111
+ */
112
+ function sameModelValue(current: unknown, next: unknown): boolean {
113
+ if (Object.is(current, next)) return true;
114
+ if (Array.isArray(current) && Array.isArray(next)) {
115
+ return current.length === next.length && current.every((item, index) => Object.is(item, next[index]));
116
+ }
117
+ return false;
118
+ }
119
+
120
+ /**
121
+ * 把模型值写到控件上;目标状态与控件现状一致时不写。
122
+ * 文本类经适配器 setValue 写入:控件正聚焦时保住选区(处理器把值改写后光标不跳到末尾)。
123
+ */
124
+ function writeModelValue(adapter: IContainerAdapter, element: HTMLElement, value: unknown): void {
125
+ switch (modelControlKind(element)) {
126
+ case 'checkbox': {
127
+ const checkbox = element as HTMLInputElement;
128
+ const next = Boolean(value);
129
+ if (checkbox.checked !== next) checkbox.checked = next;
130
+ return;
131
+ }
132
+ case 'radio': {
133
+ const radio = element as HTMLInputElement;
134
+ const next = value != null && String(value) === radio.value;
135
+ if (radio.checked !== next) radio.checked = next;
136
+ return;
137
+ }
138
+ case 'multi-select': {
139
+ const wanted = new Set(Array.isArray(value) ? value.map((item) => String(item)) : []);
140
+ for (const option of Array.from((element as HTMLSelectElement).options)) {
141
+ const next = wanted.has(option.value);
142
+ if (option.selected !== next) option.selected = next;
143
+ }
144
+ return;
145
+ }
146
+ case 'file': {
147
+ // 只清空:非空模型值(多半就是上次回写的 fakepath 串)不写,已选的文件保持不动
148
+ if (String(value ?? '') === '' && adapter.getValue(element) !== '') {
149
+ adapter.setValue(element, '');
150
+ }
151
+ return;
152
+ }
153
+ case 'number': {
154
+ const next = value == null || (typeof value === 'number' && !Number.isFinite(value)) ? '' : String(value);
155
+ const current = adapter.getValue(element);
156
+ // 数值相等不写:键入中的 "7.0"、"1e3" 与模型值 7、1000 是同一个数,改写成规范写法会打断输入
157
+ if (current === next || (current !== '' && next !== '' && Number(current) === Number(next))) return;
158
+ adapter.setValue(element, next);
159
+ return;
160
+ }
161
+ default: {
162
+ const strValue = String(value ?? '');
163
+ if (adapter.getValue(element) !== strValue) {
164
+ adapter.setValue(element, strValue);
165
+ }
166
+ }
167
+ }
168
+ }
169
+
170
+ /**
171
+ * 输入法组合闸门:组合进行中控件上的值是中间态(拼音、注音、还没选定的候选),不回写模型;
172
+ * 组合结束后只回写一次最终值。控件上的显示归浏览器管,闸门不碰。
173
+ * 页面 model 绑定(按控件挂,见 SomaDomRenderer.attachModelComposition)与组件 model prop 回写
174
+ * (按组件宿主挂,见 component-manager 的 installModelProps)用的是这同一份判断。
175
+ *
176
+ * 什么算组合中:compositionstart 之后、compositionend 之前,或事件自带 `isComposing === true`。
177
+ * 脚本在组合中改写 value 时 Chromium 静默丢掉组合、不派发 compositionend(实测),所以收到一次不在组合中的
178
+ * input(英文键入、粘贴、脚本派发的 input)就认定组合已经不在了,闸门不会一直关着。
179
+ *
180
+ * 最终值落定的时机各浏览器不同,compositionend 时不读值:
181
+ * - Chromium(147 实测):带最终值的 input(isComposing 为 true)→ compositionend,之后不再派发 input;
182
+ * - WebKit:先派发 compositionend,再把选定的字写进控件并派发 input(isComposing 为 false),
183
+ * compositionend 那一刻控件上还可能是拼音。
184
+ * 所以 compositionend 只排一个宏任务补写。同一轮派发里随后来了不在组合中的 input / change / blur(WebKit 的顺序,
185
+ * 或失焦结束组合),那次回写读到的已是最终值,调用方调 `settled()` 撤掉补写;没有来(Chromium 的顺序)就由补写回写。
186
+ * 两条路只走一条,最终值恰好回写一次。补写还没跑就开始了下一次组合(连续打字)时,compositionstart 先把补写做掉,
187
+ * 每个字提交各回写一次。
188
+ * 事件引擎按容器挂同一份闸门挡 input 声明(见 event-engine 的 attachInputComposition):补发处理器要知道是哪次组合,
189
+ * 所以两个回调带上对应的组合事件(按控件回写的调用方用不到,不接这个参数);失焦结束组合时补发要排在 change / blur 的
190
+ * 处理器之前,所以另有 `flush()` 立即做掉排着的补写。
191
+ *
192
+ * 「组合中」与「补写还排着」是两件事,判断也分成两个:`holds()` 管回写(补写还排着时读控件读到的已是最终值,
193
+ * 该回写就回写),`settling()` 管往控件写(补写还没跑完,模型上停的还是上一个字,这时写下去会把刚上屏的字冲掉,
194
+ * 见 SomaDomRenderer.compileRenderFn 的 model 分支)。
195
+ */
196
+ export class SomaCompositionGate {
197
+ private composing = false;
198
+ private settleTimer: ReturnType<typeof setTimeout> | undefined;
199
+ private settleEnd: Event | undefined;
200
+
201
+ /**
202
+ * @param listen 在控件或宿主上挂监听(随 signal 一起撤)
203
+ * @param onSettle 组合结束、最终值落定后的补写(参数为这次组合的 compositionend 事件)
204
+ * @param onStart 组合开始时调用,调用方记下组合前的状态(参数为 compositionstart 事件)
205
+ */
206
+ constructor(
207
+ listen: (type: string, listener: (event: Event) => void) => void,
208
+ signal: AbortSignal,
209
+ private readonly onSettle: (end: Event) => void,
210
+ onStart?: (start: Event) => void,
211
+ ) {
212
+ listen('compositionstart', (event) => {
213
+ this.flushSettle();
214
+ this.composing = true;
215
+ onStart?.(event);
216
+ });
217
+ listen('compositionend', (event) => {
218
+ this.composing = false;
219
+ this.cancelSettle();
220
+ this.settleEnd = event;
221
+ this.settleTimer = setTimeout(() => this.flushSettle(), 0);
222
+ });
223
+ listen('input', (event) => {
224
+ if ((event as InputEvent).isComposing !== true) this.composing = false;
225
+ });
226
+ signal.addEventListener('abort', () => this.cancelSettle(), { once: true });
227
+ }
228
+
229
+ /** 此刻控件上的值是否为组合中间态:组合进行中,或给出的事件自带 isComposing */
230
+ holds(event?: Event): boolean {
231
+ return this.composing || (event as InputEvent | undefined)?.isComposing === true;
232
+ }
233
+
234
+ /**
235
+ * 组合刚结束、最终值还没回写:compositionend 排下的补写还在队列里没执行。
236
+ * 这一小段里控件上已经是用户上屏的字,模型上还是上一个字——比控件旧,不能拿它去写控件。
237
+ * 连续打字时这一小段真的会被踩中:下一次 compositionstart 才把补写做掉,而下一帧渲染可能先到。
238
+ */
239
+ settling(): boolean {
240
+ return this.settleTimer !== undefined;
241
+ }
242
+
243
+ /** 调用方这次回写读到的已是最终值:撤掉组合结束时排下的补写 */
244
+ settled(): void {
245
+ this.cancelSettle();
246
+ }
247
+
248
+ /** 最终值已落定、补写不必等到下一个宏任务(组合结束后同一轮派发里来了 change / 失焦):有排着的补写就立即做掉 */
249
+ flush(): void {
250
+ this.flushSettle();
251
+ }
252
+
253
+ private flushSettle(): void {
254
+ const end = this.settleEnd;
255
+ if (this.settleTimer === undefined || end === undefined) return;
256
+ this.cancelSettle();
257
+ this.onSettle(end);
258
+ }
259
+
260
+ private cancelSettle(): void {
261
+ if (this.settleTimer === undefined) return;
262
+ clearTimeout(this.settleTimer);
263
+ this.settleTimer = undefined;
264
+ this.settleEnd = undefined;
265
+ }
266
+ }
267
+
268
+ /** 文本控件上的组合状态:闸门 + 组合开始时的控件值 + 组合期间没写到控件上的模型值 */
269
+ interface ModelComposition {
270
+ gate: SomaCompositionGate;
271
+ startValue: string;
272
+ deferredView?: { value: unknown };
273
+ }
274
+
275
+ /** 把一段管道调用还原成声明里的写法:`money`、`round(2)`、`tpl(count: page.n)`(位置式实参不带键名) */
276
+ function formatPipeCall(pipe: SomaPipeCall): string {
277
+ const args = Object.entries(pipe.args ?? {});
278
+ if (args.length === 0) return pipe.name;
279
+ const parts = args.map(([key, arg]) => (/^\d+$/.test(key) ? String(arg.value) : `${key}: ${String(arg.value)}`));
280
+ return `${pipe.name}(${parts.join(', ')})`;
281
+ }
282
+
283
+ /**
284
+ * 把编译后的绑定还原成 `_ui.yaml` 里那一行的写法,用于管道失败告警的点名与去重。
285
+ * 路径是**编译后的根相对路径**(`page.` 这类 var 前缀在编译期已经解析掉了,运行时拿不回原文),
286
+ * 配上锚点足以定位到具体那一行;行作用域的声明本来就是原文,直接用不经这里。
287
+ */
288
+ function formatPipedDecl(basePath: string, pipes: SomaPipeCall[]): string {
289
+ return [basePath, ...pipes.map(formatPipeCall)].join(' | ');
290
+ }
48
291
 
49
292
 
50
293
  /**
@@ -76,12 +319,15 @@ export class SomaDomRenderer {
76
319
  private scopeTracker: WeakMap<HTMLElement, any> | null = null;
77
320
  private modelWriteBack: ModelWriteBack | null = null;
78
321
  private modelListeners = new WeakSet<HTMLElement>();
322
+ private modelCompositions = new WeakMap<HTMLElement, ModelComposition>();
79
323
  private modelAbort = new AbortController();
80
324
  private componentHook: SomaRowComponentHook | null = null;
81
325
  private componentReleaseHook: SomaRowComponentReleaseHook | null = null;
82
326
  private pipeRegistry: SomaPipeRegistry = defaultPipeRegistry;
83
327
  private explicitLists = new Map<HTMLElement, ExplicitListState>();
84
328
  private repeatPlans = new Map<string, RepeatPlan>();
329
+ /** 已报过管道执行失败的位置(键见 warnPipeFailure):同一锚点同一声明只报一次,随渲染器一起销毁 */
330
+ private readonly warnedPipeSites = new Set<string>();
85
331
 
86
332
  setRepeatPlans(plans: readonly RepeatPlan[]): void {
87
333
  this.repeatPlans.clear();
@@ -189,8 +435,39 @@ export class SomaDomRenderer {
189
435
  };
190
436
  }
191
437
 
192
- private applyPipes(value: unknown, pipes: SomaPipeCall[], ctx: SomaPipeContext): string {
193
- return this.pipeRegistry.apply(value, pipes, ctx);
438
+ /**
439
+ * 页面级 text / @属性 绑定的管道执行。
440
+ *
441
+ * 走 tryApply 而不是 apply:apply 自己吞掉异常、打一条 `管道 "x" 执行失败` 就完了——
442
+ * 那条告警没有锚点也没有声明,一页十几个管道绑定时看不出是哪一个;且它在**每次重算**都打,
443
+ * 首帧更是两条(组件页面装好行钩子后会补一次全量渲染,见 component-manager 的 activate),
444
+ * 一个写错的管道能把控制台刷满。这里把失败接回来,按「锚点 + 声明」点名并去重。
445
+ * 降级值与 apply 完全一致:原值字符串——用户看到的文本一个字不变。
446
+ */
447
+ private applyPipes(value: unknown, pipes: SomaPipeCall[], ctx: SomaPipeContext, where: string, decl: string): string {
448
+ const result = this.pipeRegistry.tryApply(value, pipes, ctx);
449
+ if (result.ok) return result.value;
450
+ this.warnPipeFailure(where, decl, result.pipeName, result.error);
451
+ return String(value ?? '');
452
+ }
453
+
454
+ /**
455
+ * 管道执行失败的告警:点名锚点(`data-soma-id`)、绑定声明、出错的管道名与原因,
456
+ * 原始异常作为第二个实参(控制台可展开堆栈)。
457
+ * 同一锚点同一声明只报一次——重算不再重复,行级绑定按列表锚点去重(几百行只报一条)。
458
+ * 去重表随渲染器生存,页面卸载重挂会重新报一遍。
459
+ */
460
+ private warnPipeFailure(where: string, decl: string, pipeName: string, error: unknown): void {
461
+ const key = `${where}\u0000${decl}`;
462
+ if (this.warnedPipeSites.has(key)) return;
463
+ this.warnedPipeSites.add(key);
464
+ // SomaPipeError 的消息自带 [soma-core][管道] 前缀,拼进本条告警前去掉,免得一行两个前缀
465
+ const reason = error instanceof Error ? error.message.replace(/^\[soma-core\]\[管道\]\s*/, '') : String(error);
466
+ console.warn(
467
+ `[soma-core][管道] ${where} 绑定为 ${JSON.stringify(decl)},管道 "${pipeName}" 执行失败(${reason})。`
468
+ + '该绑定降级为原值字符串照常上屏;同一锚点同一声明只报这一次',
469
+ error,
470
+ );
194
471
  }
195
472
 
196
473
  setModelWriteBack(fn: ModelWriteBack): void {
@@ -201,12 +478,76 @@ export class SomaDomRenderer {
201
478
  private ensureModelListener(element: HTMLElement, dataPath: string): void {
202
479
  if (!this.adapter || !this.modelWriteBack || this.modelListeners.has(element)) return;
203
480
  this.modelListeners.add(element);
204
- const writeModel = () => this.modelWriteBack?.(dataPath, this.adapter!.getValue(element));
205
- for (const eventType of ['input', 'change', 'blur']) {
481
+ const writeModel = (event: Event) => this.syncModel(element, dataPath, event);
482
+ const kind = modelControlKind(element);
483
+ // 闸门的监听先于回写监听挂上:同一个事件到达控件时,闸门先更新组合状态,回写再按它判断
484
+ if (kind === 'text' && element.localName !== 'select') {
485
+ this.attachModelComposition(this.adapter, element, dataPath);
486
+ }
487
+ const eventTypes = kind === 'text' || kind === 'number' ? ['input', 'change', 'blur'] : ['change'];
488
+ for (const eventType of eventTypes) {
206
489
  this.adapter.listen(element, eventType, writeModel, this.modelAbort.signal);
207
490
  }
208
491
  }
209
492
 
493
+ /**
494
+ * 文本输入控件(input / textarea)挂输入法组合闸门(见 SomaCompositionGate)。
495
+ * 组合结束后的补写:控件值与组合开始时不同(上屏了字)才回写;没变(取消、删光)不回写,
496
+ * 若组合期间(含组合结束、补写还排着那一小段)模型被外部改过、当时没写到控件上,这时补写到控件。
497
+ */
498
+ private attachModelComposition(adapter: IContainerAdapter, element: HTMLElement, dataPath: string): void {
499
+ const signal = this.modelAbort.signal;
500
+ const composition: ModelComposition = {
501
+ startValue: '',
502
+ gate: new SomaCompositionGate(
503
+ (type, listener) => adapter.listen(element, type, listener, signal),
504
+ signal,
505
+ () => {
506
+ const deferred = composition.deferredView;
507
+ composition.deferredView = undefined;
508
+ if (adapter.getValue(element) !== composition.startValue) {
509
+ this.syncModel(element, dataPath);
510
+ } else if (deferred) {
511
+ writeModelValue(adapter, element, deferred.value);
512
+ }
513
+ },
514
+ () => {
515
+ composition.startValue = adapter.getValue(element);
516
+ composition.deferredView = undefined;
517
+ },
518
+ ),
519
+ };
520
+ this.modelCompositions.set(element, composition);
521
+ }
522
+
523
+ /**
524
+ * 把控件当前状态写回模型:checkbox 写布尔、选中的 radio 写它的 value、select[multiple] 写数组,
525
+ * number / range 写数值(空为 null),其余写适配器 getValue 读到的字符串。控件事件触发的回写走这里。
526
+ * 事件处理器执行前的整表同步若绕过这里直接读适配器 getValue,checkbox 会被写成它的 value(缺省 "on"),
527
+ * radio 组会被写成排在最后的那个 radio 的 value。
528
+ * 文本输入控件在输入法组合进行中不回写(event 为触发回写的控件事件,整表同步不传);
529
+ * 组合结束后的这次回写读到的已是最终值,撤掉闸门排下的补写,组合期间没写到控件上的模型值作废(控件值为准)。
530
+ *
531
+ * 与模型现值相同的回写直接丢:代理的 set 只对基本类型去重,多选这类数组值到不了那一层(每次读出来都是新数组)。
532
+ * 不去重的话同一个值会连发好几遍——文本控件的 input / change / blur 各一次,处理器执行前的整表同步再一次
533
+ * (页面同时声明 model 与 input 处理器时,每次按键、每次上屏都收到两次相同的通知就是这么来的)。
534
+ * 丢的只是"值没变"的那次写:控件读出来的值与模型现值不同就照常写,checkbox / radio / 多选 / number / file
535
+ * 的真实变化一次不少(多选每次读出来都是新数组,按元素比,见 sameModelValue)。
536
+ */
537
+ syncModel(element: HTMLElement, dataPath: string, event?: Event): void {
538
+ if (!this.adapter || !this.modelWriteBack) return;
539
+ const composition = this.modelCompositions.get(element);
540
+ if (composition) {
541
+ if (composition.gate.holds(event)) return;
542
+ composition.gate.settled();
543
+ composition.deferredView = undefined;
544
+ }
545
+ const read = readModelValue(this.adapter, element);
546
+ if (read.skip) return;
547
+ if (this.resolver && sameModelValue(this.resolver.readFromRootCompiled(dataPath), read.value)) return;
548
+ this.modelWriteBack(dataPath, read.value);
549
+ }
550
+
210
551
  bindEntry(binding: SomaCompiledBinding, container: HTMLElement): BindingEntry | null {
211
552
  if (!this.adapter) {
212
553
  return null;
@@ -271,7 +612,10 @@ export class SomaDomRenderer {
271
612
  case 'text': {
272
613
  const pipes = entry.pipes;
273
614
  if (pipes?.length) {
274
- return (v) => adapter.setText(el, this.applyPipes(v, pipes, this.rootPipeContext()));
615
+ // 告警用的锚点与声明在编译期定下(每帧不再拼串)
616
+ const where = `锚点 "${entry.somaId}"`;
617
+ const decl = formatPipedDecl(entry.dataPath, pipes);
618
+ return (v) => adapter.setText(el, this.applyPipes(v, pipes, this.rootPipeContext(), where, decl));
275
619
  }
276
620
  return (v) => {
277
621
  if (v != null && typeof v !== 'object') {
@@ -284,7 +628,9 @@ export class SomaDomRenderer {
284
628
  const attrName = entry.attrName!;
285
629
  const pipes = entry.pipes;
286
630
  if (pipes?.length) {
287
- return (v) => adapter.setAttribute(el, attrName, this.applyPipes(v, pipes, this.rootPipeContext()));
631
+ const where = `锚点 "${entry.somaId}@${attrName}"`;
632
+ const decl = formatPipedDecl(entry.dataPath, pipes);
633
+ return (v) => adapter.setAttribute(el, attrName, this.applyPipes(v, pipes, this.rootPipeContext(), where, decl));
288
634
  }
289
635
  return (v) => {
290
636
  if (v != null && typeof v !== 'object') {
@@ -304,9 +650,19 @@ export class SomaDomRenderer {
304
650
  const dataPath = entry.dataPath;
305
651
  const self = this;
306
652
  return (v) => {
307
- const strValue = String(v ?? '');
308
- if (modelAdapter.getValue(modelEl) !== strValue) {
309
- modelAdapter.setValue(modelEl, strValue);
653
+ // 控件上是用户正在输入、还没回写到模型的内容时不改写控件,这一帧的模型值留到组合结束
654
+ // (见 attachModelComposition)。两种情况:
655
+ // - 组合进行中:控件上是组合串,模型还是组合前的值,写进去会冲掉组合;
656
+ // - 组合刚结束、补写还排着(settling):控件上已是刚上屏的字,模型还停在上一个字。
657
+ // 连续打字时这一帧就落在这里——上一个字的回写排进了渲染,渲染的 rAF 又落在下一个字的
658
+ // compositionend 之后、补写之前,照写就把刚上屏的字冲回上一个字(丢字)。
659
+ // 模型被外部真的改了(处理器清空、转大写、定时器改值)不受影响:补写/回写时控件与组合开始时
660
+ // 相同就把这一帧的值写上去,不同则以控件为准,两种都不会停在旧值上。
661
+ const composition = self.modelCompositions.get(modelEl);
662
+ if (composition && (composition.gate.holds() || composition.gate.settling())) {
663
+ composition.deferredView = { value: v };
664
+ } else {
665
+ writeModelValue(modelAdapter, modelEl, v);
310
666
  }
311
667
  if (modelWriteBack && !modelListeners.has(modelEl)) {
312
668
  self.ensureModelListener(modelEl, dataPath);
@@ -364,13 +720,34 @@ export class SomaDomRenderer {
364
720
  this.modelAbort.abort();
365
721
  this.modelAbort = new AbortController();
366
722
  this.modelListeners = new WeakSet<HTMLElement>();
723
+ this.modelCompositions = new WeakMap<HTMLElement, ModelComposition>();
367
724
  this.modelWriteBack = null;
725
+ // 管道告警去重表随渲染器一起清空:热换/重挂后的同一处失败值得再报一次
726
+ this.warnedPipeSites.clear();
368
727
  }
369
728
 
370
729
  renderEntry(entry: BindingEntry, value: any): void {
371
730
  if (!this.adapter) return;
372
731
  // 所有 entry 在 bindEntry 时已编译 compiledRender,直接调用
373
- entry.compiledRender!(value);
732
+ if (entry.type === 'list' || entry.type === 'obj') {
733
+ // 结构绑定的错误(CT-* 预算、产物缺失、行内控制器失败)按设计失败即停,且自带行回滚,照常抛出
734
+ entry.compiledRender!(value);
735
+ return;
736
+ }
737
+ try {
738
+ entry.compiledRender!(value);
739
+ } catch (error) {
740
+ // 叶子绑定(text / attr / html / model / when / class)互不依赖:一项写 DOM 或读条件出错,
741
+ // 不该让同一帧里排在它后面的绑定全部停在旧值。抛出去也没人接——全量渲染的 Promise 被 void 掉,
742
+ // patch 阶段被调度器的 catch 吞掉——所以在这里记下来,带上锚点与数据路径
743
+ const target = entry.attrName ? `${entry.somaId}@${entry.attrName}` : entry.somaId;
744
+ // class 绑定没有单一数据路径,报它的条件路径
745
+ const path = entry.type === 'class' ? Object.values(entry.classRules ?? {}).join(' / ') : entry.dataPath;
746
+ console.error(
747
+ `[soma-core][引擎] 绑定渲染失败,已跳过这一项(同帧其余绑定照常渲染):${target}(${entry.type},路径 "${path}")`,
748
+ error,
749
+ );
750
+ }
374
751
  }
375
752
 
376
753
  private renderList(entry: BindingEntry, dataArray: any[]): void {
@@ -439,7 +816,7 @@ export class SomaDomRenderer {
439
816
  // 更新数据
440
817
  this.scopeTracker?.set(old.el, dataArray[i]);
441
818
  if (entry.childShowConfig) {
442
- this.applyShowConfig(old.el, dataArray[i], entry.childShowConfig);
819
+ this.applyShowConfig(old.el, dataArray[i], entry.childShowConfig, entry.somaId);
443
820
  }
444
821
  }
445
822
  }
@@ -464,7 +841,7 @@ export class SomaDomRenderer {
464
841
  if (variant) wrapper.setAttribute('data-soma-variant', variant);
465
842
  this.scopeTracker?.set(wrapper, dataArray[i]);
466
843
  if (entry.childShowConfig) {
467
- this.applyShowConfig(wrapper, dataArray[i], entry.childShowConfig);
844
+ this.applyShowConfig(wrapper, dataArray[i], entry.childShowConfig, entry.somaId);
468
845
  }
469
846
 
470
847
  if (nextSibling) {
@@ -561,7 +938,7 @@ export class SomaDomRenderer {
561
938
  for (const root of range.roots) {
562
939
  this.scopeTracker?.set(root, range.scope);
563
940
  if (range.variant) root.setAttribute('data-soma-variant', range.variant);
564
- if (entry.childShowConfig) this.applyShowConfig(root, range.scope, entry.childShowConfig);
941
+ if (entry.childShowConfig) this.applyShowConfig(root, range.scope, entry.childShowConfig, entry.somaId);
565
942
  this.activateDefaultRowComponents(root, range.scope, entry.childShowConfig);
566
943
  }
567
944
  }); } catch (error) {
@@ -710,7 +1087,7 @@ export class SomaDomRenderer {
710
1087
  if (variant) wrapper.setAttribute('data-soma-variant', variant);
711
1088
  this.scopeTracker?.set(wrapper, itemData);
712
1089
  if (entry.childShowConfig) {
713
- this.applyShowConfig(wrapper, itemData, entry.childShowConfig);
1090
+ this.applyShowConfig(wrapper, itemData, entry.childShowConfig, entry.somaId);
714
1091
  }
715
1092
 
716
1093
  this.adapter.appendFragment(entry.element, fragment);
@@ -730,7 +1107,7 @@ export class SomaDomRenderer {
730
1107
 
731
1108
  this.scopeTracker?.set(wrapper, data);
732
1109
  if (entry.childShowConfig) {
733
- this.applyShowConfig(wrapper, data, entry.childShowConfig);
1110
+ this.applyShowConfig(wrapper, data, entry.childShowConfig, entry.somaId);
734
1111
  }
735
1112
 
736
1113
  this.adapter.appendFragment(entry.element, fragment);
@@ -740,13 +1117,14 @@ export class SomaDomRenderer {
740
1117
  root: HTMLElement,
741
1118
  data: any,
742
1119
  showConfig: ShowMapping,
1120
+ scopeAnchor: string | undefined,
743
1121
  ): void {
744
1122
  for (const [rawKey, config] of Object.entries(showConfig)) {
745
1123
  const groupMatch = rawKey.match(/^item:(.+)$/);
746
1124
  if (groupMatch) {
747
1125
  const sub = (config as ExtendedShowValue | undefined)?.show;
748
1126
  if (sub && (root.getAttribute('data-soma-variant') ?? '') === groupMatch[1]) {
749
- this.applyShowConfig(root, data, sub);
1127
+ this.applyShowConfig(root, data, sub, scopeAnchor);
750
1128
  }
751
1129
  continue;
752
1130
  }
@@ -756,7 +1134,7 @@ export class SomaDomRenderer {
756
1134
  }
757
1135
 
758
1136
  if (this.isExtendedShowValue(config)) {
759
- this.applyExtendedShowConfig(root, data, rawKey, config);
1137
+ this.applyExtendedShowConfig(root, data, rawKey, config, scopeAnchor);
760
1138
  continue;
761
1139
  }
762
1140
 
@@ -766,7 +1144,7 @@ export class SomaDomRenderer {
766
1144
  continue;
767
1145
  }
768
1146
 
769
- const value = this.resolveRowValue(config, data);
1147
+ const value = this.resolveRowValue(config, data, scopeAnchor, rawKey);
770
1148
  if (attrName === 'html') {
771
1149
  this.adapter?.setHtml(element, String(value ?? ''));
772
1150
  continue;
@@ -817,7 +1195,7 @@ export class SomaDomRenderer {
817
1195
  // activation render; never infer their outlet from the authored source.
818
1196
  if (!this.componentHook && !element.hasAttribute('data-soma-component-ready')) return;
819
1197
  const entry: BindingEntry = {
820
- element, type: 'list', dataPath: config.bind.obj,
1198
+ element, somaId, type: 'list', dataPath: config.bind.obj,
821
1199
  itemAlias: config.bind.var, childShowConfig: config.show,
822
1200
  };
823
1201
  this.attachRepeatPlan(entry, config.bind.repeatPlanId);
@@ -834,7 +1212,7 @@ export class SomaDomRenderer {
834
1212
 
835
1213
  if (config.bind.key) {
836
1214
  this.renderExplicitList({
837
- element, type: 'list', dataPath: config.bind.obj, templateCache,
1215
+ element, somaId, type: 'list', dataPath: config.bind.obj, templateCache,
838
1216
  templatesByRole, roleBy, childShowConfig: config.show,
839
1217
  keyPath: config.bind.key, itemAlias: config.bind.var,
840
1218
  }, nestedData, data instanceof SomaRowScope ? data : undefined);
@@ -849,13 +1227,13 @@ export class SomaDomRenderer {
849
1227
  for (const itemData of items) {
850
1228
  const picked = this.pickNestedTemplate(templateCache, templatesByRole, roleBy, itemData);
851
1229
  if (!picked.template) continue;
852
- this.appendNestedTemplateItem(element, picked.template, itemData, config.show, picked.variant);
1230
+ this.appendNestedTemplateItem(element, picked.template, itemData, config.show, somaId, picked.variant);
853
1231
  }
854
1232
  return;
855
1233
  }
856
1234
 
857
1235
  if (nestedData && typeof nestedData === 'object') {
858
- this.appendNestedTemplateItem(element, templateCache, nestedData, config.show);
1236
+ this.appendNestedTemplateItem(element, templateCache, nestedData, config.show, somaId);
859
1237
  }
860
1238
  }
861
1239
 
@@ -882,6 +1260,7 @@ export class SomaDomRenderer {
882
1260
  templateCache: DocumentFragment,
883
1261
  itemData: any,
884
1262
  showConfig: ShowMapping,
1263
+ scopeAnchor: string | undefined,
885
1264
  variant = '',
886
1265
  ): void {
887
1266
  if (!this.adapter) {
@@ -906,15 +1285,24 @@ export class SomaDomRenderer {
906
1285
  }
907
1286
 
908
1287
  this.scopeTracker?.set(wrapper, itemData);
909
- this.applyShowConfig(wrapper, itemData, showConfig);
1288
+ this.applyShowConfig(wrapper, itemData, showConfig, scopeAnchor);
910
1289
  this.adapter.appendFragment(container, fragment);
911
1290
  }
912
1291
 
1292
+ /**
1293
+ * 行 / 对象作用域的扩展绑定:props、text、when、class 与嵌套 show。
1294
+ *
1295
+ * 这里没有 model 分支,是引擎的裁决而不是遗漏:行数据是数组元素、不经响应式代理,
1296
+ * 页面路径形态又缺同屏刷新与整表同步(syncModels 只认编译出来的绑定表)。
1297
+ * 写了 model 的声明进不到这一步——编译期就被拒收(manifest-compiler 的 scopedModelRefusal
1298
+ * 讲清了两种写法各自的原因与改法),不再是挂载期一条告警加一个失效的控件。
1299
+ */
913
1300
  private applyExtendedShowConfig(
914
1301
  root: HTMLElement,
915
1302
  data: any,
916
1303
  rawKey: string,
917
1304
  config: ExtendedShowValue,
1305
+ scopeAnchor: string | undefined,
918
1306
  ): void {
919
1307
  const [somaId, attrName] = rawKey.split('@');
920
1308
  const element = this.findScopedAnchor(root, somaId);
@@ -925,7 +1313,7 @@ export class SomaDomRenderer {
925
1313
 
926
1314
  if (element) {
927
1315
  if (config.text) {
928
- const value = this.resolveRowValue(config.text, data);
1316
+ const value = this.resolveRowValue(config.text, data, scopeAnchor, rawKey);
929
1317
  if (attrName === 'html') {
930
1318
  this.adapter?.setHtml(element, String(value ?? ''));
931
1319
  } else if (attrName) {
@@ -953,12 +1341,19 @@ export class SomaDomRenderer {
953
1341
 
954
1342
  if (config.show) {
955
1343
  const showRoot = element ?? root;
956
- this.applyShowConfig(showRoot, data, config.show);
1344
+ this.applyShowConfig(showRoot, data, config.show, scopeAnchor);
957
1345
  }
958
1346
  }
959
1347
 
960
- /** 行作用域展示值:透明处理管道(`item.price | money`),无管道走原路径 */
961
- private resolveRowValue(raw: string, itemData: any): any {
1348
+ /**
1349
+ * 行作用域展示值:透明处理管道(`item.price | money`),无管道走原路径。
1350
+ *
1351
+ * 与页面绑定同样走 tryApply:管道抛错时点名「列表锚点 + 行内锚点 + 声明」并按这个键去重——
1352
+ * 一个写错的管道在五百行的表里只报一条,不是五百条(还要乘上每次重算)。
1353
+ * 声明这里拿得到原文(行内绑定是解释执行的,不经编译期的路径解析)。
1354
+ * 降级值与 apply 一致:原值字符串,上屏文本不变。
1355
+ */
1356
+ private resolveRowValue(raw: string, itemData: any, scopeAnchor: string | undefined, anchorKey: string): any {
962
1357
  if (!hasPipes(raw)) {
963
1358
  return this.resolveShowValue(raw, itemData);
964
1359
  }
@@ -969,7 +1364,15 @@ export class SomaDomRenderer {
969
1364
  read: (path) => this.resolveShowValue(path, itemData),
970
1365
  locale: () => resolver?.readFromRoot('_locale') as Record<string, unknown> | undefined,
971
1366
  };
972
- return this.pipeRegistry.apply(base, parsed.pipes, ctx);
1367
+ const result = this.pipeRegistry.tryApply(base, parsed.pipes, ctx);
1368
+ if (result.ok) return result.value;
1369
+ this.warnPipeFailure(
1370
+ scopeAnchor ? `锚点 "${scopeAnchor}" 行内的 "${anchorKey}"` : `行内锚点 "${anchorKey}"`,
1371
+ raw,
1372
+ result.pipeName,
1373
+ result.error,
1374
+ );
1375
+ return String(base ?? '');
973
1376
  }
974
1377
 
975
1378
  private resolveShowValue(valuePath: string, itemData: any): any {