diff --git a/CHANGELOG.md b/CHANGELOG.md index 64a6635..807923b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,61 @@ > 3. 核验正式地址版本 → 之后才移动 `stable` / `stable-verified-<日期>` 标签、更新本 CHANGELOG、写 GitHub Release(`gh release create`)。 > 测试发现问题 → 改 → 再 `deploy:test`,正式环境不受影响。回滚点 `git reset --hard stable`。 +## v3.26.50(2026-11-23)M5 闸门:让「下次又散掉」变得不可能 + +> **状态:待发布**(`verify:ci` 全绿含新闸门;零观感,视觉基线应 12/12 不变) + +M4 把 token 铺完了,但那时**没有任何东西阻止下一个人再写 `padding: 13px`**。 +这一批只做一件事:把「token 化」从**约定**变成**构建会红**。 + +**三道闸门,各自打过红一次才算数** + +| 闸门 | 做什么 | 反向验证(注入 → 结果) | +|---|---|---| +| `npm run lint:tokens` | 只看 diff **新增行**:裸 `#hex`/`rgba()`、裸 `px`(字号/圆角/内距/间距/外边距)、裸 `z-index` | 8 行探针文件 → 6 处全部点名,含「例外已过期 raw-until=3.26.10」 | +| `tests/design-token-ratchet-test.js` | 13 项计数**只降不升**(基线 `tests/fixtures/token-baseline.json`) | `style.css` 追加一条 `font-size: 13px` → `170 → 171(+1)` 立刻红 | +| `tests/theme-contrast-test.js` | 亮/暗 × 文字 × 表面 **114 对**,阈值取 `RATIO` | 把 `clampAgainst` 短路成 `return fg` → 100+ 对跌破阈值 | + +**为什么只查新增行**:存量还有 170 处写死字号、415 处写死间距。要求一次清零只会逼人加白名单, +而白名单比债务更难发现 —— B6 刚把 `ALLOW_FILES` 拆掉,不能隔一批又造一张。 +分工是:闸门管增量(立刻生效),棘轮管存量(只降不升),视觉基线管观感(改了必须看得见)。 + +**四条白名单**(缺一条都会逼人绕路) +- 内容色 `/*iwb:content*/`:画布内容不是主题表面色,同一个 `#fff` 在按钮上是 bug、在图片上是内容 +- 变量兜底 `var(--x, 8px)`:兜底值不生效时 var 本身就会红 +- 布局尺寸:`width/height/top/left` 没有闭集,强行 token 化只会发明只用一次的名字 +- 临时例外 `/*iwb:raw-until=3.28.0*/`:**必须带到期版本**,到期后闸门直接失败 + +**一个被证伪的直觉**:本想用「调坏 mix 权重」验证对比度守卫,改完仍全绿 —— 因为 `clampAgainst` +会重新夹回阈值。这反过来证明夹紧有效,也说明守卫真正防的是「有人删掉夹紧 / 把 token 写死成常数」, +于是按这个失效模式重做了注入。 + +**口径不重复实现**:棘轮不自己写正则,直接跑 `token-audit.js --json`。两套口径一旦漂移, +就会出现「守卫绿、审计红」这种最难查的分歧。 + +**④ 顺带堵住最后一个口子:JS 内联样式** + +写完闸门回头验证时发现,B6 说的「`style.css` 里半像素一个不剩」是**真的**,但 `src/*.js` 里 +还留着一处 `font-size:12.5px` —— 就在**首次启动「选择资料目录」弹窗**里。那个弹窗是纯手写 +`cssText` + `innerHTML`,白底 `#fff`/字色 `#111`/主按钮 `#2563eb`,**亮暗主题都不跟**, +深色模式下会跳出一个刺眼的白盒子。 + +于是闸门扩到 JS,判据分两种(否则会误伤布局参数): +- **CSS 文本**(`cssText` / `innerHTML` 里的 `style="…"`):必须带 `px` 单位或 `#`/`rgb` 才判, + 所以 `CanvasGeometry.snapToFreeSpot({ gap: 20 })` 这种**画布布局参数**不会被误报; +- **React 内联对象**:只认无歧义的 camelCase(`fontSize` / `marginLeft` / `zIndex`…), + `gap`、`padding` 在 JS 对象里可能是任何东西,不查。 + +弹窗本身也 token 化了:表面/文字/圆角/间距/主按钮全部走 token,每个 `var()` 都带字面兜底 +(这个弹窗在主题 CSS 生效前就要显示,token 缺席时必须退化成旧样子,而不是透明盒子)。 +**遮罩色是唯一没法 token 化的**——全仓 3 处 rgba 遮罩(`.iwb-modal-mask` / `.iwb-command-mask` / +这里),既不是 `bg` 也不是 `panel`,硬凑会让亮色主题出现「浅色遮罩」。留给 RFC 决定 +`--iwb-scrim` 的取值,在此之前用 `/*iwb:raw-until=3.28.0*/` 登记——这条也是例外机制的真实样本。 + +**存量一个没动**:`style.css` 的字号 170 / 圆角 103 / 间距 415 / z-index 31 与 B6 完全一致。 +本批唯一可见变化是那个首启动弹窗:深色下不再是白盒子,主按钮从蓝色 `#2563eb` 变成品牌灰 +`--iwb-brand`(与全站主按钮一致)。 + ## v3.26.49(2026-11-23)M4-B6 收尾:插件契约从「赦免文件」改成「声明皮肤」 > **状态:✅ 已发布**(2026-11-23)macOS 双架构 dmg + 签名更新产物 + `latest.json` 更新源已上线; diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d160c07..52a0ed3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -41,7 +41,7 @@ build esbuild 打包到 dist/(单 dist/app.js = src/ 模块按序 node --check app.js 与 dist/app.js 语法 check-dist.js 产物体检 lint ESLint(src/ + app.js,正确性规则) -test:unit 121 个单测文件(jsdom 契约测试;2026-11-07 新增 sync-facts-guard(事实一致性)/ _helpers/source(统一源文本);2026-11-12 新增 provider-ping(测速 no-cors 兜底)) +test:unit 123 个单测文件(jsdom 契约测试;2026-11-07 新增 sync-facts-guard(事实一致性)/ _helpers/source(统一源文本);2026-11-12 新增 provider-ping(测速 no-cors 兜底)) test relay-task-graph-fixture + iwb-v32-web-test test:facts 事实基线守门(见 §5) typecheck 渐进类型检查(tsc --noEmit;范围 = src/*.js,不含 app.js) diff --git a/docs/plans/design-tokens-rollout.md b/docs/plans/design-tokens-rollout.md index be259cf..f13df94 100644 --- a/docs/plans/design-tokens-rollout.md +++ b/docs/plans/design-tokens-rollout.md @@ -305,18 +305,44 @@ hover 完全没反馈。改成 `color-mix(text 10%, transparent)` 后两个主 **回滚**:一批一个 commit,revert 即回。 -### M5 闸门与治理(1d) +### M5 闸门与治理(1d)—— ✅ 已完成 2026-11-23(v3.26.50) | ID | 任务 | 文件 | 规则 | |---|---|---|---| -| T5.1 | `scripts/token-lint.js` | 新增 | 新增行出现裸 `#hex` / 裸 `px`(font-size、border-radius、padding、gap)→ 失败;四级白名单(内容色、布局尺寸、变量兜底、临时例外带到期版本) | +| T5.1 | `scripts/token-lint.js` | 新增 | 新增行出现裸 `#hex` / 裸 `px`(font-size、border-radius、padding、gap、margin)→ 失败;四条白名单(内容色、布局尺寸、变量兜底、临时例外带到期版本) | | T5.2 | 棘轮基线 | `tests/fixtures/token-baseline.json` | 计数只降不升 | | T5.3 | 完整性 + 自引用 | `tests/design-token-ratchet-test.js` | `var()` 目标必须存在;禁 `var(--x, var(--x))` | | T5.4 | 对比度回归 | `tests/theme-contrast-test.js` | text 对 5 表面 ≥4.5;tertiary ≥3.0;on-brand ≥4.5 | -| T5.5 | 层级规则 | token-lint | 禁裸 `z-index`;插件不得超 `--iwb-z-popover` | +| T5.5 | 层级规则 | token-lint | 禁裸 `z-index`;插件不得超 `--iwb-z-anchor`(3200) | | T5.6 | 接入 `verify:ci` | `package.json` | 三条命令失败即 CI 失败 | | T5.7 | 流程文档落章 | `design-tokens.md` §5 | RFC 模板、9 步、弃用窗口、PR 三问 | +**执行结果(2026-11-23,零观感)**: + +| ID | 计划 | 实际 | 偏差与理由 | +|---|---|---|---| +| T5.1 | 新增行禁裸值 | ✅ `scripts/token-lint.js`,默认 diff HEAD、`--base` 查已提交批次、`--file` 整文件 | 加 `--file` 是因为迁移时要看整份文件;默认只看未提交改动,才符合「写的时候就知道」。**验证时发现 CSS 闸门看不见 JS 内联样式**(首启动弹窗里还留着 12.5px),遂扩到 `cssText` 与 React 内联对象两套判据 | +| T5.2 | 基线 JSON | ✅ 13 项计数(原计划只提了硬编码项,实际把「取值种数」也纳入) | 种数下降比处数下降更能反映「收敛」,且不会被「同一行写两个裸值」这种记账差异干扰 | +| T5.3 | 完整性 | ✅ 并入棘轮测试 | 同一份审计数据能同时判三件事,不必再开一个进程 | +| T5.4 | 5 表面 ≥4.5 | ✅ 实际断言 **114 对**,阈值直接取 `theme-tokens.js` 的 `RATIO`(text 7 / text2 4.5 / dim 3 / canvas 6) | 计划写的 4.5 是下限;实现里正文本来就夹到 7,按 7 断言更严且只有一个数字来源 | +| T5.5 | 插件不超 popover | ⚠️ **没有 `--iwb-z-popover` 这个 token** → 改用 `--iwb-z-anchor`(3200) | B2 的层级表里最接近的是 anchor;palette/alert 是宿主专属层,插件不该碰到 | +| T5.6 | 接 verify:ci | ✅ `tokens:check` 之后插 `lint:tokens`;两个新测试进 `test:unit` | 放进 `test:unit` 而不是单开一段,是为了让「孤儿测试」守卫自动覆盖到 | +| T5.7 | 文档 | ✅ §5.2/5.4/5.5/5.6 重写 + §1.12 执行记录 + S3 闸门表按实际命令改写 | 旧 §5 还写着 `--iwb-color-*` / `--iwb-gap-*` 这些**不存在的名字**,照着写会直接踩空 | + +**反向验证**(三道闸门各自打红一次,确认不是「绿得不明不白」): + +| 闸门 | 注入 | 结果 | +|---|---|---| +| token-lint | 8 行探针文件,含裸色/裸字号/裸 z-index/过期例外 | 6 处全部点名,含「例外已过期:raw-until=3.26.10」 | +| 棘轮 | `style.css` 追加 `font-size: 13px` | `font-size 写死 170 → 171(+1)` 立刻红,还原后绿 | +| 对比度 | 把 `clampAgainst` 短路成 `return fg` | 100+ 对跌破阈值(亮色 text × inset 2.97 vs 下限 7) | + +**一个被证伪的直觉**:本想用「调坏某个 mix 权重」验证对比度守卫,改完却仍全绿 —— 因为 `clampAgainst` 会重新夹回阈值。这反过来证明夹紧机制有效,也说明守卫该防的是「有人删掉夹紧 / 把 token 写死成常数」,于是按这个失效模式重做了注入。 + +**计划外但必须现在做**:验证闸门时发现 CSS 闸门**看不见 JS 内联样式**,而 `src/storage-file.js` 的首启动「选择资料目录」弹窗是全仓唯一完全未 token 化的界面(`font-size:12.5px` / 白底 `#fff` / 主按钮 `#2563eb`,深色下是白盒子)。已一并处理:闸门扩到 JS(CSS 文本按 `px` 单位判、React 对象按 camelCase 判,避免误伤 `{ gap: 20 }` 这类布局参数),弹窗本身 token 化并给每个 `var()` 加字面兜底。 + +**留给 RFC 的一件事**:遮罩色 `rgba(18,20,26,.72)` 没有对应 token(全仓 3 处遮罩各写一份),硬凑 `bg`/`panel` 会让亮色主题出现浅色遮罩 —— 登记 `/*iwb:raw-until=3.28.0*/`,待 `--iwb-scrim` 的取值与消费者定案。 + ### M6 延展验证(0.5d) | ID | 任务 | 验收 | @@ -336,8 +362,8 @@ hover 完全没反馈。改成 `color-mix(text 10%, transparent)` 后两个主 | 3.26.47 | M4-B4 设置/外观 | **轻微** | 设置页正文 14→13、标题 16→15;同行输入框与按钮由「33 vs 24」变成都在 30。用户能感知的是「设置页更整齐」,不是「字变小」——13 与 15 仍在原层级里 | | 3.26.48 | M4-B5 节点 | **有变化(暗色改善明显)** | 端口从「黑洞」变成可见小圆点、提示词节点高度 −4px、浮条阴影统一。用户能感知的是「深色画布上端口看得见了」,附放大对比图 | | 3.26.49 | M4-B6 ✅ | 肉眼零,基线 8 张变化 | 收尾:16 处半像素清零、`control-h` 不扩全局(裁定)、插件契约文件级豁免 **6 → 0**。②④⑤⑥ 含画布场景因字号栅格化变化更新基线,逐张核对无错位 | -| 3.27.7 | M5 | 零 | 闸门上线 | -| 3.28.0 | M6 | 零 | 案例沉淀 | +| 3.26.50 | M5 ✅ | 零 | 闸门上线:`lint:tokens`(只查新增行)+ 13 项棘轮 + 114 对对比度;三道闸门各打红一次 | +| 3.26.51 | M6 | 零 | 案例沉淀(用 token 重做插件管理 + 改版 batch-export) | > 前置:3.26.42(外观 v2)需先发,本计划从 3.26.43 起。 > 插件侧:`batch-export` / `browser` / `image-vectorize` / `image-annotate` 各自在下次 patch 版本清硬编码,**不阻塞客户端**。 @@ -360,6 +386,11 @@ hover 完全没反馈。改成 `color-mix(text 10%, transparent)` 后两个主 | text 对表面 <4.5 的格数 | 暗色 4 格 | 0 | 0 | | 插件契约白名单文件 | 6(文件级豁免) | 0(B6 起按角色分类:私有皮肤自声明 + 强调色 chroma 判据 + 内容色行内标记) | 0 | | 视觉基线 diff | 无基线 | 每批 0 | 每批 0 | +| **新增裸值(增量)** | 无闸门 | **0/PR**(M5:`lint:tokens` 只查新增行) | 0 | +| **对比度达标对** | 未度量 | **114/114**(M5:亮/暗 × 文字 × 表面) | 114/114 | + +**M5 后(v3.26.50)**:存量计数**一个没变**(字号 170 / 圆角 103 / 间距 415 / z-index 31)—— 这一批只装闸门,不动样式。 +棘轮已把基线钉在 `tests/fixtures/token-baseline.json`:从这一版起存量只能降,涨一个就红。存量清理留给 M6 起的真实改版(改哪个面板就清哪块)。 ## 7. 风险登记 diff --git a/docs/standards/design-tokens.md b/docs/standards/design-tokens.md index ccc5de0..4015e67 100644 --- a/docs/standards/design-tokens.md +++ b/docs/standards/design-tokens.md @@ -321,6 +321,36 @@ ④ 6 个死类名不复现。第 ① 条是这一批的根因,写成断言比写进文档有效 —— 下一个加节点内控件的人 会在 CI 上被拦,而不是在暗色截图里被用户发现。 +### 1.12 M5 闸门执行记录(2026-11-23,v3.26.50) + +三个新增文件、三道闸门,全部进 `verify:ci`。 + +| 闸门 | 文件 | 职责 | 反向验证 | +|---|---|---|---| +| 禁新增裸值 | `scripts/token-lint.js`(+ `npm run lint:tokens`) | 只看 diff 新增行;**CSS 与 JS 内联样式两种写法**;四条白名单 | 探针文件 8 行造出 6 处违规,全被点名(含「例外已过期」);JS 探针 8 处全中且 `{ gap: 20 }` 未被误伤 | +| 棘轮 + 完整性 | `tests/design-token-ratchet-test.js` + `tests/fixtures/token-baseline.json` | 13 项计数只降不升;`var()` 目标必须存在;禁自我兜底 | 往 `style.css` 追加一条 `font-size: 13px` → `170 → 171` 立刻红 | +| 对比度 | `tests/theme-contrast-test.js` | 亮/暗 × 文字 × 表面共 **114 对**,阈值取 `RATIO` | 把 `clampAgainst` 短路成 `return fg` → 100+ 对跌破阈值 | + +**JS 内联样式必须单独定判据**(这一步是被一个真实漏网案例逼出来的): +B6 声称「`style.css` 里半像素一个不剩」属实,但 `src/storage-file.js` 的**首启动选目录弹窗** +还写着 `font-size:12.5px`、白底 `#fff`、字号 `17/13/12` 全套硬编码 —— 那是纯手写 `cssText`, +CSS 闸门看不见它,深色模式下会跳出一个白盒子。所以规则要按写法分两套: + +| 写法 | 判据 | 为什么 | +|---|---|---| +| CSS 文本(`cssText` / `innerHTML` 的 `style="…"`) | 必须带 `px` 单位或 `#`/`rgb` | 只按 `prop: value` 会误伤 `{ gap: 20 }` 这类**画布布局参数**(`snapToFreeSpot` 就吃这个) | +| React 内联对象 | 只认无歧义 camelCase(`fontSize` / `marginLeft` / `zIndex`…) | `gap`、`padding` 在 JS 对象里可能是任何东西 | + +**三个设计取舍**(都是踩过之后才定下来的): + +1. **只查新增行,不查存量。** 存量 170 处写死字号,一次性要求清零只会逼人加白名单;白名单比债务更难发现(B6 刚把 `ALLOW_FILES` 拆掉,不能隔一批又造一张)。增量由 `token-lint` 拦,存量由棘轮慢慢降。 +2. **口径不重复实现。** 棘轮不自己写正则,直接 `execFileSync` 跑 `token-audit.js --json`。两套口径一旦漂移,就会出现「守卫绿、审计红」这种最难查的分歧。 +3. **例外必须到期。** `/*iwb:raw-until=3.28.0*/` 到期后由 `lint:tokens` 判失败,而不是靠人记得回来删。普通白名单的失效方式是「没人记得」,行内标记的失效方式是「构建红」。 + +**一个被证伪的直觉**:最初想用「调坏某个 mix 权重」来验证对比度守卫,结果改完仍然全绿 —— 因为 `clampAgainst` 会把结果重新夹回阈值以上。**这恰好说明夹紧机制有效**,也说明对比度守卫真正防的回归是「有人删掉了夹紧」或「有人把 token 值写死成常数」,而不是「有人调了调色权重」。守卫按这个真实失效模式做了反向验证。 + +**当前最紧的一对**:暗色 `textDim × field` = 3.12(下限 3.0)。`field` 是 B5 为节点端口新增的档(暗 `#494949`),再暗一档就会破线 —— 下一个想压深 `field` 的人会先在 CI 上撞到它。 + ## 2. 目标架构 ### 2.1 三层模型(与 Primer / Ant Design v5 / Radix 同一套骨架) @@ -728,18 +758,18 @@ L3 Contract ── 组件整体换肤时才加,能不建就不建 ### S3 上闸门(约 0.5 天) -新增 `tests/design-token-ratchet-test.js` + `scripts/token-lint.js`,接入 `verify:ci`: +三道闸门,全部接入 `verify:ci`(M5 已落地,见 §1.12): -| 闸门 | 规则 | 失败示例 | -|---|---|---| -| **棘轮** | 硬编码计数不得超过基线(只降不升) | 基线 `font-size 写死 281`,新 PR 变成 282 → 失败 | -| **禁新增** | diff 新增行里出现裸 `#hex` / 裸 `px`(font-size、border-radius、padding、gap)→ 失败 | `padding: 13px` | -| **完整性** | 所有 `var(--iwb-*)` 必须有定义;禁止 `var(--x, var(--x))` | §1.3-②③ | -| **层级** | 禁止裸数字 `z-index`;插件不超过 40 | `z-index: 9999` | -| **插件契约** | **没有文件级豁免**。颜色按角色分三类:中性色(chroma < 24)必须用 token;强调色(chroma ≥ 24)允许写死;自带整套皮肤的在 manifest 声明 `skin: 'private'`,代价是不得引用宿主颜色 token。内容色用行内 `/*iwb:content*/` 标记 | §3.10 | +| 闸门 | 命令 | 规则 | 失败示例 | +|---|---|---|---| +| **禁新增裸值** | `npm run lint:tokens` | 只看 diff 的**新增行**:裸 `#hex` / `rgba()`(颜色属性)、裸 `px`(font-size / border-radius / padding / gap / margin)、裸数字 `z-index` → 失败 | `padding: 13px`、`color: #666`、`z-index: 9999` | +| **棘轮** | `node tests/design-token-ratchet-test.js` | 13 项硬编码计数不得超过 `tests/fixtures/token-baseline.json`(只降不升) | 基线 `font-size 写死 170`,新 PR 变成 171 → 失败 | +| **完整性 + 对比度** | 同上 + `node tests/theme-contrast-test.js` | `var(--iwb-*)` 必须有定义;禁 `var(--x, var(--x))`;亮/暗两套主题下 114 对「文字 × 表面」必须达 `RATIO` 阈值 | 拼成 `--iwb-radius-pill`;文字对 `field` 掉到 4.4 | **棘轮是这套方案能不能活下来的关键**:没有它,三个月后又是一次 281 个字号。 +**为什么闸门只查新增行**:M4 结束时仍有 170 处写死字号、415 处写死间距。要求「一次清零」只会逼人加白名单,而白名单比债务更难发现。所以分工是——**闸门管增量(立刻生效),棘轮管存量(只降不升),视觉基线管观感(改了必须看得见)**。 + ### S4 长期治理(流程,见 §5) --- @@ -759,15 +789,17 @@ L3 Contract ── 组件整体换肤时才加,能不建就不建 ### 5.2 新增模块 / 插件的 9 步 1. 先翻 §3 找现成 L2,**80% 的情况不需要新 token** -2. 颜色只用 `--iwb-color-*`,不写 `#hex`/`rgba()` -3. 间距只用 `--iwb-gap-*` / `--iwb-pad-*` -4. 字号只用 `--iwb-font-*`(禁止 10.5/11.5/12.5/13.5) -5. 圆角只用 `--iwb-radius-*` -6. 浮层阴影用 `--iwb-elevation-*`,暗色另有描边 -7. z-index 用 `--iwb-z-*`,插件不超 40 +2. 颜色只用语义色(`--iwb-text` / `--iwb-text2` / `--iwb-layer2` / `--iwb-field` / `--iwb-border` / `--iwb-brand`),不写 `#hex`/`rgba()` +3. 间距只用 `--iwb-space-{2..64}` +4. 字号只用 `--iwb-font-{xs,sm,md,lg,xl,2xl,display}`(禁止 10.5/11.5/12.5/13.5 这类半像素) +5. 圆角只用 `--iwb-radius-{chip,control,card,panel,popover,full}` +6. 浮层阴影用 `--iwb-elevation-{1..4}`,暗色另有描边 +7. z-index 用 `--iwb-z-*`,插件不得高于 `--iwb-z-anchor`(3200) 8. 动效用 `--iwb-dur-*` + `--iwb-ease-*`,并支持 reduced-motion 9. 亮/暗各截一张图放进 PR +第 2–7 步现在**有机器守卫兜着**:写完跑 `npm run lint:tokens`,它会指着 `file:line` 告诉你哪一行漏了。 + ### 5.3 token 变更的版本与弃用 | 变更 | 版本位 | 要求 | @@ -782,19 +814,31 @@ L3 Contract ── 组件整体换肤时才加,能不建就不建 ### 5.4 PR 评审清单(三问) -- [ ] 这个 PR 新增了硬编码数值吗?(有 → 为什么不能进 token) -- [ ] 亮色/暗色都看过了吗?(截图) +- [ ] 这个 PR 新增了硬编码数值吗?(`npm run lint:tokens` 绿了没?没绿就有) +- [ ] 亮色/暗色都看过了吗?(截图;改了 token 还要看 `npm run test:tokens`) - [ ] 如果改了 token:`design-tokens.md` 同步了吗?需要弃用窗口吗? -### 5.5 例外登记 +### 5.5 例外登记(行内标记,不是文件白名单) -任何 `ALLOW` 白名单条目必须写成: +**没有文件级白名单**。B6 之前 `ALLOW_FILES` 把 6 个文件整份赦免,结果其中 2 个是过期豁免、2 个是自带皮肤、2 个是混合态——三类完全不同的东西挤在同一张名单里,它只会一直长。 -```js -// owner: @xxx · 到期: 3.28.0 · 原因: 画笔颜色属用户内容,不是主题表面色 -``` +现在只有两个行内标记,粒度到「这一条声明」: -无 owner 或无到期版本的例外,S3 闸门直接失败。 +| 标记 | 用途 | 到期 | +|---|---|---| +| `/*iwb:content*/` | 画布**内容色**(图片上的纯白文字、截图底色)。同一个 `#fff` 在按钮上是 bug、在图片上是内容,值分不出来 | 不过期(内容色永远是内容色) | +| `/*iwb:raw-until=3.28.0*/` | **临时例外**,必须写到期版本。到期后 `lint:tokens` 直接判失败 | **必须填**,到期不会自动续 | + +插件的私有皮肤是第三种情况,写在 manifest(`skin: 'private'`)而不是注释里——它声明的是「这个插件不跟随主题」这个**产品决定**,代价是不得引用宿主颜色 token(契约测试会检查自洽性,见 §3.10)。 + +### 5.6 闸门红了怎么办 + +| 现象 | 处理 | +|---|---| +| `lint:tokens` 指出某行裸值 | 换成对应闭集 token。如果确实是内容色 → 加 `/*iwb:content*/`;如果确实没法 token 化 → `/*iwb:raw-until=<下个 minor>*/` 并在 PR 里说明 | +| `ratchet` 说某项涨了 | 说明有裸值混进去了(先看 `npm run audit:tokens` 的分布)。**不要直接把基线数字调大**;确属必要的减法(例如删掉一批死规则)反而应该**下调**基线并提交 | +| `theme-contrast-test` 红了 | 调色改坏了某对「文字 × 表面」。改回去,或用 `clampAgainst` 重新夹紧;阈值在 `theme-tokens.js` 的 `RATIO`,别在测试里改数字 | +| 想全局看存量 | `node scripts/token-lint.js --file style.css`(整文件模式,迁移时用) | --- diff --git a/package.json b/package.json index 23de9f8..bd17933 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "image-workbench-web", "private": true, - "version": "3.26.49", + "version": "3.26.50", "description": "Browser-based node canvas image workbench", "scripts": { "build": "node scripts/build.js && node scripts/copy-plugins.js", @@ -10,7 +10,7 @@ "app:build": "tauri build", "lint": "eslint src app.js", "test": "node tests/relay-task-graph-fixture-test.js && node tests/plugin-style-node-test.js && node tests/inherit-text-test.js", - "test:unit": "node tests/plugin-archive-test.js && node tests/plugin-files-policy-test.js && node tests/theme-plugin-tokens-test.js && node tests/theme-static-snapshot-test.js && node tests/overlay-layer-test.js && node tests/settings-scope-test.js && node tests/node-scope-test.js && node tests/appearance-v2-test.js && node tests/image-prompt-cover-test.js && node tests/unit-test.js && node tests/canvas-actions-test.js && node tests/canvas-performance-test.js && node tests/canvas-drag-performance-test.js && node tests/resize-observer-performance-test.js && node tests/node-drag-jank-test.js && node tests/edge-graph-performance-test.js && node tests/persistence-export-images-test.js && node tests/settings-hotkeys-size-presets-test.js && node tests/custom-size-validation-test.js && node tests/image-node-performance-test.js && node tests/drag-frame-performance-test.js && node tests/canvas-edge-memo-test.js && node tests/canvas-node-transform-test.js && node tests/canvas-dot-zoom-test.js && node tests/duplicate-ws-fresh-source-test.js && node tests/single-canvas-shell-test.js && node tests/navigation-layout-test.js && node tests/home-canvas-library-test.js && node tests/left-resource-layout-test.js && node tests/interface-hierarchy-test.js && node tests/three-page-layout-test.js && node tests/page-chrome-regression-test.js && node tests/task-cache-test.js && node tests/storage-config-shape-test.js && node tests/storage-zip-test.js && node tests/canvas-node-factory-test.js && node tests/runtime-error-test.js && node tests/runtime-lifecycle-test.js && node tests/runtime-fetch-test.js && node tests/runtime-channel-test.js && node tests/runtime-boundary-test.js && node tests/runtime-record-test.js && node tests/runtime-recovery-test.js && node tests/runtime-diagnostic-record-test.js && node tests/runtime-metrics-test.js && node tests/runtime-run-context-test.js && node tests/runtime-execution-context-test.js && node tests/storage-gc-canvas-refs-test.js && node tests/storage-health-test.js && node tests/generation-state-test.js && node tests/canvas-store-test.js && node tests/storage-result-test.js && node tests/storage-local-test.js && node tests/storage-domain-test.js && node tests/storage-runtime-test.js && node tests/storage-indexeddb-test.js && node tests/generation-scheduler-test.js && node tests/gallery-query-test.js && node tests/gallery-hydration-test.js && node tests/image-blob-store-test.js && node tests/image-preview-derive-test.js && node tests/image-format-test.js && node tests/clipboard-graph-test.js && node tests/clipboard-graph-fallback-test.js && node tests/image-admit-test.js && node tests/image-dedupe-test.js && node tests/clipboard-kind-test.js && node tests/clipboard-paste-test.js && node tests/clipboard-copy-test.js && node tests/paste-once-test.js && node tests/list-e2e-specs-test.js && node tests/desktop-csp-loopback-test.js && node tests/architecture-boundary-guard-test.js && node tests/plugin-host-test.js && node tests/plugin-data-test.js && node tests/plugin-loader-test.js && node tests/plugin-bundle-test.js && node tests/plugin-install-test.js && node tests/plugin-gallery-render-test.js && node tests/plugin-gallery-recovery-test.js && node tests/plugin-gallery-ux-test.js && node tests/plugin-grouplib-render-test.js && node tests/plugin-promptlib-render-test.js && node tests/image-compress-test.js && node tests/storage-fallback-indexeddb-test.js && node tests/canvas-normalize-test.js && node tests/canvas-geometry-test.js && node tests/canvas-history-test.js && node tests/canvas-ops-test.js && node tests/canvas-text-test.js && node tests/history-store-test.js && node tests/plugin-integrity-test.js && node tests/workspace-sessions-store-test.js && node tests/workspace-list-test.js && node tests/workspace-purge-test.js && node tests/provider-ping-test.js && node tests/runtime-diagnostics-hotpath-test.js && node tests/storage-log-failure-test.js && node tests/storage-convergence-test.js && node tests/swallow-wiring-test.js && node tests/workbench-logic-test.js && node tests/storage-autobackup-test.js && node tests/storage-outputs-mutate-test.js && node tests/sync-facts-guard-test.js && node tests/e2e-fixed-waits-ratchet-test.js && node tests/shell-render-behavior-test.js && node tests/canvas-meta-notice-behavior-test.js && node tests/source-text-ratchet-test.js && node tests/plugin-theme-contract-test.js && node tests/prompt-md-roundtrip-test.js && node tests/generation-results-store-test.js && node tests/out-groups-order-test.js && node tests/native-overlay-park-test.js && node tests/ui-dock-sanitize-test.js && node tests/ui-minimap-fit-test.js && node tests/ui-workbench-hooks-test.js", + "test:unit": "node tests/plugin-archive-test.js && node tests/plugin-files-policy-test.js && node tests/theme-plugin-tokens-test.js && node tests/theme-static-snapshot-test.js && node tests/overlay-layer-test.js && node tests/settings-scope-test.js && node tests/node-scope-test.js && node tests/appearance-v2-test.js && node tests/image-prompt-cover-test.js && node tests/unit-test.js && node tests/canvas-actions-test.js && node tests/canvas-performance-test.js && node tests/canvas-drag-performance-test.js && node tests/resize-observer-performance-test.js && node tests/node-drag-jank-test.js && node tests/edge-graph-performance-test.js && node tests/persistence-export-images-test.js && node tests/settings-hotkeys-size-presets-test.js && node tests/custom-size-validation-test.js && node tests/image-node-performance-test.js && node tests/drag-frame-performance-test.js && node tests/canvas-edge-memo-test.js && node tests/canvas-node-transform-test.js && node tests/canvas-dot-zoom-test.js && node tests/duplicate-ws-fresh-source-test.js && node tests/single-canvas-shell-test.js && node tests/navigation-layout-test.js && node tests/home-canvas-library-test.js && node tests/left-resource-layout-test.js && node tests/interface-hierarchy-test.js && node tests/three-page-layout-test.js && node tests/page-chrome-regression-test.js && node tests/task-cache-test.js && node tests/storage-config-shape-test.js && node tests/storage-zip-test.js && node tests/canvas-node-factory-test.js && node tests/runtime-error-test.js && node tests/runtime-lifecycle-test.js && node tests/runtime-fetch-test.js && node tests/runtime-channel-test.js && node tests/runtime-boundary-test.js && node tests/runtime-record-test.js && node tests/runtime-recovery-test.js && node tests/runtime-diagnostic-record-test.js && node tests/runtime-metrics-test.js && node tests/runtime-run-context-test.js && node tests/runtime-execution-context-test.js && node tests/storage-gc-canvas-refs-test.js && node tests/storage-health-test.js && node tests/generation-state-test.js && node tests/canvas-store-test.js && node tests/storage-result-test.js && node tests/storage-local-test.js && node tests/storage-domain-test.js && node tests/storage-runtime-test.js && node tests/storage-indexeddb-test.js && node tests/generation-scheduler-test.js && node tests/gallery-query-test.js && node tests/gallery-hydration-test.js && node tests/image-blob-store-test.js && node tests/image-preview-derive-test.js && node tests/image-format-test.js && node tests/clipboard-graph-test.js && node tests/clipboard-graph-fallback-test.js && node tests/image-admit-test.js && node tests/image-dedupe-test.js && node tests/clipboard-kind-test.js && node tests/clipboard-paste-test.js && node tests/clipboard-copy-test.js && node tests/paste-once-test.js && node tests/list-e2e-specs-test.js && node tests/desktop-csp-loopback-test.js && node tests/architecture-boundary-guard-test.js && node tests/plugin-host-test.js && node tests/plugin-data-test.js && node tests/plugin-loader-test.js && node tests/plugin-bundle-test.js && node tests/plugin-install-test.js && node tests/plugin-gallery-render-test.js && node tests/plugin-gallery-recovery-test.js && node tests/plugin-gallery-ux-test.js && node tests/plugin-grouplib-render-test.js && node tests/plugin-promptlib-render-test.js && node tests/image-compress-test.js && node tests/storage-fallback-indexeddb-test.js && node tests/canvas-normalize-test.js && node tests/canvas-geometry-test.js && node tests/canvas-history-test.js && node tests/canvas-ops-test.js && node tests/canvas-text-test.js && node tests/history-store-test.js && node tests/plugin-integrity-test.js && node tests/workspace-sessions-store-test.js && node tests/workspace-list-test.js && node tests/workspace-purge-test.js && node tests/provider-ping-test.js && node tests/runtime-diagnostics-hotpath-test.js && node tests/storage-log-failure-test.js && node tests/storage-convergence-test.js && node tests/swallow-wiring-test.js && node tests/workbench-logic-test.js && node tests/storage-autobackup-test.js && node tests/storage-outputs-mutate-test.js && node tests/sync-facts-guard-test.js && node tests/e2e-fixed-waits-ratchet-test.js && node tests/shell-render-behavior-test.js && node tests/canvas-meta-notice-behavior-test.js && node tests/source-text-ratchet-test.js && node tests/plugin-theme-contract-test.js && node tests/prompt-md-roundtrip-test.js && node tests/generation-results-store-test.js && node tests/out-groups-order-test.js && node tests/native-overlay-park-test.js && node tests/ui-dock-sanitize-test.js && node tests/ui-minimap-fit-test.js && node tests/ui-workbench-hooks-test.js && node tests/design-token-ratchet-test.js && node tests/theme-contrast-test.js", "test:diagnostics": "node tests/runtime-diagnostics-hotpath-test.js", "test:facts": "node tests/architecture-facts-guard-test.js && node tests/release-records-test.js", "plugins:gc": "node scripts/plugins-publish.mjs --gc", @@ -24,14 +24,16 @@ "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 tokens:check && 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:tokens && 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", "sync:facts": "node scripts/sync-facts.js", "sync:facts:write": "node scripts/sync-facts.js --write", "typecheck": "tsc -p tsconfig.json", - "release:desktop": "bash scripts/release.sh" + "release:desktop": "bash scripts/release.sh", + "lint:tokens": "node scripts/token-lint.js", + "test:tokens": "node tests/design-token-ratchet-test.js && node tests/theme-contrast-test.js" }, "devDependencies": { "@playwright/test": "^1.64.1", diff --git a/scripts/token-lint.js b/scripts/token-lint.js new file mode 100644 index 0000000..8b75abb --- /dev/null +++ b/scripts/token-lint.js @@ -0,0 +1,234 @@ +#!/usr/bin/env node +/* token-lint.js —— 设计 token 的「新增行」闸门(M5-T5.1 / T5.5) + * + * 只查**新增行**,不查存量:M4 结束时 style.css 里还有 170 处写死字号、55 处裸 z-index, + * 一次性要求全清零只会逼人加白名单。闸门的作用是**不让数字再涨**(存量交给棘轮测试慢慢降)。 + * + * 四条白名单(缺一条都会逼人绕路,绕路比违规更难发现): + * ① 内容色:行内 `/*iwb:content*​/` —— 画布内容(图片上的纯白文字、截图底色)不是主题表面色, + * 同一个 #fff 在按钮上是 bug、在图片上是内容,值分不出来,只能靠标记。 + * ② 变量兜底:`var(--x, 8px)` 里的 8px 不查 —— 兜底值不生效时才是问题,而那时 var 本身就会红。 + * ③ 布局尺寸:只查排版属性(字号/圆角/内距/间距/外边距)。width/height/left/top 没有闭集, + * 强行 token 化只会发明一堆只用一次的名字。 + * ④ 临时例外:行内 `/*iwb:raw-until=3.27.0*​/` —— **必须带到期版本**,到期后本脚本把它当失败, + * 不会像普通白名单那样悄悄变成永久免死金牌。 + * + * 用法: + * node scripts/token-lint.js # 查工作区未提交的改动(diff vs HEAD) + * node scripts/token-lint.js --base origin/main # 查 base..HEAD 的改动(CI / 发版前) + * node scripts/token-lint.js --file style.css # 查整个文件(迁移时看全局) + * node scripts/token-lint.js --json + */ +'use strict' + +const fs = require('fs') +const path = require('path') +const { execFileSync } = require('child_process') + +const ROOT = path.join(__dirname, '..') +const VERSION = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version + +// 排版属性:这些值必须来自闭集。其余属性(width/height/top/left/flex/…)没有闭集,不查。 +const SIZE_PROPS = new Set([ + 'font-size', 'border-radius', 'gap', 'row-gap', 'column-gap' +]) +const SIZE_PREFIX = /^(padding|margin)(-(top|right|bottom|left|block|inline)(-(start|end))?)?$/ + +// 颜色属性:表面色/文字色必须来自语义 token。 +// box-shadow 不在其中 —— 它已由 --iwb-elevation-* 四档收口,阴影里的 rgba 是笔画而不是表面。 +const COLOR_PROPS = new Set([ + 'color', 'background', 'background-color', + 'border-color', 'border-top-color', 'border-right-color', 'border-bottom-color', 'border-left-color', + 'fill', 'stroke', 'outline-color' +]) + +// 生成物与第三方:token 定义本身当然满地是值;站点是另一套系统(自己的 --*-变量) +const SKIP_FILE = /^(src\/design-tokens\.css|dist\/|node_modules\/|site\/|docs\/)/ +// 插件产物由 tests/plugin-theme-contract-test.js 按「私有皮肤 / 强调色 / 中性色」分类治理, +// 这里只对插件查层级(插件内部若写死 9999,会盖住宿主的弹窗与确认框)。 +const PLUGIN_FILE = /^plugins\// + +const CONTENT_MARK = /\/\*iwb:content\*\// +const EXCEPTION_MARK = /\/\*iwb:raw-until=(\d+\.\d+\.\d+)\*\// + +const argv = process.argv.slice(2) +const argOf = (name) => { + const i = argv.indexOf(name) + return i >= 0 ? argv[i + 1] : null +} +const jsonOut = argv.includes('--json') +const base = argOf('--base') +const onlyFile = argOf('--file') + +/* ── 1. 取「要检查的行」:{file, line, text} ───────────────────────────── */ +function linesFromDiff () { + const args = ['-C', ROOT, 'diff', '--no-color', '--unified=0', '--no-ext-diff'] + // 默认 diff HEAD 会把已暂存与未暂存都算进来(工作区真实状态);--base 则是「已提交的这批」 + args.push(base ? base + '...HEAD' : 'HEAD') + args.push('--', '*.css', 'app.js', 'src', 'plugins') + + let out = '' + try { + out = execFileSync('git', args, { encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 }) + } catch (e) { + // 不是 git 仓库(或浅克隆取不到 base)时不要假装通过:直接报错,让人显式用 --file + console.error('token-lint: 无法取到 git diff(' + (e.message || e) + ')') + console.error(' 不在 git 仓库里时用 --file 指定要查的文件') + process.exit(2) + } + + const rows = [] + let file = null + let next = 0 + for (const raw of out.split('\n')) { + if (raw.startsWith('+++ b/')) { file = raw.slice(6); continue } + if (raw.startsWith('+++ /dev/null')) { file = null; continue } + const h = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/.exec(raw) + if (h) { next = Number(h[1]); continue } + if (!file) continue + if (raw.startsWith('+')) { rows.push({ file, line: next, text: raw.slice(1) }); next++ } + } + return rows +} + +function linesFromFile (p) { + const abs = path.isAbsolute(p) ? p : path.join(ROOT, p) + const rel = path.relative(ROOT, abs).split(path.sep).join('/') + return fs.readFileSync(abs, 'utf8').split('\n').map((text, i) => ({ file: rel, line: i + 1, text })) +} + +/* ── 2. 判定 ──────────────────────────────────────────────────────────── */ +const cmpVer = (a, b) => { + const pa = a.split('.').map(Number) + const pb = b.split('.').map(Number) + for (let i = 0; i < 3; i++) if (pa[i] !== pb[i]) return pa[i] - pb[i] + return 0 +} + +// 把 var(...) 整段抠掉:兜底值不查(见白名单 ②),剩下的才是「裸值」 +const stripVar = (v) => v.replace(/var\([^()]*\)/g, 'var') + +const problems = [] +const warn = [] + +// JS 里的样式有两种写法,判据必须分开(否则会误伤布局参数): +// ① CSS 文本:`cssText = 'padding:16px;font-size:12px'`、`innerHTML = '
'` +// —— 只有**带 px 单位**或带 #/rgb 颜色才算样式;`{ gap: 20 }` 这种是画布布局参数,不能管 +// ② React 内联对象:`style={{ fontSize: 13 }}` —— 数字即 px,但键名要用**无歧义**的 +// camelCase(fontSize / marginLeft / zIndex…);`gap` / `padding` 在 JS 对象里可能是 +// 任何东西(CanvasGeometry 的 snapToFreeSpot 就吃 `{ gap: 20 }`),所以不查 +const JS_PX = /(font-size|border-radius|padding[a-z-]*|gap|row-gap|column-gap|margin[a-z-]*)\s*:\s*(-?\d*\.?\d+)px/g +const JS_COLOR = /(color|background|border-color)\s*:\s*(#[0-9a-f]{3,8}|rgba?\([^)]*\))/gi +const JS_Z = /z-index\s*:\s*(-?\d+)/g +const JS_CAMEL = /(fontSize|borderRadius|paddingTop|paddingBottom|paddingLeft|paddingRight|marginTop|marginBottom|marginLeft|marginRight|rowGap|columnGap|zIndex)\s*:\s*(\d+)/g + +function check (row) { + const { file, line, text } = row + if (!text || !text.includes(':')) return + if (SKIP_FILE.test(file)) return + + const isPlugin = PLUGIN_FILE.test(file) + const isJs = /\.js$/.test(file) + const contentOk = CONTENT_MARK.test(text) + const ex = EXCEPTION_MARK.exec(text) + const expired = ex ? cmpVer(VERSION, ex[1]) >= 0 : false + const bucket = (msg, hard) => { + if (ex && !expired) { + warn.push({ file, line, msg, until: ex[1] }) + return + } + if (expired) msg += '(例外已过期:raw-until=' + ex[1] + ',当前 ' + VERSION + ')' + if (hard) problems.push({ file, line, msg }) + else warn.push({ file, line, msg }) + } + + if (isJs) { + // 纯注释行跳过:注释里写「z-index:2 是局部层级」这种说明不该被判违规 + if (/^\s*(\/\/|\/\*|\*)/.test(text)) return + // 插件产物里的颜色/字号交给插件契约测试;层级照样管(见下) + const scanPx = !isPlugin + if (scanPx) { + for (const m of text.matchAll(JS_PX)) { + if (Number(m[2]) === 0) continue + bucket(m[1] + ': ' + m[2] + 'px → 用 --iwb-(font|radius|space)-* 闭集', true) + } + for (const m of text.matchAll(JS_CAMEL)) { + if (m[1] === 'zIndex') continue // 层级单独报,避免同一条重复计数 + if (Number(m[2]) === 0) continue + bucket(m[1] + ': ' + m[2] + ' → 用 --iwb-(font|radius|space)-* 闭集(数字即 px)', true) + } + for (const m of text.matchAll(JS_COLOR)) { + const white = m[1].toLowerCase() === 'color' && /^#(fff|ffffff)$/i.test(m[2]) + if (contentOk || white) continue + bucket(m[1] + ': ' + m[2] + ' → 用语义色 token(内容色加 /*iwb:content*/ 标记)', true) + } + } + for (const m of text.matchAll(JS_Z)) { + bucket('z-index: ' + m[1] + ' → 用 --iwb-z-*(插件不得高于 --iwb-z-anchor / 3200)', true) + } + for (const m of text.matchAll(JS_CAMEL)) { + if (m[1] !== 'zIndex') continue + bucket('zIndex: ' + m[2] + ' → 用 --iwb-z-*', true) + } + return + } + + for (const m of text.matchAll(/([a-z-]+)\s*:\s*([^;{}]+)/g)) { + const prop = m[1].toLowerCase() + const value = m[2].trim() + const bare = stripVar(value) + + // z-index:任何裸数字都不行(层级表见 docs/standards/design-tokens.md §3.4) + if (prop === 'z-index') { + if (/^-?\d+$/.test(bare)) { + bucket('z-index: ' + bare + ' → 用 --iwb-z-*(插件不得高于 --iwb-z-anchor / 3200)', true) + } + continue + } + // 插件内部除了层级,其余交给插件契约测试(私有皮肤是允许整套调色板的) + if (isPlugin) continue + + if (SIZE_PROPS.has(prop) || SIZE_PREFIX.test(prop)) { + const hit = /(-?\d*\.?\d+)px/.exec(bare) + if (hit && Number(hit[1]) !== 0) { + bucket(prop + ': ' + value + ' → 用 --iwb-(font|radius|space)-* 闭集', true) + } + continue + } + + if (COLOR_PROPS.has(prop)) { + const hex = /#[0-9a-f]{3,8}\b/i.test(bare) + const rgb = /rgba?\(/i.test(bare) + if (!hex && !rgb) continue + // 纯白文字压在图片/色块上:内容对比,不是主题表面色(与插件契约同款例外) + const whiteText = prop === 'color' && /^#(fff|ffffff)$/i.test(bare) + if (contentOk || whiteText) continue + bucket(prop + ': ' + value + ' → 用语义色 token(内容色加 /*iwb:content*/ 标记)', true) + } + } +} + +/* ── 3. 跑 ────────────────────────────────────────────────────────────── */ +const rows = onlyFile ? linesFromFile(onlyFile) : linesFromDiff() +for (const r of rows) check(r) + +const anchor = (() => { + const css = fs.readFileSync(path.join(ROOT, 'src', 'design-tokens.css'), 'utf8') + const m = /--iwb-z-anchor:\s*(\d+)/.exec(css) + return m ? Number(m[1]) : null +})() + +if (jsonOut) { + console.log(JSON.stringify({ version: VERSION, mode: onlyFile ? 'file' : (base || 'HEAD'), checked: rows.length, problems, warn, pluginZCeiling: anchor }, null, 2)) + process.exit(problems.length ? 1 : 0) +} + +const mode = onlyFile ? '整个文件 ' + onlyFile : (base ? base + '...HEAD' : '工作区未提交改动') +console.log('token-lint: ' + mode + ',检查 ' + rows.length + ' 行新增' + (anchor ? '(插件层级上限 ' + anchor + ')' : '')) +for (const w of warn) console.log(' · ' + w.file + ':' + w.line + ' ' + w.msg + (w.until ? ' [例外至 ' + w.until + ']' : '')) +if (problems.length) { + for (const p of problems) console.log(' ✗ ' + p.file + ':' + p.line + ' ' + p.msg) + console.log('\ntoken-lint: ' + problems.length + ' 处新增裸值 —— 失败。理由与白名单见 docs/standards/design-tokens.md §5') + process.exit(1) +} +console.log('token-lint: 通过(0 处新增裸值' + (warn.length ? ',' + warn.length + ' 条已登记的例外' : '') + ')') diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 5536587..285c9b6 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "image-workbench" -version = "3.26.44" +version = "3.26.50" description = "无限画布 · 生图工作台桌面版" authors = ["malongan"] license = "" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 53fcb72..1f9bec4 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "../node_modules/@tauri-apps/cli/config.schema.json", "productName": "image-workbench", - "version": "3.26.49", + "version": "3.26.50", "identifier": "com.malongan.imageworkbench", "build": { "frontendDist": "../dist", diff --git a/src/canvas-webview.js b/src/canvas-webview.js index 2994bb9..863eaf5 100644 --- a/src/canvas-webview.js +++ b/src/canvas-webview.js @@ -261,8 +261,10 @@ var d = document.createElement('div') d.setAttribute('data-iwb-cwv-hint', '') d.style.cssText = 'position:absolute;inset:0;display:flex;align-items:center;justify-content:center;' - + 'padding:16px;text-align:center;font-size:12px;line-height:1.7;white-space:pre-line;' - + 'background:rgba(128,128,128,.08);pointer-events:none;z-index:2' + + 'padding:var(--iwb-space-16,16px);text-align:center;font-size:var(--iwb-font-sm,12px);line-height:1.7;white-space:pre-line;' + // z-index:2 是**组件内部**的局部层级(与宿主层级表无关,只在本容器内压住画布), + // 不是 body 级浮层,所以不走 --iwb-z-*;登记为例外,到期复核是否还需要它 + + 'background:color-mix(in srgb,var(--iwb-text) 8%,transparent);pointer-events:none;z-index:2 /*iwb:raw-until=3.28.0*/' st.hintEl = d el.appendChild(d) } diff --git a/src/storage-file.js b/src/storage-file.js index 585cbcf..b012a46 100644 --- a/src/storage-file.js +++ b/src/storage-file.js @@ -767,22 +767,36 @@ return new Promise((resolve) => { try { const mask = document.createElement('div') - mask.style.cssText = 'position:fixed;inset:0;z-index:2147483647;background:rgba(18,20,26,.72);display:flex;align-items:center;justify-content:center;font:14px/1.6 -apple-system,BlinkMacSystemFont,"PingFang SC",sans-serif' + // 这个弹窗在首启动、主题 CSS 可能尚未生效时就要显示,所以每个 var() 都带字面兜底: + // token 缺席时退化成本版改动前的样子(白底深字),而不是透明盒子。 + mask.style.cssText = 'position:fixed;inset:0;z-index:var(--iwb-z-alert,4000);' + // 遮罩色是全仓唯一没有 token 的表面色(.iwb-modal-mask / .iwb-command-mask 也各写了一份 rgba)。 + // 它既不是 bg 也不是 panel,硬凑会让亮色主题出现「浅色遮罩」。留给 RFC 决定 --iwb-scrim + // 的取值与消费者(现有 3 处),在此之前登记为例外,不让它悄悄变成永久的裸值。 + + 'background:rgba(18,20,26,.72) /*iwb:raw-until=3.28.0*/;' + + 'display:flex;align-items:center;justify-content:center;' + + 'font-family:-apple-system,BlinkMacSystemFont,"PingFang SC",sans-serif;' + + 'font-size:var(--iwb-font-md,14px);line-height:1.6' const box = document.createElement('div') - box.style.cssText = 'background:#fff;color:#111;border-radius:14px;padding:24px 26px;max-width:580px;box-shadow:0 24px 60px rgba(0,0,0,.35)' + box.style.cssText = 'background:var(--iwb-panel,#fff);color:var(--iwb-text,#111);' + + 'border-radius:var(--iwb-radius-popover,14px);padding:var(--iwb-space-24,24px);' + + 'max-width:580px;box-shadow:var(--iwb-elevation-4,0 24px 60px rgba(0,0,0,.35))' const btn = (t, primary) => { const b = document.createElement('button') b.textContent = t - b.style.cssText = 'padding:8px 16px;border-radius:8px;font-size:13px;cursor:pointer;border:1px solid ' + - (primary ? '#2563eb' : '#d0d4dc') + ';background:' + (primary ? '#2563eb' : '#fff') + ';color:' + (primary ? '#fff' : '#111') + b.style.cssText = 'padding:var(--iwb-space-8,8px) var(--iwb-space-16,16px);' + + 'border-radius:var(--iwb-radius-control,8px);font-size:var(--iwb-font-md,13px);cursor:pointer;' + + 'border:1px solid ' + (primary ? 'var(--iwb-brand,#2563eb)' : 'var(--iwb-border,#d0d4dc)') + ';' + + 'background:' + (primary ? 'var(--iwb-brand,#2563eb)' : 'var(--iwb-panel,#fff)') + ';' + + 'color:' + (primary ? 'var(--iwb-on-brand,#fff)' : 'var(--iwb-text,#111)') return b } - box.innerHTML = '
首次启动:选择资料目录
' + - '
设置、提示词、画布和所有图片都会存在这个目录里。
' + - '
换机器/重装时,把目录指到旧的资料目录就能直接继续用。
' + - '
默认位置:' + defaultDir + '
' + box.innerHTML = '
首次启动:选择资料目录
' + + '
设置、提示词、画布和所有图片都会存在这个目录里。
' + + '
换机器/重装时,把目录指到旧的资料目录就能直接继续用。
' + + '
默认位置:' + defaultDir + '
' const row = document.createElement('div') - row.style.cssText = 'display:flex;gap:10px;justify-content:flex-end' + row.style.cssText = 'display:flex;gap:var(--iwb-space-10,10px);justify-content:flex-end' const bDefault = btn('用默认目录', true) const bPick = btn('选择已有目录…', false) const done = (v) => { try { mask.remove() } catch (e) { /* ignore */ } resolve(v) } diff --git a/tests/design-token-ratchet-test.js b/tests/design-token-ratchet-test.js new file mode 100644 index 0000000..63a770c --- /dev/null +++ b/tests/design-token-ratchet-test.js @@ -0,0 +1,95 @@ +'use strict' +// 设计 token 棘轮 + 完整性守卫(M5-T5.2 / T5.3) +// +// 为什么是「棘轮」而不是「全清零」:M4 结束时 style.css 里还有 170 处写死字号、415 处写死间距, +// 一次清零等于重写一遍样式表。棘轮把「数字不许涨」变成机械规则,迁移可以分版本做, +// 而**新代码**从今天起不可能再增加存量(涨一个就红)。 +// +// 数字口径唯一来源是 scripts/token-audit.js --json —— 这里不重新实现一遍正则: +// 两套口径一旦漂移,就会变成「守卫绿、审计红」这种最难查的分歧。 +// +// 三条断言: +// A. 每一项计数 ≤ tests/fixtures/token-baseline.json(只降不升) +// B. 引用了但从未定义的变量 = 0(`var(--iwb-radius-pill)` 这种拼错必须当场红, +// 而不是等到浏览器里那条声明静默失效) +// C. 没有 `var(--x, var(--x))` 自我兜底 —— 兜底值等于引用值,等于「失效时再失效一次」, +// 既掩盖问题又没有默认值可用 +const assert = require('assert') +const fs = require('fs') +const path = require('path') +const { execFileSync } = require('child_process') + +const root = path.resolve(__dirname, '..') +const baseline = JSON.parse(fs.readFileSync(path.join(root, 'tests', 'fixtures', 'token-baseline.json'), 'utf8')) +const audit = JSON.parse(execFileSync(process.execPath, [path.join(root, 'scripts', 'token-audit.js'), '--json'], { cwd: root, encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 })) + +const now = { + undefinedRefs: audit.undefinedRefs.length, + deadTokens: audit.deadTokens.length, + hex: audit.hard['十六进制色值'], + rgb: audit.hard['rgba()/rgb()'], + fontSize: audit.hard['font-size 写死'], + radius: audit.hard['border-radius 写死'], + spacing: audit.hard['padding/margin/gap 写死'], + zIndexDecl: audit.hard['z-index 声明'], + fontSizeDistinct: audit.fontSizes.distinct.length, + radiusDistinct: audit.radii.distinct.length, + spacingDistinct: audit.spacings.distinct.length, + zIndexDistinct: audit.zindex.distinct.length, + shadowDistinct: audit.shadows.distinct +} + +// ── A. 只降不升 ── +// 基线里有的项必须都在(防手滑删项让某项「凭空消失」),当前也必须有同名项(防改名后静默跳过) +assert.deepStrictEqual( + Object.keys(now).sort(), + Object.keys(baseline.counts).sort(), + '棘轮口径与基线键不一致:审计脚本加了/改了指标却没更新基线(或反之)' +) + +const LABEL = { + undefinedRefs: '引用未定义变量', + deadTokens: '定义了没用的变量', + hex: '十六进制色值', + rgb: 'rgba()/rgb()', + fontSize: 'font-size 写死', + radius: 'border-radius 写死', + spacing: 'padding/margin/gap 写死', + zIndexDecl: 'z-index 声明', + fontSizeDistinct: '字号取值种数', + radiusDistinct: '圆角取值种数', + spacingDistinct: '间距取值种数', + zIndexDistinct: 'z-index 取值种数', + shadowDistinct: '阴影写法种数' +} + +const grew = [] +for (const k of Object.keys(now)) { + if (now[k] > baseline.counts[k]) grew.push(LABEL[k] + ':' + baseline.counts[k] + ' → ' + now[k] + '(+' + (now[k] - baseline.counts[k]) + ')') +} +assert.deepStrictEqual(grew, [], + '以下计数涨了 —— 新增样式请用 token(--iwb-font-* / --iwb-radius-* / --iwb-space-* / --iwb-z-* / 语义色):\n ' + grew.join('\n ') + + '\n确属必要的例外:用行内 /*iwb:raw-until=<版本>*/(会到期)或 /*iwb:content*/(画布内容色),不要直接把基线数字调大。') + +// ── B. 引用必须存在 ── +assert.deepStrictEqual(audit.undefinedRefs, [], + '有 var() 引用了从未定义的变量:' + audit.undefinedRefs.join(' ') + '(拼错的 token 名会让整条声明静默失效)') + +// ── C. 自我兜底 ── +const cssFiles = ['style.css'].concat( + fs.readdirSync(path.join(root, 'src')).filter((f) => f.endsWith('.css') && f !== 'design-tokens.css').map((f) => 'src/' + f) +) +const selfFallback = [] +for (const f of cssFiles) { + const src = fs.readFileSync(path.join(root, f), 'utf8') + src.split('\n').forEach((line, i) => { + const m = /var\(\s*(--[a-z0-9-]+)\s*,\s*var\(\s*(--[a-z0-9-]+)/i.exec(line) + if (m && m[1] === m[2]) selfFallback.push(f + ':' + (i + 1) + ' ' + m[1]) + }) +} +assert.deepStrictEqual(selfFallback, [], 'var() 用自己做兜底(等于没有兜底):' + selfFallback.join(' | ')) + +const drop = Object.keys(now).filter((k) => now[k] < baseline.counts[k]) +console.log('token-ratchet: ' + Object.keys(now).length + ' 项计数均未超过基线' + + (drop.length ? '(可下调基线:' + drop.map((k) => LABEL[k] + ' ' + baseline.counts[k] + '→' + now[k]).join(',') + ')' : '')) +console.log('token-ratchet: 引用未定义变量 0、自我兜底 0') diff --git a/tests/fixtures/token-baseline.json b/tests/fixtures/token-baseline.json new file mode 100644 index 0000000..5cb9639 --- /dev/null +++ b/tests/fixtures/token-baseline.json @@ -0,0 +1,20 @@ +{ + "_comment": "设计 token 棘轮基线(M5-T5.2)。数字只允许下降,不允许上升 —— 涨了就说明新代码写了裸值,改用 token 或(确有必要时)在 PR 里说明并下调别的项把总数补回来。口径来源:node scripts/token-audit.js --json", + "measuredAt": "2026-11-23", + "measuredVersion": "3.26.49", + "counts": { + "undefinedRefs": 0, + "deadTokens": 32, + "hex": 84, + "rgb": 55, + "fontSize": 170, + "radius": 103, + "spacing": 415, + "zIndexDecl": 71, + "fontSizeDistinct": 13, + "radiusDistinct": 18, + "spacingDistinct": 26, + "zIndexDistinct": 31, + "shadowDistinct": 45 + } +} diff --git a/tests/theme-contrast-test.js b/tests/theme-contrast-test.js new file mode 100644 index 0000000..88f6f4d --- /dev/null +++ b/tests/theme-contrast-test.js @@ -0,0 +1,73 @@ +'use strict' +// 对比度回归守卫(M5-T5.4) +// +// 为什么需要它:暗色主题的对比度不是「写死一个值」就完了 —— derive() 用 mix + clampAgainst +// 现算,任何一次调色(把 panel 提亮、把 layer 压深、把文字权重从 .30 改成 .34)都可能让 +// 某对「文字 × 表面」掉到阈值以下,而**肉眼在编辑器里看不出来**(唯一能看出来的用户会截图说 +// 「这个字看不清」,但我们拿不到他的主题参数)。 +// +// 阈值取 theme-tokens.js 的 RATIO(单一源,禁止在这里再写 4.5 / 7 这种魔数): +// text 7.0(AAA 正文)/ text2 4.5(AA 正文)/ dim 3.0(AA 大字下限)/ canvas 6.0(节点标题,预留 opacity .8)/ onBrand 4.5 +// 计划里写的「text ≥4.5、tertiary ≥3.0、on-brand ≥4.5」是**下限**,实现取更严的 7.0/3.0/4.5。 +// +// 亮暗两套主题都跑;DEFAULTS 之外的配色(用户在设置里改过的)不在本守卫范围 —— +// 那条路由 clampText 在运行时兜底。 +const assert = require('assert') +const fs = require('fs') +const path = require('path') + +const root = path.resolve(__dirname, '..') +const sandbox = {} +new Function('window', fs.readFileSync(path.join(root, 'src', 'theme-tokens.js'), 'utf8'))(sandbox) +const T = sandbox.IWBThemeTokens +assert.ok(T && typeof T.derive === 'function', 'theme-tokens.js 没暴露 IWBThemeTokens.derive') + +// 承载文字的整套表面。canvas 单独处理:画布上只有节点标题,它用 canvasText(阈值 6, +// 且要预留 opacity .8 的余量)—— 界面文字本来就不该直接压在画布底上,混进通用集合会误报。 +const SURFACES = ['bg', 'panel', 'layer', 'layer2', 'nodeBg', 'field', 'pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover', 'hover', 'dockBg', 'chrome', 'inset'] + +const rows = [] +function check (theme, fgName, fg, surfaces, min, label) { + for (const s of surfaces) { + const bg = theme[s] + if (!bg || !/^#[0-9a-f]{6}$/i.test(bg)) continue + const r = T.contrast(fg, bg) + rows.push({ name: fgName + ' × ' + s, ratio: r, min, label, ok: r + 1e-9 >= min }) + } +} + +for (const dark of [false, true]) { + const t = T.derive(T.DEFAULTS, dark) + const label = dark ? '暗色' : '亮色' + const present = SURFACES.filter((s) => t[s]) + check(t, 'text', t.text, present, T.RATIO.text, label) + check(t, 'text2', t.text2, present, T.RATIO.text2, label) + check(t, 'textDim', t.textDim, present, T.RATIO.dim, label) + check(t, 'brandText', t.brandText, present, T.RATIO.text2, label) + check(t, 'canvasText', t.canvasText, ['canvasBg'], T.RATIO.canvas, label) + check(t, 'nodeText2', t.nodeText2, ['nodeBg', 'layer', 'panel'], T.RATIO.text2, label) + // 品牌底上的字:onBrand 是按对比度自动选的黑/白,选错了就会「主按钮白字压浅蓝」 + rows.push({ + name: 'onBrand × brand', + ratio: T.contrast(t.onBrand, t.brand), + min: T.RATIO.onBrand, + label, + ok: T.contrast(t.onBrand, t.brand) + 1e-9 >= T.RATIO.onBrand + }) +} + +const bad = rows.filter((r) => !r.ok) +if (bad.length) { + console.log('对比度不达标(阈值来自 theme-tokens.js 的 RATIO):') + for (const r of bad) console.log(' ✗ ' + r.label + ' ' + r.name.padEnd(26) + r.ratio.toFixed(2) + ' < ' + r.min) + console.log('\n最低的三对(含达标的):') + for (const r of rows.slice().sort((a, b) => a.ratio - b.ratio).slice(0, 3)) { + console.log(' · ' + r.label + ' ' + r.name.padEnd(26) + r.ratio.toFixed(2) + '(下限 ' + r.min + ')') + } +} +assert.deepStrictEqual(bad.map((r) => r.label + ' ' + r.name + ' ' + r.ratio.toFixed(2) + '<' + r.min), [], + '文字与表面的对比度掉到阈值以下 —— 调色时改坏了,改回去或用 clampAgainst 重新夹紧') + +const minRow = rows.slice().sort((a, b) => a.ratio - b.ratio)[0] +console.log('token-contrast: ' + rows.length + ' 对(亮/暗 × 文字 × 表面)全部达标;最紧的一对是 ' + + minRow.label + ' ' + minRow.name + ' = ' + minRow.ratio.toFixed(2) + '(下限 ' + minRow.min + ')')