soma-core 1.3.2 → 1.3.4
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 +43 -0
- package/app/SomaPageClient.tsx +22 -68
- package/app/[...path]/page.tsx +7 -6
- package/app/api/[...path]/route.ts +3 -3
- package/app/api/soma-registry/[...path]/route.ts +67 -0
- package/app/components/SomaHostPage.tsx +3 -8
- package/app/components/SomaPageClientBoundary.tsx +13 -0
- package/app/layout.tsx +6 -1
- package/app/page.tsx +3 -3
- package/app/soma/manifest/route.ts +1 -0
- package/app/soma/page-object/route.ts +1 -1
- package/bin/soma-lsp.cjs +1 -4
- package/core/build/compiler/full-manifest-builder.ts +19 -23
- package/core/build/compiler/html-include-expander.ts +56 -0
- package/core/build/compiler/page-assets.ts +26 -0
- package/core/build/compiler/page-declaration-compiler.ts +85 -0
- package/core/build/compiler/rollup-plugin-soma.ts +4 -13
- package/core/build/compiler/route-builder.ts +0 -2
- package/core/build/compiler/rspack-plugin-soma.js +1 -10
- package/core/build/compiler/soma-manifest-webpack-plugin.js +54 -9
- package/core/build/compiler/soma-precompile.js +3581 -2387
- package/core/build/compiler/soma-precompile.ts +44 -2
- package/core/build/compiler/soma-rspack-yaml-loader.js +5 -8
- package/core/build/compiler/soma-webpack-loader.js +6 -15
- package/core/build/compiler/vite-plugin-soma.ts +9 -21
- package/core/build/component/pack-from-dir.ts +58 -0
- package/core/build/component/package-loader.ts +281 -0
- package/core/build/eject/apply-eject.ts +133 -0
- package/core/build/eject/plan-eject.ts +133 -0
- package/core/build/lsp/cross-file-diagnostics.cjs +82 -113
- package/core/build/lsp/extra-completion.cjs +14 -8
- package/core/build/lsp/lsp-navigation.cjs +50 -43
- package/core/build/lsp/soma-declaration.cjs +85 -0
- package/core/build/lsp/ui-yaml-completion.cjs +98 -104
- package/core/build/v2/block-compile.ts +200 -0
- package/core/build/v2/block-loader.ts +33 -0
- package/core/build/v2/html-index.ts +120 -0
- package/core/build/v2/migrate-v1.ts +370 -0
- package/core/build/v2/soma-file-loader.ts +216 -0
- package/core/client/adaptive/flush-scheduler.ts +96 -0
- package/core/client/adaptive/hysteresis.ts +56 -0
- package/core/client/adaptive/index.ts +23 -0
- package/core/client/adaptive/observer-pool.ts +90 -0
- package/core/client/base/SomaPageRootBase.ts +50 -0
- package/core/client/compiler/adaptive-compile.ts +136 -0
- package/core/client/compiler/manifest-compiler.ts +155 -27
- package/core/client/component/ai-catalog.ts +277 -0
- package/core/client/component/component-definition.types.ts +65 -0
- package/core/client/component/component-expander.ts +119 -0
- package/core/client/component/component-manager.ts +933 -0
- package/core/client/component/component-pack.ts +445 -0
- package/core/client/component/component-registry.ts +71 -0
- package/core/client/component/contract-compat.ts +132 -0
- package/core/client/component/example-v2.ts +132 -0
- package/core/client/component/pack-styles.ts +94 -0
- package/core/client/component/scoped-adapter.ts +85 -0
- package/core/client/component/verify-pack.ts +247 -0
- package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
- package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
- package/core/client/container/SomaContainerAdapter.ts +186 -180
- package/core/client/data/data-object-resolver.ts +417 -0
- package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
- package/core/client/diagnostics/boot-timings.ts +152 -0
- package/core/client/engine/asset-resolver.ts +12 -2
- package/core/client/engine/dom-renderer.ts +249 -24
- package/core/client/engine/emit-event.ts +21 -0
- package/core/client/engine/event-engine.ts +34 -4
- package/core/client/engine/on-change-engine.ts +1 -1
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +11 -10
- package/core/client/engine/render-engine.ts +39 -4
- package/core/client/engine/runtime-builder.ts +23 -0
- package/core/client/engine/soma-bootstrap.ts +380 -7
- package/core/client/engine/style-binder.ts +47 -1
- package/core/client/observability/error-reporter.ts +173 -0
- package/core/client/parser/adaptive-parser.ts +264 -0
- package/core/client/parser/data-config-parser.ts +396 -0
- package/core/client/parser/pipeline-parser.ts +15 -3
- package/core/client/parser/style-config-parser.ts +65 -2
- package/core/client/parser/ui-config-parser.ts +52 -3
- package/core/client/parser/v2/namespace.ts +182 -0
- package/core/client/parser/v2/page-parser.ts +359 -0
- package/core/client/parser/v2/use-parser.ts +412 -0
- package/core/client/parser/value-lang.ts +340 -0
- package/core/client/pipes/pipe-registry.ts +249 -0
- package/core/client/quality/SomaQualityRegistry.ts +3 -0
- package/core/client/registry/declaration-renderer.ts +161 -0
- package/core/client/registry/index.ts +28 -0
- package/core/client/registry/page-lifecycle.ts +171 -0
- package/core/client/registry/remote-page.ts +139 -0
- package/core/client/runtime/SomaRuntimeServices.ts +24 -0
- package/core/client/security/attribute-presence.ts +70 -0
- package/core/client/security/html-sanitizer.ts +113 -0
- package/core/client/security/url-guard.ts +0 -0
- package/core/client/theme/harmony.ts +307 -0
- package/core/client/theme/scene-tokens.ts +134 -0
- package/core/client/theme/theme-axes.ts +228 -0
- package/core/client/theme/theme-engine.ts +1196 -0
- package/core/client/theme/tone-solver.ts +118 -0
- package/core/client/typography/typography-base.ts +34 -0
- package/core/client/validation/contract-validator.ts +48 -5
- package/core/index.ts +262 -2
- package/core/server/api/IService.ts +18 -6
- package/core/server/api/load-host-service.ts +3 -3
- package/core/server/host/content-security-policy.ts +190 -0
- package/core/server/host/csp-nonce-metadata.ts +75 -0
- package/core/server/host/resolve-host-request-hook.ts +5 -0
- package/core/server/host/server-page-assets.ts +34 -7
- package/core/server/host/soma-page-compile.ts +33 -0
- package/core/server/index.ts +45 -1
- package/core/server/infra/cache/ServerFileCache.ts +1 -1
- package/core/server/mock/node/cli.mjs +2 -2
- package/core/server/registry/api-registry-store.ts +518 -0
- package/core/server/registry/conditional-object-store.ts +29 -0
- package/core/server/registry/declaration-precompiler.ts +99 -0
- package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
- package/core/server/registry/index.ts +142 -0
- package/core/server/registry/page-registry-repository.ts +373 -0
- package/core/server/registry/page-registry-store.ts +932 -0
- package/core/server/registry/registry-error.ts +17 -0
- package/core/server/registry/registry-file-lock.ts +173 -0
- package/core/server/registry/registry-response.ts +71 -0
- package/core/server/registry/registry-subjects.ts +194 -0
- package/core/server/registry/registry-write-pipeline.ts +240 -0
- package/core/server/registry/request-body-limit.ts +99 -0
- package/core/server/registry/services/apis/Get.ts +25 -0
- package/core/server/registry/services/apis/List.ts +41 -0
- package/core/server/registry/services/apis/Register.ts +39 -0
- package/core/server/registry/services/apis/Unregister.ts +24 -0
- package/core/server/registry/services/index.ts +40 -0
- package/core/server/registry/services/pages/Get.ts +96 -0
- package/core/server/registry/services/pages/List.ts +53 -0
- package/core/server/registry/services/pages/Register.ts +49 -0
- package/core/server/registry/services/pages/Rollback.ts +36 -0
- package/core/server/registry/services/pages/Unregister.ts +23 -0
- package/core/server/registry/services/service-utils.ts +21 -0
- package/core/server/registry/validation/compile-gate.ts +54 -0
- package/core/server/registry/validation/component-existence.ts +55 -0
- package/core/server/registry/validation/credential-ban.ts +86 -0
- package/core/server/registry/validation/cross-check.ts +67 -0
- package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
- package/core/shared/types/adaptive.types.ts +109 -0
- package/core/shared/types/component-contract.types.ts +120 -0
- package/core/shared/types/data-config.types.ts +105 -0
- package/core/shared/types/domain.types.ts +6 -0
- package/core/shared/types/engine.types.ts +41 -35
- package/core/shared/types/manifest.types.ts +55 -32
- package/core/shared/types/scope-manifest.types.ts +96 -0
- package/core/shared/types/style-config.types.ts +9 -0
- package/core/shared/types/ui-config.types.ts +118 -98
- package/dist/cli/soma.js +20 -5
- package/dist/core/index.cjs +7811 -1380
- package/dist/types/build/compiler/full-manifest-builder.d.ts +6 -2
- package/dist/types/build/compiler/html-include-expander.d.ts +19 -0
- package/dist/types/build/compiler/page-assets.d.ts +3 -0
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +33 -0
- package/dist/types/build/compiler/rollup-plugin-soma.d.ts +0 -2
- package/dist/types/build/compiler/route-builder.d.ts +0 -1
- package/dist/types/build/compiler/soma-precompile.d.ts +17 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +2 -7
- package/dist/types/build/component/pack-from-dir.d.ts +17 -0
- package/dist/types/build/component/package-loader.d.ts +40 -0
- package/dist/types/build/eject/apply-eject.d.ts +22 -0
- package/dist/types/build/eject/plan-eject.d.ts +32 -0
- package/dist/types/build/v2/block-compile.d.ts +28 -0
- package/dist/types/build/v2/block-loader.d.ts +9 -0
- package/dist/types/build/v2/html-index.d.ts +37 -0
- package/dist/types/build/v2/migrate-v1.d.ts +32 -0
- package/dist/types/build/v2/soma-file-loader.d.ts +45 -0
- package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
- package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
- package/dist/types/client/adaptive/index.d.ts +5 -0
- package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
- package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
- package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
- package/dist/types/client/component/ai-catalog.d.ts +73 -0
- package/dist/types/client/component/component-definition.types.d.ts +54 -0
- package/dist/types/client/component/component-expander.d.ts +25 -0
- package/dist/types/client/component/component-manager.d.ts +160 -0
- package/dist/types/client/component/component-pack.d.ts +110 -0
- package/dist/types/client/component/component-registry.d.ts +12 -0
- package/dist/types/client/component/contract-compat.d.ts +16 -0
- package/dist/types/client/component/example-v2.d.ts +9 -0
- package/dist/types/client/component/pack-styles.d.ts +22 -0
- package/dist/types/client/component/scoped-adapter.d.ts +18 -0
- package/dist/types/client/component/verify-pack.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +71 -0
- package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
- package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
- package/dist/types/client/engine/asset-resolver.d.ts +7 -1
- package/dist/types/client/engine/dom-renderer.d.ts +51 -1
- package/dist/types/client/engine/emit-event.d.ts +6 -0
- package/dist/types/client/engine/event-engine.d.ts +6 -0
- package/dist/types/client/engine/render-engine.d.ts +25 -1
- package/dist/types/client/engine/runtime-builder.d.ts +7 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +118 -0
- package/dist/types/client/observability/error-reporter.d.ts +51 -0
- package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
- package/dist/types/client/parser/data-config-parser.d.ts +34 -0
- package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
- package/dist/types/client/parser/style-config-parser.d.ts +5 -3
- package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
- package/dist/types/client/parser/v2/namespace.d.ts +52 -0
- package/dist/types/client/parser/v2/page-parser.d.ts +21 -0
- package/dist/types/client/parser/v2/use-parser.d.ts +72 -0
- package/dist/types/client/parser/value-lang.d.ts +63 -0
- package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +50 -0
- package/dist/types/client/registry/index.d.ts +10 -0
- package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
- package/dist/types/client/registry/remote-page.d.ts +52 -0
- package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
- package/dist/types/client/security/attribute-presence.d.ts +7 -0
- package/dist/types/client/security/html-sanitizer.d.ts +37 -0
- package/dist/types/client/security/url-guard.d.ts +11 -0
- package/dist/types/client/theme/harmony.d.ts +77 -0
- package/dist/types/client/theme/scene-tokens.d.ts +25 -0
- package/dist/types/client/theme/theme-axes.d.ts +81 -0
- package/dist/types/client/theme/theme-engine.d.ts +77 -0
- package/dist/types/client/theme/tone-solver.d.ts +47 -0
- package/dist/types/client/typography/typography-base.d.ts +15 -0
- package/dist/types/client/validation/contract-validator.d.ts +2 -0
- package/dist/types/index.d.ts +47 -2
- package/dist/types/server/api/IService.d.ts +18 -6
- package/dist/types/server/host/content-security-policy.d.ts +53 -0
- package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
- package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
- package/dist/types/server/host/server-page-assets.d.ts +2 -0
- package/dist/types/server/host/soma-page-compile.d.ts +7 -0
- package/dist/types/server/index.d.ts +6 -1
- package/dist/types/server/registry/api-registry-store.d.ts +141 -0
- package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
- package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
- package/dist/types/server/registry/index.d.ts +29 -0
- package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
- package/dist/types/server/registry/page-registry-store.d.ts +259 -0
- package/dist/types/server/registry/registry-error.d.ts +12 -0
- package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
- package/dist/types/server/registry/registry-response.d.ts +18 -0
- package/dist/types/server/registry/registry-subjects.d.ts +59 -0
- package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
- package/dist/types/server/registry/request-body-limit.d.ts +31 -0
- package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
- package/dist/types/server/registry/services/apis/List.d.ts +10 -0
- package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
- package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
- package/dist/types/server/registry/services/index.d.ts +3 -0
- package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
- package/dist/types/server/registry/services/pages/List.d.ts +13 -0
- package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
- package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
- package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
- package/dist/types/server/registry/services/service-utils.d.ts +10 -0
- package/dist/types/server/registry/validation/compile-gate.d.ts +12 -0
- package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
- package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
- package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
- package/dist/types/shared/types/adaptive.types.d.ts +88 -0
- package/dist/types/shared/types/component-contract.types.d.ts +78 -0
- package/dist/types/shared/types/data-config.types.d.ts +81 -0
- package/dist/types/shared/types/domain.types.d.ts +6 -0
- package/dist/types/shared/types/engine.types.d.ts +6 -0
- package/dist/types/shared/types/manifest.types.d.ts +23 -0
- package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
- package/dist/types/shared/types/style-config.types.d.ts +8 -0
- package/dist/types/shared/types/ui-config.types.d.ts +20 -0
- package/editor/vscode/package.json +1 -24
- package/global.d.ts +5 -11
- package/next.config.js +85 -28
- package/package.json +33 -11
- package/proxy.ts +62 -0
- package/tailwind.config.ts +0 -1
- package/core/build/lsp/event-yaml-completion.cjs +0 -106
- package/core/client/engine/theme-variables.ts +0 -37
- package/dist/tsconfig.types.tsbuildinfo +0 -1
- package/dist/types/client/engine/theme-variables.d.ts +0 -3
- package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +0 -30
- package/middleware.ts +0 -33
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// `@html` 绑定的净化收口(B6 安全域)。
|
|
2
|
+
//
|
|
3
|
+
// 为什么要有这一层:`somaId@html` 是引擎里唯一一条「把字符串当 DOM 解释」的通道,
|
|
4
|
+
// 而 innerHTML 插入的 `<script>` 虽然不执行,`<img onerror=…>` 却会立刻执行——
|
|
5
|
+
// 也就是说这条通道一旦喂进不可信内容(接口富文本、AI 产物、用户输入),
|
|
6
|
+
// DSL「没有脚本能力」的守线就被从数据侧绕过了。
|
|
7
|
+
//
|
|
8
|
+
// 早期实现把探测与降级直接写死在 Web 容器适配器里:全局有 DOMPurify 就 sanitize,
|
|
9
|
+
// 没有就整串转义。行为是安全的,但有三个工程问题:
|
|
10
|
+
// 1. 宿主想接自研/服务端同款 sanitizer 时,只能去污染 globalThis;
|
|
11
|
+
// 2. 三个容器适配器(Web / 小程序 / RN)各写各的降级,语义无处对账;
|
|
12
|
+
// 3. sanitize 配置散落——富文本页面各自调参,等于没有统一策略。
|
|
13
|
+
// 故抽出本模块:**一个可注入的 sanitizer 抽象 + 一份统一 profile + 最安全的默认降级**。
|
|
14
|
+
//
|
|
15
|
+
// 优先级(高 → 低),故意让"显式注入"压过"全局探测",避免第三方脚本挂个假 DOMPurify
|
|
16
|
+
// 就能把净化策略换掉:
|
|
17
|
+
// 1. 宿主经 setSomaHtmlSanitizer() 显式注入的 sanitizer;
|
|
18
|
+
// 2. globalThis.DOMPurify(宿主打包 dompurify 后的常规姿势),按 SOMA_PURIFY_PROFILE 调用;
|
|
19
|
+
// 3. 都没有 → 整串转义成文本(fail-safe:宁可显示成源码,也不产生可执行 DOM)。
|
|
20
|
+
|
|
21
|
+
/** 宿主可注入的净化器最小面:一个纯函数级的 sanitize */
|
|
22
|
+
export interface SomaHtmlSanitizer {
|
|
23
|
+
sanitize(html: string): string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** globalThis.DOMPurify 的最小结构(第二个入参为配置对象) */
|
|
27
|
+
interface GlobalDomPurifyLike {
|
|
28
|
+
sanitize(html: string, config?: Record<string, unknown>): string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 统一 sanitize 策略:全站一份,不许各页面自配。
|
|
33
|
+
*
|
|
34
|
+
* 取舍说明:
|
|
35
|
+
* - `script` / `style` 一律剔除——声明态页面的样式走 `_style.yaml` 与主题令牌,
|
|
36
|
+
* 富文本里出现 `<style>` 只可能是越权改版面(模板纯度闸门在手写侧也是这条规矩);
|
|
37
|
+
* - `style` 属性剔除:内联样式既是 CSP `style-src` 的敌人,也是颜色纪律的漏口;
|
|
38
|
+
* - 表单/框架类标签剔除:富文本没有合法理由携带 `<form>` / `<iframe>` / `<base>`;
|
|
39
|
+
* - `ALLOW_DATA_ATTR: false`:data-soma-* 是引擎的锚点命名空间,不能被内容伪造。
|
|
40
|
+
* DOMPurify 默认已剔除 on* 事件属性与 `javascript:` URL,此处不重复声明,只做加严。
|
|
41
|
+
*/
|
|
42
|
+
export const SOMA_PURIFY_PROFILE: Record<string, unknown> = {
|
|
43
|
+
USE_PROFILES: { html: true },
|
|
44
|
+
FORBID_TAGS: ['script', 'style', 'iframe', 'object', 'embed', 'form', 'base', 'link', 'meta'],
|
|
45
|
+
FORBID_ATTR: ['style', 'srcdoc', 'formaction', 'ping'],
|
|
46
|
+
ALLOW_DATA_ATTR: false,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** HTML 文本转义表(降级路径不依赖 document,小程序 / RN 容器同样可用) */
|
|
50
|
+
const ESCAPE_MAP: Record<string, string> = {
|
|
51
|
+
'&': '&',
|
|
52
|
+
'<': '<',
|
|
53
|
+
'>': '>',
|
|
54
|
+
'"': '"',
|
|
55
|
+
"'": ''',
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
let injected: SomaHtmlSanitizer | null = null;
|
|
59
|
+
let degradeWarned = false;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 宿主注入净化器(在 bootstrap 里调用一次)。
|
|
63
|
+
* 传 null 复位为「未注入」,便于测试与运行期关停。
|
|
64
|
+
*/
|
|
65
|
+
export function setSomaHtmlSanitizer(sanitizer: SomaHtmlSanitizer | null): void {
|
|
66
|
+
injected = sanitizer;
|
|
67
|
+
degradeWarned = false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 当前生效的净化器;返回 null 表示将走转义降级 */
|
|
71
|
+
export function resolveSomaHtmlSanitizer(): SomaHtmlSanitizer | null {
|
|
72
|
+
if (injected) {
|
|
73
|
+
return injected;
|
|
74
|
+
}
|
|
75
|
+
const purify = (globalThis as { DOMPurify?: GlobalDomPurifyLike }).DOMPurify;
|
|
76
|
+
if (purify && typeof purify.sanitize === 'function') {
|
|
77
|
+
// 包一层而不是直接返回:统一 profile 必须由我们施加,不能指望调用方记得传
|
|
78
|
+
return { sanitize: (html: string) => purify.sanitize(html, SOMA_PURIFY_PROFILE) };
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 纯字符串转义,不经 DOM——降级路径与非 Web 容器共用 */
|
|
84
|
+
export function escapeSomaHtml(value: string): string {
|
|
85
|
+
return value.replace(/[&<>"']/g, (char) => ESCAPE_MAP[char]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 净化结果:`sanitized=false` 表示走了转义降级(内容以纯文本呈现) */
|
|
89
|
+
export interface SomaHtmlSanitizeResult {
|
|
90
|
+
html: string;
|
|
91
|
+
sanitized: boolean;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* `@html` 落地前的统一净化。
|
|
96
|
+
*
|
|
97
|
+
* 降级时给一次 console.warn(只警告一次,避免长列表刷屏):
|
|
98
|
+
* 静默降级会让富文本页面「显示成源码」而没人知道为什么,这是实测最难排查的一类困惑。
|
|
99
|
+
*/
|
|
100
|
+
export function sanitizeSomaHtml(value: string): SomaHtmlSanitizeResult {
|
|
101
|
+
const sanitizer = resolveSomaHtmlSanitizer();
|
|
102
|
+
if (sanitizer) {
|
|
103
|
+
return { html: sanitizer.sanitize(value), sanitized: true };
|
|
104
|
+
}
|
|
105
|
+
if (!degradeWarned) {
|
|
106
|
+
degradeWarned = true;
|
|
107
|
+
console.warn(
|
|
108
|
+
'[SomaSecurity] @html 未接净化器,已按纯文本转义降级——'
|
|
109
|
+
+ '需要富文本请在宿主 bootstrap 调用 setSomaHtmlSanitizer(DOMPurify) 或挂 globalThis.DOMPurify',
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return { html: escapeSomaHtml(value), sanitized: false };
|
|
113
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
// 色相编排与语义分离闸(REQ-007 S1 + S3)。
|
|
2
|
+
//
|
|
3
|
+
// 本模块回答两个问题:
|
|
4
|
+
// S1 八个语气 + 图表序列的**色相**各自落在哪;
|
|
5
|
+
// S3 品牌色撞进语义色的地盘时怎么办。
|
|
6
|
+
//
|
|
7
|
+
// 为什么它值得独立成模块:现状引擎把色相散写在三处字面常量里
|
|
8
|
+
// (④.1 的 danger h=27 / success h=150 / warning h=75,④.1b 的 +48/+160 偏移),
|
|
9
|
+
// 于是"半套跟着品牌转、半套钉死"这个不一致没人看得见——直到某个红色品牌
|
|
10
|
+
// 撞上 danger(cartoonPop 实测色相差 10.5°,"确认提交"与"永久删除"同色)。
|
|
11
|
+
// 色相决策集中一处之后,撞色成为一个**可判定、可测试、可自动纠正**的条件。
|
|
12
|
+
//
|
|
13
|
+
// 纯函数模块:只依赖 Math,单测不需要任何桩。
|
|
14
|
+
|
|
15
|
+
import type { SomaAxisParams, SomaResolvedAxes, SomaSemantics } from './theme-axes';
|
|
16
|
+
|
|
17
|
+
export type SomaToneName =
|
|
18
|
+
| 'primary' | 'secondary' | 'accent' | 'info' | 'success' | 'warning' | 'danger';
|
|
19
|
+
|
|
20
|
+
export interface SomaHueAnchors {
|
|
21
|
+
/** 各语气的色相锚(度,0–360) */
|
|
22
|
+
readonly tones: Readonly<Record<SomaToneName, number>>;
|
|
23
|
+
/** 图表序列 8 锚 */
|
|
24
|
+
readonly chart: readonly number[];
|
|
25
|
+
/** 色相带宽 < 120° 时为 true:序列区分改以明度阶梯为主 */
|
|
26
|
+
readonly chartLightnessLadder: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 功能色相:danger 偏红、success 偏绿、warning 偏琥珀、info 偏蓝。
|
|
31
|
+
*
|
|
32
|
+
* 这四个数从现状引擎 ④.1 的字面常量迁来,**取值一个没改**——
|
|
33
|
+
* functional 档(缺省)下产出与重构前完全一致。
|
|
34
|
+
*/
|
|
35
|
+
export const FUNCTIONAL_HUES = { info: 230, success: 150, warning: 75, danger: 27 } as const;
|
|
36
|
+
|
|
37
|
+
/** 表达性语气相对品牌的色相偏移(自现状 ④.1b 迁来,取值不改) */
|
|
38
|
+
export const EXPRESSIVE_OFFSETS = { secondary: 48, accent: 160 } as const;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 判撞阈值:色相与明度**同时**低于阈值才算撞。
|
|
42
|
+
*
|
|
43
|
+
* 只看色相不够——深红按钮配亮红徽标是可辨的;只看明度更不够。
|
|
44
|
+
* 阈值以 cartoonPop 病例(Δh 10.5°、ΔL 0.05)为下界校准。
|
|
45
|
+
* 导出是为了让棘轮测试直接引用,不在测试里抄一遍数字。
|
|
46
|
+
*/
|
|
47
|
+
export const SEPARATION_THRESHOLD = { hue: 30, lightness: 0.12 } as const;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 语义色的**色相区**:推开可以移动色相,但不能移出这个语气还能被认出来的范围。
|
|
51
|
+
*
|
|
52
|
+
* 实测教训:红色品牌(#f43f5e,h=16)下 danger(h=27)往上推撞 warning、
|
|
53
|
+
* 往下推出红区变品红——两边都不通。区间约束把这件事变成可判定的:
|
|
54
|
+
* 区内推不出安全距离时,分离**退到明度维**(见 needsLightnessSeparation),
|
|
55
|
+
* 而不是把 danger 推成一个不像危险的颜色。
|
|
56
|
+
*
|
|
57
|
+
* 这与 M3 的做法一致:错误色恒为红系,红色品牌靠 tone 阶梯与主色拉开,
|
|
58
|
+
* 不靠改色相——"危险必须看起来危险"优先于"必须和品牌不同色相"。
|
|
59
|
+
*/
|
|
60
|
+
const SEMANTIC_ZONES: Record<'success' | 'warning' | 'danger', readonly [number, number]> = {
|
|
61
|
+
danger: [12, 45],
|
|
62
|
+
warning: [58, 100],
|
|
63
|
+
success: [125, 178],
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 推开的目标余量:推到"阈值 × 1.15"而不是刚好等于阈值。
|
|
68
|
+
*
|
|
69
|
+
* 全色轮扫描实测:推到刚好等于时有两例卡在 dh = 29.9 / 30.0——
|
|
70
|
+
* 步进是 6° 的整数倍,落点未必正好压线,加上后续量化就会擦边掉回来。
|
|
71
|
+
* 与明度分离的余量同源同值:**硬保障不能停在边界上**。
|
|
72
|
+
*/
|
|
73
|
+
const PUSH_MARGIN = 1.15;
|
|
74
|
+
|
|
75
|
+
/** tinted 档的保底分离度:既入色调、又不至于认不出 */
|
|
76
|
+
const TINTED_MIN_SEPARATION = 40;
|
|
77
|
+
/** 推开步长与上限 */
|
|
78
|
+
const PUSH_STEP = 6;
|
|
79
|
+
const PUSH_MAX = 16;
|
|
80
|
+
/** 图表序列改用明度阶梯的带宽阈值 */
|
|
81
|
+
const LADDER_BANDWIDTH = 120;
|
|
82
|
+
|
|
83
|
+
/** 最短弧角差,返回 (-180, 180] */
|
|
84
|
+
export function angDiff(a: number, b: number): number {
|
|
85
|
+
let d = (a - b) % 360;
|
|
86
|
+
if (d > 180) d -= 360;
|
|
87
|
+
if (d <= -180) d += 360;
|
|
88
|
+
return d;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 归一到 [0, 360) */
|
|
92
|
+
function norm(h: number): number {
|
|
93
|
+
return ((h % 360) + 360) % 360;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 色相压缩:把目标色相朝品牌色相压缩 k 倍。
|
|
98
|
+
* k=0 原值不动(功能优先);k=1 完全归一(单色系)。走最短弧,不绕远路。
|
|
99
|
+
*/
|
|
100
|
+
export function compress(hue: number, brandHue: number, k: number): number {
|
|
101
|
+
return norm(brandHue + angDiff(hue, brandHue) * (1 - k));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* S1:解出全部色相锚。
|
|
106
|
+
*
|
|
107
|
+
* 作用域与优先序(《架构设计》§3 的表):
|
|
108
|
+
* accents 显式锁位 > semantics 决定语义三色 > harmony 决定其余 > 缺省
|
|
109
|
+
*/
|
|
110
|
+
export function resolveHueAnchors(
|
|
111
|
+
brandHue: number,
|
|
112
|
+
axes: SomaResolvedAxes,
|
|
113
|
+
params: SomaAxisParams,
|
|
114
|
+
accentHues: readonly number[] = [],
|
|
115
|
+
): SomaHueAnchors {
|
|
116
|
+
const { harmonyK, accentComplement } = params;
|
|
117
|
+
const { semantics } = axes.palette;
|
|
118
|
+
|
|
119
|
+
// 表达性语气:偏移后压缩;complementary 档 accent 先跳到品牌对侧
|
|
120
|
+
const secondarySeed = norm(brandHue + EXPRESSIVE_OFFSETS.secondary);
|
|
121
|
+
const accentSeed = accentComplement
|
|
122
|
+
? norm(brandHue + 180)
|
|
123
|
+
: norm(brandHue + EXPRESSIVE_OFFSETS.accent);
|
|
124
|
+
|
|
125
|
+
const tones: Record<SomaToneName, number> = {
|
|
126
|
+
primary: brandHue,
|
|
127
|
+
secondary: accentHues[0] ?? compress(secondarySeed, brandHue, harmonyK),
|
|
128
|
+
accent: accentHues[1] ?? (accentComplement ? accentSeed : compress(accentSeed, brandHue, harmonyK)),
|
|
129
|
+
info: accentHues[2] ?? compress(FUNCTIONAL_HUES.info, brandHue, harmonyK),
|
|
130
|
+
success: semanticHue(FUNCTIONAL_HUES.success, brandHue, harmonyK, semantics),
|
|
131
|
+
warning: semanticHue(FUNCTIONAL_HUES.warning, brandHue, harmonyK, semantics),
|
|
132
|
+
danger: semanticHue(FUNCTIONAL_HUES.danger, brandHue, harmonyK, semantics),
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const bandwidth = hueBandwidth(Object.values(tones));
|
|
136
|
+
return {
|
|
137
|
+
tones,
|
|
138
|
+
chart: chartAnchors(brandHue, bandwidth),
|
|
139
|
+
chartLightnessLadder: bandwidth < LADDER_BANDWIDTH,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 语义色的色相:由 semantics 轴决定被 harmony 带走多远。
|
|
145
|
+
*
|
|
146
|
+
* faithful 不带走(功能色相原值)——后台/交易系统的正确缺省;
|
|
147
|
+
* tinted 压缩后若离品牌太近,推回到 40° 保底;
|
|
148
|
+
* immersive 完全服从编排,可辨性由信号通道承担(S5 的 signal 令牌)。
|
|
149
|
+
*/
|
|
150
|
+
function semanticHue(
|
|
151
|
+
functional: number, brandHue: number, k: number, semantics: SomaSemantics,
|
|
152
|
+
): number {
|
|
153
|
+
if (semantics === 'faithful') return functional;
|
|
154
|
+
const compressed = compress(functional, brandHue, k);
|
|
155
|
+
if (semantics === 'immersive') return compressed;
|
|
156
|
+
|
|
157
|
+
const gap = Math.abs(angDiff(compressed, brandHue));
|
|
158
|
+
if (gap >= TINTED_MIN_SEPARATION) return compressed;
|
|
159
|
+
const away = angDiff(compressed, brandHue) >= 0 ? 1 : -1;
|
|
160
|
+
return norm(brandHue + away * TINTED_MIN_SEPARATION);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 一组色相的极差(考虑环绕:取覆盖全部点的最小弧长) */
|
|
164
|
+
export function hueBandwidth(hues: readonly number[]): number {
|
|
165
|
+
const sorted = [...hues].map(norm).sort((a, b) => a - b);
|
|
166
|
+
if (sorted.length <= 1) return 0;
|
|
167
|
+
let widestGap = (sorted[0] + 360) - sorted[sorted.length - 1];
|
|
168
|
+
for (let i = 1; i < sorted.length; i += 1) {
|
|
169
|
+
widestGap = Math.max(widestGap, sorted[i] - sorted[i - 1]);
|
|
170
|
+
}
|
|
171
|
+
return 360 - widestGap;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 图表序列的 8 个色相锚。
|
|
176
|
+
*
|
|
177
|
+
* 带宽足够时沿带均布;带太窄(单色系)时色相已无区分力——
|
|
178
|
+
* 此时仍返回均布锚,但 `chartLightnessLadder` 置位,由调用方改用明度阶梯区分。
|
|
179
|
+
* 单色系里 8 条曲线靠色相分不开是感知事实,不是风格选择。
|
|
180
|
+
*/
|
|
181
|
+
function chartAnchors(brandHue: number, bandwidth: number): number[] {
|
|
182
|
+
// 带宽为 0(全归一)时退化为品牌色相原地 8 份;由明度阶梯承担区分
|
|
183
|
+
const span = bandwidth < 1 ? 0 : Math.min(bandwidth, 360);
|
|
184
|
+
const start = norm(brandHue - span / 2);
|
|
185
|
+
return Array.from({ length: 8 }, (_, i) => norm(start + (span * i) / 8));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface SeparationResult {
|
|
189
|
+
readonly anchors: SomaHueAnchors;
|
|
190
|
+
readonly applied: boolean;
|
|
191
|
+
readonly pushed: readonly ('success' | 'warning' | 'danger')[];
|
|
192
|
+
/**
|
|
193
|
+
* 色相区内推不出安全距离的语气:分离改由**明度**承担。
|
|
194
|
+
* 值是相对品牌明度需要的最小偏移方向与幅度,由编排者施加到该语气的明度上。
|
|
195
|
+
*/
|
|
196
|
+
readonly lightnessSeparation: Readonly<Partial<Record<
|
|
197
|
+
'success' | 'warning' | 'danger', { direction: 1 | -1; minGap: number }
|
|
198
|
+
>>>;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* S3 语义分离闸:品牌与语义色撞了就**推开语义色**,不动品牌。
|
|
203
|
+
*
|
|
204
|
+
* 为什么推语义色不推品牌:品牌是宿主给的身份,引擎无权改;
|
|
205
|
+
* 而语义色本来就是派生物,移动它只需保证仍在自己的语义区间内可辨。
|
|
206
|
+
*
|
|
207
|
+
* immersive 档下闸退场——那一档的可辨性责任已移交信号通道,
|
|
208
|
+
* 此时再推色相等于既要色相编码又要动效编码,两头不讨好。
|
|
209
|
+
*/
|
|
210
|
+
export function applySeparationGuard(
|
|
211
|
+
anchors: SomaHueAnchors,
|
|
212
|
+
brandHue: number,
|
|
213
|
+
brandLightness: number,
|
|
214
|
+
toneLightness: Readonly<Record<SomaToneName, number>>,
|
|
215
|
+
semantics: SomaSemantics,
|
|
216
|
+
): SeparationResult {
|
|
217
|
+
if (semantics === 'immersive') {
|
|
218
|
+
return { anchors, applied: false, pushed: [], lightnessSeparation: {} };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const pushed: ('success' | 'warning' | 'danger')[] = [];
|
|
222
|
+
const lightnessSeparation: Partial<Record<
|
|
223
|
+
'success' | 'warning' | 'danger', { direction: 1 | -1; minGap: number }
|
|
224
|
+
>> = {};
|
|
225
|
+
const tones = { ...anchors.tones };
|
|
226
|
+
|
|
227
|
+
for (const tone of ['success', 'warning', 'danger'] as const) {
|
|
228
|
+
const hueGap = Math.abs(angDiff(tones[tone], brandHue));
|
|
229
|
+
const lightGap = Math.abs((toneLightness[tone] ?? 0) - brandLightness);
|
|
230
|
+
// 判定端与推开端**对称留余量**:判"≥阈值就放行"会让恰好落在
|
|
231
|
+
// 29.7~30.0 的配对溜过去(加密扫描 16200 个配对实测 7 例)——
|
|
232
|
+
// 那些色相锚是压缩/偏移算出来的,落点本就可能贴着线。
|
|
233
|
+
// 判定用 ×1.15 的门槛,与 PUSH_MARGIN 同值:
|
|
234
|
+
// 认定得比放行更严,否则"推开"永远够不到"判撞"的边。
|
|
235
|
+
const hueBar = SEPARATION_THRESHOLD.hue * PUSH_MARGIN;
|
|
236
|
+
const lightBar = SEPARATION_THRESHOLD.lightness * PUSH_MARGIN;
|
|
237
|
+
if (hueGap >= hueBar || lightGap >= lightBar) continue;
|
|
238
|
+
|
|
239
|
+
// 同伴语义色:推开时必须同时避开它们。
|
|
240
|
+
//
|
|
241
|
+
// 只避品牌是不够的——实测教训:红色品牌(#f43f5e,h=16)把 danger(h=27)
|
|
242
|
+
// 朝远离品牌的方向推,会推到 h=46,那是**橙色**,正好撞进 warning(h=75)的地盘。
|
|
243
|
+
// 结果是"修好了主色与危险色,却让危险色和警告色分不开"。
|
|
244
|
+
// 所以候选方向要按"到品牌与到同伴的最小距离"择优,不是简单地朝一边推。
|
|
245
|
+
const siblings = (['success', 'warning', 'danger'] as const)
|
|
246
|
+
.filter((other) => other !== tone)
|
|
247
|
+
.map((other) => tones[other]);
|
|
248
|
+
|
|
249
|
+
const scoreOf = (hue: number): number => Math.min(
|
|
250
|
+
Math.abs(angDiff(hue, brandHue)),
|
|
251
|
+
...siblings.map((sibling) => Math.abs(angDiff(hue, sibling))),
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
// 候选只在本语气的色相区内取——出了区就不是这个语气了
|
|
255
|
+
const [zoneLow, zoneHigh] = SEMANTIC_ZONES[tone];
|
|
256
|
+
const inZone = (hue: number): boolean => {
|
|
257
|
+
const h = norm(hue);
|
|
258
|
+
return zoneLow <= zoneHigh
|
|
259
|
+
? h >= zoneLow && h <= zoneHigh
|
|
260
|
+
: h >= zoneLow || h <= zoneHigh; // 跨 0° 的区间
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const target = SEPARATION_THRESHOLD.hue * PUSH_MARGIN;
|
|
264
|
+
const candidateFor = (direction: 1 | -1): number | null => {
|
|
265
|
+
let hue = tones[tone];
|
|
266
|
+
for (let i = 0; i < PUSH_MAX; i += 1) {
|
|
267
|
+
if (Math.abs(angDiff(hue, brandHue)) >= target) return hue;
|
|
268
|
+
const next = norm(hue + direction * PUSH_STEP);
|
|
269
|
+
if (!inZone(next)) return null; // 推出语义区:此方向不通
|
|
270
|
+
hue = next;
|
|
271
|
+
}
|
|
272
|
+
return Math.abs(angDiff(hue, brandHue)) >= target ? hue : null;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const candidates = [candidateFor(1), candidateFor(-1)]
|
|
276
|
+
.filter((hue): hue is number => hue !== null);
|
|
277
|
+
|
|
278
|
+
if (candidates.length === 0) {
|
|
279
|
+
// 色相维走不通(红色品牌 × danger 就是这一类):分离退到明度维。
|
|
280
|
+
// 方向取"远离品牌明度"的那一侧,幅度用判撞阈值本身
|
|
281
|
+
const direction: 1 | -1 = (toneLightness[tone] ?? 0) >= brandLightness ? 1 : -1;
|
|
282
|
+
lightnessSeparation[tone] = { direction, minGap: SEPARATION_THRESHOLD.lightness };
|
|
283
|
+
pushed.push(tone);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
candidates.sort((a, b) => scoreOf(b) - scoreOf(a));
|
|
288
|
+
tones[tone] = candidates[0];
|
|
289
|
+
pushed.push(tone);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (pushed.length === 0) {
|
|
293
|
+
return { anchors, applied: false, pushed: [], lightnessSeparation: {} };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const bandwidth = hueBandwidth(Object.values(tones));
|
|
297
|
+
return {
|
|
298
|
+
anchors: {
|
|
299
|
+
tones,
|
|
300
|
+
chart: chartAnchors(brandHue, bandwidth),
|
|
301
|
+
chartLightnessLadder: bandwidth < LADDER_BANDWIDTH,
|
|
302
|
+
},
|
|
303
|
+
applied: true,
|
|
304
|
+
pushed,
|
|
305
|
+
lightnessSeparation,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// 场景令牌(REQ-007 S5 + S7′):背景场、材质与信号通道。
|
|
2
|
+
//
|
|
3
|
+
// 拆成两个导出不是为了好看,是因为**它们必须在派生链的两个不同位置执行**:
|
|
4
|
+
//
|
|
5
|
+
// S5 结构参数(时长/模糊/饱和/位移)与颜色无关,早算晚算一样,放前面;
|
|
6
|
+
// S7′ 场景颜色取自派生色板,而 daisy 那 35 套预设的最终色来自 overrides
|
|
7
|
+
// (在 ⑥ 段合并)。场景色若在 overrides 之前算,daisy × aurora 的组合
|
|
8
|
+
// 会出现"表面一个色系、光球另一个色系"——这正是 ⑥.1(淡底跟随实心色)
|
|
9
|
+
// 已经修过一遍的同类病。所以场景色必须跟 soft/on-soft 一起,
|
|
10
|
+
// 吃**最终生效**的变量表。
|
|
11
|
+
//
|
|
12
|
+
// G1(REQ-006)时期这些值是写死的类与常量;本模块把它们收编成受轴控制的派生物,
|
|
13
|
+
// 且 `field:'aurora', intensity:'soft'` 的 alpha 与 G1 逐字节一致——
|
|
14
|
+
// aurora 预设参数化后观感不动,这是零破坏的锚点。
|
|
15
|
+
|
|
16
|
+
import type { SomaResolvedAxes } from './theme-axes';
|
|
17
|
+
|
|
18
|
+
/** 信号通道在 faithful/tinted 档下的惰性值:规则挂着也不显形,零成本 */
|
|
19
|
+
const SIGNAL_IDLE = {
|
|
20
|
+
'--soma-signal-edge': 'none',
|
|
21
|
+
'--soma-signal-glow': 'none',
|
|
22
|
+
'--soma-signal-pulse-duration': '0s',
|
|
23
|
+
'--soma-signal-pulse-duration-calm': '0s',
|
|
24
|
+
'--soma-signal-edge-duration': '0s',
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 信号节奏三档,取自原型 workbench.css 的实测值。
|
|
29
|
+
*
|
|
30
|
+
* 为什么由引擎产出而不是让 soma-ui 用 calc 从一个时长派生:
|
|
31
|
+
* **急与缓的对比是主题的节奏性格**,与"动效快慢"(motion 轴)是两件事——
|
|
32
|
+
* 一个主题可以整体很慢但危险与警告的对比很强。派生比写在样式层,
|
|
33
|
+
* 等于把一个气质参数藏进 CSS,预设调不了、闸门也量不到。
|
|
34
|
+
*
|
|
35
|
+
* urgent 给 danger(急)、calm 给 warning(缓)、edge 给流光扫边——
|
|
36
|
+
* edge 与 urgent 刻意不同频(2.4 vs 2.8),两条动画错开才不会看着像同一下。
|
|
37
|
+
*/
|
|
38
|
+
const SIGNAL_DURATIONS = { urgent: '2.8s', calm: '4.9s', edge: '2.4s' } as const;
|
|
39
|
+
|
|
40
|
+
/** 环境光球的呼吸时长:G1 keyframes 里写死的 12s,本批参数化 */
|
|
41
|
+
const AMBIENT_DURATION = '12s';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* S5:不含颜色的场景结构参数。
|
|
45
|
+
*
|
|
46
|
+
* 玻璃三档的 rgba 从引擎主体迁到这里(值一个没改)——它们是材质的结构属性,
|
|
47
|
+
* 与 field/material 轴同族,散在派生链中间只会让人以为它们是色板的一部分。
|
|
48
|
+
*/
|
|
49
|
+
export function sceneStructureTokens(axes: SomaResolvedAxes): Record<string, string> {
|
|
50
|
+
const signal = axes.palette.semantics === 'immersive'
|
|
51
|
+
? {
|
|
52
|
+
...SIGNAL_IDLE,
|
|
53
|
+
'--soma-signal-pulse-duration': SIGNAL_DURATIONS.urgent,
|
|
54
|
+
'--soma-signal-pulse-duration-calm': SIGNAL_DURATIONS.calm,
|
|
55
|
+
'--soma-signal-edge-duration': SIGNAL_DURATIONS.edge,
|
|
56
|
+
}
|
|
57
|
+
: { ...SIGNAL_IDLE };
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
// 环境光球时长:motion 轴(soma-ui recipe)会覆盖它——
|
|
61
|
+
// `motion: 'none'` 的主题里光球因此自动静止,与 prefers-reduced-motion
|
|
62
|
+
// 走同一条退化路径,一份实现两个触发源
|
|
63
|
+
'--soma-ambient-duration': AMBIENT_DURATION,
|
|
64
|
+
...signal,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 暗色下的玻璃:半透明**黑**而不是白——白玻璃压在深底上会变成发光板 */
|
|
69
|
+
export function glassTokens(dark: boolean): Record<string, string> {
|
|
70
|
+
return dark
|
|
71
|
+
? {
|
|
72
|
+
'--soma-surface-glass': 'rgba(255, 255, 255, 0.06)',
|
|
73
|
+
'--soma-surface-glass-strong': 'rgba(255, 255, 255, 0.1)',
|
|
74
|
+
'--soma-surface-glass-weak': 'rgba(255, 255, 255, 0.04)',
|
|
75
|
+
}
|
|
76
|
+
: {
|
|
77
|
+
'--soma-surface-glass': 'rgba(255, 255, 255, 0.72)',
|
|
78
|
+
'--soma-surface-glass-strong': 'rgba(255, 255, 255, 0.88)',
|
|
79
|
+
'--soma-surface-glass-weak': 'rgba(255, 255, 255, 0.52)',
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* S7′:场景颜色——吃 overrides 合并后的**最终**变量表。
|
|
85
|
+
*
|
|
86
|
+
* 三团环境光取自最终色板的 300/500/400 三档:宿主用 overrides 改了主色,
|
|
87
|
+
* 光球会跟着改,不会出现表面与场分属两个色系。
|
|
88
|
+
*/
|
|
89
|
+
export function sceneColorTokens(
|
|
90
|
+
finalVars: Readonly<Record<string, string>>,
|
|
91
|
+
axes: SomaResolvedAxes,
|
|
92
|
+
alphas: readonly [number, number, number] | null,
|
|
93
|
+
hexWithAlpha: (hex: string, alpha: number) => string,
|
|
94
|
+
/** 品牌色阶:三团光的缺省取色源(G1 用的就是 300/500/400 三档) */
|
|
95
|
+
scale: Readonly<Record<string, string>>,
|
|
96
|
+
/**
|
|
97
|
+
* 覆盖后的色阶重建器:宿主用 overrides 换掉了主色时,
|
|
98
|
+
* 光球必须跟着换色系,否则 daisy × aurora 会"表面一个色系、光球另一个色系"。
|
|
99
|
+
* 传 null 表示主色未被覆盖,直接用原色阶。
|
|
100
|
+
*/
|
|
101
|
+
rescale: ((primaryHex: string) => Record<string, string>) | null,
|
|
102
|
+
): Record<string, string> {
|
|
103
|
+
const out: Record<string, string> = {};
|
|
104
|
+
|
|
105
|
+
// 环境光:flat 档产 transparent —— 页面可以常挂 .soma-ambient 类,
|
|
106
|
+
// 显不显形由主题决定。场景归主题管,不归 markup 管(控制反转)
|
|
107
|
+
if (!alphas) {
|
|
108
|
+
out['--soma-ambient-1'] = 'transparent';
|
|
109
|
+
out['--soma-ambient-2'] = 'transparent';
|
|
110
|
+
out['--soma-ambient-3'] = 'transparent';
|
|
111
|
+
} else {
|
|
112
|
+
// 取色源:主色被 overrides 换过就按新主色重建色阶,否则用原色阶。
|
|
113
|
+
// 缺省路径(无 overrides)走的是与 G1 完全相同的 300/500/400 三档
|
|
114
|
+
const effective = rescale ? rescale(finalVars['--soma-c-primary']) : scale;
|
|
115
|
+
const seeds = [effective['300'], effective['500'], effective['400']];
|
|
116
|
+
seeds.forEach((seed, i) => {
|
|
117
|
+
out[`--soma-ambient-${i + 1}`] = seed ? hexWithAlpha(seed, alphas[i]) : 'transparent';
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 信号色:immersive 下取主色系作为呼吸与流光的色相来源。
|
|
122
|
+
// faithful 下仍产出(值为主色),但 pulse-duration=0s 让它不显形——
|
|
123
|
+
// 令牌恒存在是刻意的:组件的 var() 引用永远有值,不必写两套回退
|
|
124
|
+
const tint = finalVars['--soma-c-primary'] ?? '#4f8cff';
|
|
125
|
+
out['--soma-signal-tint'] = tint;
|
|
126
|
+
|
|
127
|
+
if (axes.palette.semantics === 'immersive') {
|
|
128
|
+
out['--soma-signal-glow'] = `0 0 0 6px ${hexWithAlpha(tint, 0.25)}`;
|
|
129
|
+
out['--soma-signal-edge'] =
|
|
130
|
+
`linear-gradient(90deg, transparent, ${hexWithAlpha(tint, 0.86)}, transparent)`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return out;
|
|
134
|
+
}
|