soma-core 1.0.1 → 1.0.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 (227) hide show
  1. package/app/SomaPageClient.tsx +336 -0
  2. package/app/api/[...path]/route.ts +9 -85
  3. package/app/api/page/route.ts +18 -16
  4. package/app/api/soma-manifest/route.ts +39 -0
  5. package/app/components/SomaHydrationShell.tsx +23 -0
  6. package/app/layout.tsx +2 -2
  7. package/app/loading.tsx +5 -0
  8. package/app/page.tsx +19 -128
  9. package/bin/soma-lsp.cjs +195 -0
  10. package/bin/soma-sync.cjs +3 -0
  11. package/bin/soma.cjs +14 -2
  12. package/core/api/IService.ts +6 -10
  13. package/core/api/load-host-service.ts +72 -0
  14. package/core/api/service-path.ts +39 -0
  15. package/core/base/SomaEntity.ts +3 -10
  16. package/core/base/SomaLocaleOnlyPageRoot.ts +3 -4
  17. package/core/base/SomaPageRootBase.ts +20 -3
  18. package/core/base/decorators/Event.ts +23 -0
  19. package/core/base/decorators/Locale.ts +35 -0
  20. package/core/base/decorators/Page.ts +16 -0
  21. package/core/base/decorators/Show.ts +20 -0
  22. package/core/base/decorators/ShowList.ts +34 -0
  23. package/core/base/decorators/index.ts +5 -0
  24. package/core/base/index.ts +2 -0
  25. package/core/base/runAsync.ts +32 -0
  26. package/core/compiler/full-manifest-builder.ts +33 -7
  27. package/core/compiler/host-config-sync.cjs +38 -0
  28. package/core/compiler/manifest-compiler.ts +3 -2
  29. package/core/compiler/page-base-resolver.ts +23 -0
  30. package/core/compiler/precompiled-manifest-loader.ts +34 -0
  31. package/core/compiler/rollup-plugin-soma.ts +45 -0
  32. package/core/compiler/rspack-plugin-soma.js +33 -0
  33. package/core/compiler/soma-manifest-webpack-plugin.js +72 -0
  34. package/core/compiler/soma-precompile.js +3513 -0
  35. package/core/compiler/soma-precompile.ts +52 -6
  36. package/core/compiler/soma-rspack-yaml-loader.js +22 -0
  37. package/core/compiler/soma-webpack-loader.js +7 -1
  38. package/core/compiler/vite-plugin-soma.ts +11 -1
  39. package/core/container/IContainerAdapter.ts +36 -0
  40. package/core/container/MiniProgramContainerAdapter.ts +190 -0
  41. package/core/container/ReactNativeContainerAdapter.ts +189 -0
  42. package/core/container/SomaContainerAdapter.ts +17 -6
  43. package/core/diagnostics/SomaDiagnostics.ts +1 -1
  44. package/core/diagnostics/SomaTelemetry.ts +100 -0
  45. package/core/diagnostics/telemetry-context.ts +15 -0
  46. package/core/engine/asset-resolver.ts +39 -4
  47. package/core/engine/dom-renderer.ts +9 -11
  48. package/core/engine/event-engine.ts +110 -41
  49. package/core/engine/on-change-engine.ts +71 -0
  50. package/core/engine/property-watcher.ts +117 -0
  51. package/core/engine/reactive-proxy.ts +11 -5
  52. package/core/engine/render-engine.ts +97 -11
  53. package/core/engine/runtime-builder.ts +88 -11
  54. package/core/engine/scope-resolver.ts +65 -9
  55. package/core/engine/soma-bootstrap.ts +448 -244
  56. package/core/engine/style-binder.ts +423 -0
  57. package/core/engine/theme-variables.ts +37 -0
  58. package/core/engine/timer-engine.ts +92 -0
  59. package/core/engine/trigger-engine.ts +55 -0
  60. package/core/host/resolve-host-bootstrap.ts +28 -0
  61. package/core/host/soma-bootstrap-host.stub.ts +4 -0
  62. package/core/index.ts +33 -2
  63. package/core/infra/auth/TokenAuthService.ts +17 -3
  64. package/core/infra/http/IHttpClient.ts +11 -1
  65. package/core/infra/http/SomaHttpService.ts +89 -31
  66. package/core/infra/i18n/DefaultI18n.ts +6 -18
  67. package/core/infra/i18n/II18n.ts +2 -3
  68. package/core/infra/notify/DomNotifyService.ts +98 -0
  69. package/core/infra/notify/INotifyTheme.ts +22 -0
  70. package/core/infra/realtime/IRealtimeService.ts +23 -0
  71. package/core/infra/realtime/IRealtimeTransport.ts +9 -0
  72. package/core/infra/realtime/SomaRealtimeService.ts +95 -0
  73. package/core/infra/realtime/SseRealtimeTransport.ts +36 -0
  74. package/core/infra/realtime/WebSocketRealtimeTransport.ts +102 -0
  75. package/core/infra/router/router-path.ts +28 -0
  76. package/core/infra/storage/SessionStorageService.ts +59 -0
  77. package/core/lsp/cross-file-diagnostics.cjs +160 -0
  78. package/core/lsp/event-yaml-completion.cjs +106 -0
  79. package/core/lsp/extra-completion.cjs +64 -0
  80. package/core/lsp/lsp-navigation.cjs +191 -0
  81. package/core/lsp/ui-yaml-completion.cjs +170 -0
  82. package/core/manifest/manifest-contract.ts +12 -0
  83. package/core/middleware/createPermissionMiddleware.ts +37 -0
  84. package/core/parser/annotation-extractor.ts +113 -0
  85. package/core/parser/pipeline-parser.ts +344 -276
  86. package/core/parser/style-config-parser.ts +340 -0
  87. package/core/parser/timer-parser.ts +70 -0
  88. package/core/parser/trigger-parser.ts +90 -0
  89. package/core/parser/ui-config-parser.ts +73 -1
  90. package/core/quality/SomaQualityRegistry.ts +68 -0
  91. package/core/runtime/SomaRuntimeServices.ts +100 -0
  92. package/core/runtime/SomaSnapshotStore.ts +5 -3
  93. package/core/types/domain.types.ts +5 -2
  94. package/core/types/event.types.ts +82 -57
  95. package/core/types/manifest.types.ts +2 -0
  96. package/core/types/style-config.types.ts +76 -0
  97. package/core/types/timer.types.ts +23 -0
  98. package/core/types/trigger.types.ts +14 -0
  99. package/core/types/ui-config.types.ts +9 -0
  100. package/core/types/watcher.types.ts +29 -0
  101. package/dist/cli/soma.js +23 -23
  102. package/dist/core/index.cjs +8968 -0
  103. package/dist/diagnostics/telemetry.cjs +105 -0
  104. package/dist/preview-export.cjs +3401 -0
  105. package/dist/tsconfig.types.tsbuildinfo +1 -0
  106. package/dist/types/api/IService.d.ts +28 -0
  107. package/dist/types/api/load-host-service.d.ts +5 -0
  108. package/dist/types/api/service-path.d.ts +13 -0
  109. package/dist/types/base/SomaApi.d.ts +5 -0
  110. package/dist/types/base/SomaDict.d.ts +89 -0
  111. package/dist/types/base/SomaEntity.d.ts +15 -0
  112. package/dist/types/base/SomaHttpClient.d.ts +66 -0
  113. package/dist/types/base/SomaLocaleOnlyPageRoot.d.ts +3 -0
  114. package/dist/types/base/SomaPageRootBase.d.ts +49 -0
  115. package/dist/types/base/SomaValidator.d.ts +55 -0
  116. package/dist/types/base/decorators/Event.d.ts +10 -0
  117. package/dist/types/base/decorators/Locale.d.ts +9 -0
  118. package/dist/types/base/decorators/Page.d.ts +4 -0
  119. package/dist/types/base/decorators/Show.d.ts +7 -0
  120. package/dist/types/base/decorators/ShowList.d.ts +12 -0
  121. package/dist/types/base/decorators/index.d.ts +5 -0
  122. package/dist/types/base/index.d.ts +9 -0
  123. package/dist/types/base/runAsync.d.ts +12 -0
  124. package/dist/types/compiler/full-manifest-builder.d.ts +35 -0
  125. package/dist/types/compiler/manifest-compiler.d.ts +29 -0
  126. package/dist/types/compiler/page-base-resolver.d.ts +6 -0
  127. package/dist/types/compiler/precompiled-manifest-loader.d.ts +5 -0
  128. package/dist/types/compiler/rollup-plugin-soma.d.ts +14 -0
  129. package/dist/types/compiler/route-builder.d.ts +12 -0
  130. package/dist/types/compiler/soma-precompile.d.ts +40 -0
  131. package/dist/types/compiler/vite-plugin-soma.d.ts +34 -0
  132. package/dist/types/container/IContainerAdapter.d.ts +30 -0
  133. package/dist/types/container/MiniProgramContainerAdapter.d.ts +52 -0
  134. package/dist/types/container/ReactNativeContainerAdapter.d.ts +47 -0
  135. package/dist/types/container/SomaContainerAdapter.d.ts +30 -0
  136. package/dist/types/diagnostics/SomaDiagnostics.d.ts +64 -0
  137. package/dist/types/diagnostics/SomaTelemetry.d.ts +32 -0
  138. package/dist/types/diagnostics/telemetry-context.d.ts +4 -0
  139. package/dist/types/engine/asset-resolver.d.ts +17 -0
  140. package/dist/types/engine/dom-renderer.d.ts +35 -0
  141. package/dist/types/engine/event-engine.d.ts +64 -0
  142. package/dist/types/engine/lis.d.ts +10 -0
  143. package/dist/types/engine/on-change-engine.d.ts +16 -0
  144. package/dist/types/engine/property-watcher.d.ts +18 -0
  145. package/dist/types/engine/reactive-proxy.d.ts +58 -0
  146. package/dist/types/engine/render-engine.d.ts +73 -0
  147. package/dist/types/engine/runtime-builder.d.ts +58 -0
  148. package/dist/types/engine/scope-resolver.d.ts +90 -0
  149. package/dist/types/engine/soma-bootstrap.d.ts +112 -0
  150. package/dist/types/engine/style-binder.d.ts +81 -0
  151. package/dist/types/engine/theme-variables.d.ts +3 -0
  152. package/dist/types/engine/timer-engine.d.ts +19 -0
  153. package/dist/types/engine/trigger-engine.d.ts +16 -0
  154. package/dist/types/host/resolve-host-bootstrap.d.ts +11 -0
  155. package/dist/types/host/soma-bootstrap-host.stub.d.ts +3 -0
  156. package/dist/types/index.d.ts +35 -0
  157. package/dist/types/infra/auth/IAuthService.d.ts +12 -0
  158. package/dist/types/infra/auth/TokenAuthService.d.ts +26 -0
  159. package/dist/types/infra/cache/ICache.d.ts +12 -0
  160. package/dist/types/infra/cache/MemoryCache.d.ts +14 -0
  161. package/dist/types/infra/cache/ServerFileCache.d.ts +37 -0
  162. package/dist/types/infra/config/IConfig.d.ts +9 -0
  163. package/dist/types/infra/config/SomaConfig.d.ts +21 -0
  164. package/dist/types/infra/event-bus/IEventBus.d.ts +8 -0
  165. package/dist/types/infra/event-bus/SomaEventBus.d.ts +9 -0
  166. package/dist/types/infra/http/IHttpClient.d.ts +28 -0
  167. package/dist/types/infra/http/SomaHttpService.d.ts +27 -0
  168. package/dist/types/infra/i18n/DefaultI18n.d.ts +7 -0
  169. package/dist/types/infra/i18n/II18n.d.ts +6 -0
  170. package/dist/types/infra/logger/ConsoleLogger.d.ts +14 -0
  171. package/dist/types/infra/logger/ILogger.d.ts +17 -0
  172. package/dist/types/infra/notify/DefaultNotifyService.d.ts +16 -0
  173. package/dist/types/infra/notify/DomNotifyService.d.ts +16 -0
  174. package/dist/types/infra/notify/INotifyService.d.ts +12 -0
  175. package/dist/types/infra/notify/INotifyTheme.d.ts +12 -0
  176. package/dist/types/infra/permission/IPermission.d.ts +10 -0
  177. package/dist/types/infra/permission/SomaPermission.d.ts +19 -0
  178. package/dist/types/infra/realtime/IRealtimeService.d.ts +13 -0
  179. package/dist/types/infra/realtime/IRealtimeTransport.d.ts +8 -0
  180. package/dist/types/infra/realtime/SomaRealtimeService.d.ts +20 -0
  181. package/dist/types/infra/realtime/SseRealtimeTransport.d.ts +11 -0
  182. package/dist/types/infra/realtime/WebSocketRealtimeTransport.d.ts +30 -0
  183. package/dist/types/infra/router/IRouter.d.ts +16 -0
  184. package/dist/types/infra/router/SomaRouter.d.ts +18 -0
  185. package/dist/types/infra/router/router-path.d.ts +6 -0
  186. package/dist/types/infra/storage/IStorageService.d.ts +7 -0
  187. package/dist/types/infra/storage/MemoryStorageService.d.ts +15 -0
  188. package/dist/types/infra/storage/SessionStorageService.d.ts +14 -0
  189. package/dist/types/infra/timer/ITimer.d.ts +13 -0
  190. package/dist/types/infra/timer/SomaTimer.d.ts +21 -0
  191. package/dist/types/manifest/manifest-contract.d.ts +4 -0
  192. package/dist/types/middleware/SomaMiddlewareChain.d.ts +6 -0
  193. package/dist/types/middleware/createPermissionMiddleware.d.ts +14 -0
  194. package/dist/types/parser/annotation-extractor.d.ts +12 -0
  195. package/dist/types/parser/pipeline-parser.d.ts +62 -0
  196. package/dist/types/parser/style-config-parser.d.ts +20 -0
  197. package/dist/types/parser/timer-parser.d.ts +6 -0
  198. package/dist/types/parser/trigger-parser.d.ts +13 -0
  199. package/dist/types/parser/ui-config-parser.d.ts +34 -0
  200. package/dist/types/parser/yaml-loader.d.ts +21 -0
  201. package/dist/types/quality/SomaQualityRegistry.d.ts +31 -0
  202. package/dist/types/runtime/SomaRuntimeServices.d.ts +35 -0
  203. package/dist/types/runtime/SomaSnapshotStore.d.ts +13 -0
  204. package/dist/types/scheduler/SomaScheduler.d.ts +25 -0
  205. package/dist/types/types/domain.types.d.ts +82 -0
  206. package/dist/types/types/engine.types.d.ts +22 -0
  207. package/dist/types/types/event.types.d.ts +61 -0
  208. package/dist/types/types/keyboard.types.d.ts +4 -0
  209. package/dist/types/types/manifest.types.d.ts +29 -0
  210. package/dist/types/types/scheduler.types.d.ts +8 -0
  211. package/dist/types/types/style-config.types.d.ts +62 -0
  212. package/dist/types/types/timer.types.d.ts +19 -0
  213. package/dist/types/types/trigger.types.d.ts +12 -0
  214. package/dist/types/types/ui-config.types.d.ts +85 -0
  215. package/dist/types/types/watcher.types.d.ts +24 -0
  216. package/dist/types/validation/contract-validator.d.ts +40 -0
  217. package/editor/vscode/extension.js +39 -0
  218. package/editor/vscode/language-configuration.json +17 -0
  219. package/editor/vscode/package.json +98 -0
  220. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +30 -0
  221. package/editor/vscode/syntaxes/soma-ui-yaml.tmLanguage.json +42 -0
  222. package/global.d.ts +19 -0
  223. package/next.config.js +9 -4
  224. package/package.json +53 -16
  225. package/tailwind.config.ts +26 -7
  226. package/tsconfig.json +6 -2
  227. package/core/router/RouteHandler.ts +0 -93
@@ -0,0 +1,423 @@
1
+ // core/engine/style-binder.ts
2
+ // 职责:将 _style.yaml DSL 解析结果转化为属性变更事件的注册与触发执行
3
+ // 链路:StyleConfig → 识别属性事件 → 注册事件索引 → 属性变化时触发类名切换
4
+
5
+ import type { IContainerAdapter } from '../container/IContainerAdapter';
6
+ import type {
7
+ StyleConfig,
8
+ StyleStateRule,
9
+ StyleTransition,
10
+ ThemeConfig,
11
+ } from '../types/style-config.types';
12
+ import { applyThemeVariables } from './theme-variables';
13
+
14
+ // ─── 内部类型 ─────────────────────────────────────────────────────────────────
15
+
16
+ interface StateBinding {
17
+ somaId: string;
18
+ element: HTMLElement;
19
+ rules: StyleStateRule[];
20
+ /** 当前被 states 机制附加的类名(用于正交替换时一次性移除) */
21
+ currentClasses: Set<string>;
22
+ }
23
+
24
+ interface ActiveStateBinding {
25
+ somaId: string;
26
+ element: HTMLElement;
27
+ value: string | number | boolean | null;
28
+ applyClasses: string[];
29
+ active: boolean;
30
+ }
31
+
32
+ interface LeaveAnimation {
33
+ element: HTMLElement;
34
+ classes: string[];
35
+ }
36
+
37
+ // ─── 公共接口 ─────────────────────────────────────────────────────────────────
38
+
39
+ export interface SomaStyleBinderOptions {
40
+ container: HTMLElement;
41
+ adapter: IContainerAdapter;
42
+ styleConfig: StyleConfig;
43
+ }
44
+
45
+ /**
46
+ * SOMA 样式事件绑定器
47
+ *
48
+ * 将 _style.yaml 的声明式配置转化为运行时的事件注册与执行:
49
+ * - base:初始化时一次性附加固定类名
50
+ * - states:监听属性路径变化,正交切换匹配类名
51
+ * - transitions:绑定 DOM 原生事件(hover/focus/active)或状态驱动动画
52
+ * - themes:主题切换时覆盖 base 和 states 类名
53
+ */
54
+ export class SomaStyleBinder {
55
+ private readonly container: HTMLElement;
56
+ private readonly adapter: IContainerAdapter;
57
+ private readonly styleConfig: StyleConfig;
58
+ private readonly abortController = new AbortController();
59
+
60
+ /** 属性路径 → StateBinding[]:states 块的事件索引 */
61
+ private stateIndex = new Map<string, StateBinding[]>();
62
+
63
+ /** 属性路径 → ActiveStateBinding[]:transitions active_state 的事件索引 */
64
+ private activeStateIndex = new Map<string, ActiveStateBinding[]>();
65
+
66
+ /** somaId → base 类名数组:用于主题切换时回退 */
67
+ private baseClasses = new Map<string, string[]>();
68
+
69
+ /** somaId → 当前主题附加的 base 类名:主题切换时清理 */
70
+ private themeBaseClasses = new Map<string, string[]>();
71
+
72
+ /** somaId → leave 动画配置 */
73
+ private leaveAnimations = new Map<string, LeaveAnimation>();
74
+
75
+ /** 当前激活的主题名称 */
76
+ private currentTheme: string | null = null;
77
+
78
+ constructor(options: SomaStyleBinderOptions) {
79
+ this.container = options.container;
80
+ this.adapter = options.adapter;
81
+ this.styleConfig = options.styleConfig;
82
+ }
83
+
84
+ /**
85
+ * 初始化:解析 StyleConfig,注册所有事件
86
+ *
87
+ * 1. 应用 base 类名
88
+ * 2. 建立 states 属性事件索引
89
+ * 3. 建立 transitions active_state 索引
90
+ * 4. 绑定 transitions DOM 事件
91
+ */
92
+ init(): void {
93
+ this.applyBase();
94
+ this.buildStateIndex();
95
+ this.buildActiveStateIndex();
96
+ this.bindTransitionEvents();
97
+ }
98
+
99
+ /**
100
+ * 属性变更事件入口 — 由 ReactiveProxy.onChange 驱动
101
+ *
102
+ * 查询 stateIndex 和 activeStateIndex,对匹配的绑定执行类名切换
103
+ */
104
+ onPropertyChange(path: string, newValue: any): void {
105
+ // 1. states 正交替换
106
+ const stateBindings = this.stateIndex.get(path);
107
+ if (stateBindings) {
108
+ for (const binding of stateBindings) {
109
+ // 移除当前所有 state 附加的类名
110
+ if (binding.currentClasses.size > 0) {
111
+ this.adapter.toggleClasses(
112
+ binding.element,
113
+ [...binding.currentClasses],
114
+ false,
115
+ );
116
+ binding.currentClasses.clear();
117
+ }
118
+
119
+ // 找到匹配的规则,应用其类名
120
+ for (const rule of binding.rules) {
121
+ if (rule.value === newValue) {
122
+ const classes = rule.apply.split(' ').filter(Boolean);
123
+ if (classes.length > 0) {
124
+ this.adapter.toggleClasses(binding.element, classes, true);
125
+ for (const c of classes) {
126
+ binding.currentClasses.add(c);
127
+ }
128
+ }
129
+ }
130
+ }
131
+ }
132
+ }
133
+
134
+ // 2. active_state 切换
135
+ const activeBindings = this.activeStateIndex.get(path);
136
+ if (activeBindings) {
137
+ for (const binding of activeBindings) {
138
+ const shouldApply = binding.value === newValue;
139
+ if (shouldApply !== binding.active) {
140
+ this.adapter.toggleClasses(binding.element, binding.applyClasses, shouldApply);
141
+ binding.active = shouldApply;
142
+ }
143
+ }
144
+ }
145
+ }
146
+
147
+ /**
148
+ * 切换主题
149
+ *
150
+ * @param themeName - 目标主题名,null 表示回退到默认
151
+ */
152
+ /** 返回 _style.yaml themes 块中声明的主题名 */
153
+ getAvailableThemes(): string[] {
154
+ return Object.keys(this.styleConfig.themes ?? {});
155
+ }
156
+
157
+ switchTheme(themeName: string | null): void {
158
+ // 清除当前主题覆盖的 base 类名
159
+ for (const [somaId, classes] of this.themeBaseClasses) {
160
+ const el = this.adapter.findAnchor(this.container, somaId);
161
+ if (el && classes.length > 0) {
162
+ this.adapter.toggleClasses(el, classes, false);
163
+ }
164
+ }
165
+ this.themeBaseClasses.clear();
166
+
167
+ this.currentTheme = themeName;
168
+
169
+ if (!themeName) {
170
+ // 回退默认 base
171
+ this.applyBase();
172
+ applyThemeVariables(null, undefined);
173
+ return;
174
+ }
175
+
176
+ const theme = this.styleConfig.themes[themeName];
177
+ if (!theme) return;
178
+
179
+ applyThemeVariables(themeName, theme);
180
+
181
+ // 应用主题 base 覆盖
182
+ for (const [somaId, classes] of Object.entries(theme.base)) {
183
+ const el = this.adapter.findAnchor(this.container, somaId);
184
+ if (!el) continue;
185
+
186
+ // 移除默认 base 中该 somaId 的类名
187
+ const defaultClasses = this.baseClasses.get(somaId);
188
+ if (defaultClasses && defaultClasses.length > 0) {
189
+ this.adapter.toggleClasses(el, defaultClasses, false);
190
+ }
191
+
192
+ // 应用主题类名
193
+ const themeClassList = classes.split(' ').filter(Boolean);
194
+ if (themeClassList.length > 0) {
195
+ this.adapter.toggleClasses(el, themeClassList, true);
196
+ this.themeBaseClasses.set(somaId, themeClassList);
197
+ }
198
+ }
199
+
200
+ // 重建 stateIndex 以合并主题 states 覆盖
201
+ this.rebuildStateIndexWithTheme(theme);
202
+ }
203
+
204
+ /**
205
+ * 获取 leave 动画配置(供外部在隐藏节点前调用)
206
+ */
207
+ getLeaveAnimation(somaId: string): LeaveAnimation | undefined {
208
+ return this.leaveAnimations.get(somaId);
209
+ }
210
+
211
+ /**
212
+ * 销毁:移除所有事件监听,清空索引
213
+ */
214
+ destroy(): void {
215
+ this.abortController.abort();
216
+ this.stateIndex.clear();
217
+ this.activeStateIndex.clear();
218
+ this.baseClasses.clear();
219
+ this.themeBaseClasses.clear();
220
+ this.leaveAnimations.clear();
221
+ }
222
+
223
+ // ─── 内部方法 ──────────────────────────────────────────────────────────────
224
+
225
+ /** 应用 base 类名到 DOM */
226
+ private applyBase(): void {
227
+ for (const [somaId, classes] of Object.entries(this.styleConfig.base)) {
228
+ const el = this.adapter.findAnchor(this.container, somaId);
229
+ if (!el) continue;
230
+
231
+ const classList = classes.split(' ').filter(Boolean);
232
+ if (classList.length > 0) {
233
+ this.adapter.toggleClasses(el, classList, true);
234
+ this.baseClasses.set(somaId, classList);
235
+ }
236
+ }
237
+ }
238
+
239
+ /** 从 states 配置构建属性事件索引 */
240
+ private buildStateIndex(): void {
241
+ this.stateIndex.clear();
242
+
243
+ for (const [somaId, rules] of Object.entries(this.styleConfig.states)) {
244
+ const el = this.adapter.findAnchor(this.container, somaId);
245
+ if (!el) continue;
246
+
247
+ // 按 condition 路径分组(同一路径的规则归为一个 StateBinding)
248
+ const byCondition = new Map<string, StyleStateRule[]>();
249
+ for (const rule of rules) {
250
+ const existing = byCondition.get(rule.condition);
251
+ if (existing) {
252
+ existing.push(rule);
253
+ } else {
254
+ byCondition.set(rule.condition, [rule]);
255
+ }
256
+ }
257
+
258
+ for (const [condition, condRules] of byCondition) {
259
+ const binding: StateBinding = {
260
+ somaId,
261
+ element: el,
262
+ rules: condRules,
263
+ currentClasses: new Set(),
264
+ };
265
+
266
+ const existing = this.stateIndex.get(condition);
267
+ if (existing) {
268
+ existing.push(binding);
269
+ } else {
270
+ this.stateIndex.set(condition, [binding]);
271
+ }
272
+ }
273
+ }
274
+ }
275
+
276
+ /** 从 transitions active_state 配置构建属性事件索引 */
277
+ private buildActiveStateIndex(): void {
278
+ this.activeStateIndex.clear();
279
+
280
+ for (const [somaId, transition] of Object.entries(this.styleConfig.transitions)) {
281
+ if (transition.trigger !== 'active_state') continue;
282
+ if (!transition.condition) continue;
283
+
284
+ const el = this.adapter.findAnchor(this.container, somaId);
285
+ if (!el) continue;
286
+
287
+ const binding: ActiveStateBinding = {
288
+ somaId,
289
+ element: el,
290
+ value: transition.value ?? null,
291
+ applyClasses: transition.apply.split(' ').filter(Boolean),
292
+ active: false,
293
+ };
294
+
295
+ const existing = this.activeStateIndex.get(transition.condition);
296
+ if (existing) {
297
+ existing.push(binding);
298
+ } else {
299
+ this.activeStateIndex.set(transition.condition, [binding]);
300
+ }
301
+ }
302
+ }
303
+
304
+ /** 绑定 transitions 中非 active_state 的 DOM 事件 */
305
+ private bindTransitionEvents(): void {
306
+ const signal = this.abortController.signal;
307
+
308
+ for (const [somaId, transition] of Object.entries(this.styleConfig.transitions)) {
309
+ if (transition.trigger === 'active_state') continue;
310
+
311
+ const el = this.adapter.findAnchor(this.container, somaId);
312
+ if (!el) continue;
313
+
314
+ const classes = transition.apply.split(' ').filter(Boolean);
315
+ if (classes.length === 0) continue;
316
+
317
+ switch (transition.trigger) {
318
+ case 'hover':
319
+ this.adapter.listen(
320
+ el,
321
+ 'mouseenter',
322
+ () => this.adapter.toggleClasses(el, classes, true),
323
+ signal,
324
+ );
325
+ this.adapter.listen(
326
+ el,
327
+ 'mouseleave',
328
+ () => this.adapter.toggleClasses(el, classes, false),
329
+ signal,
330
+ );
331
+ break;
332
+
333
+ case 'focus':
334
+ this.adapter.listen(
335
+ el,
336
+ 'focusin',
337
+ () => this.adapter.toggleClasses(el, classes, true),
338
+ signal,
339
+ );
340
+ this.adapter.listen(
341
+ el,
342
+ 'focusout',
343
+ () => this.adapter.toggleClasses(el, classes, false),
344
+ signal,
345
+ );
346
+ break;
347
+
348
+ case 'active':
349
+ this.adapter.listen(
350
+ el,
351
+ 'mousedown',
352
+ () => this.adapter.toggleClasses(el, classes, true),
353
+ signal,
354
+ );
355
+ this.adapter.listen(
356
+ el,
357
+ 'mouseup',
358
+ () => this.adapter.toggleClasses(el, classes, false),
359
+ signal,
360
+ );
361
+ break;
362
+
363
+ case 'enter':
364
+ // 入场动画:初始化时直接附加类名
365
+ this.adapter.toggleClasses(el, classes, true);
366
+ break;
367
+
368
+ case 'leave':
369
+ // 离场动画:记录配置,由外部在隐藏节点前调用
370
+ this.leaveAnimations.set(somaId, { element: el, classes });
371
+ break;
372
+ }
373
+ }
374
+ }
375
+
376
+ /** 合并主题 states 覆盖后重建索引 */
377
+ private rebuildStateIndexWithTheme(theme: ThemeConfig): void {
378
+ // 合并主题 states 到当前 states
379
+ if (Object.keys(theme.states).length === 0) return;
380
+
381
+ for (const [somaId, rules] of Object.entries(theme.states)) {
382
+ const el = this.adapter.findAnchor(this.container, somaId);
383
+ if (!el) continue;
384
+
385
+ // 清除该 somaId 在 stateIndex 中的旧绑定
386
+ for (const [condition, bindings] of this.stateIndex) {
387
+ const filtered = bindings.filter((b) => b.somaId !== somaId);
388
+ if (filtered.length === 0) {
389
+ this.stateIndex.delete(condition);
390
+ } else {
391
+ this.stateIndex.set(condition, filtered);
392
+ }
393
+ }
394
+
395
+ // 用主题规则重新注册
396
+ const byCondition = new Map<string, StyleStateRule[]>();
397
+ for (const rule of rules) {
398
+ const existing = byCondition.get(rule.condition);
399
+ if (existing) {
400
+ existing.push(rule);
401
+ } else {
402
+ byCondition.set(rule.condition, [rule]);
403
+ }
404
+ }
405
+
406
+ for (const [condition, condRules] of byCondition) {
407
+ const binding: StateBinding = {
408
+ somaId,
409
+ element: el,
410
+ rules: condRules,
411
+ currentClasses: new Set(),
412
+ };
413
+
414
+ const existing = this.stateIndex.get(condition);
415
+ if (existing) {
416
+ existing.push(binding);
417
+ } else {
418
+ this.stateIndex.set(condition, [binding]);
419
+ }
420
+ }
421
+ }
422
+ }
423
+ }
@@ -0,0 +1,37 @@
1
+ import type { ThemeConfig } from '../types/style-config.types';
2
+
3
+ const BUILTIN_THEME_VARS: Record<string, Record<string, string>> = {
4
+ dark: {
5
+ '--soma-bg': '#111827',
6
+ '--soma-fg': '#f9fafb',
7
+ '--soma-primary': '#60a5fa',
8
+ '--soma-border': '#374151',
9
+ },
10
+ light: {
11
+ '--soma-bg': '#ffffff',
12
+ '--soma-fg': '#111827',
13
+ '--soma-primary': '#3b82f6',
14
+ '--soma-border': '#e5e7eb',
15
+ },
16
+ };
17
+
18
+ /** _style.yaml 主题切换时注入 CSS 变量(样式类仍由 Tailwind 提供) */
19
+ export function applyThemeVariables(
20
+ themeName: string | null,
21
+ themeConfig?: ThemeConfig,
22
+ root?: HTMLElement,
23
+ ): void {
24
+ const el = root ?? (typeof document !== 'undefined' ? document.documentElement : null);
25
+ if (!el) return;
26
+
27
+ const vars = !themeName
28
+ ? BUILTIN_THEME_VARS.light
29
+ : {
30
+ ...(BUILTIN_THEME_VARS[themeName] ?? {}),
31
+ ...(themeConfig?.vars ?? {}),
32
+ };
33
+
34
+ for (const [key, value] of Object.entries(vars)) {
35
+ el.style.setProperty(key, value);
36
+ }
37
+ }
@@ -0,0 +1,92 @@
1
+ // core/engine/timer-engine.ts
2
+ // 定时器事件 — 对应 _event.yaml timers 块,委托 EventEngine 执行 Pipeline
3
+
4
+ import type { ITimer } from '../infra/timer/ITimer';
5
+ import type { IContainerAdapter } from '../container/IContainerAdapter';
6
+ import type { SomaCompiledTimer, ParsedTimer } from '../types/timer.types';
7
+ import type { SomaCompiledEvent } from '../types/manifest.types';
8
+ import { compileTimer } from '../parser/timer-parser';
9
+ import type { SomaEventEngine } from './event-engine';
10
+
11
+ export interface SomaTimerEngineOptions {
12
+ container: HTMLElement;
13
+ adapter: IContainerAdapter;
14
+ timer: ITimer;
15
+ eventEngine: SomaEventEngine;
16
+ timers: ParsedTimer[];
17
+ }
18
+
19
+ type TimerHandle = { id: string; kind: 'timeout' | 'interval' };
20
+
21
+ export class SomaTimerEngine {
22
+ private readonly handles: TimerHandle[] = [];
23
+ private anchorEl: HTMLElement | null = null;
24
+
25
+ /** 在 onMount 之后调用,开始注册定时器 */
26
+ start(options: SomaTimerEngineOptions): void {
27
+ this.stop(options.timer);
28
+ if (options.timers.length === 0) {
29
+ return;
30
+ }
31
+
32
+ this.anchorEl =
33
+ options.container.querySelector<HTMLElement>('[data-soma-host]') ??
34
+ options.container;
35
+
36
+ for (const timer of options.timers) {
37
+ const compiled = compileTimer(timer);
38
+ const run = () => {
39
+ void options.eventEngine.execute(
40
+ this.toCompiledEvent(compiled),
41
+ this.anchorEl!,
42
+ new Event('tick'),
43
+ );
44
+ };
45
+
46
+ const register = () => {
47
+ if (timer.once) {
48
+ this.handles.push({
49
+ id: options.timer.setTimeout(run, timer.intervalMs),
50
+ kind: 'timeout',
51
+ });
52
+ } else {
53
+ this.handles.push({
54
+ id: options.timer.setInterval(run, timer.intervalMs),
55
+ kind: 'interval',
56
+ });
57
+ }
58
+ };
59
+
60
+ if (timer.delayMs > 0) {
61
+ this.handles.push({
62
+ id: options.timer.setTimeout(register, timer.delayMs),
63
+ kind: 'timeout',
64
+ });
65
+ } else {
66
+ register();
67
+ }
68
+ }
69
+ }
70
+
71
+ stop(timer: ITimer): void {
72
+ for (const handle of this.handles) {
73
+ if (handle.kind === 'interval') {
74
+ timer.clearInterval(handle.id);
75
+ } else {
76
+ timer.clearTimeout(handle.id);
77
+ }
78
+ }
79
+ this.handles.length = 0;
80
+ this.anchorEl = null;
81
+ }
82
+
83
+ private toCompiledEvent(timer: SomaCompiledTimer): SomaCompiledEvent {
84
+ return {
85
+ ...timer.pipeline,
86
+ somaId: timer.somaId,
87
+ domEvent: timer.domEvent,
88
+ eventKey: timer.eventKey,
89
+ priority: 'visible',
90
+ };
91
+ }
92
+ }
@@ -0,0 +1,55 @@
1
+ // core/engine/trigger-engine.ts
2
+ // 属性变化触发 DOM 事件 — 对应 _event.yaml triggers 块
3
+
4
+ import type { IContainerAdapter } from '../container/IContainerAdapter';
5
+ import type { PropertyTrigger } from '../types/trigger.types';
6
+ import type { SomaScopeResolver } from './scope-resolver';
7
+ import type { SomaPropertyWatcher } from './property-watcher';
8
+
9
+ export interface SomaTriggerEngineOptions {
10
+ propertyWatcher: SomaPropertyWatcher;
11
+ resolver: SomaScopeResolver;
12
+ container: HTMLElement;
13
+ adapter: IContainerAdapter;
14
+ triggers: PropertyTrigger[];
15
+ }
16
+
17
+ export class SomaTriggerEngine {
18
+ private readonly unwatchers: Array<() => void> = [];
19
+
20
+ init(options: SomaTriggerEngineOptions): void {
21
+ for (const trigger of options.triggers) {
22
+ const resolvedPath = options.resolver.resolvePath(trigger.condition);
23
+
24
+ const unwatch = options.propertyWatcher.watch(
25
+ resolvedPath,
26
+ (newValue) => {
27
+ if (newValue !== trigger.value) {
28
+ return;
29
+ }
30
+
31
+ const targetEl = options.adapter.findAnchor(options.container, trigger.target);
32
+ if (!targetEl) {
33
+ return;
34
+ }
35
+
36
+ const eventInit: CustomEventInit = {
37
+ bubbles: true,
38
+ detail: trigger.detail,
39
+ };
40
+ targetEl.dispatchEvent(new CustomEvent(trigger.event, eventInit));
41
+ },
42
+ { prefix: false },
43
+ );
44
+
45
+ this.unwatchers.push(unwatch);
46
+ }
47
+ }
48
+
49
+ destroy(): void {
50
+ for (const unwatch of this.unwatchers) {
51
+ unwatch();
52
+ }
53
+ this.unwatchers.length = 0;
54
+ }
55
+ }
@@ -0,0 +1,28 @@
1
+ import type { SomaBootstrapOptions } from '../engine/soma-bootstrap';
2
+ import type { SomaRuntimeServices } from '../runtime/SomaRuntimeServices';
3
+
4
+ /** Optional host module `@soma-host/bootstrap/soma-bootstrap.ts` */
5
+ export type SomaBootstrapConfigurator = (ctx: {
6
+ getServices: () => SomaRuntimeServices | undefined;
7
+ }) => Partial<SomaBootstrapOptions>;
8
+
9
+ /**
10
+ * Load host-provided `configureSomaBootstrap` when present.
11
+ * Keeps soma-core shell free of business middleware; host opts in explicitly.
12
+ */
13
+ export async function resolveHostBootstrapOptions(
14
+ getServices: () => SomaRuntimeServices | undefined,
15
+ ): Promise<Partial<SomaBootstrapOptions>> {
16
+ try {
17
+ const mod = await import(
18
+ /* webpackInclude: /\/bootstrap\/soma-bootstrap\.ts$/ */
19
+ '@soma-host/bootstrap/soma-bootstrap'
20
+ ) as { configureSomaBootstrap?: SomaBootstrapConfigurator };
21
+ if (typeof mod.configureSomaBootstrap === 'function') {
22
+ return mod.configureSomaBootstrap({ getServices }) ?? {};
23
+ }
24
+ } catch {
25
+ /* optional host hook */
26
+ }
27
+ return {};
28
+ }
@@ -0,0 +1,4 @@
1
+ /** Typecheck stub for `resolve-host-bootstrap` dynamic import — runtime resolves real host file. */
2
+ import type { SomaBootstrapConfigurator } from './resolve-host-bootstrap';
3
+
4
+ export const configureSomaBootstrap: SomaBootstrapConfigurator | undefined = undefined;
package/core/index.ts CHANGED
@@ -1,9 +1,40 @@
1
1
  /**
2
- * soma-core — 业务应用通过 bundler 解析子路径时请使用 exports。
2
+ * soma-core — host apps should import subpaths via package exports.
3
3
  */
4
4
  export * from './base/index';
5
5
 
6
6
  export type { SomaAppConfig, SomaPageRoot } from './types/domain.types';
7
7
 
8
- // 后端服务接口
8
+ // Host API service contract
9
9
  export type { IService } from './api/IService';
10
+
11
+ // Bootstrap & runtime
12
+ export { SomaBootstrap } from './engine/soma-bootstrap';
13
+ export type { SomaPageAssets, SomaBootstrapOptions } from './engine/soma-bootstrap';
14
+ export { createDefaultRuntimeServices } from './runtime/SomaRuntimeServices';
15
+ export type { SomaRuntimeServices } from './runtime/SomaRuntimeServices';
16
+ export { SomaDiagnostics } from './diagnostics/SomaDiagnostics';
17
+ export type { SomaDiagnosticsSnapshot } from './diagnostics/SomaDiagnostics';
18
+ export { matchGlobPath } from './engine/property-watcher';
19
+ export { extractConfigFromDomainClass } from './parser/annotation-extractor';
20
+ export { somaRollupPlugin } from './compiler/rollup-plugin-soma';
21
+ export { Page, Show, ShowList, Event } from './base/decorators';
22
+ export { runAsync } from './base/runAsync';
23
+ export type { AsyncStateHost } from './base/runAsync';
24
+ export { SomaQualityRegistry } from './quality/SomaQualityRegistry';
25
+ export { SomaTelemetry } from './diagnostics/SomaTelemetry';
26
+ export { setActiveTelemetry, recordMetric } from './diagnostics/telemetry-context';
27
+ export type { IContainerAdapter, SomaAnchorStats } from './container/IContainerAdapter';
28
+ export { SomaContainerAdapter } from './container/SomaContainerAdapter';
29
+ export { ReactNativeContainerAdapter } from './container/ReactNativeContainerAdapter';
30
+ export { MiniProgramContainerAdapter } from './container/MiniProgramContainerAdapter';
31
+ export type { IRealtimeTransport, RealtimeMessageHandler } from './infra/realtime/IRealtimeTransport';
32
+ export type { IRealtimeService, RealtimeSubscribeOptions } from './infra/realtime/IRealtimeService';
33
+ export { SseRealtimeTransport } from './infra/realtime/SseRealtimeTransport';
34
+ export { WebSocketRealtimeTransport } from './infra/realtime/WebSocketRealtimeTransport';
35
+ export { SomaRealtimeService } from './infra/realtime/SomaRealtimeService';
36
+ export { SomaMiddlewareChain } from './middleware/SomaMiddlewareChain';
37
+ export { createPermissionMiddleware } from './middleware/createPermissionMiddleware';
38
+ export type { SomaEventMiddlewareContext } from './engine/event-engine';
39
+ export type { SomaBootstrapConfigurator } from './host/resolve-host-bootstrap';
40
+ export { resolveHostBootstrapOptions } from './host/resolve-host-bootstrap';