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
@@ -75,7 +75,7 @@ export interface SomaComponentPack {
75
75
 
76
76
  export class ComponentPackError extends Error {
77
77
  constructor(message: string) {
78
- super(`[SomaComponentPack] ${message}`);
78
+ super(`[soma-core][组件] ${message}`);
79
79
  this.name = 'ComponentPackError';
80
80
  }
81
81
  }
@@ -7,7 +7,7 @@ import { findNestedMutableContractDefault } from '../../shared/types/component-c
7
7
 
8
8
  export class SomaComponentRegistryError extends Error {
9
9
  constructor(message: string) {
10
- super(`[SomaComponentRegistry] ${message}`);
10
+ super(`[soma-core][组件] ${message}`);
11
11
  this.name = 'SomaComponentRegistryError';
12
12
  }
13
13
  }
@@ -104,12 +104,19 @@ export class SomaContainerAdapter implements IContainerAdapter {
104
104
  if (safe === null) {
105
105
  return;
106
106
  }
107
+ const previous = element.getAttribute(attrName);
108
+ const inputValueBefore = readInputValue(element, attrName);
107
109
  // 空串在布尔/ARIA 状态属性上意味着「本行不该有这个属性」——删除而非写空串
108
110
  if (shouldRemoveAttribute(attrName, safe)) {
109
111
  element.removeAttribute(attrName);
110
- return;
112
+ } else {
113
+ element.setAttribute(attrName, safe);
114
+ }
115
+ // 特性没变 = 绑定值没变:不碰控件当前状态。重渲染(props 下行、行复用、无关字段变更)
116
+ // 每次都会走到这里,用户键入中尚未回写的内容、点掉的勾选都原样保留
117
+ if (element.getAttribute(attrName) !== previous) {
118
+ syncFormControlState(element, attrName, inputValueBefore);
111
119
  }
112
- element.setAttribute(attrName, safe);
113
120
  }
114
121
 
115
122
  setHtml(element: HTMLElement, value: string): void {
@@ -137,7 +144,20 @@ export class SomaContainerAdapter implements IContainerAdapter {
137
144
  element.classList.remove(...classes);
138
145
  }
139
146
 
147
+ /**
148
+ * 读写控件的 `.value`(文本值)。checkbox 的 `.value` 是提交值(缺省 "on"),不是勾选状态;
149
+ * checkbox / radio / select[multiple] 的模型值分别是布尔、组内选中值、数组,
150
+ * 渲染器的 model 读写按控件形态直接处理 `.checked` / `.selected`,不经过这两个方法。
151
+ * number / file 的取值规整(转数值、只许清空)也在渲染器里,到这里的一律是字符串。
152
+ * input / textarea 经 writeTextValue 写入:控件正聚焦时保住选区——model 回写被处理器改写过的值
153
+ * (大写化、去空格)与特性路径同一套写法,光标不跳到末尾。
154
+ */
140
155
  setValue(element: HTMLElement, value: string): void {
156
+ const tag = element.localName;
157
+ if (tag === 'input' || tag === 'textarea') {
158
+ writeTextValue(element as HTMLInputElement | HTMLTextAreaElement, value);
159
+ return;
160
+ }
141
161
  (element as HTMLInputElement).value = value;
142
162
  }
143
163
 
@@ -188,3 +208,84 @@ export class SomaContainerAdapter implements IContainerAdapter {
188
208
  }
189
209
  }
190
210
  }
211
+
212
+ /**
213
+ * 表单控件的特性只管初始值:value / checked / selected 特性对应的是 defaultValue / defaultChecked /
214
+ * defaultSelected。用户输入过、点选过,或脚本写过 property 之后控件就是 dirty 的,再改特性不会改变
215
+ * 显示——页面「重置」「清空」这类程序化改值会静默失效。故特性变化时把控件当前状态对齐到刚写下的特性:
216
+ * - input / textarea / select 的 value → `.value`(file 输入框除外);
217
+ * - checkbox / radio 的 checked → `.checked`(特性存在即为真,与布尔属性语义一致);
218
+ * - option 的 selected → `.selected`。
219
+ * property 已等于目标时不写:写 `.value` 会把光标移到末尾。
220
+ * `inputValueBefore` 是写特性前 input 的 `.value`(非 input 或非 value 特性时为 null)。
221
+ */
222
+ function syncFormControlState(element: HTMLElement, attrName: string, inputValueBefore: string | null): void {
223
+ const tag = element.localName;
224
+ switch (attrName.toLowerCase()) {
225
+ case 'value': {
226
+ const next = element.getAttribute('value') ?? '';
227
+ if (tag === 'select') {
228
+ const select = element as HTMLSelectElement;
229
+ if (select.value !== next) select.value = next;
230
+ return;
231
+ }
232
+ if (tag === 'textarea') {
233
+ writeTextValue(element as HTMLTextAreaElement, next);
234
+ return;
235
+ }
236
+ const input = element as HTMLInputElement;
237
+ // file 输入框的 value 只能由用户选文件改变,脚本写非空值会抛 InvalidStateError
238
+ if (tag !== 'input' || input.type === 'file') return;
239
+ // 值随特性变了 = 控件没被改过,原生语义已经生效(含取值规整,如 range 按 min/max 夹取)。
240
+ // 此时不写 property:写了控件就成了 dirty,之后 min/max 等特性再变,它不会再按特性重新规整
241
+ if (input.value !== inputValueBefore) return;
242
+ writeTextValue(input, next);
243
+ return;
244
+ }
245
+ case 'checked': {
246
+ const input = element as HTMLInputElement;
247
+ if (tag !== 'input' || (input.type !== 'checkbox' && input.type !== 'radio')) return;
248
+ const next = input.hasAttribute('checked');
249
+ if (input.checked !== next) input.checked = next;
250
+ return;
251
+ }
252
+ case 'selected': {
253
+ if (tag !== 'option') return;
254
+ const option = element as HTMLOptionElement;
255
+ const next = option.hasAttribute('selected');
256
+ if (option.selected !== next) option.selected = next;
257
+ return;
258
+ }
259
+ default:
260
+ return;
261
+ }
262
+ }
263
+
264
+ /** 写特性前 input 的当前值(只有 input 的 value 用得上:据此识别控件是否跟随了特性) */
265
+ function readInputValue(element: HTMLElement, attrName: string): string | null {
266
+ return element.localName === 'input' && attrName.toLowerCase() === 'value'
267
+ ? (element as HTMLInputElement).value
268
+ : null;
269
+ }
270
+
271
+ /**
272
+ * 写文本类控件的 `.value`。控件正聚焦时保住选区:转换类页面(大写化、去空格)每敲一个字
273
+ * 值就被改写一次,不保的话光标每次都跳到末尾。
274
+ */
275
+ function writeTextValue(control: HTMLInputElement | HTMLTextAreaElement, next: string): void {
276
+ if (control.value === next) return;
277
+ const root = control.getRootNode() as Document | ShadowRoot;
278
+ let selection: { start: number; end: number; direction: 'forward' | 'backward' | 'none' } | null = null;
279
+ if (root.activeElement === control) {
280
+ try {
281
+ const { selectionStart: start, selectionEnd: end, selectionDirection: direction } = control;
282
+ if (start !== null && end !== null) selection = { start, end, direction: direction ?? 'none' };
283
+ } catch { /* 类型不支持选区(如 number / email)时忽略 */ }
284
+ }
285
+ control.value = next;
286
+ if (!selection) return;
287
+ const length = control.value.length;
288
+ try {
289
+ control.setSelectionRange(Math.min(selection.start, length), Math.min(selection.end, length), selection.direction);
290
+ } catch { /* 类型不支持选区时忽略 */ }
291
+ }
@@ -698,6 +698,9 @@ export class SomaDataObjectResolver {
698
698
  /** 派生列 AST 缓存:对象名 → 字段名 → AST */
699
699
  private readonly derivedAsts = new Map<string, Map<string, CalcAst>>();
700
700
 
701
+ /** 已报过 transform 管道执行失败的位置(对象 + 字段 + 声明):同一处只报一次,随解析器生存 */
702
+ private readonly warnedTransformPipes = new Set<string>();
703
+
701
704
  /** 数据窗口:对象名 → 窗口实例(惰性建,reload 时销毁) */
702
705
  private readonly windows = new Map<string, SomaWindowHandle>();
703
706
 
@@ -719,6 +722,14 @@ export class SomaDataObjectResolver {
719
722
  return this.applyTransform(this.applyDerived(value, decl), decl);
720
723
  }
721
724
 
725
+ /**
726
+ * 展示管道(`transform:`)求值。
727
+ *
728
+ * 与页面绑定、行绑定、组件 props 同样走 tryApply 而不是 apply:apply 自己吞掉异常、
729
+ * 打一条不带出处的 `管道 "x" 执行失败` 就完了——一份 data.yaml 里十几个 transform 声明时
730
+ * 看不出是哪一个,而取数每重来一次(reload、窗口翻页)就再刷一遍。这里把失败接回来,
731
+ * 按「对象 + 字段 + 声明」点名并去重。降级值与 apply 完全一致:原值字符串,上屏内容一个字不变。
732
+ */
722
733
  private applyTransform(value: unknown, decl: SomaDataObjectConfig): unknown {
723
734
  if (!decl.transform) return value;
724
735
  if (!isPlainObject(value)) {
@@ -739,11 +750,44 @@ export class SomaDataObjectResolver {
739
750
  }
740
751
  const parsed = parsePipedValue(normalized, () => false);
741
752
  const base = readPath(value, parsed.basePath.split('.'));
742
- value[key] = this.pipes.apply(base, parsed.pipes, ctx);
753
+ const result = this.pipes.tryApply(base, parsed.pipes, ctx);
754
+ if (result.ok) {
755
+ value[key] = result.value;
756
+ continue;
757
+ }
758
+ this.warnTransformPipeFailure(decl.name, key, normalized, result.pipeName, result.error);
759
+ value[key] = String(base ?? '');
743
760
  }
744
761
  return value;
745
762
  }
746
763
 
764
+ /**
765
+ * transform 管道执行失败的告警:点名对象、派生字段、声明原文、出错的管道名与原因,
766
+ * 原始异常作为第二个实参(控制台可展开堆栈)。同一对象同一字段同一声明只报一次——
767
+ * 取数会重来(reload、窗口翻页),一个写错的管道不该每取一次就刷一遍控制台。
768
+ *
769
+ * 未注册的管道名不会走到这里:构造时的声明期白名单已经 fail fast(见 constructor)。
770
+ * 能到这里的只有管道函数自己抛错——多半是值的形状与管道不匹配(date 收到非日期串这类)。
771
+ */
772
+ private warnTransformPipeFailure(
773
+ objectName: string,
774
+ field: string,
775
+ decl: string,
776
+ pipeName: string,
777
+ error: unknown,
778
+ ): void {
779
+ const key = `${objectName}\u0000${field}\u0000${decl}`;
780
+ if (this.warnedTransformPipes.has(key)) return;
781
+ this.warnedTransformPipes.add(key);
782
+ // SomaPipeError 的消息自带 [soma-core][管道] 前缀,拼进本条告警前去掉,免得一行两个前缀
783
+ const reason = error instanceof Error ? error.message.replace(/^\[soma-core\]\[管道\]\s*/, '') : String(error);
784
+ console.warn(
785
+ `[soma-core][数据] 对象 "${objectName}" 的 transform.${field} 声明为 ${JSON.stringify(decl)},`
786
+ + `管道 "${pipeName}" 执行失败(${reason})。该字段降级为原值字符串照常上屏;同一对象同一字段只报这一次`,
787
+ error,
788
+ );
789
+ }
790
+
747
791
  /**
748
792
  * 派生列求值(REQ-010 F-10):把 `fields.<x>.calc` 算出来挂到值上。
749
793
  *