From 8cbe1d592355934de2cb7641882074a2764bac4b Mon Sep 17 00:00:00 2001 From: qiqi Date: Mon, 23 Nov 2026 14:39:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(tokens):=20M3=20=E5=8D=95=E4=B8=80?= =?UTF-8?q?=E6=BA=90=20+=20=E7=94=9F=E6=88=90=E4=BA=A7=E7=89=A9=EF=BC=88de?= =?UTF-8?q?sign-tokens.css=20/=20tokens.json=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit D1「同一批 token 三个写入者」收尾:颜色快照从**手写**变成**产物**。 - 新增 scripts/gen-tokens.js:theme-tokens.js 的 derive() 是唯一源, --write 生成 src/design-tokens.css(158 行::root 77 / .theme-dark 39 / 亮色动效 6) 与 dist/tokens.json(DTCG,暗色值在 $extensions.com.image-workbench.dark) - 尺度层(间距/圆角/字号/阴影/过渡)也搬进生成器的 SCALE 表,同样只写一次 - style.css 2407 行只剩组件规则;build 时把 design-tokens.css 拼接在最前 (不是 @import:Tauri 不靠它兜底),dist 里仍只有一个 style.css - 三道闸门:tokens:check(进 verify:ci,报第一处差异行)、 build 漂移即失败、theme-static-snapshot-test 逐 token 报错 - 12 张视觉基线 0 diff(换文件不动值;77/39/6 条声明逐条比对过) 偏差:tokens.json 不单独写 scripts/tokens-export.js —— 与 CSS 同进程产出, 避免「导出的 JSON」和「生效的 CSS」两次解析口径。 --- docs/plans/design-tokens-rollout.md | 20 +- docs/standards/design-tokens.md | 21 ++ package.json | 4 +- scripts/build.js | 19 +- scripts/gen-tokens.js | 353 +++++++++++++++++++++++++++ scripts/token-audit.js | 14 +- src/design-tokens.css | 157 ++++++++++++ style.css | 187 +------------- tests/appearance-v2-test.js | 5 +- tests/page-chrome-regression-test.js | 4 +- tests/theme-static-snapshot-test.js | 5 +- 11 files changed, 598 insertions(+), 191 deletions(-) create mode 100644 scripts/gen-tokens.js create mode 100644 src/design-tokens.css diff --git a/docs/plans/design-tokens-rollout.md b/docs/plans/design-tokens-rollout.md index cf9a6c4..40958e1 100644 --- a/docs/plans/design-tokens-rollout.md +++ b/docs/plans/design-tokens-rollout.md @@ -97,7 +97,7 @@ **回滚**:M2 是一个 commit;`.theme-dark` 与 `:root` 的旧值都在 git 里,`git revert` 即回到「中灰暗色」。 -### M3 单一源与导出(1d,零观感) +### M3 单一源与导出(1d,零观感)—— ✅ 已完成 2026-11-23 | ID | 任务 | 文件 | 验收 | |---|---|---|---| @@ -107,6 +107,24 @@ | T3.4 | 别名方向:阶段 1 legacy 为源,新名 `var(--legacy)`;文档写明阶段 2(M5 后)翻转 | `design-tokens.css` | 插件读 legacy 仍有值 | | T3.5 | 视觉基线零 diff | — | 12 张截图 `maxDiffPixels: 0` | +**执行结果(2026-11-23)**: + +| ID | 计划 | 实际 | 偏差与理由 | +|---|---|---|---| +| T3.1 | `src/design-tokens.css`:L1 + L2 + legacy 别名,只放静态值 | ✅ 158 行(含 `:root` 77 条 / `.theme-dark` 39 条 / 亮色动效 6 条) | 文件头写明「生成文件勿手改」。**L1 尺度也搬进来了**(`SCALE` 表在生成器里),否则「单一源」只覆盖颜色 | +| T3.2 | 构建时内联到 style.css 之前 | ✅ `scripts/build.js` 拼接 `design-tokens.css + style.css` 再 minify | 拼接而非复制:dist 里仍只有一个 `style.css`,`` 不用改,Tauri 也不依赖 `@import` | +| T3.3 | `scripts/tokens-export.js` 解析 CSS → `dist/tokens.json` | ✅ 合并进 `scripts/gen-tokens.js`(`--write` 同时产出 CSS 与 JSON;JSON 由**解析生成的 CSS** 得到) | 分成两个脚本会让「导出的 JSON」与「实际生效的 CSS」有两次解析口径,多一个漂移点。本仓库的规矩是「一个产物一个生成者」 | +| T3.4 | 别名方向:阶段 1 legacy 为源 | ✅ 未翻转(插件仍读 `--iwb-plugin-*` 别名) | 与计划一致,翻转留到 M5 之后再评估 | +| T3.5 | 12 张基线 `maxDiffPixels: 0` | ✅ 12/12 通过,零 diff | 换文件不动值,所以像素级零变化(这也反过来证明搬家没丢声明:77/39/6 条逐条比对过) | + +**M3 的真正收益不是「文件搬家」**,而是把静态快照从**手写**变成**产物**:D1「同一批 token 有三个写入者」到此只剩 +`derive()` 一个写入者 + 两个生成产物,人不用再记「改公式要同步 CSS」(M2 期间我自己就漏过 `nodeBg` 与 `brand-text` 两次)。 + +三道闸门:`tokens:check`(进 verify:ci,报第一处差异行)、`build` 漂移即失败、`theme-static-snapshot-test`(逐 token 报错)。 + +**已知限制**:`--iwb-anim-*` 与 `--sh-*` 的暗色值是生成器里的常量表(不来自 `derive()`),因为它们是「主题皮肤」而非派生色; +将来若要支持自定义暗色皮肤,需要把它们并入 `derive()`。 + ### M4 六批迁移(3.5d) 每批一个 PR,**等值替换为主**,仅 B1 有行为收敛(5 套 tab 合一)。 diff --git a/docs/standards/design-tokens.md b/docs/standards/design-tokens.md index e65b3a3..6c2037f 100644 --- a/docs/standards/design-tokens.md +++ b/docs/standards/design-tokens.md @@ -329,6 +329,10 @@ L3 Contract ── 组件整体换肤时才加,能不建就不建 现状 66 个平铺名保留为 legacy;下表是**新代码必须使用**的名字。`外观面板` 列标「可覆盖」的,用户在「外观」里能改,改完仍走同一条 `derive → overrides` 管线。 +> ⚠ **下表是目标名(M4 迁移后才全部存在)**。当前真正生效的仍是平铺 legacy 名(`--iwb-bg` / `--iwb-text` …), +> 它们是 `src/theme-tokens.js` 的产物,落在 `src/design-tokens.css`,要查当前值请看那里或 `dist/tokens.json`, +> 不要照抄本表的默认值(表是计划书,不是快照)。 + | L2 token | 亮色默认 | 暗色默认 | 用在哪 | 外观面板 | |---|---|---|---|---| | `--iwb-color-bg-app` | `#f4f5f7` | `#757575` | 页面底 | 可覆盖 | @@ -355,6 +359,23 @@ L3 Contract ── 组件整体换肤时才加,能不建就不建 - 正文对所在表面 ≥ **4.5:1**;次要文字 ≥ **3.2:1**;非文字元素(边框/图标)≥ **3:1** - 品牌底上的字由 `contrast()` 自动选黑/白,**禁止手工指定** +#### 3.1.1 单一源与产物(M3) + +颜色只有一个源:`src/theme-tokens.js` 的 `derive()`。它派生出两份产物,**都不许手改**: + +| 产物 | 用途 | 生成 | +|---|---|---| +| `src/design-tokens.css` | 静态快照(`:root` / `.theme-dark` / 亮色动效 / 尺度 L1),顶住 JS 执行前那一帧 | `npm run tokens:write` | +| `dist/tokens.json` | DTCG(`{$value,$type}`)导出,可导入 Penpot / Figma Variables;暗色值在 `$extensions.com.image-workbench.dark` | 构建时自动写 | + +三道闸门: + +1. `npm run tokens:check`(在 `verify:ci` 里)—— 快照与 `derive()` 不一致就失败,并指出第一处差异行; +2. `npm run build` —— 漂移时直接构建失败(杜绝发出去的包用旧色板); +3. `tests/theme-static-snapshot-test.js` —— 逐 token 比对(60 个色值 × 亮暗),报错能指到具体 token。 + +**尺度层(间距/圆角/字号/阴影/动效)也只写一次**:在 `scripts/gen-tokens.js` 的 `SCALE` 表里,产物同样进 `design-tokens.css`。 + ### 3.2 间距 L1 闭集(14 档,多余一律不加): diff --git a/package.json b/package.json index 8d809fd..4d4fb4c 100644 --- a/package.json +++ b/package.json @@ -16,13 +16,15 @@ "plugins:gc": "node scripts/plugins-publish.mjs --gc", "coverage:browser": "npm run build && node scripts/coverage-browser.js", "audit:pins": "node scripts/audit-source-pins.js", + "tokens:write": "node scripts/gen-tokens.js --write", + "tokens:check": "node scripts/gen-tokens.js --check", "audit:tokens": "node scripts/token-audit.js", "audit:tokens:contrast": "node scripts/token-audit.js --contrast", "test:visual": "npx playwright test _visual-tokens", "test:visual:update": "npx playwright test _visual-tokens --update-snapshots", "test:e2e": "npm run build && node scripts/serve-test.js", "test:e2e:local": "node scripts/serve-test.js", - "verify:ci": "npm run build && node --check app.js && node --check dist/app.js && node scripts/check-dist.js && npm run lint && npm run typecheck && npm run test:unit && npm test && npm run test:facts", + "verify:ci": "npm run build && node --check app.js && node --check dist/app.js && node scripts/check-dist.js && npm run tokens:check && npm run lint && npm run typecheck && npm run test:unit && npm test && npm run test:facts", "verify": "npm run verify:ci && npm run test:e2e", "verify:release": "node scripts/verify-release.js", "coverage:e2e": "node scripts/coverage-e2e.js", diff --git a/scripts/build.js b/scripts/build.js index 01b9e31..d62c5fc 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -182,7 +182,24 @@ if (sourcemap) { appBundle = '/*! IWB ' + version + ' */\n' + appBundle fs.writeFileSync(path.join(dist, 'app.js'), appBundle) -const minifiedCss = transformSync(fs.readFileSync(path.join(root, 'style.css'), 'utf8'), { +/* 设计 token 必须排在最前:src/design-tokens.css 是生成的静态快照(:root / .theme-dark), + * 组件规则靠它里面的变量。Tauri 的 CSP 不做 @import 兜底,所以这里是**拼接**而不是 @import —— + * 顺序 = design-tokens.css → style.css,改顺序会让静态快照失效(首帧变量全空)。 + * 顺带做两件事:① 校验它没和 derive() 漂移(漂了就 build 失败,杜绝发出去的包用旧色板); + * ② 把 DTCG 版 token 写到 dist/tokens.json 供设计工具导入。 */ +const { build: buildTokens } = require('./gen-tokens') +const tokensBuilt = buildTokens() +const tokensCssPath = path.join(root, 'src/design-tokens.css') +const tokensCss = fs.readFileSync(tokensCssPath, 'utf8') +if (tokensCss !== tokensBuilt.css) { + console.error('FAIL src/design-tokens.css 与 src/theme-tokens.js 的 derive() 不一致') + console.error(' → 先跑 npm run tokens:write(改了配色公式/种子却没重新生成快照)') + process.exit(1) +} +fs.writeFileSync(path.join(dist, 'tokens.json'), tokensBuilt.json) +const cssSources = ['src/design-tokens.css', 'style.css'] +const cssRaw = cssSources.map((f) => fs.readFileSync(path.join(root, f), 'utf8')).join('\n') +const minifiedCss = transformSync(cssRaw, { loader: 'css', minify: true, target: ['chrome100', 'firefox100', 'safari15'], diff --git a/scripts/gen-tokens.js b/scripts/gen-tokens.js new file mode 100644 index 0000000..8e096f2 --- /dev/null +++ b/scripts/gen-tokens.js @@ -0,0 +1,353 @@ +#!/usr/bin/env node +'use strict' +/* gen-tokens.js —— 设计 token 的单一源 → 产物生成(v3.26.44 / M3) + + * 为什么要有这个脚本(D1 的终极修法): + * 颜色此前写在两个地方 —— `src/theme-tokens.js` 的 derive()(运行时)与 `style.css` 的 + * `:root`/`.theme-dark`(JS 执行前那一帧)。两份必须逐值相等,靠人同步,于是历史上反复错位 + * (D2:暗色两套值;M2 期间我自己就漏同步过 nodeBg 与 brand-text)。 + * + * 现在改成:**theme-tokens.js 是唯一源,design-tokens.css 是产物**。 + * - `node scripts/gen-tokens.js --write` 重新生成 CSS 与 tokens.json + * - `node scripts/gen-tokens.js --check` 只比对,不一致就退出 1(进 verify:ci) + * + * 产物: + * src/design-tokens.css 静态快照(:root / .theme-dark / 亮色动效),build 时会内联到 style.css 前面 + * dist/tokens.json DTCG({$value,$type})格式,可导入 Penpot / Figma Variables + * + * 三条约束: + * ① 这里**不许**出现新的颜色常量 —— 颜色只能来自 derive(); + * ② 结构层(chrome/home/dock)用 color-mix 表达关系式,切主题自动跟随(D1 第二刀); + * ③ 尺度(间距/圆角/字号/阴影/动效)写在下面的 SCALE 里,也只写一次。 + */ + +const fs = require('fs') +const path = require('path') + +const ROOT = path.resolve(__dirname, '..') +const CSS_OUT = path.join(ROOT, 'src/design-tokens.css') +const JSON_OUT = path.join(ROOT, 'dist/tokens.json') + +// 载入 theme-tokens(UMD:挂到传入对象上) +const sandbox = {} +new Function('window', fs.readFileSync(path.join(ROOT, 'src/theme-tokens.js'), 'utf8'))(sandbox) +const T = sandbox.IWBThemeTokens + +const LIGHT = T.derive(T.DEFAULTS, false) +const DARK = T.derive(T.DEFAULTS, true) + +/* derive 的键 → CSS 变量名(去掉 --iwb- 前缀)。特例写在 ALIAS 里。 */ +const ALIAS = { + canvasText: 'color-text-on-canvas', + menuBg: 'bg-menu', + canvasBg: 'canvas-bg', + nodeBg: 'node-bg', + nodeText: 'node-text', + nodeText2: 'node-text2', + textDim: 'text-dim', + homeBg: 'home-bg', + dockBg: 'dock-bg', + dockHeadBg: 'dock-head-bg', + brandText: 'brand-text', + onBrand: 'on-brand', + pluginSurface: 'plugin-surface', + pluginCard: 'plugin-card', + pluginInset: 'plugin-inset', + pluginHover: 'plugin-hover', + gridDot: 'grid-dot', + edgeHot: 'edge-hot', +} +const varName = (k) => '--iwb-' + (ALIAS[k] || k.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase())) + +/* 一个 token 在 CSS 里出现的顺序(= 阅读顺序:先表面、再描边、再文字、再品牌、再插件) */ +const COLOR_ORDER = [ + 'bg', 'canvasBg', 'layer', 'layer2', 'panel', 'nodeBg', 'field', + 'border', 'border2', + 'text', 'text2', 'textDim', 'canvasText', 'nodeText', 'nodeText2', 'tint', + 'brand', 'brandText', 'onBrand', 'edge', 'edgeHot', 'gridDot', + 'surface', 'hover', 'menuBg', + 'pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover', + 'err', 'ok', 'warn', +] + +/* 尺度层(L1):只在这里定义一次。@deprecated 的名字保留到 M4 迁移结束。 */ +const SCALE = [ + ['--sp-1', '4px', '@deprecated 全部 5 个 → M3 的 --iwb-space-4/8/12/16/24'], + ['--sp-2', '8px', null], + ['--sp-3', '12px', null], + ['--sp-4', '16px', null], + ['--sp-5', '24px', null], + ['--rd-sm', '6px', '@deprecated rd-sm/rd-md/rd-lg → M3 的 --iwb-radius-chip/control/popover'], + ['--rd', '8px', null], + ['--rd-md', '10px', null], + ['--rd-lg', '12px', null], + ['--fs-xs', '10px', '@deprecated fs-xs/fs-md/fs-lg/fs-xl → M3 的 --iwb-font-*(fs-sm 仍被引用)'], + ['--fs-sm', '11px', null], + ['--fs-md', '12px', null], + ['--fs-lg', '13px', null], + ['--fs-xl', '14px', null], + ['--tr-fast', '0.1s ease-out', null], + ['--tr-base', '0.2s ease-out', '@deprecated tr-base/tr-slow → M3 的 --iwb-motion-*(tr-fast 仍被引用)'], + ['--tr-slow', '0.3s ease-out', null], +] + +/* 只在亮色出现的结构/契约声明(暗色由 :root 的 color-mix 在 html 上重新求值,或单独覆盖) */ +const LIGHT_ONLY = [ + ['--iwb-chrome', 'color-mix(in srgb, var(--iwb-bg) 60%, var(--iwb-canvas-bg) 40%)', '顶部 chrome 色:页面底与画布之间的过渡层(标签条用)'], + ['--iwb-home-bg', 'color-mix(in srgb, var(--iwb-bg) 35%, var(--iwb-canvas-bg) 65%)', '首页底色:画布色与顶栏色之间取 35/65'], + ['--iwb-hr', 'color-mix(in srgb, var(--iwb-border) 50%, var(--iwb-border2) 50%)', '首页上的细分隔线'], + ['--iwb-dock-bg', 'color-mix(in srgb, var(--iwb-chrome) 55%, var(--iwb-canvas-bg) 45%)', '左右停靠栏底色:排进 bg→chrome→home→canvas 灰阶'], + ['--iwb-dock-head-bg', 'color-mix(in srgb, var(--iwb-panel) 55%, var(--iwb-dock-bg))', '顶栏底色比停靠栏更浅一档'], + ['--iwb-plugin-bg', 'var(--iwb-bg)', 'v3.25.85 插件主题契约:插件只能用这组别名'], + ['--iwb-plugin-panel', 'var(--iwb-panel)', null], + ['--iwb-plugin-layer', 'var(--iwb-layer)', null], + ['--iwb-plugin-border', 'var(--iwb-border)', null], + ['--iwb-plugin-text', 'var(--iwb-text)', null], + ['--iwb-plugin-text2', 'var(--iwb-text2)', null], + ['--iwb-plugin-brand', 'var(--iwb-brand)', null], + ['--iwb-plugin-on-brand', 'var(--iwb-on-brand)', null], + ['--iwb-plugin-err', 'var(--iwb-err)', null], + ['--iwb-plugin-ok', 'var(--iwb-ok)', null], + ['--iwb-plugin-warn', 'var(--iwb-warn)', null], + ['--iwb-edge', 'var(--iwb-border2)', '连线默认色:跟随边框灰(主题色只用于选中/悬停/上游高亮)'], + ['--iwb-edge-hot', 'var(--iwb-brand)', null], + ['--iwb-focus-ring', '2px solid color-mix(in srgb, var(--iwb-brand-text) 70%, transparent)', '键盘焦点环(D8):颜色走 brand-text,暗色下 brand 画不出环'], + ['--iwb-edge-w', '1.8px', '外观自定义:由 src/ui-appearance.js 按配置写入,这里只给兜底默认值'], + ['--iwb-edge-dash', '4 3', null], + ['--iwb-node-radius', '14px', null], + ['--iwb-node-sel-w', '1px', null], + ['--iwb-grid-size', '1.4px', null], + ['--iwb-grid-gap', '22px', null], + ['--iwb-grid-on', '1', null], + ['--iwb-nav-w', '52px', '一级导航宽度:左停靠栏/工具栏让位都以它为基准'], +] + +/* 暗色独有的动效:亮色用品牌色,暗色必须用「亮色元素 + 深色轨道」 */ +const DARK_ANIM = [ + ['--iwb-anim', '#ffffff'], + ['--iwb-anim-track', 'color-mix(in srgb, #ffffff 18%, transparent)'], + ['--iwb-anim-glow', 'rgba(255, 255, 255, 0.72)'], + ['--iwb-anim-tint', 'color-mix(in srgb, #ffffff 16%, transparent)'], + ['--iwb-anim-flow-a', 'color-mix(in srgb, #ffffff 26%, var(--iwb-layer))'], + ['--iwb-anim-flow-b', 'color-mix(in srgb, #ffffff 76%, var(--iwb-layer))'], +] +const LIGHT_ANIM = [ + ['--iwb-anim', 'var(--iwb-brand)'], + ['--iwb-anim-track', 'color-mix(in srgb, var(--iwb-brand) 16%, transparent)'], + ['--iwb-anim-glow', 'color-mix(in srgb, var(--iwb-brand) 75%, transparent)'], + ['--iwb-anim-tint', 'color-mix(in srgb, var(--iwb-brand) 7%, transparent)'], + ['--iwb-anim-flow-a', 'color-mix(in srgb, var(--iwb-brand) 16%, var(--iwb-layer))'], + ['--iwb-anim-flow-b', 'color-mix(in srgb, var(--iwb-brand) 46%, var(--iwb-layer))'], +] + +const DARK_SHADOW = [ + ['--sh-sm', '0 2px 8px rgba(0, 0, 0, 0.35)'], + ['--sh-md', '0 6px 20px rgba(0, 0, 0, 0.45)'], + ['--sh-lg', '0 12px 32px rgba(0, 0, 0, 0.6)'], +] +const LIGHT_SHADOW = [ + ['--sh-sm', '0 2px 8px rgba(0, 0, 0, 0.12)', '@deprecated → M3 的 --iwb-elevation-1'], + ['--sh-md', '0 6px 20px rgba(0, 0, 0, 0.16)', null], + ['--sh-lg', '0 12px 32px rgba(0, 0, 0, 0.24)', null], +] + +function decl ([name, value, note]) { + return ' ' + name + ': ' + value + ';' + (note ? ' /* ' + note + ' */' : '') +} + +function colorDecls (tokens, only) { + return COLOR_ORDER + .filter((k) => !only || only.includes(k)) + .map((k) => decl([varName(k), tokens[k]])) +} + +function buildCss () { + const L = [] + L.push('/* 设计 token 静态快照 —— **生成文件,勿手改**') + L.push(' *') + L.push(' * 源:src/theme-tokens.js(derive() + 本文件生成器的 SCALE 表)') + L.push(' * 生成:node scripts/gen-tokens.js --write (校验:--check,已进 verify:ci)') + L.push(' *') + L.push(' * 为什么要有这一层:JS 未执行的那一帧没有内联变量,用它顶住,避免首屏闪白/跳变。') + L.push(' * 所以它必须逐值等于 derive() 的输出 —— 这条约束由 scripts/gen-tokens.js --check 强制。') + L.push(' * 结构层(chrome/home/dock/edge/hr)刻意用 color-mix 表达关系式:换主题时 :root 的声明会在') + L.push(' * html 元素上按新的 --iwb-bg/--iwb-canvas-bg 重新求值,天然跟随。') + L.push(' */') + L.push(':root {') + L.push(' /* —— 表面阶(亮色往白里 mix) —— */') + L.push(...colorDecls(LIGHT, ['bg', 'canvasBg', 'layer', 'layer2', 'panel', 'nodeBg', 'field'])) + L.push(' /* —— 结构层(公式与 derive 一致,见文件头) —— */') + L.push(...LIGHT_ONLY.map(decl)) + L.push(' /* —— 描边 / 文字三级(目标对比度见 IWBThemeTokens.RATIO) —— */') + L.push(...colorDecls(LIGHT, ['border', 'border2', 'text', 'text2', 'textDim', 'canvasText', 'nodeText', 'nodeText2', 'tint'])) + L.push(' /* —— 品牌色:填充与文字是两个角色(D7) —— */') + L.push(...colorDecls(LIGHT, ['brand', 'brandText', 'onBrand', 'gridDot'])) + L.push(' /* —— 插件表面四层(只提供取值,不做变量重指) —— */') + L.push(...colorDecls(LIGHT, ['pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover', 'surface', 'hover', 'menuBg'])) + L.push(' /* —— 语义色 —— */') + L.push(...colorDecls(LIGHT, ['err', 'ok', 'warn'])) + L.push(' /* —— 尺度层(L1)—— */') + L.push(...SCALE.map(decl)) + L.push(' /* —— 阴影 —— */') + L.push(...LIGHT_SHADOW.map(decl)) + L.push('}') + L.push('') + L.push('/* 暗色主题:html.theme-dark') + L.push(' * 同一套语义名,值来自 derive(DEFAULTS, true)。结构层不在这里重复定义 ——') + L.push(' * :root 的 color-mix 公式会在本元素上按新的 --iwb-bg/--iwb-canvas-bg 重新求值。 */') + L.push('.theme-dark {') + L.push(' /* 生图动效在暗色下必须「发亮」:--iwb-brand 是中性灰,直接用会糊在深底里 */') + L.push(...DARK_ANIM.map(decl)) + L.push(' /* —— 表面阶 —— */') + L.push(...colorDecls(DARK, ['bg', 'canvasBg', 'layer', 'layer2', 'panel', 'nodeBg', 'field', 'border', 'border2'])) + L.push(' /* —— 文字 / 品牌 —— */') + L.push(...colorDecls(DARK, ['text', 'text2', 'textDim', 'canvasText', 'nodeText', 'nodeText2', 'tint', 'brand', 'brandText', 'onBrand', 'gridDot'])) + L.push(' /* —— 插件表面 / 浮层 / 语义色 —— */') + L.push(...colorDecls(DARK, ['pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover', 'surface', 'hover', 'menuBg', 'err', 'ok', 'warn'])) + L.push(' /* —— 阴影 —— */') + L.push(...DARK_SHADOW.map(decl)) + L.push('}') + L.push('') + L.push('/* 亮色主题的生图动效 token:与暗色同结构,色取品牌色 */') + L.push(':root:not(.theme-dark) {') + L.push(...LIGHT_ANIM.map(decl)) + L.push('}') + L.push('') + return L.join('\n') +} + +/* ── DTCG 导出 ────────────────────────────────────────────── + * 解析生成好的 CSS(而不是再 derive 一遍):保证「导出的 JSON == 实际生效的 CSS」。 + * 关系式(color-mix / var)不是值,跳过 —— 它们在 Figma/Penpot 里没有对应概念。 */ +function parseBlocks (css) { + const out = {} + const re = /(:root:not\(\.theme-dark\)|:root|\.theme-dark)\s*\{([\s\S]*?)\n\}/g + let m + while ((m = re.exec(css))) { + const body = m[2].replace(/\/\*[\s\S]*?\*\//g, '') + const d = {} + const dre = /(--[a-z0-9-]+)\s*:\s*([^;]+);/g + let x + while ((x = dre.exec(body))) d[x[1]] = x[2].trim() + out[m[1]] = d + } + return out +} + +function inferType (value, name) { + if (/^#[0-9a-f]{3,8}$/i.test(value)) return 'color' + if (/^rgba?\(/i.test(value)) return 'color' + if (/^-?[\d.]+px$/.test(value)) return 'dimension' + if (/^-?[\d.]+m?s$/.test(value)) return 'duration' + if (/\d+px [\d.]+px/.test(value)) return 'shadow' + // 过渡是「时长 + 缓动」的复合值,DTCG 里是两种类型:时长给 $value,缓动放 $description + if (/^--tr-/.test(name) && /^[\d.]+m?s\s+\S+$/.test(value)) return 'duration' + return null +} + +// 变量名 → DTCG 分组 +function groupOf (name) { + const n = name.replace(/^--(?:iwb-)?/, '') + if (/^sp-/.test(n)) return 'space' + if (/^rd(-|$)/.test(n)) return 'radius' + if (/^fs-/.test(n)) return 'font.size' + if (/^tr-/.test(n)) return 'motion' + if (/^sh-/.test(n)) return 'shadow' + if (n === 'grid-dot') return 'color' + if (/^(grid-|nav-|node-radius|node-sel-w|edge-w|edge-dash)/.test(n)) return 'layout' + if (/^plugin-/.test(n)) return 'color.plugin' + return 'color' +} + +function dtcgKey (name) { + const n = name.replace(/^--(?:iwb-)?/, '') + if (/^sp-/.test(n)) return n.slice(3) + if (n === 'rd-sm') return 'sm' + if (n === 'rd') return 'base' + if (/^rd-/.test(n)) return n.slice(3) + if (/^fs-/.test(n)) return n.slice(3) + if (/^tr-/.test(n)) return n.slice(3) + if (/^sh-/.test(n)) return n.slice(3) + if (/^plugin-/.test(n)) return n.slice(7) + return n +} + +function put (root, groupPath, key, node) { + const parts = groupPath.split('.') + let cur = root + for (const p of parts) { + if (!cur[p]) cur[p] = {} + cur = cur[p] + } + cur[key] = node +} + +function buildJson (css) { + const blocks = parseBlocks(css) + const light = Object.assign({}, blocks[':root'] || {}, blocks[':root:not(.theme-dark)'] || {}) + const dark = blocks['.theme-dark'] || {} + const root = {} + const skipped = [] + for (const name of Object.keys(light)) { + const value = light[name] + const type = inferType(value, name) + if (!type) { skipped.push(name); continue } + let node + if (type === 'duration' && /^--tr-/.test(name)) { + const m = /^([\d.]+m?s)\s+(\S+)$/.exec(value) + node = { $value: m[1], $type: 'duration', $description: '缓动 ' + m[2] } + } else { + node = { $value: value, $type: type } + } + if (dark[name]) node.$extensions = { 'com.image-workbench.dark': { $value: dark[name], $type: type } } + put(root, groupOf(name), dtcgKey(name), node) + } + return { root, skipped } +} + +function build () { + const css = buildCss() + const { root, skipped } = buildJson(css) + const json = { + $description: 'Image Workbench 设计 token(由 src/theme-tokens.js 生成;暗色值在 $extensions.com.image-workbench.dark)', + $version: '3.26.44', + ...root, + } + return { css, json: JSON.stringify(json, null, 2) + '\n', skipped } +} + +function main () { + const write = process.argv.includes('--write') + const { css, json, skipped } = build() + + if (write) { + fs.mkdirSync(path.dirname(JSON_OUT), { recursive: true }) + fs.writeFileSync(CSS_OUT, css) + fs.writeFileSync(JSON_OUT, json) + console.log('✓ src/design-tokens.css(' + css.split('\n').length + ' 行)') + console.log('✓ dist/tokens.json(DTCG;跳过 ' + skipped.length + ' 个关系式:' + skipped.join(', ') + ')') + return + } + + const problems = [] + const cur = fs.existsSync(CSS_OUT) ? fs.readFileSync(CSS_OUT, 'utf8') : '' + if (cur !== css) { + // 逐行指出第一处差异,别让人对着两千行找 + const a = cur.split('\n') + const b = css.split('\n') + let i = 0 + while (i < a.length && i < b.length && a[i] === b[i]) i++ + problems.push('src/design-tokens.css 与 derive() 不一致(第 ' + (i + 1) + ' 行)\n' + + ' 文件: ' + JSON.stringify(a[i] || '(缺)') + '\n' + + ' 应为: ' + JSON.stringify(b[i] || '(多)') + '\n' + + ' → 跑 node scripts/gen-tokens.js --write') + } + if (problems.length) { + console.error('✗ ' + problems.join('\n')) + process.exit(1) + } + console.log('design-tokens: CSS 静态快照与 derive() 一致(' + css.split('\n').length + ' 行)') +} + +if (require.main === module) main() +module.exports = { build, buildCss, buildJson } diff --git a/scripts/token-audit.js b/scripts/token-audit.js index c6c829e..2b9101b 100644 --- a/scripts/token-audit.js +++ b/scripts/token-audit.js @@ -15,6 +15,10 @@ const ROOT = path.join(__dirname, '..') const read = (p) => { try { return fs.readFileSync(p, 'utf8') } catch (e) { return '' } } const css = read(path.join(ROOT, 'style.css')) +// M3 起 token 定义搬进生成的 src/design-tokens.css:变量清单要把两份合起来看, +// 否则「定义的变量」会突然变成 0(脚本比代码先过时,是最容易误导人的失败模式)。 +const tokensCss = read(path.join(ROOT, 'src', 'design-tokens.css')) +const allCss = css + '\n' + tokensCss const siteCss = read(path.join(ROOT, 'site', 'assets', 'style.css')) function walk (dir, out) { @@ -34,8 +38,8 @@ const uniqSorted = (arr, numeric) => Array.from(new Set(arr)).sort(numeric ? (a, /* ---------- 1. 变量:定义 / 引用 / 未定义 / 死变量 ---------- */ // 定义可以写在一行里的任意位置(--rd-sm: 6px; --rd: 8px; 这种),不能只认行首 -const defs = uniqSorted((css.match(/(?:^|[\s;{])--[a-z0-9-]+\s*:/gim) || []).map((s) => s.trim().replace(/^[;{]/, '').trim().replace(/\s*:$/, ''))) -const refs = uniqSorted((css.match(/var\(\s*--[a-z0-9-]+/gi) || []).map((s) => s.replace(/var\(\s*/, ''))) +const defs = uniqSorted((allCss.match(/(?:^|[\s;{])--[a-z0-9-]+\s*:/gim) || []).map((s) => s.trim().replace(/^[;{]/, '').trim().replace(/\s*:$/, ''))) +const refs = uniqSorted((allCss.match(/var\(\s*--[a-z0-9-]+/gi) || []).map((s) => s.replace(/var\(\s*/, ''))) // 运行时由 JS 写入的变量(app.js / canvas-*.js 里 setProperty),不算缺失 const RUNTIME_SET = new Set(['--iwb-dock-l', '--iwb-dock-r', '--iwb-fb-k', '--iwb-node-color']) const undefinedRefs = refs.filter((r) => !defs.includes(r) && !RUNTIME_SET.has(r)) @@ -45,7 +49,7 @@ for (const r of refs) usedCount[r] = count(new RegExp('var\\(\\s*' + r + '(?=[,) const deadTokens = defs.filter((d) => !usedCount[d] && !new RegExp('var\\(\\s*' + d + '\\s*[,)]').test(css)) /* ---------- 2. 命名空间:几套并存 ---------- */ -const ns = (re) => uniqSorted((css.match(re) || [])).length +const ns = (re) => uniqSorted((allCss.match(re) || [])).length const namespaces = { '--iwb-*(旧宿主)': ns(/--iwb-[a-z0-9-]+/g), '--sp-*(间距)': ns(/--sp-[a-z0-9-]+/g), @@ -107,6 +111,7 @@ const siteVars = uniqSorted((siteCss.match(/--[a-z0-9-]+:/gi) || []).map((s) => const report = { cssLines: css.split('\n').length, + tokenLines: tokensCss.split('\n').length, defs: defs.length, refs: refs.length, undefinedRefs, @@ -152,7 +157,7 @@ if (process.argv.includes('--contrast')) { * 之前这里硬编码着旧的「中灰底」值,脚本自己成了过时信息的来源 —— 现在从 .theme-dark 实时解析。 */ console.log('\n### 静态快照 vs derive()(暗色)—— 必须一致\n') console.log('| token | `.theme-dark` 静态 | derive 默认 | 一致 |\n|---|---|---|---|') - const darkBlock = (css.match(/\.theme-dark\s*\{([\s\S]*?)\n\}/) || [, ''])[1].replace(/\/\*[\s\S]*?\*\//g, '') + const darkBlock = (tokensCss.match(/\.theme-dark\s*\{([\s\S]*?)\n\}/) || [, ''])[1].replace(/\/\*[\s\S]*?\*\//g, '') const statDark = {} for (const m of darkBlock.matchAll(/--(?:iwb-)?([a-z0-9-]+)\s*:\s*(#[0-9a-fA-F]{6})\s*;/g)) statDark[m[1]] = m[2].toLowerCase() const d = T.derive(T.DEFAULTS, true) @@ -175,6 +180,7 @@ const pad = (s, n) => String(s) + ' '.repeat(Math.max(0, n - String(s).replace(/ console.log('## 1. 变量规模\n') console.log(`| 指标 | 数值 |\n|---|---|`) console.log(`| style.css 行数 | ${report.cssLines} |`) +console.log(`| design-tokens.css 行数(生成) | ${report.tokenLines} |`) console.log(`| 定义的 --* 变量 | ${defs.length} |`) console.log(`| 被 var() 引用的变量 | ${refs.length} |`) console.log(`| **引用了但从未定义** | **${undefinedRefs.length}** → ${undefinedRefs.join(' ')} |`) diff --git a/src/design-tokens.css b/src/design-tokens.css new file mode 100644 index 0000000..a9514af --- /dev/null +++ b/src/design-tokens.css @@ -0,0 +1,157 @@ +/* 设计 token 静态快照 —— **生成文件,勿手改** + * + * 源:src/theme-tokens.js(derive() + 本文件生成器的 SCALE 表) + * 生成:node scripts/gen-tokens.js --write (校验:--check,已进 verify:ci) + * + * 为什么要有这一层:JS 未执行的那一帧没有内联变量,用它顶住,避免首屏闪白/跳变。 + * 所以它必须逐值等于 derive() 的输出 —— 这条约束由 scripts/gen-tokens.js --check 强制。 + * 结构层(chrome/home/dock/edge/hr)刻意用 color-mix 表达关系式:换主题时 :root 的声明会在 + * html 元素上按新的 --iwb-bg/--iwb-canvas-bg 重新求值,天然跟随。 + */ +:root { + /* —— 表面阶(亮色往白里 mix) —— */ + --iwb-bg: #f7f7f7; + --iwb-canvas-bg: #c6c6c6; + --iwb-layer: #f5f5f5; + --iwb-layer2: #ffffff; + --iwb-panel: #fefefe; + --iwb-node-bg: #efefef; + --iwb-field: #ffffff; + /* —— 结构层(公式与 derive 一致,见文件头) —— */ + --iwb-chrome: color-mix(in srgb, var(--iwb-bg) 60%, var(--iwb-canvas-bg) 40%); /* 顶部 chrome 色:页面底与画布之间的过渡层(标签条用) */ + --iwb-home-bg: color-mix(in srgb, var(--iwb-bg) 35%, var(--iwb-canvas-bg) 65%); /* 首页底色:画布色与顶栏色之间取 35/65 */ + --iwb-hr: color-mix(in srgb, var(--iwb-border) 50%, var(--iwb-border2) 50%); /* 首页上的细分隔线 */ + --iwb-dock-bg: color-mix(in srgb, var(--iwb-chrome) 55%, var(--iwb-canvas-bg) 45%); /* 左右停靠栏底色:排进 bg→chrome→home→canvas 灰阶 */ + --iwb-dock-head-bg: color-mix(in srgb, var(--iwb-panel) 55%, var(--iwb-dock-bg)); /* 顶栏底色比停靠栏更浅一档 */ + --iwb-plugin-bg: var(--iwb-bg); /* v3.25.85 插件主题契约:插件只能用这组别名 */ + --iwb-plugin-panel: var(--iwb-panel); + --iwb-plugin-layer: var(--iwb-layer); + --iwb-plugin-border: var(--iwb-border); + --iwb-plugin-text: var(--iwb-text); + --iwb-plugin-text2: var(--iwb-text2); + --iwb-plugin-brand: var(--iwb-brand); + --iwb-plugin-on-brand: var(--iwb-on-brand); + --iwb-plugin-err: var(--iwb-err); + --iwb-plugin-ok: var(--iwb-ok); + --iwb-plugin-warn: var(--iwb-warn); + --iwb-edge: var(--iwb-border2); /* 连线默认色:跟随边框灰(主题色只用于选中/悬停/上游高亮) */ + --iwb-edge-hot: var(--iwb-brand); + --iwb-focus-ring: 2px solid color-mix(in srgb, var(--iwb-brand-text) 70%, transparent); /* 键盘焦点环(D8):颜色走 brand-text,暗色下 brand 画不出环 */ + --iwb-edge-w: 1.8px; /* 外观自定义:由 src/ui-appearance.js 按配置写入,这里只给兜底默认值 */ + --iwb-edge-dash: 4 3; + --iwb-node-radius: 14px; + --iwb-node-sel-w: 1px; + --iwb-grid-size: 1.4px; + --iwb-grid-gap: 22px; + --iwb-grid-on: 1; + --iwb-nav-w: 52px; /* 一级导航宽度:左停靠栏/工具栏让位都以它为基准 */ + /* —— 描边 / 文字三级(目标对比度见 IWBThemeTokens.RATIO) —— */ + --iwb-border: #c2c2c2; + --iwb-border2: #ababab; + --iwb-text: #404040; + --iwb-text2: #5c5c5c; + --iwb-text-dim: #767676; + --iwb-color-text-on-canvas: #404040; + --iwb-node-text: #404040; + --iwb-node-text2: #6b6b6b; + --iwb-tint: #404040; + /* —— 品牌色:填充与文字是两个角色(D7) —— */ + --iwb-brand: #6b7280; + --iwb-brand-text: #575d69; + --iwb-on-brand: #ffffff; + --iwb-grid-dot: #a6a6a6; + /* —— 插件表面四层(只提供取值,不做变量重指) —— */ + --iwb-surface: #fefefe; + --iwb-hover: #e9e9e9; + --iwb-bg-menu: #fefefe; + --iwb-plugin-surface: #f5f5f5; + --iwb-plugin-card: #fefefe; + --iwb-plugin-inset: #ebebeb; + --iwb-plugin-hover: #e2e2e2; + /* —— 语义色 —— */ + --iwb-err: #ef4444; + --iwb-ok: #22c55e; + --iwb-warn: #d97706; + /* —— 尺度层(L1)—— */ + --sp-1: 4px; /* @deprecated 全部 5 个 → M3 的 --iwb-space-4/8/12/16/24 */ + --sp-2: 8px; + --sp-3: 12px; + --sp-4: 16px; + --sp-5: 24px; + --rd-sm: 6px; /* @deprecated rd-sm/rd-md/rd-lg → M3 的 --iwb-radius-chip/control/popover */ + --rd: 8px; + --rd-md: 10px; + --rd-lg: 12px; + --fs-xs: 10px; /* @deprecated fs-xs/fs-md/fs-lg/fs-xl → M3 的 --iwb-font-*(fs-sm 仍被引用) */ + --fs-sm: 11px; + --fs-md: 12px; + --fs-lg: 13px; + --fs-xl: 14px; + --tr-fast: 0.1s ease-out; + --tr-base: 0.2s ease-out; /* @deprecated tr-base/tr-slow → M3 的 --iwb-motion-*(tr-fast 仍被引用) */ + --tr-slow: 0.3s ease-out; + /* —— 阴影 —— */ + --sh-sm: 0 2px 8px rgba(0, 0, 0, 0.12); /* @deprecated → M3 的 --iwb-elevation-1 */ + --sh-md: 0 6px 20px rgba(0, 0, 0, 0.16); + --sh-lg: 0 12px 32px rgba(0, 0, 0, 0.24); +} + +/* 暗色主题:html.theme-dark + * 同一套语义名,值来自 derive(DEFAULTS, true)。结构层不在这里重复定义 —— + * :root 的 color-mix 公式会在本元素上按新的 --iwb-bg/--iwb-canvas-bg 重新求值。 */ +.theme-dark { + /* 生图动效在暗色下必须「发亮」:--iwb-brand 是中性灰,直接用会糊在深底里 */ + --iwb-anim: #ffffff; + --iwb-anim-track: color-mix(in srgb, #ffffff 18%, transparent); + --iwb-anim-glow: rgba(255, 255, 255, 0.72); + --iwb-anim-tint: color-mix(in srgb, #ffffff 16%, transparent); + --iwb-anim-flow-a: color-mix(in srgb, #ffffff 26%, var(--iwb-layer)); + --iwb-anim-flow-b: color-mix(in srgb, #ffffff 76%, var(--iwb-layer)); + /* —— 表面阶 —— */ + --iwb-bg: #121212; + --iwb-canvas-bg: #313131; + --iwb-layer: #161616; + --iwb-layer2: #1c1c1c; + --iwb-panel: #1c1c1c; + --iwb-node-bg: #3d3d3d; + --iwb-field: #494949; + --iwb-border: #333333; + --iwb-border2: #4d4d4d; + /* —— 文字 / 品牌 —— */ + --iwb-text: #e3e3e3; + --iwb-text2: #bababa; + --iwb-text-dim: #989898; + --iwb-color-text-on-canvas: #b2b2b2; + --iwb-node-text: #e3e3e3; + --iwb-node-text2: #aaaaaa; + --iwb-tint: #e3e3e3; + --iwb-brand: #6b7280; + --iwb-brand-text: #b6bac0; + --iwb-on-brand: #ffffff; + --iwb-grid-dot: #525252; + /* —— 插件表面 / 浮层 / 语义色 —— */ + --iwb-surface: #1c1c1c; + --iwb-hover: #2a2a2a; + --iwb-bg-menu: #3c3c3c; + --iwb-plugin-surface: #232323; + --iwb-plugin-card: #2c2c2c; + --iwb-plugin-inset: #191919; + --iwb-plugin-hover: #383838; + --iwb-err: #f26666; + --iwb-ok: #4acf7b; + --iwb-warn: #e1953d; + /* —— 阴影 —— */ + --sh-sm: 0 2px 8px rgba(0, 0, 0, 0.35); + --sh-md: 0 6px 20px rgba(0, 0, 0, 0.45); + --sh-lg: 0 12px 32px rgba(0, 0, 0, 0.6); +} + +/* 亮色主题的生图动效 token:与暗色同结构,色取品牌色 */ +:root:not(.theme-dark) { + --iwb-anim: var(--iwb-brand); + --iwb-anim-track: color-mix(in srgb, var(--iwb-brand) 16%, transparent); + --iwb-anim-glow: color-mix(in srgb, var(--iwb-brand) 75%, transparent); + --iwb-anim-tint: color-mix(in srgb, var(--iwb-brand) 7%, transparent); + --iwb-anim-flow-a: color-mix(in srgb, var(--iwb-brand) 16%, var(--iwb-layer)); + --iwb-anim-flow-b: color-mix(in srgb, var(--iwb-brand) 46%, var(--iwb-layer)); +} diff --git a/style.css b/style.css index 4c92756..4334cba 100644 --- a/style.css +++ b/style.css @@ -1,175 +1,10 @@ /* 生图工作台 Image Workbench — 独立版样式(亮/暗主题手动切换,设置 → 通用 → 主题) - * 亮色配色:节点背景灰、内容区(输入框/标题栏/参数区)白,保证文字清晰阅读 */ -/* ⚠ 本段是设计 token 的「静态快照」:颜色值必须等于 IWBThemeTokens.derive(DEFAULTS, false) - * 的输出。JS 未执行的那一帧用它顶住,跑完由 derive() 写内联同名变量 —— 两者一致才不会 - * 首屏跳一下。改派生公式后请同步这里,tests/theme-tokens-test.js 会比对并报错。 - * 结构层(chrome/home/dock)刻意用 color-mix 表达:公式与 derive 一致,换主题时 - * :root 的声明会在 html 元素上按新的 --iwb-bg/--iwb-canvas-bg 重新求值,自动跟随。 */ -:root { - /* —— 表面阶(亮色往白里 mix) —— */ - --iwb-bg: #f7f7f7; - --iwb-canvas-bg: #c6c6c6; - /* --iwb-layer 是「内嵌层」:面板里的凹槽(任务条/引用块/输入框)。 - 两个主题里都必须比 --iwb-panel 暗 —— 旧实现暗色是「更亮」,同一个 token 表达相反层次(D6)。 */ - --iwb-layer: #f5f5f5; - --iwb-layer2: #ffffff; - --iwb-panel: #fefefe; - --iwb-node-bg: #efefef; - /* 卡片内的控件面(输入框 / 下拉 / 任务卡 / 输出组):亮色=白。 - 暗色不是白色,而是「比节点亮一档」的灰(见 theme-tokens 的 field)。 - 用途区别很重要:卡片**外面**的控件仍用 --iwb-layer / --iwb-panel(凹槽语义), - 卡片**里面**的控件必须用 --iwb-field,否则 #3d 的卡片里会出现 #16 的黑洞(D10)。 */ - --iwb-field: #ffffff; - /* 顶部 chrome 色:页面底与画布之间的过渡层(标签条用)。 - 比 --iwb-bg 深一号、比画布浅 → 顶部亮带贴画布的对比减弱(v3.1.0 顶栏配色); - 调深浅改下面 60/40 两个百分比即可(越往画布色靠,对比越弱) */ - --iwb-chrome: color-mix(in srgb, var(--iwb-bg) 60%, var(--iwb-canvas-bg) 40%); - /* 首页底色:左栏与右主区同色(v3.1.0 首页配色)→ v3.1.0-pre.26 提亮一档。 - 在画布色与顶栏色之间取 35/65 混色:比画布浅一点点、仍明显暗于顶栏 chrome(保持层次), - 白卡片/白搜索框在上面依旧突出。想再调深浅改下面 35/65 两个百分比即可 */ - --iwb-home-bg: color-mix(in srgb, var(--iwb-bg) 35%, var(--iwb-canvas-bg) 65%); - /* 首页(暗一档底)上的细分隔线:border 与 border2 混色, - 在 --iwb-home-bg 上保持与旧版(亮底+--iwb-border)相近的可见度,不隐形也不过重 */ - --iwb-hr: color-mix(in srgb, var(--iwb-border) 50%, var(--iwb-border2) 50%); - /* 左右停靠栏底色(v3.25.58):栏是「结构层」不是「浮层」,之前直接用 --iwb-panel(纯白), - 比页面底还亮 ⇒ 亮色下与画布 1.39:1、暗色下 2.04:1,两侧像亮度泄漏。 - 这里把它排进既有灰阶 ramp(bg → chrome → home → canvas):取 chrome 与画布的中段, - 与标签条同级、只比画布亮一点点;两主题都用同一公式(chrome/canvas 本身随主题变)。 */ - --iwb-dock-bg: color-mix(in srgb, var(--iwb-chrome) 55%, var(--iwb-canvas-bg) 45%); - /* v3.26.19:顶栏底色比停靠栏更浅一档(原来是压暗 6%,现在向面板色靠 55%) */ - --iwb-dock-head-bg: color-mix(in srgb, var(--iwb-panel) 55%, var(--iwb-dock-bg)); - --iwb-border: #c2c2c2; - --iwb-border2: #ababab; - /* 品牌色 v3.26.44:从「等于次要文字色」恢复为真正的强调色种子(D7)。 - 主按钮用它做底,--iwb-on-brand 按对比度自动取黑/白。 */ - --iwb-brand: #6b7280; - /* 品牌色的文字版:按对比度夹紧(暗色会明显亮于填充色) */ - --iwb-brand-text: #575d69; - --iwb-on-brand: #ffffff; - /* v3.25.85 插件主题契约:插件只能用这组别名(宿主负责让它们跟随主题/自动配色) */ - --iwb-plugin-bg: var(--iwb-bg); - --iwb-plugin-panel: var(--iwb-panel); - --iwb-plugin-layer: var(--iwb-layer); - --iwb-plugin-border: var(--iwb-border); - --iwb-plugin-text: var(--iwb-text); - --iwb-plugin-text2: var(--iwb-text2); - /* 插件面板四层(v3.25.96):只提供取值,不做任何变量重指(3.25.89 的事故不再重演) */ - --iwb-plugin-surface: #f5f5f5; - --iwb-plugin-card: #fefefe; - --iwb-plugin-inset: #ebebeb; - --iwb-plugin-hover: #e2e2e2; - --iwb-plugin-brand: var(--iwb-brand); - --iwb-plugin-on-brand: var(--iwb-on-brand); - --iwb-plugin-err: var(--iwb-err); - --iwb-plugin-ok: var(--iwb-ok); - --iwb-plugin-warn: var(--iwb-warn); - /* 连线默认色:跟随边框灰(亮/暗自适应)。主题色只用于选中/悬停/上下游高亮(v3.25.18 用户裁定) */ - --iwb-edge: var(--iwb-border2); - /* 外观自定义(v3.25.22):以下变量都由 src/ui-appearance.js 按配置写入,这里只给兜底默认值 */ - --iwb-edge-w: 1.8px; - --iwb-edge-dash: 4 3; - --iwb-edge-hot: var(--iwb-brand); - --iwb-node-radius: 14px; - --iwb-node-sel-w: 1px; - --iwb-grid-dot: #a6a6a6; - --iwb-grid-size: 1.4px; - --iwb-grid-gap: 22px; - --iwb-grid-on: 1; - /* 一级导航宽度:左停靠栏/工具栏让位都以它为基准(v3.25.59) */ - --iwb-nav-w: 52px; - /* —— 文字三级(v3.26.44 重定):目标对比度见 IWBThemeTokens.RATIO - text 7:1(AAA 正文)/ text2 4.5:1(AA 次要)/ text-dim 3:1(辅助、占位) - 旧值只对 --iwb-bg 夹紧,暗色下正文在 layer/nodeBg/pluginCard 上只有 3.9–4.4(D3/D4/D5) */ - --iwb-text: #404040; - --iwb-text2: #5c5c5c; - --iwb-text-dim: #767676; - /* 画布上的文字专用色:节点标题画在画布上(透明底)再叠 opacity .8, - 复用 text2 实测只有 1.9:1。它只对 --iwb-canvas-bg 负责,不参与界面文字的灰阶 */ - --iwb-color-text-on-canvas: #404040; - /* v3.26.42 外观 v2:节点文字与界面文字分家;tint 是「半透明文字底色」的基色(不随调字变化) */ - --iwb-node-text: #404040; - --iwb-node-text2: #6b6b6b; - --iwb-tint: #404040; - /* 插件历史变量补洞:此前未定义,插件只能吃硬编码兜底(暗色下会突然白底) - @deprecated --iwb-surface:全仓零引用。要用「卡片底」请用 --iwb-plugin-card / --iwb-layer; - M3 起用 --iwb-bg-inset。保留仅为不打断未来引用。 */ - --iwb-surface: #fefefe; - --iwb-hover: #e9e9e9; - /* 浮层底:亮色=面板色,暗色会比面板亮一档(见 theme-tokens 的 menuBg) */ - --iwb-bg-menu: #fefefe; - /* 键盘焦点环(v3.26.44 / D8):此前只有三处各自发明(brand 60% / tint 35% / tint 30%), - 其余可聚焦元素完全没有焦点反馈。统一成一个 token,颜色走 brand-text —— - 暗色下 brand 是填充色偏暗,画不出环。 */ - --iwb-focus-ring: 2px solid color-mix(in srgb, var(--iwb-brand-text) 70%, transparent); - --iwb-err: #ef4444; - --iwb-ok: #22c55e; - --iwb-warn: #d97706; - /* —— 设计 token 第一代(v3.1.0-stability2 起):间距 / 圆角 / 字号 / 阴影 / 过渡 —— - 2026-11-23 审计结论:这套尺度 token **采用率约 0.5%**(`--sp-*` 被引用 0 次,而间距写死 644 处)。 - 下面标 @deprecated 的 15 个名字当前无任何 `var()` 消费者,保留只为不打断外部引用; - 由 docs/plans/design-tokens-rollout.md 的 M3(`design-tokens.css` 的 L1/L2)取代, - **新代码不得再引用带 @deprecated 的名字**,M4 迁移结束后统一删除。 - (`--sp-*` / `--rd-sm|md|lg` / `--fs-xs|md|lg|xl` / `--sh-sm` / `--tr-base|tr-slow`) - 仍在使用的 `--rd`(8px) / `--fs-sm`(11px) / `--sh-md|lg` / `--tr-fast` 不在此列。 */ - --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px; --sp-5: 24px; /* @deprecated 全部 5 个 → M3 的 --iwb-space-4/8/12/16/24 */ - --rd-sm: 6px; --rd: 8px; --rd-md: 10px; --rd-lg: 12px; /* @deprecated rd-sm/rd-md/rd-lg → M3 的 --iwb-radius-chip/control/popover */ - --fs-xs: 10px; --fs-sm: 11px; --fs-md: 12px; --fs-lg: 13px; --fs-xl: 14px; /* @deprecated fs-xs/fs-md/fs-lg/fs-xl → M3 的 --iwb-font-*(fs-sm 仍被引用) */ - --sh-sm: 0 2px 8px rgba(0, 0, 0, 0.12); /* @deprecated → M3 的 --iwb-elevation-1 */ - --sh-md: 0 6px 20px rgba(0, 0, 0, 0.16); - --sh-lg: 0 12px 32px rgba(0, 0, 0, 0.24); - --tr-fast: 0.1s ease-out; --tr-base: 0.2s ease-out; --tr-slow: 0.3s ease-out; /* @deprecated tr-base/tr-slow → M3 的 --iwb-motion-*(tr-fast 仍被引用) */ -} -/* 暗色主题:html.theme-dark(设置 → 通用 → 主题 切换;不再跟随系统) - * ⚠ 同样是静态快照:这些值必须等于 IWBThemeTokens.derive(DEFAULTS, true) 的输出。 - * 旧版这里是「中灰底(#757575)+ 深字(#111)」的独立设计,与 derive 的「近黑底 + 浅字」 - * 同名不同值 —— 切主题等于换了个产品(D2)。现在两者统一为同一套派生结果。 - * 结构层(chrome/home/dock)不在这里重复定义::root 的 color-mix 公式会在本元素上 - * 按新的 --iwb-bg/--iwb-canvas-bg 重新求值,天然跟随。 */ -.theme-dark { - /* 生图动效在暗色下必须"发亮":--iwb-brand 是中性灰,直接用它会让进度线/旋转环/流动渐变 - 都糊在深底里。这里统一用「亮色元素 + 深色轨道」靠明暗反差获得清晰度。 */ - --iwb-anim: #ffffff; - --iwb-anim-track: color-mix(in srgb, #ffffff 18%, transparent); - --iwb-anim-glow: rgba(255, 255, 255, 0.72); - --iwb-anim-tint: color-mix(in srgb, #ffffff 16%, transparent); - /* 旧值是「往 layer 里混 26% 近黑」,layer 从 #9c9c9c 变成 #161616 后,混出来几乎等于底色 - ⇒ 流动渐变左端不可见。改成从「比层亮 26%」到「比层亮 76%」,保持深→浅的流动方向。 */ - --iwb-anim-flow-a: color-mix(in srgb, #ffffff 26%, var(--iwb-layer)); - --iwb-anim-flow-b: color-mix(in srgb, #ffffff 76%, var(--iwb-layer)); - --iwb-bg: #121212; - --iwb-canvas-bg: #313131; - --iwb-layer: #161616; - --iwb-layer2: #1c1c1c; - --iwb-panel: #1c1c1c; - --iwb-node-bg: #3d3d3d; - --iwb-field: #494949; - --iwb-border: #333333; - --iwb-border2: #4d4d4d; - --iwb-brand: #6b7280; - --iwb-brand-text: #b6bac0; - --iwb-text: #e3e3e3; - --iwb-text2: #bababa; - --iwb-text-dim: #989898; - --iwb-color-text-on-canvas: #b2b2b2; - --iwb-node-text: #e3e3e3; - --iwb-node-text2: #aaaaaa; - --iwb-tint: #e3e3e3; - --iwb-surface: #1c1c1c; - --iwb-hover: #2a2a2a; - --iwb-bg-menu: #3c3c3c; - --iwb-grid-dot: #525252; - --iwb-plugin-surface: #232323; - --iwb-plugin-card: #2c2c2c; - --iwb-plugin-inset: #191919; - --iwb-plugin-hover: #383838; - --iwb-err: #f26666; - --iwb-ok: #4acf7b; - --iwb-warn: #e1953d; - --iwb-on-brand: #ffffff; - --sh-sm: 0 2px 8px rgba(0, 0, 0, 0.35); - --sh-md: 0 6px 20px rgba(0, 0, 0, 0.45); - --sh-lg: 0 12px 32px rgba(0, 0, 0, 0.6); -} + * 亮色配色:节点背景灰、内容区(输入框/标题栏/参数区)白,保证文字清晰阅读 + * + * ⚠ 设计 token(颜色 / 间距 / 圆角 / 字号 / 阴影 / 动效)**不在这里**: + * 它们由 src/theme-tokens.js 生成到 src/design-tokens.css,构建时内联在本文件前面。 + * 本文件只写组件规则:颜色/间距一律引用 token 变量,不要再写死色值(M5 起由 token-lint 拦)。 + */ * { box-sizing: border-box; } html, body { margin: 0; height: 100%; } @@ -191,15 +26,7 @@ button { font-family: inherit; } .iwb-canvas.iwb-tool-pan:active { cursor: grabbing; } .iwb-grid { position: absolute; left: -50%; top: -50%; width: 200%; height: 200%; pointer-events: none; will-change: transform; background-repeat: repeat; background-image: radial-gradient(circle, var(--iwb-grid-dot) var(--iwb-grid-size), transparent calc(var(--iwb-grid-size) + 0.4px)); background-size: var(--iwb-grid-gap) var(--iwb-grid-gap); background-repeat: repeat; opacity: calc(0.85 * var(--iwb-grid-on)); } /* 亮色主题:网格点更浅,降低与背景对比度(暗色主题不受影响) */ -/* 亮色主题的生图动效 token:与改动前逐值一致,保证亮色观感零变化 */ -:root:not(.theme-dark) { - --iwb-anim: var(--iwb-brand); - --iwb-anim-track: color-mix(in srgb, var(--iwb-brand) 16%, transparent); - --iwb-anim-glow: color-mix(in srgb, var(--iwb-brand) 75%, transparent); - --iwb-anim-tint: color-mix(in srgb, var(--iwb-brand) 7%, transparent); - --iwb-anim-flow-a: color-mix(in srgb, var(--iwb-brand) 16%, var(--iwb-layer)); - --iwb-anim-flow-b: color-mix(in srgb, var(--iwb-brand) 46%, var(--iwb-layer)); -} + .iwb-world { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; } .iwb-edges { position: absolute; left: 0; top: 0; pointer-events: none; } .iwb-edge-hit { fill: none; stroke: transparent; stroke-width: 14; pointer-events: stroke; cursor: pointer; } diff --git a/tests/appearance-v2-test.js b/tests/appearance-v2-test.js index 21b6cbc..eac01ee 100644 --- a/tests/appearance-v2-test.js +++ b/tests/appearance-v2-test.js @@ -115,7 +115,10 @@ for (const dark of [false, true]) { } // ⑨ 插件兼容:变量名只增不改(改名 = 插件落到硬编码兜底,暗色下会白底白字) -const css = fs.readFileSync(path.join(ROOT, 'style.css'), 'utf8') +// M3 起 token 定义搬进生成的 src/design-tokens.css(build 时内联在 style.css 之前): +// 契约检查要连起来看,否则「插件能引用的变量」会误判为缺失 +const css = fs.readFileSync(path.join(ROOT, 'style.css'), 'utf8') + '\n' + + fs.readFileSync(path.join(ROOT, 'src', 'design-tokens.css'), 'utf8') for (const v of [ '--iwb-bg', '--iwb-panel', '--iwb-layer', '--iwb-layer2', '--iwb-border', '--iwb-border2', '--iwb-text', '--iwb-text2', '--iwb-text-dim', '--iwb-brand', '--iwb-on-brand', '--iwb-warn', diff --git a/tests/page-chrome-regression-test.js b/tests/page-chrome-regression-test.js index 5d5c5cb..9aac97d 100644 --- a/tests/page-chrome-regression-test.js +++ b/tests/page-chrome-regression-test.js @@ -13,7 +13,9 @@ const source = uiAllSrc() // 「不该出现的视图里不出现」(如首页不应有 .iwb-canvas-topbar / .iwb-fullmask)。 // 被替换的 class:iwb-home-side / iwb-home-panel / iwb-canvas-topbar / iwb-fullmask / // iwb-res-tab / iwb-home-card-title / iwb-home-card-foot / iwb-canvas-meta -const css = fs.readFileSync(path.join(__dirname, '..', 'style.css'), 'utf8') +// M3 起 token(:root / .theme-dark)在生成的 src/design-tokens.css 里,组件规则仍在 style.css +const css = fs.readFileSync(path.join(__dirname, '..', 'style.css'), 'utf8') + '\n' + + fs.readFileSync(path.join(__dirname, '..', 'src', 'design-tokens.css'), 'utf8') assert.ok(source.includes("lsRawGet('iwb_shell_view'")) // 2026-11-06 存储收敛:改为统一 helper 入口 assert.ok(source.includes("panel: ''")) assert.ok(source.includes("const [enteringWs, setEnteringWs] = useState(false)")) diff --git a/tests/theme-static-snapshot-test.js b/tests/theme-static-snapshot-test.js index 3a0f57d..2d94068 100644 --- a/tests/theme-static-snapshot-test.js +++ b/tests/theme-static-snapshot-test.js @@ -1,6 +1,6 @@ // CSS 静态 token 必须等于 IWBThemeTokens.derive() 的输出(v3.26.44,M2/D2 守卫) // -// 背景:style.css 的 :root / .theme-dark 是设计 token 的「静态快照」,用途是顶住 JS 执行前 +// 背景:design-tokens.css 的 :root / .theme-dark 是设计 token 的「静态快照」,用途是顶住 JS 执行前 // 那一帧。历史上这两段是**另一套独立设计**(暗色是中灰底 #757575 + 深字 #111),与 derive // 的「近黑底 + 浅字」同名不同值 —— 结果切主题像换了个产品,而且首屏会闪一下再跳变。 // @@ -11,7 +11,8 @@ const fs = require('fs') const path = require('path') const ROOT = path.resolve(__dirname, '..') -const css = fs.readFileSync(path.join(ROOT, 'style.css'), 'utf8') +// M3 起 token 从 style.css 搬到生成的 src/design-tokens.css(构建时内联到 style.css 前面) +const css = fs.readFileSync(path.join(ROOT, 'src/design-tokens.css'), 'utf8') // 载入 theme-tokens(它是 UMD,挂到传入的对象上) const sandbox = {}