soma-core 2.2.4 → 2.2.5

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.
@@ -128,9 +128,168 @@ async function loadPrecompiledFromManifest(
128
128
  }
129
129
  }
130
130
 
131
+ // 9 维主题与视觉方案跨页实时同步(支持工作台用户操作记忆与多窗口协同)
132
+ function applyVisualSchemeToHost(hostEl: HTMLElement, state: Record<string, unknown>): void {
133
+ const palette = (state.palette || state.colorEnum || "CLASSIC_TECH_BLUE") as string;
134
+ const colorDepth = (state.colorDepth || state.depth || "NORMAL") as string;
135
+ const colorSaturation = (state.colorSaturation || state.sat || "STANDARD_SATURATION") as string;
136
+ const fontFamily = (state.fontFamily || state.family || "SYSTEM_SANS") as string;
137
+ const fontScale = (state.fontScale || state.scale || state.typoEnum || "MEDIUM_STANDARD") as string;
138
+ const boundary = (state.boundary || state.boundaryScheme || "HAIRLINE_BORDER") as string;
139
+ const shape = (state.shape || state.componentShape || state.styleEnum || "STANDARD_ROUNDED") as string;
140
+ const surfaceTexture = (state.surfaceTexture || state.texture || "SOLID_MATTE") as string;
141
+ const density = (state.density || state.spatialDensity || "STANDARD_BALANCED") as string;
142
+
143
+ const normDepth = (colorDepth === "LIGHTENED" || colorDepth === "SLIGHTLY_LIGHT" || colorDepth === "VERY_LIGHT" || colorDepth === "soft")
144
+ ? "LIGHTENED"
145
+ : (colorDepth === "DARKENED" || colorDepth === "SLIGHTLY_DEEP" || colorDepth === "VERY_DEEP" || colorDepth === "dimmed" || colorDepth === "dark")
146
+ ? "DARKENED"
147
+ : "NORMAL";
148
+ hostEl.setAttribute("data-kit-palette", palette);
149
+ hostEl.setAttribute("data-kit-depth", normDepth);
150
+ hostEl.setAttribute("data-kit-boundary", boundary);
151
+ hostEl.setAttribute("data-kit-shape", shape);
152
+ hostEl.setAttribute("data-kit-texture", surfaceTexture);
153
+ hostEl.setAttribute("data-kit-density", density);
154
+ document.documentElement.setAttribute("data-kit-palette", palette);
155
+ document.documentElement.setAttribute("data-kit-depth", normDepth);
156
+ document.documentElement.setAttribute("data-kit-boundary", boundary);
157
+ document.documentElement.setAttribute("data-kit-shape", shape);
158
+ document.documentElement.setAttribute("data-kit-texture", surfaceTexture);
159
+ document.documentElement.setAttribute("data-kit-density", density);
160
+
161
+ const s = document.documentElement.style;
162
+ s.setProperty("--soma-theme-palette", palette);
163
+ s.setProperty("--soma-theme-color-depth", colorDepth);
164
+ s.setProperty("--soma-theme-color-saturation", colorSaturation);
165
+ s.setProperty("--soma-theme-font-family", fontFamily);
166
+ s.setProperty("--soma-theme-font-scale", fontScale);
167
+ s.setProperty("--soma-theme-boundary", boundary);
168
+ s.setProperty("--soma-theme-shape", shape);
169
+ s.setProperty("--soma-theme-surface-texture", surfaceTexture);
170
+ s.setProperty("--soma-theme-density", density);
171
+
172
+ const isBorderless = boundary === "BORDERLESS_SHADOW";
173
+ s.setProperty("--soma-border-width", isBorderless ? "0px" : "0.5px");
174
+
175
+ let radiusCard = "8px";
176
+ let radiusControl = "6px";
177
+ let radiusSm = "6px";
178
+ let radiusMd = "8px";
179
+ let radiusLg = "10px";
180
+ let radiusXl = "14px";
181
+ let radiusFull = "9999px";
182
+
183
+ if (shape === "SMALL_SHARP") {
184
+ radiusCard = "4px";
185
+ radiusControl = "2px";
186
+ radiusSm = "2px";
187
+ radiusMd = "4px";
188
+ radiusLg = "4px";
189
+ radiusXl = "6px";
190
+ } else if (shape === "PILL_CAPSULE") {
191
+ radiusCard = "16px";
192
+ radiusControl = "9999px";
193
+ radiusSm = "9999px";
194
+ radiusMd = "12px";
195
+ radiusLg = "16px";
196
+ radiusXl = "20px";
197
+ } else if (shape === "PURE_SQUARE") {
198
+ radiusCard = "0px";
199
+ radiusControl = "0px";
200
+ radiusSm = "0px";
201
+ radiusMd = "0px";
202
+ radiusLg = "0px";
203
+ radiusXl = "0px";
204
+ radiusFull = "0px";
205
+ }
206
+
207
+ s.setProperty("--soma-radius-card", radiusCard);
208
+ s.setProperty("--soma-radius-control", radiusControl);
209
+ s.setProperty("--soma-radius-sm", radiusSm);
210
+ s.setProperty("--soma-radius-md", radiusMd);
211
+ s.setProperty("--soma-radius-lg", radiusLg);
212
+ s.setProperty("--soma-radius-xl", radiusXl);
213
+ s.setProperty("--soma-radius-full", radiusFull);
214
+
215
+ let controlHeight = "32px";
216
+ let space1 = "0.25rem";
217
+ if (density === "COMPACT_DENSE" || density === "compact") {
218
+ controlHeight = "28px";
219
+ space1 = "0.22rem";
220
+ } else if (density === "SPACIOUS_AIRY" || density === "spacious") {
221
+ controlHeight = "40px";
222
+ space1 = "0.3rem";
223
+ }
224
+ s.setProperty("--soma-control-height", controlHeight);
225
+ s.setProperty("--soma-space-1", space1);
226
+
227
+ s.setProperty("--soma-shadow-step-0", isBorderless ? "0 1px 3px 0 rgba(0, 0, 0, 0.06), 0 4px 12px 0 rgba(0, 0, 0, 0.05)" : "none");
228
+ }
229
+
230
+ function setupVisualSchemeSync(containerEl: HTMLElement): () => void {
231
+ const syncFromStorage = () => {
232
+ try {
233
+ const kitHost = containerEl.querySelector('[data-soma-host="SomaLocaleOnlyPageRoot"]') as HTMLElement | null;
234
+ if (!kitHost) return;
235
+ if (typeof localStorage === "undefined") return;
236
+ const raw = localStorage.getItem("soma-visual-scheme-state-v1") || localStorage.getItem("soma-visual-scheme-state");
237
+ if (!raw) return;
238
+ const parsed = JSON.parse(raw);
239
+ if (parsed && typeof parsed === "object") {
240
+ applyVisualSchemeToHost(kitHost, parsed);
241
+ }
242
+ } catch {}
243
+ };
244
+
245
+ syncFromStorage();
246
+
247
+ const onMessage = (ev: MessageEvent) => {
248
+ try {
249
+ const kitHost = containerEl.querySelector('[data-soma-host="SomaLocaleOnlyPageRoot"]') as HTMLElement | null;
250
+ if (!kitHost) return;
251
+ if (ev.data && typeof ev.data === "object") {
252
+ applyVisualSchemeToHost(kitHost, ev.data);
253
+ }
254
+ } catch {}
255
+ };
256
+
257
+ const onStorage = (ev: StorageEvent) => {
258
+ if ((ev.key === "soma-visual-scheme-state-v1" || ev.key === "soma-visual-scheme-state") && ev.newValue) {
259
+ try {
260
+ const kitHost = containerEl.querySelector('[data-soma-host="SomaLocaleOnlyPageRoot"]') as HTMLElement | null;
261
+ if (!kitHost) return;
262
+ const parsed = JSON.parse(ev.newValue);
263
+ if (parsed && typeof parsed === "object") {
264
+ applyVisualSchemeToHost(kitHost, parsed);
265
+ }
266
+ } catch {}
267
+ }
268
+ };
269
+
270
+ let channel: BroadcastChannel | null = null;
271
+ try {
272
+ if (typeof BroadcastChannel !== "undefined") {
273
+ channel = new BroadcastChannel("soma-visual-scheme-sync");
274
+ channel.addEventListener("message", onMessage);
275
+ }
276
+ } catch {}
277
+
278
+ window.addEventListener("storage", onStorage);
279
+
280
+ return () => {
281
+ try {
282
+ channel?.close();
283
+ } catch {}
284
+ window.removeEventListener("storage", onStorage);
285
+ };
286
+ }
287
+
131
288
  class NativePageClient {
289
+
132
290
  private bootstrap: SomaBootstrapType | null = null;
133
291
  private routerBridgeUnsub: (() => void) | null = null;
292
+ private visualSchemeUnsub: (() => void) | null = null;
134
293
  private currentRunId = 0;
135
294
  private appRoot: HTMLElement;
136
295
  private hydrationShell: HTMLElement;
@@ -187,14 +346,33 @@ class NativePageClient {
187
346
 
188
347
  private setupDevHmr() {
189
348
  if (typeof window === 'undefined' || !window.__SOMA_DEV_SSE__) return;
349
+ const onReload = () => {
350
+ pageAssetCache.clear();
351
+ const cur = window.location.pathname + window.location.search;
352
+ void this.renderPage(cur);
353
+ };
354
+ const bc = typeof BroadcastChannel !== 'undefined' ? new BroadcastChannel('soma:dev-hmr') : null;
355
+ if (bc) {
356
+ bc.addEventListener('message', (ev) => {
357
+ if (ev.data === 'reload') onReload();
358
+ });
359
+ }
360
+ // 仅在顶层窗口建立单条 SSE 连接并通过 BroadcastChannel 广播给所有内嵌 iframe,避免耗尽 HTTP/1.1 同源 6 连接上限导致多标签/嵌套 iframe 死锁
361
+ let isTop = true;
362
+ try {
363
+ isTop = window.self === window.top;
364
+ } catch {
365
+ isTop = false;
366
+ }
367
+ if (!isTop && bc) return;
368
+
190
369
  let es: EventSource | null = null;
191
370
  const connectHmr = () => {
192
371
  try {
193
372
  es = new EventSource('/_soma/hmr');
194
373
  es.addEventListener('reload', () => {
195
- pageAssetCache.clear();
196
- const cur = window.location.pathname + window.location.search;
197
- void this.renderPage(cur);
374
+ bc?.postMessage('reload');
375
+ onReload();
198
376
  });
199
377
  es.onerror = () => {
200
378
  es?.close();
@@ -407,6 +585,11 @@ class NativePageClient {
407
585
  await this.bootstrap.unmount();
408
586
  this.bootstrap = null;
409
587
  }
588
+ const prevVisualSchemeUnsub = this.visualSchemeUnsub;
589
+ if (prevVisualSchemeUnsub) {
590
+ prevVisualSchemeUnsub();
591
+ this.visualSchemeUnsub = null;
592
+ }
410
593
  if (runId !== this.currentRunId) return;
411
594
 
412
595
  const getServices = () =>
@@ -431,6 +614,9 @@ class NativePageClient {
431
614
 
432
615
  this.showContainer();
433
616
 
617
+ if (this.visualSchemeUnsub) { (this.visualSchemeUnsub as () => void)(); }
618
+ this.visualSchemeUnsub = setupVisualSchemeSync(this.containerEl);
619
+
434
620
  if (typeof window !== 'undefined') {
435
621
  window.__SOMA_DIAGNOSTICS__ = {
436
622
  isSettled: () => bs.isSettled(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soma-core",
3
- "version": "2.2.4",
3
+ "version": "2.2.5",
4
4
  "description": "SOMA — Semantic Oriented Modular Architecture. A declarative frontend framework runtime with no UI framework dependency; native host shell ships in host-native/.",
5
5
  "keywords": [
6
6
  "soma",