Files
image-workbench-desktop/scripts/gen-tokens.js
T
qiqi 04cc527002
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m27s
feat(tokens): 新增 --iwb-accent-on-surface / --iwb-accent-tint 并接入生成链
「强调色当文字用」以前各插件各自混色,暗色下对比度不足。现在固化成两个语义 token:

- --iwb-accent-on-surface = color-mix(--iwb-brand 45%, --iwb-text):文字/图标用的安全强调色
- --iwb-accent-tint = color-mix(--iwb-brand 18%, transparent):选中标签/药丸的淡底
  两者都是关系式(跟随主题与自定义强调色),不写死 hex

生成链:scripts/gen-tokens.js 的 LIGHT_ONLY 表 → src/design-tokens.css / dist/tokens.json
(npm run tokens:write 重生成,tokens:check 守卫)

落地:
- plugins/promptlib 1.14.7:参考图药丸改用这两个 token
- style.css:质量档位选中态、右键菜单 hover 底改用这两个 token(顺手去掉两处散落的 12% mix)
- docs/standards/design-tokens.md 新增 §3.9:取值、成对用法、四条规则(填充用 brand / 文字用 accent-on-surface 等)
2026-11-28 20:48:09 +08:00

597 lines
32 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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', 'selSurface', 'selRing',
'text', 'text2', 'textDim', 'canvasText', 'nodeText', 'nodeText2', 'tint',
'brand', 'brandText', 'onBrand', 'edge', 'edgeHot', 'gridDot',
'surface', 'hover', 'menuBg',
'pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover',
'err', 'ok', 'warn',
'errBg', 'errBd', 'okBg', 'okBd', 'warnBg', 'warnBd',
]
/* 尺度层(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],
/* ── M4 公共接口(2026-11-23)─────────────────────────────────────────
* 上面那批平铺名(`--sp-1` 这类、rd / fs 同)是 legacy,长期保留给插件与既有代码。
* 新代码只用下面这组**语义名**:改一档就全局一致,不用再猜「这处 12px 是间距还是字号」。
*/
/* 命名用**像素值**而不是序号:`--iwb-space-12` 一眼知道是 12px,`--iwb-space-3` 得背。
* 14 档与规范 §3.2 一致;不加 L2 语义别名层(gap-*),避免每个尺寸多一个没人用的名字。 */
['--iwb-space-2', '2px', null],
['--iwb-space-4', '4px', null],
['--iwb-space-6', '6px', null],
['--iwb-space-8', '8px', null],
['--iwb-space-10', '10px', null],
['--iwb-space-12', '12px', null],
['--iwb-space-16', '16px', null],
['--iwb-space-20', '20px', null],
['--iwb-space-24', '24px', null],
['--iwb-space-32', '32px', null],
['--iwb-space-40', '40px', null],
['--iwb-space-48', '48px', null],
['--iwb-space-64', '64px', null],
['--iwb-radius-chip', '6px', 'M4 圆角:胶囊/标签'],
['--iwb-radius-control', '8px', '输入框/按钮'],
['--iwb-radius-card', '10px', '卡片/分组'],
['--iwb-radius-panel', '12px', '面板/工具栏'],
['--iwb-radius-popover', '14px', '浮层/菜单/右键'],
['--iwb-radius-full', '999px', '圆形/药丸'],
['--iwb-font-2xs', '10px', '极小标注:徽标角标/画布坐标/缩略图角标(2026-11-24 承认它,此前 29 处裸写)'],
['--iwb-font-xs', '11px', 'M4 字号:状态栏/徽标'],
['--iwb-font-sm', '12px', '标签/次要正文'],
['--iwb-font-md', '13px', '紧凑正文/工具栏标题'],
// 14px 是浏览器默认正文字号,也是全站第四高频(M6 实测 10 处:表单行标题、命令输入、
// 帮助正文、尺寸数字)。闭集原本 13 → 15 跨 2px,中间有洞,凡是「标准正文」都无处安放,
// 只能就近塞进 13(偏小)或 15(偏大)。补这一档而不是继续塞,是因为它不是一处特例。
['--iwb-font-base', '14px', '标准正文:表单行标题/命令输入/帮助正文'],
['--iwb-font-lg', '15px', '小标题/节点标题'],
['--iwb-font-xl', '17px', '面板标题'],
['--iwb-font-2xl', '20px', '页面大标题'],
['--iwb-font-display', '24px', '展示级标题:页面/视图主标题(≤5 处)'],
['--iwb-chrome-h', '36px', '顶部标签条高度:tabstrip / dockarea / home-panel 三处共用(原来各写一遍 36px)'],
['--iwb-tab-h', '28px', '标签高度'],
['--iwb-tab-radius', '7px', '标签圆角'],
['--iwb-control-h', '30px', '表单控件高度:同一行里的输入框与默认按钮必须等高(设置页实测曾是 33 vs 24)'],
['--iwb-bw', '1px', '描边宽度(2026-11-24):常规分隔/控件边框'],
['--iwb-bw-strong', '2px', '强调描边:选中/焦点/危险操作(不要再用 2px 裸值)'],
['--iwb-icon-sm', '14px', '小图标:行内/按钮内'],
['--iwb-icon-md', '16px', '标准图标:工具栏/菜单'],
['--iwb-icon-lg', '18px', '大图标:导航/空态'],
['--iwb-opacity-disabled', '0.45', '禁用态透明度(此前 0.45/0.55 混用)'],
['--iwb-opacity-muted', '0.7', '次要/未激活元素透明度'],
['--iwb-ease-out', 'cubic-bezier(0.16, 1, 0.3, 1)', '入场/展开缓动:快出慢停'],
['--iwb-ease-in-out', 'cubic-bezier(0.4, 0, 0.2, 1)', '状态切换缓动'],
['--iwb-motion-fast', '0.12s', 'M6 动效:悬停/开关/小位移'],
['--iwb-motion-base', '0.15s', 'M6 动效:面板展开/尺寸变化'],
['--iwb-motion-slow', '0.3s', 'M6 动效:整屏过渡/抽屉'],
/* ── L2 悬停动效(2026-11-26)───────────────────────────────────────
* 图库卡片 / 输出节点结果图共用同一组值:改一档就两处一起变。
* scale 只给 1.02(卡片够大,再大就压到相邻列);相册格子是固定正方形网格,
* 单独用 --iwb-hover-scale 之外的 1.08,且**不位移**(位移会让相邻格抖动,见 album 插件)。 */
['--iwb-motion-hover', '0.15s', 'L2 悬停:卡片/结果图浮层统一时长'],
['--iwb-ease-hover', 'cubic-bezier(0.16, 1, 0.3, 1)', 'L2 悬停:快出慢停'],
['--iwb-hover-lift', '-2px', 'L2 悬停:上浮位移'],
['--iwb-hover-scale', '1.02', 'L2 悬停:缩放(相册格子单独用 1.08)'],
['--iwb-hover-shadow', '0 6px 16px rgba(0, 0, 0, 0.35)', 'L2 悬停:投影(单值,供 filter: drop-shadow 用;box-shadow 会多一种写法)'],
['--iwb-hover-border', 'var(--iwb-brand)', 'L2 悬停:描边色'],
['--iwb-scrim', 'rgba(0, 0, 0, 0.62)', '图片上的遮罩渐变底:下载浮层/预览条(两套主题都用深色,保证白字可读)'],
/* ── 选中态语义层(2026-11-26)──────────────────────────────────────
* 图库/节点/卡片的「已选中」统一用这组;默认全部跟随强调色。
* 以后若要让图库单独一套颜色,只改这里(或在设置里覆盖),不用再翻各处 var(--iwb-brand)。 */
['--iwb-selection-ring', 'var(--iwb-brand)', '选中描边(跟随强调色)'],
['--iwb-selection-on', 'var(--iwb-on-brand)', '选中描边的隔离内环(品牌底上的文字色)'],
['--iwb-selection-bg', 'color-mix(in srgb, var(--iwb-brand) 12%, transparent)', '选中格淡底(不压黑缩略图)'],
['--iwb-selection-halo', 'color-mix(in srgb, var(--iwb-brand) 45%, transparent)', '选中外发光(与 hover 区分)'],
/* ── 层级表(M4-B2)───────────────────────────────────────────────
* 迁移前 z-index 有 42 种取值,其中「谁盖谁」全靠散落在各处的数字对撞,出过两次真 bug:
* ① toast(200) 低于弹窗(300) → 弹窗里点保存看不到提示;
* ② 资源中心的右键菜单(91) 被全窗口面板(260) 盖住 → 只能挪到 270 打补丁。
* 现在数值只在这张表里写,组件引名字。三条关系必须成立,改数字前先读这三条:
* A. ctx-menu(91) < sheet(260) < modal(300):层越上,数字越大,不许交叉;
* B. toast(500) > modal(300):提示条永远在最上面,否则「操作成功」等于没提示;
* C. sheet-menu(270) 是「容器内 portal 菜单」,> sheet 且 < modal(它是面板的子菜单,不该越过弹窗)。
* 高段位(3200/3300/4000)保留历史数值:1000 段还有未迁移的 home-menu(1000/1001),
* 贸然压缩会和它交错,等 M5 的 token-lint 把裸 z-index 清零后再统一。
*/
['--iwb-z-ctx', '90', '右键遮罩'],
['--iwb-z-ctx-menu', '91', '右键菜单'],
['--iwb-z-curtain', '100', '应用遮罩 / 图片预览底'],
['--iwb-z-command', '120', '命令面板(⌘K)'],
['--iwb-z-sheet', '260', '全窗口面板(资源中心)'],
['--iwb-z-sheet-menu', '270', '面板内 portal 菜单:> sheet、< modal'],
['--iwb-z-modal', '300', '弹窗 + 遮罩'],
['--iwb-z-modal-menu', '310', '侧栏/弹窗里挂 body 的菜单'],
['--iwb-z-splash', '400', '启动页'],
['--iwb-z-toast', '500', '提示条:必须高于弹窗'],
['--iwb-z-preview', '800', '图片预览(全屏)'],
['--iwb-z-anchor', '3200', '锚定浮层:挂 body 的面板/首页菜单'],
['--iwb-z-palette', '3300', '背景色板 host(含锚定浮层)'],
['--iwb-z-alert', '4000', '确认框 / 崩溃屏(最后一道)'],
]
/* 只在亮色出现的结构/契约声明(暗色由 :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-accent-on-surface', 'color-mix(in srgb, var(--iwb-brand) 45%, var(--iwb-text))', '强调色当文字/图标用:与正文色混,亮色更沉、暗色更亮,两端过 4.5:1'],
['--iwb-accent-tint', 'color-mix(in srgb, var(--iwb-brand) 18%, transparent)', '强调色淡底:选中标签/药丸的底(与 --iwb-accent-on-surface 配对使用)'],
['--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', '一级导航宽度:左停靠栏/工具栏让位都以它为基准'],
]
/* ── L3 组件作用域(2026-11-24,M6)───────────────────────────────
* 为什么放这里:这 30 个 token 原先手写在 style.css 的 `.iwb-settings-modal` / `.iwb-pop`
* 作用域里 —— 文档有表格、代码有值,但没有单一源、也没有任何断言,已经漂过一次
* (--iwb-pop-w 从 328 被改到 360,文档同步晚了一步)。
* 现在值只在这张表里写,生成进 design-tokens.css;style.css 只留布局属性。
* 一致性断言:tests/design-tokens-component-doc-test.js(文档表格 ↔ tokens.json ↔ CSS)。
*/
const COMPONENT_SCOPED = [
{
selector: '.iwb-settings-modal',
group: 'component.settings',
vars: [
['--iwb-set-label-w', '180px', '标签列宽'],
['--iwb-set-col-gap', 'var(--iwb-space-16)', '标签列与控件列间距'],
['--iwb-set-row-h', '40px', '设置行高'],
['--iwb-set-row-pad-y', 'var(--iwb-space-8)', '行上下内边距'],
['--iwb-set-group-gap', 'var(--iwb-space-32)', '分组之间(规范 §3.11:32px)'],
['--iwb-set-title-gap', 'var(--iwb-space-8)', '分组标题与分组体间距'],
['--iwb-set-group-bg', 'hsl(0 0% 97.6%)', '分组体底色:设计值 #F9F9F9(用 hsl 表达以过 hex 闸门)'],
['--iwb-set-group-radius', 'var(--iwb-radius-card)', '分组体圆角'],
['--iwb-set-group-pad-x', 'var(--iwb-space-20)', '分组体左右内边距'],
['--iwb-set-group-pad-y', 'var(--iwb-space-12)', '分组体上下内边距'],
['--iwb-set-divider', 'color-mix(in srgb, var(--iwb-border) 55%, transparent)', '行分隔线'],
['--iwb-set-hint-color', 'var(--iwb-text-dim)', '说明文字色'],
['--iwb-set-hint-size', 'var(--iwb-font-xs)', '说明文字字号'],
['--iwb-set-hint-gap', 'var(--iwb-space-2)', '说明文字上间距'],
['--iwb-set-input-border', 'color-mix(in srgb, var(--iwb-border) 40%, var(--iwb-panel))', '输入框描边'],
['--iwb-set-check-size', '16px', '复选项尺寸'],
['--iwb-set-check-radius', '4px', '复选项圆角'],
['--iwb-set-btn-gap', 'var(--iwb-space-8)', '操作按钮间距'],
],
dark: [
['--iwb-set-group-bg', 'color-mix(in srgb, var(--iwb-text) 8%, var(--iwb-panel))', '暗色分组体:文字色 8% 混面板'],
],
},
{
selector: '.iwb-pop',
group: 'component.popover',
vars: [
['--iwb-pop-w', '360px', '面板宽度(窄屏靠 max-width 兜底)'],
['--iwb-pop-pad-y', 'var(--iwb-space-10)', '面板上下内边距'],
['--iwb-pop-pad-x', 'var(--iwb-space-12)', '面板左右内边距'],
['--iwb-pop-gap', 'var(--iwb-space-8)', '分组之间'],
['--iwb-pop-radius', 'var(--iwb-radius-popover)', '面板圆角'],
['--iwb-pop-title-size', 'var(--iwb-font-base)', '面板标题字号'],
['--iwb-pop-chip-h', '28px', '选择项/下拉/数字框高度'],
['--iwb-pop-chip-pad-x', 'var(--iwb-space-10)', '选择项左右内边距'],
['--iwb-pop-chip-radius', '6px', '选择项圆角'],
['--iwb-pop-chip-gap', 'var(--iwb-space-6)', '选择项间距'],
['--iwb-pop-chip-size', 'var(--iwb-font-md)', '选择项字号'],
],
dark: [],
},
]
/* 暗色独有的动效:亮色用品牌色,暗色必须用「亮色元素 + 深色轨道」 */
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)'],
/* 暗色下阴影本来就画不出形(底已经近黑),必须靠「更大的不透明度 + 更大的扩散」压出层次差 */
['--iwb-elevation-1', '0 2px 8px rgba(0, 0, 0, 0.40)'],
['--iwb-elevation-2', '0 8px 28px rgba(0, 0, 0, 0.55), 0 1px 3px rgba(0, 0, 0, 0.40)'],
['--iwb-elevation-3', '0 18px 56px rgba(0, 0, 0, 0.62)'],
['--iwb-elevation-4', '0 24px 64px rgba(0, 0, 0, 0.70)'],
]
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],
/* ── 阴影四档(M4-B2)────────────────────────────────────────────
* 迁移前浮层有 8 种写法(52 种全局),同一层的两个菜单阴影不一样 —— 层深不再表达「离内容多远」。
* 现在按「离内容多远」分四档,一档一个值:
* 1 贴面(浮动条/状态栏/HUD)· 2 浮窗/菜单/toast · 3 命令面板/锚定大浮层 · 4 弹窗/全窗口面板
* 只改形,不改色:颜色仍是中性黑(暗色另有覆盖)。
*/
['--iwb-elevation-1', '0 2px 8px rgba(0, 0, 0, 0.12)', '贴面浮层:浮动条 / 状态栏'],
['--iwb-elevation-2', '0 10px 30px rgba(0, 0, 0, 0.18), 0 1px 3px rgba(0, 0, 0, 0.10)', '浮窗 / 菜单 / toast'],
['--iwb-elevation-3', '0 18px 56px rgba(0, 0, 0, 0.26)', '命令面板 / 锚定大浮层'],
['--iwb-elevation-4', '0 24px 64px rgba(0, 0, 0, 0.30)', '弹窗 / 全窗口面板'],
]
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', 'selSurface', 'selRing', '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(...colorDecls(LIGHT, ['errBg', 'errBd', 'okBg', 'okBd', 'warnBg', 'warnBd']))
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', 'selSurface', 'selRing']))
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('')
/* —— L3 组件作用域(见 COMPONENT_SCOPED) —— */
for (const c of COMPONENT_SCOPED) {
L.push('')
L.push('/* ' + c.group + ':由 gen-tokens 生成,勿手改;布局属性仍在 style.css */')
L.push(c.selector + ' {')
for (const [name, value, desc] of c.vars) L.push(' ' + name + ': ' + value + ';' + (desc ? ' /* ' + desc + ' */' : ''))
L.push('}')
if (c.dark && c.dark.length) {
L.push('')
L.push('/* ' + c.group + ' · 暗色覆盖 */')
L.push('.theme-dark ' + c.selector + ' {')
for (const [name, value, desc] of c.dark) L.push(' ' + name + ': ' + value + ';' + (desc ? ' /* ' + desc + ' */' : ''))
L.push('}')
}
}
return L.join('\n')
}
/* ── DTCG 导出 ──────────────────────────────────────────────
* 解析生成好的 CSS(而不是再 derive 一遍):保证「导出的 JSON == 实际生效的 CSS」。
* 关系式(color-mix / var)不是值,跳过 —— 它们在 Figma/Penpot 里没有对应概念。 */
function parseDeclBody (body) {
const d = {}
const dre = /(--[a-z0-9-]+)\s*:\s*([^;]+);/g
let x
while ((x = dre.exec(body.replace(/\/\*[\s\S]*?\*\//g, '')))) d[x[1]] = x[2].trim()
return d
}
function parseBlocks (css) {
const out = {}
const comp = {}
// 先摘出 L3 组件作用域(亮色 + .theme-dark 覆盖)。必须在主循环之前:
// 主循环的 .theme-dark 分支会把 `.theme-dark .iwb-pop {…}` 当成整块暗色主题,把真正的暗色值覆盖掉。
const compRe = /(\.theme-dark\s+)?(\.iwb-settings-modal|\.iwb-pop)\s*\{([\s\S]*?)\n\}/g
let cm
while ((cm = compRe.exec(css))) comp[(cm[1] ? 'dark:' : '') + cm[2]] = parseDeclBody(cm[3])
css = css.replace(compRe, '')
const re = /(:root:not\(\.theme-dark\)|:root|\.theme-dark)\s*\{([\s\S]*?)\n\}/g
let m
while ((m = re.exec(css))) out[m[1]] = parseDeclBody(m[2])
return { blocks: out, comp: comp }
}
// 这些值推断不出类型(关系式 / 复合值 / 纯数字),但对外的 DTCG 清单必须覆盖到
const TYPE_HINT = {
'--iwb-focus-ring': 'border',
'--iwb-edge-dash': 'string',
'--iwb-grid-on': 'number',
}
function inferType (value, name) {
if (TYPE_HINT[name]) return TYPE_HINT[name]
if (/^#[0-9a-f]{3,8}$/i.test(value)) return 'color'
if (/^rgba?\(/i.test(value)) return 'color'
if (/^cubic-bezier\(/.test(value)) return 'cubicBezier'
// 组件作用域里的几何值:纯 px / var() 别名 / color-mix 关系式
if (/^var\(--iwb-[a-z0-9-]+\)$/.test(value)) return /(?:-bg|-border|-text|-color|^--iwb-(?:err|ok|warn|chrome|home|dock|hr|edge))/i.test(name) ? 'color' : 'dimension'
if (/^color-mix\(/.test(value) || /^hsl\(/.test(value)) return 'color'
if (/-bg$|-bd$|-border$/.test(name) && /^[^;]+$/.test(value)) return null
if (/^--iwb-opacity-/.test(name) && /^[\d.]+$/.test(value)) return 'number'
if (/^-?[\d.]+px$/.test(value)) return 'dimension'
if (/^-?[\d.]+m?s$/.test(value)) return 'duration'
if (/\d+px [\d.]+px/.test(value)) return 'shadow'
// 层级是纯数字(z-index 认的是整数),DTCG 里就是 number
if (/^--(?:iwb-)?z-/.test(name) && /^\d+$/.test(value)) return 'number'
// 过渡是「时长 + 缓动」的复合值,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 'legacy.space'
if (/^space-/.test(n)) return 'space'
if (/^rd(-|$)/.test(n)) return 'legacy.radius'
if (/^radius-/.test(n)) return 'radius'
if (/^fs-/.test(n)) return 'legacy.font'
if (/^font-/.test(n)) return 'font.size'
if (/^tr-/.test(n)) return 'motion'
if (/^motion-/.test(n)) return 'motion'
if (/^elevation-/.test(n)) return 'shadow.elevation'
if (/^sh-/.test(n)) return 'shadow'
if (/^z-/.test(n)) return 'z'
if (n === 'grid-dot') return 'color'
if (/^(grid-|nav-|node-radius|node-sel-w|edge-w|edge-dash|chrome-h|tab-h|tab-radius|control-h)/.test(n)) return 'layout'
if (/^plugin-/.test(n)) return 'color.plugin'
if (/^set-/.test(n)) return 'component.settings'
if (/^pop-/.test(n)) return 'component.popover'
if (/^opacity-/.test(n)) return 'opacity'
if (/^ease-/.test(n)) return 'motion.ease'
if (/^icon-/.test(n)) return 'icon'
if (/^bw(-|$)/.test(n)) return 'border.width'
return 'color'
}
function dtcgKey (name) {
const n = name.replace(/^--(?:iwb-)?/, '')
if (/^sp-/.test(n)) return n.slice(3)
if (/^space-/.test(n)) return n.slice(6)
if (/^radius-/.test(n)) return n.slice(7)
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 (/^font-/.test(n)) return n.slice(5)
if (/^tr-/.test(n)) return n.slice(3)
if (/^motion-/.test(n)) return 'ms-' + n.slice(7)
if (/^elevation-/.test(n)) return n.slice(10)
if (/^sh-/.test(n)) return n.slice(3)
if (/^z-/.test(n)) return n.slice(2)
if (/^plugin-/.test(n)) return n.slice(7)
if (/^set-/.test(n)) return n.slice(4)
if (/^pop-/.test(n)) return n.slice(4)
if (/^opacity-/.test(n)) return n.slice(8)
if (/^ease-/.test(n)) return n.slice(5)
if (/^icon-/.test(n)) return n.slice(5)
if (n === 'bw') return 'base'
if (/^bw-/.test(n)) return n.slice(3)
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 parsed = parseBlocks(css)
const blocks = parsed.blocks
const light = Object.assign({}, blocks[':root'] || {}, blocks[':root:not(.theme-dark)'] || {})
const dark = blocks['.theme-dark'] || {}
// L3 组件作用域:亮色并进同一张表(靠 groupOf 的 component.* 分支分到独立组),暗色作为 $extensions
Object.assign(light, parsed.comp['.iwb-settings-modal'] || {}, parsed.comp['.iwb-pop'] || {})
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 }
}
const darkVal = dark[name] || (parsed.comp['dark:.iwb-settings-modal'] || {})[name] || (parsed.comp['dark:.iwb-pop'] || {})[name]
if (darkVal) node.$extensions = { 'com.image-workbench.dark': { $value: darkVal, $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 = []
// Windows 默认 core.autocrlf=true 会把 CSS 检出成 CRLF;内容一致但字符串不等,
// 会误报「快照与 derive() 不一致」。比较前统一换行符(写入始终 LF,见 .gitattributes)。
const cur = (fs.existsSync(CSS_OUT) ? fs.readFileSync(CSS_OUT, 'utf8') : '').replace(/\r\n/g, '\n')
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 }