feat(appearance): 外观 v2 —— 种子/覆盖双层管线,字体颜色可调,插件变量名冻结
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 46s

用户反馈两套外观系统互相破坏(自动配色下改色无效、关掉后 19 个变量停在派生值)、
字体颜色不能调。管线改成 derive(seeds,dark) → 叠加 overrides → 一次写完。

- 去掉「自动配色」大开关:每项默认跟随派生,改哪项覆盖哪项,↺ 恢复跟随
  (VS Code workbench.colorCustomizations 模型);亮/暗覆盖分开存,互不串味
- 数据模型 v2(seeds/overrides/overridesDark/sizes/presets),v1 配置自动迁移:
  只有旧版明确关掉自动配色时的手写色才固化成覆盖,不凭空制造覆盖
- 字体开放:界面主/次 + 节点主/次 4 项,带对比度徽标与「夹到 4.5:1」按钮
- 新增 --iwb-node-text/-text2;.iwb-node 作用域内把 --iwb-text/-text2 重映射过去,
  插件节点零改动跟随节点文字(插件界面全部走 CSS 变量,变量名只增不改)
- 新增 --iwb-tint:半透明底色基色与文字色解耦(32 处 color-mix 换基色),
  调字不再连带改悬停/标签/分隔底色
- 覆盖 bg/panel 后联动重算 chrome/dock/插件四层,避免「暖面板 + 冷灰顶栏」割裂
- 补洞 --iwb-surface/--iwb-hover/--iwb-text-dim(插件此前只能吃硬编码兜底,暗色会白底)
- 内置「休息」预设:低刺激暖灰,亮/暗正文均 >= 4.5:1
- 修 --iwb-canvas-bg/--iwb-border/--iwb-warn 三处 var(自己) 的无效声明
- 测试:新增 appearance-v2-test(迁移/覆盖/一次写完/休息对比度/插件变量名不变);
  page-chrome 的 --iwb-tab-active 断言改到 tint 基色
This commit is contained in:
qiqi committed 2026-11-23 12:36:43 +08:00
1 parent 81abda87ae
commit c158d7c37e
7 files changed
+641 -232

No files matched your search

+23
View File
@@ -86,6 +86,17 @@
const brandHover = mix(A, d ? '#ffffff' : '#000000', 0.14)
const edgeHot = d ? mix(A, '#ffffff', 0.25) : A
const gridDot = mix(N, t, d ? 0.36 : 0.30)
// v3.26.42:节点文字与界面文字分家(外观面板可分别微调)
const nodeText = text
const nodeText2 = text2
// tint:所有「以文字色做半透明底色/描边」的基色。刻意**不跟随**文字覆盖 ——
// 用户把字调深/调浅时,悬停底、标签底、分隔线不应跟着一起变(否则调字等于重排整套灰阶)。
const tint = text
// 插件历史变量补洞:--iwb-surface / --iwb-hover / --iwb-text-dim 此前未定义,
// 插件只能吃硬编码兜底(暗色下会突然变白底)。按同一灰阶派生,插件零改动即跟随主题。
const surface = surface1
const hover = d ? mix(surface0, '#ffffff', 0.10) : mix(surface0, '#000000', 0.055)
const textDim = mix(text2, surface0, 0.35)
// 语义色:色相固定,明度随主题调整(暗色提亮一档,避免发闷)
const err = d ? mix('#ef4444', '#ffffff', 0.18) : '#ef4444'
@@ -111,6 +122,12 @@
border2,
text,
text2,
textDim,
nodeText,
nodeText2,
tint,
surface,
hover,
brand,
brandHover,
edge: border2,
@@ -145,6 +162,12 @@
s('--iwb-border2', tokens.border2)
s('--iwb-text', tokens.text)
s('--iwb-text2', tokens.text2)
s('--iwb-text-dim', tokens.textDim)
s('--iwb-node-text', tokens.nodeText)
s('--iwb-node-text2', tokens.nodeText2)
s('--iwb-tint', tokens.tint)
s('--iwb-surface', tokens.surface)
s('--iwb-hover', tokens.hover)
s('--iwb-brand', tokens.brand)
s('--iwb-edge', tokens.edge)
s('--iwb-edge-hot', tokens.edgeHot)
+412 -192
View File
@@ -1,10 +1,21 @@
/* ui-appearance.js —— 外观(界面主题 / 画布 / 连线 / 节点 / 主题色)与取色器(v3.25.22)
/* ui-appearance.js —— 外观(v2:种子 → 派生 → 覆盖,v3.26.42)
*
* 设计边界(与用户确认过):
* · 开放:画布底色、网格、连线(颜色/粗细/线型/高亮)、节点(底色/边框/圆角/选中描边)、主题色
* · 不开放:界面背景 / 面板底色 / 文字颜色 —— 它们被大量 color-mix 派生,开放后极易变成不可读
* · 所有颜色项**亮/暗各存一套**;尺寸类(粗细、圆角、间距)共用一套
* · 全部最终落到 CSS 变量上(改一个变量全局生效,不需要改渲染逻辑)
* 用户裁定(2026-11-23):
* · 不再有「自动配色」这个会把手动值整体作废的大开关;
* 每项默认跟随派生,**改哪项就覆盖哪项**,点 ↺ 回到跟随(VS Code workbench.colorCustomizations 同款模型)
* · 字体颜色开放:界面主/次、节点主/次 4 项,带对比度徽标与一键修正
* · 两套系统合并成一条单向管线:derive(seeds, dark) → 叠加 overrides → **一次写完**
* (旧实现先写手写 14 项再让派生覆盖 ⇒ 关掉自动配色后 19 个变量停在派生值,半新半旧)
*
* 数据模型:
* { v:2, seeds:{neutral,accent,depth}, overrides:{亮色覆盖}, overridesDark:{暗色覆盖},
* sizes:{网格/连线/节点尺寸}, presets:[{name,seeds,overrides,overridesDark,sizes}] }
* v1 配置(auto/seedNeutral/xxxLight/xxxDark)由 normalize 自动迁移,不丢用户配色。
*
* 与插件的关系(重要):变量名**只增不改**。插件全部通过 CSS 变量取色,
* 且都带硬编码兜底;这里只改「值」不改「名」,插件外观自动跟随、功能零影响。
* 新增 --iwb-node-text / --iwb-node-text2 后,节点作用域内把 --iwb-text/-text2 重映射过去
* (style.css),插件节点因此零改动跟随节点文字色。
*/
;(function (global) {
'use strict'
@@ -12,21 +23,6 @@
const React = g.React || {}
const el = (type, prop, ...children) => React.createElement(type, prop, ...children)
const DEFAULTS = Object.freeze({
canvasBgLight: '#d9dbde', canvasBgDark: '#6f6f6f',
gridDotLight: '#c9ccd6', gridDotDark: '#8f8f8f', gridSize: 1.4, gridGap: 22, gridOn: true, gridStyle: 'dots',
// v3.25.81:自动配色(默认开)——只定 4 个种子,其余由 src/theme-tokens.js 派生
auto: true, seedNeutral: '#808080', seedAccent: '#6b7280', depth: 22,
edgeColorLight: '#b9bfca', edgeColorDark: '#7a7a7a',
edgeWidth: 1.8, edgeDash: 'dashed',
edgeHotLight: '#6b7280', edgeHotDark: '#ffffff',
nodeBgLight: '#f1f3f6', nodeBgDark: '#b3b3b3',
nodeBorderLight: '#d5d9e0', nodeBorderDark: '#4f4f4f',
nodeRadius: 14, nodeSelWidth: 1,
accent: '#6b7280',
presets: [],
})
const DASH = { solid: 'none', dashed: '4 3', dotted: '1 3' }
const HEX_RE = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i
const isHex = (v) => HEX_RE.test(String(v || ''))
@@ -42,36 +38,129 @@
return Math.min(hi, Math.max(lo, n))
}
function normalize (cfg) {
const c = (cfg && cfg.canvas && cfg.canvas.appearance) || {}
const hex = (k) => normHex(c[k]) || DEFAULTS[k]
const DEFAULT_SIZES = Object.freeze({
gridSize: 1.4, gridGap: 22, gridOn: true, gridStyle: 'dots',
edgeWidth: 1.8, edgeDash: 'dashed', nodeRadius: 14, nodeSelWidth: 1,
})
const DEFAULTS = Object.freeze({
v: 2,
seeds: Object.freeze({ neutral: '#808080', accent: '#6b7280', depth: 22 }),
overrides: Object.freeze({}),
overridesDark: Object.freeze({}),
sizes: DEFAULT_SIZES,
presets: Object.freeze([]),
})
// 覆盖键 → derive() token 键。只收「颜色」,尺寸走 sizes。
const OV_TOKEN = Object.freeze({
bg: 'bg', panel: 'panel', layer: 'layer', border: 'border', border2: 'border2',
text: 'text', text2: 'text2', nodeText: 'nodeText', nodeText2: 'nodeText2',
nodeBg: 'nodeBg', canvasBg: 'canvasBg', gridDot: 'gridDot',
edge: 'edge', edgeHot: 'edgeHot', brand: 'brand',
})
const OV_KEYS = Object.keys(OV_TOKEN)
function gridStyleOf (r) {
const st = String(r.gridStyle || '')
if (st === 'dots' || st === 'lines' || st === 'blank') return st
return (r.gridOn === false) ? 'blank' : 'dots'
}
function cleanOv (o) {
const out = {}
if (!o || typeof o !== 'object') return out
for (const k of OV_KEYS) { const v = normHex(o[k]); if (v) out[k] = v }
return out
}
function cleanSizes (r) {
const s = r || {}
return {
canvasBgLight: hex('canvasBgLight'), canvasBgDark: hex('canvasBgDark'),
gridDotLight: hex('gridDotLight'), gridDotDark: hex('gridDotDark'),
gridSize: clamp(c.gridSize, 0.8, 2.6, DEFAULTS.gridSize),
gridGap: clamp(c.gridGap, 16, 40, DEFAULTS.gridGap),
gridOn: c.gridOn !== false,
gridStyle: (function () {
const st = String(c.gridStyle || '')
if (st === 'dots' || st === 'lines' || st === 'blank') return st
return (c.gridOn === false) ? 'blank' : 'dots'
})(),
edgeColorLight: hex('edgeColorLight'), edgeColorDark: hex('edgeColorDark'),
edgeWidth: clamp(c.edgeWidth, 1, 4, DEFAULTS.edgeWidth),
edgeDash: DASH[c.edgeDash] ? c.edgeDash : DEFAULTS.edgeDash,
edgeHotLight: hex('edgeHotLight'), edgeHotDark: hex('edgeHotDark'),
auto: c.auto !== false,
seedNeutral: normHex(c.seedNeutral) || DEFAULTS.seedNeutral,
seedAccent: normHex(c.seedAccent) || DEFAULTS.seedAccent,
depth: clamp(c.depth, 0, 60, DEFAULTS.depth),
nodeBgLight: hex('nodeBgLight'), nodeBgDark: hex('nodeBgDark'),
nodeBorderLight: hex('nodeBorderLight'), nodeBorderDark: hex('nodeBorderDark'),
nodeRadius: clamp(c.nodeRadius, 6, 20, DEFAULTS.nodeRadius),
nodeSelWidth: clamp(c.nodeSelWidth, 1, 4, DEFAULTS.nodeSelWidth),
accent: hex('accent'),
presets: Array.isArray(c.presets) ? c.presets.slice(0, 12) : [],
gridSize: clamp(s.gridSize, 0.8, 2.6, DEFAULT_SIZES.gridSize),
gridGap: clamp(s.gridGap, 16, 40, DEFAULT_SIZES.gridGap),
gridOn: s.gridOn !== false,
gridStyle: gridStyleOf(s),
edgeWidth: clamp(s.edgeWidth, 1, 4, DEFAULT_SIZES.edgeWidth),
edgeDash: DASH[s.edgeDash] ? s.edgeDash : DEFAULT_SIZES.edgeDash,
nodeRadius: clamp(s.nodeRadius, 6, 20, DEFAULT_SIZES.nodeRadius),
nodeSelWidth: clamp(s.nodeSelWidth, 1, 4, DEFAULT_SIZES.nodeSelWidth),
}
}
function cleanSeeds (s) {
const r = s || {}
return {
neutral: normHex(r.neutral) || DEFAULTS.seeds.neutral,
accent: normHex(r.accent) || DEFAULTS.seeds.accent,
depth: clamp(r.depth, 0, 60, DEFAULTS.seeds.depth),
}
}
function cleanPreset (p) {
const r = (p && typeof p === 'object') ? p : {}
return {
name: String(r.name || '预设').slice(0, 20),
seeds: cleanSeeds(r.seeds),
overrides: cleanOv(r.overrides),
overridesDark: cleanOv(r.overridesDark),
sizes: cleanSizes(r.sizes),
}
}
// v1 → v2:只有旧配置**明确关掉自动配色**时,那些手写颜色才是用户真实在用的,才固化成覆盖;
// 其余情况一律回到「跟随派生」(旧版自动配色下改色本来就不生效,迁过来也不该突然生效)。
function fromV1 (raw) {
const r = raw || {}
const sizes = cleanSizes({
gridSize: r.gridSize, gridGap: r.gridGap, gridOn: r.gridOn, gridStyle: r.gridStyle,
edgeWidth: r.edgeWidth, edgeDash: r.edgeDash, nodeRadius: r.nodeRadius, nodeSelWidth: r.nodeSelWidth,
})
const seeds = cleanSeeds({
neutral: r.seedNeutral,
accent: r.seedAccent || r.accent,
depth: r.depth,
})
const overrides = {}
const overridesDark = {}
if (r.auto === false) {
const pairs = [
['canvasBg', 'canvasBgLight', 'canvasBgDark'],
['nodeBg', 'nodeBgLight', 'nodeBgDark'],
['border', 'nodeBorderLight', 'nodeBorderDark'],
['edge', 'edgeColorLight', 'edgeColorDark'],
['edgeHot', 'edgeHotLight', 'edgeHotDark'],
['gridDot', 'gridDotLight', 'gridDotDark'],
]
for (const p of pairs) {
const l = normHex(r[p[1]]); const d = normHex(r[p[2]])
if (l) overrides[p[0]] = l
if (d) overridesDark[p[0]] = d
}
}
const presets = Array.isArray(r.presets)
? r.presets.slice(0, 12).map((p) => cleanPreset(Object.assign({ name: p && p.name }, fromV1(p && p.values))))
: []
return { v: 2, seeds, overrides, overridesDark, sizes, presets }
}
function normalize (cfg) {
const raw = (cfg && cfg.canvas && cfg.canvas.appearance) || {}
if (raw && (raw.v === 2 || (raw.seeds && typeof raw.seeds === 'object' && !raw.seedNeutral))) {
const r = raw
return {
v: 2,
seeds: cleanSeeds(r.seeds),
overrides: cleanOv(r.overrides),
overridesDark: cleanOv(r.overridesDark),
sizes: cleanSizes(r.sizes),
presets: Array.isArray(r.presets) ? r.presets.slice(0, 12).map(cleanPreset) : [],
}
}
return fromV1(raw)
}
// 当前模式下的「跟随值」:面板与 apply 共用,保证显示的就是实际生效的
function followTokens (a, dark) {
const T = g.IWBThemeTokens
if (!T || typeof T.derive !== 'function') return null
try { return T.derive(a.seeds, !!dark) } catch (e) { return null }
}
function apply (cfg, dark) {
try {
@@ -79,34 +168,46 @@
const root = g.document && g.document.documentElement
if (!root || !root.style) return
const set = (k, v) => root.style.setProperty(k, v)
set('--iwb-canvas-bg', dark ? a.canvasBgDark : a.canvasBgLight)
set('--iwb-brand', a.accent)
set('--iwb-grid-dot', dark ? a.gridDotDark : a.gridDotLight)
set('--iwb-grid-size', a.gridSize + 'px')
set('--iwb-grid-gap', a.gridGap + 'px')
const gs = a.gridStyle || 'dots'
set('--iwb-grid-on', (gs !== 'blank' && a.gridOn !== false) ? '1' : '0')
try { root.setAttribute('data-iwb-grid', gs) } catch (e) { /* ignore */ }
set('--iwb-edge', dark ? a.edgeColorDark : a.edgeColorLight)
set('--iwb-edge-w', a.edgeWidth + 'px')
set('--iwb-edge-dash', DASH[a.edgeDash] || DASH.dashed)
set('--iwb-edge-hot', dark ? a.edgeHotDark : a.edgeHotLight)
set('--iwb-node-bg', dark ? a.nodeBgDark : a.nodeBgLight)
set('--iwb-border', dark ? a.nodeBorderDark : a.nodeBorderLight)
set('--iwb-node-radius', a.nodeRadius + 'px')
set('--iwb-node-sel-w', a.nodeSelWidth + 'px')
// v3.25.81:自动配色 —— 由 4 个种子派生整套语义色并覆盖上面的手写值
if (a.auto) {
const T = g.IWBThemeTokens
if (T && typeof T.derive === 'function') {
try { T.applyTo(root, T.derive(a, !!dark)) } catch (e) { /* 派生失败保留手写值 */ }
}
const s = a.sizes
// 尺寸类与颜色无关,始终写(派生不可用时颜色交给 CSS 兜底)
set('--iwb-grid-size', s.gridSize + 'px')
set('--iwb-grid-gap', s.gridGap + 'px')
set('--iwb-grid-on', (s.gridStyle !== 'blank' && s.gridOn !== false) ? '1' : '0')
try { root.setAttribute('data-iwb-grid', s.gridStyle) } catch (e) { /* ignore */ }
set('--iwb-edge-w', s.edgeWidth + 'px')
set('--iwb-edge-dash', DASH[s.edgeDash] || DASH.dashed)
set('--iwb-node-radius', s.nodeRadius + 'px')
set('--iwb-node-sel-w', s.nodeSelWidth + 'px')
const tokens = followTokens(a, dark)
const T = g.IWBThemeTokens
if (!tokens || !T || typeof T.applyTo !== 'function') return
const ov = dark ? a.overridesDark : a.overrides
const final = Object.assign({}, tokens)
for (const k of OV_KEYS) { if (ov[k]) final[OV_TOKEN[k]] = ov[k] }
// 结构层与插件四层是从表面色 mix 出来的:覆盖了 bg/panel/canvasBg/layer 就跟着重算,
// 否则会出现「暖色面板 + 冷灰顶栏」「暖色面板 + 冷灰插件卡片」的割裂
const M = T.mix
if (typeof M === 'function') {
final.chrome = M(final.bg, final.canvasBg, 0.4)
final.homeBg = M(final.bg, final.canvasBg, 0.35)
final.dockBg = M(final.layer, final.canvasBg, 0.45)
final.dockHeadBg = M(final.dockBg, final.bg, 0.55)
final.surface = final.panel
final.pluginCard = final.panel
final.pluginSurface = dark ? M(final.bg, '#ffffff', 0.07) : M(final.panel, '#000000', 0.035)
final.pluginInset = dark ? M(final.bg, '#000000', 0.12) : M(final.panel, '#000000', 0.075)
final.pluginHover = dark ? M(final.bg, '#ffffff', 0.16) : M(final.panel, '#000000', 0.11)
final.hover = dark ? M(final.bg, '#ffffff', 0.10) : M(final.bg, '#000000', 0.055)
}
// tint 恒取派生文字色:调字不改悬停/标签底色(否则调一个字等于重排整套灰阶)
final.tint = tokens.tint
T.applyTo(root, final)
} catch (e) { /* 样式写失败不影响功能 */ }
}
function hexToRgb (hex) {
const h = normHex(hex) || DEFAULTS.accent
const h = normHex(hex) || DEFAULTS.seeds.accent
return { r: parseInt(h.slice(1, 3), 16), g: parseInt(h.slice(3, 5), 16), b: parseInt(h.slice(5, 7), 16) }
}
function rgbToHex (r, gg, b) {
@@ -147,7 +248,7 @@
const readableOn = (bg) => (contrast(bg, '#1c2333') >= 3 ? '#1c2333' : '#ffffff')
function ColorPicker (props) {
const value = normHex(props && props.value) || DEFAULTS.accent
const value = normHex(props && props.value) || DEFAULTS.seeds.accent
const onChange = (props && props.onChange) || function () {}
const [open, setOpen] = React.useState(false)
const [hsv, setHsv] = React.useState(() => { const c = hexToRgb(value); return rgbToHsv(c.r, c.g, c.b) })
@@ -228,26 +329,65 @@
)
}
function NumberRow (props) {
const { label, value, min, max, step, unit, onChange, title } = props
return el('div', { className: 'iwb-row' },
el('span', { className: 'iwb-label', title: title || '' }, label),
el('input', { type: 'range', min, max, step, value, className: 'iwb-appear-range', onChange: (e) => onChange(Number(e.target.value)) }),
el('span', { className: 'iwb-appear-val' }, String(value) + (unit || '')),
)
}
// ── 内置预设 ────────────────────────────────────────────────────────────────
// 「休息」= 低刺激暖灰:压蓝光、降对比刺激,但正文仍守在 4.5:1(长时间盯屏取向)。
const PRESETS = [
{ name: '默认', values: {} },
{ name: '护眼深色', values: { canvasBgLight: '#cfd6cf', canvasBgDark: '#3c4a42', gridDotLight: '#b9c4b9', gridDotDark: '#6f7f73', edgeColorLight: '#9aa79c', edgeColorDark: '#8fa694', accent: '#3f8f6b', edgeHotLight: '#3f8f6b', edgeHotDark: '#7fe0b0', nodeBgLight: '#eef2ee', nodeBgDark: '#4a574e' } },
{ name: '高对比', values: { canvasBgLight: '#ffffff', canvasBgDark: '#111111', gridDotLight: '#d0d0d0', gridDotDark: '#3a3a3a', edgeColorLight: '#111111', edgeColorDark: '#ffffff', edgeWidth: 2.4, edgeDash: 'solid', accent: '#0057d8', edgeHotLight: '#0057d8', edgeHotDark: '#59a4ff', nodeBgLight: '#ffffff', nodeBgDark: '#1e1e1e', nodeBorderLight: '#8a8a8a', nodeBorderDark: '#9a9a9a', nodeSelWidth: 2 } },
{ name: '纸质', values: { canvasBgLight: '#efe7d7', canvasBgDark: '#3b3227', gridDotLight: '#d8cbb4', gridDotDark: '#5a4c3d', edgeColorLight: '#b9a88c', edgeColorDark: '#a2917a', edgeDash: 'solid', accent: '#8a6f4a', edgeHotLight: '#8a6f4a', edgeHotDark: '#e0c9a0', nodeBgLight: '#faf6ee', nodeBgDark: '#4a4034', nodeBorderLight: '#ddd0b8', nodeBorderDark: '#6b5c49' } },
{ name: '默认', seeds: { neutral: '#808080', accent: '#6b7280', depth: 22 }, overrides: {}, overridesDark: {}, sizes: {} },
{
name: '休息',
seeds: { neutral: '#8b8578', accent: '#7a6a52', depth: 30 },
overrides: {
bg: '#f2ede4', panel: '#faf6ee', layer: '#ece6db', layer2: '#fdfbf6',
border: '#ddd5c6', border2: '#cfc5b2', canvasBg: '#e6dfd2', gridDot: '#d5cbb8',
edge: '#c3b8a4', edgeHot: '#7a6a52',
text: '#2c2823', text2: '#5a544c', nodeText: '#2c2823', nodeText2: '#5a544c',
nodeBg: '#f4f0e8',
},
overridesDark: {
bg: '#1c1a17', panel: '#24211d', layer: '#2a2622', layer2: '#302c27',
border: '#57524a', border2: '#4a443b', canvasBg: '#302c26', gridDot: '#4a443b',
edge: '#5f584e', edgeHot: '#b99c74',
text: '#e8e2d6', text2: '#b3ac9e', nodeText: '#e8e2d6', nodeText2: '#b3ac9e',
nodeBg: '#3a3630',
},
sizes: {},
},
{ name: '护眼深色', seeds: { neutral: '#7d8a80', accent: '#3f8f6b', depth: 26 }, overrides: {}, overridesDark: {}, sizes: {} },
{
name: '高对比',
seeds: { neutral: '#9a9a9a', accent: '#0057d8', depth: 44 },
overrides: {
bg: '#ffffff', panel: '#ffffff', layer: '#f4f4f4', border: '#8a8a8a', border2: '#9a9a9a',
text: '#0b0c0f', text2: '#3a3f4a', nodeBg: '#ffffff', nodeText: '#0b0c0f', nodeText2: '#3a3f4a', edge: '#111111',
},
overridesDark: {
bg: '#0d0d0d', panel: '#171717', layer: '#222222', border: '#9a9a9a', border2: '#808080',
text: '#ffffff', text2: '#c9ccd2', nodeBg: '#1e1e1e', nodeText: '#ffffff', nodeText2: '#c9ccd2', edge: '#ffffff',
},
sizes: { edgeWidth: 2.4, edgeDash: 'solid', nodeSelWidth: 2 },
},
{
name: '纸质',
seeds: { neutral: '#a99a86', accent: '#8a6f4a', depth: 30 },
overrides: {
bg: '#f6f0e4', panel: '#fdfaf3', layer: '#efe7d7', layer2: '#fffdf8',
border: '#ddd0b8', border2: '#d3c4ab', canvasBg: '#efe7d7', gridDot: '#d8cbb4',
edge: '#b9a88c', edgeHot: '#8a6f4a',
text: '#2f2a22', text2: '#5f574a', nodeText: '#2f2a22', nodeText2: '#5f574a',
nodeBg: '#faf6ee',
},
overridesDark: {
bg: '#221f1a', panel: '#2b2721', layer: '#332e27', layer2: '#3a342c',
border: '#6b5c49', border2: '#544a3d', canvasBg: '#3b3227', gridDot: '#5a4c3d',
edge: '#a2917a', edgeHot: '#e0c9a0',
text: '#efe6d8', text2: '#c0b4a1', nodeText: '#efe6d8', nodeText2: '#c0b4a1',
nodeBg: '#4a4034',
},
sizes: { edgeDash: 'solid' },
},
]
// ── 外观面板(v3.25.75 重构)───────────────────────────────────────────────
// 用户反馈:分组混乱、颜色字段看不出可点、网格三项无效、对比度警告刷屏。
// 现在:4 个分组(主题 / 画布 / 连线 / 节点),统一两栏网格(标签 120px + 控件),
// 每组组头右侧「恢复本组默认」;颜色统一为「色块 + hex + ▾」;警告收进行尾。
// ── 外观面板 ────────────────────────────────────────────────────────────────
function AppearanceTab (props) {
const draft = (props && props.draft) || {}
const setDraft = (props && props.setDraft) || function () {}
@@ -255,23 +395,57 @@
const setTheme = (props && props.setTheme) || function () {}
const applyTheme = (props && props.applyTheme) || function () {}
const value = normalize(draft)
const set = (patch) => setDraft((d) => Object.assign({}, d, {
canvas: Object.assign({}, d.canvas, { appearance: Object.assign({}, normalize(d), patch) }),
}))
const resetGroup = (keys) => { const p = {}; for (const k of keys) p[k] = DEFAULTS[k]; set(p) }
const root = g.document && g.document.documentElement
const isDark = !!(root && root.classList && root.classList.contains('theme-dark'))
const follow = followTokens(value, isDark) || {}
const ov = isDark ? value.overridesDark : value.overrides
const modeKey = isDark ? 'overridesDark' : 'overrides'
// 所有改动都走 normalize 后的完整 v2 结构,避免半截字段写进配置
const upd = (fn) => setDraft((d) => {
const a = normalize(d)
fn(a)
return Object.assign({}, d, { canvas: Object.assign({}, d.canvas, { appearance: a }) })
})
const setSeed = (k, v) => upd((a) => { a.seeds[k] = v })
const setSize = (k, v) => upd((a) => { a.sizes[k] = v })
const setOv = (k, v) => upd((a) => { a[modeKey] = Object.assign({}, a[modeKey], { [k]: v }) })
const clearOv = (k) => upd((a) => { const m = Object.assign({}, a[modeKey]); delete m[k]; a[modeKey] = m })
const followVal = (k) => follow[OV_TOKEN[k]] || '#808080'
const importRef = React.useRef(null)
const Row = function (label, control, note) {
return el('div', { className: 'iwb-appear-row' + (note ? ' iwb-appear-row-note' : '') },
const ColorRow = function (label, key, extra) {
const has = !!ov[key]
return el('div', { className: 'iwb-appear-row' },
el('span', { className: 'iwb-appear-label', title: label }, label),
el('div', { className: 'iwb-appear-ctrl' }, control),
note ? el('span', { className: 'iwb-appear-note' }, note) : null)
el('div', { className: 'iwb-appear-ctrl' },
el(ColorPicker, { value: has ? ov[key] : followVal(key), onChange: (v) => setOv(key, v) })),
el('span', { className: 'iwb-appear-note' },
has
? el('button', { type: 'button', className: 'iwb-appear-revert', title: '恢复跟随自动配色', onClick: () => clearOv(key) }, '自定义 ↺')
: '跟随 ' + followVal(key).toUpperCase(),
extra || null))
}
const Group = function (title, rows, keys) {
const TextRow = function (label, key, bgKey) {
const fg = ov[key] || followVal(key)
const bg = ov[bgKey] || followVal(bgKey)
const r = contrast(fg, bg)
const cls = r >= 4.5 ? 'ok' : (r >= 3 ? 'warn' : 'bad')
return ColorRow(label, key, el('span', { className: 'iwb-ratio ' + cls, title: '与底色的对比度(正文 AA 4.5:1)' }, r.toFixed(1) + ':1'))
}
const SizeRow = function (label, key, min, max, step, unit) {
return el('div', { className: 'iwb-appear-row' },
el('span', { className: 'iwb-appear-label', title: label }, label),
el('div', { className: 'iwb-appear-ctrl' },
el('input', { type: 'range', min, max, step, value: value.sizes[key], className: 'iwb-appear-range', onChange: (e) => setSize(key, Number(e.target.value)) })),
el('span', { className: 'iwb-appear-note' }, String(value.sizes[key]) + (unit || '')))
}
const Group = function (title, rows, note) {
return el('section', { className: 'iwb-appear-group' },
el('div', { className: 'iwb-appear-ghead' },
el('span', { className: 'iwb-appear-gtitle' }, title),
keys ? el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => resetGroup(keys) }, '恢复本组默认') : null),
note ? el('span', { className: 'iwb-appear-gnote' }, note) : null),
rows)
}
const Seg = function (val, onChange, items) {
@@ -282,35 +456,44 @@
onClick: () => onChange(item.value),
}, item.label)))
}
const warnOf = function (edge, bg) {
const r = contrast(edge, bg)
return r < 1.5 ? ('⚠ 对比度 ' + r.toFixed(1) + ':1,可能看不清') : ''
}
// 主题下拉:配色模式 + 全部预设合并(用户裁定:合并)
const themeOptions = [{ value: 'theme:light', label: '亮色(节点灰底 + 内容白)' }, { value: 'theme:dark', label: '暗色' }, { value: 'theme:system', label: '跟随系统' }]
const presetOptions = PRESETS.map((p) => ({ value: 'preset:' + p.name, label: '预设 · ' + p.name }))
.concat(value.presets.map((p, i) => ({ value: 'preset-c:' + i, label: '预设 · ' + p.name })))
const onThemePick = (raw) => {
if (raw.indexOf('theme:') === 0) {
const t = raw.slice(6)
const v = (t === 'dark' || t === 'system') ? t : 'light'
setTheme(v); applyTheme(v); return
}
if (raw.indexOf('preset-c:') === 0) {
const p = value.presets[Number(raw.slice(9)) || 0]
if (p && p.values) set(normalize({ canvas: { appearance: p.values } }))
return
}
if (raw.indexOf('preset:') === 0) {
const name = raw.slice(7)
const p = PRESETS.find((x) => x.name === name)
if (p) set(Object.assign({}, DEFAULTS, p.values))
}
// 一键把 4 个文字项夹到 4.5:1(用户显式点了才动,不自动改)
const fixText = () => {
const T = g.IWBThemeTokens
if (!T || typeof T.clampText !== 'function') return
const bg = ov.bg || followVal('bg')
const nbg = ov.nodeBg || followVal('nodeBg')
upd((a) => {
const m = Object.assign({}, a[modeKey])
m.text = T.clampText(m.text || followVal('text'), bg, 4.5)
m.text2 = T.clampText(m.text2 || followVal('text2'), bg, 4.5)
m.nodeText = T.clampText(m.nodeText || followVal('nodeText'), nbg, 4.5)
m.nodeText2 = T.clampText(m.nodeText2 || followVal('nodeText2'), nbg, 4.5)
a[modeKey] = m
})
}
const usePreset = (p) => upd((a) => {
a.seeds = Object.assign({}, a.seeds, p.seeds || {})
a.overrides = Object.assign({}, p.overrides || {})
a.overridesDark = Object.assign({}, p.overridesDark || {})
a.sizes = Object.assign({}, a.sizes, p.sizes || {})
})
const savePreset = () => {
const name = (g.prompt ? g.prompt('预设名称', '我的配色') : '') || ''
if (!name) return
upd((a) => {
a.presets = a.presets.concat([{
name: String(name).slice(0, 20),
seeds: Object.assign({}, a.seeds),
overrides: Object.assign({}, a.overrides),
overridesDark: Object.assign({}, a.overridesDark),
sizes: Object.assign({}, a.sizes),
}]).slice(0, 12)
})
}
const exportPresets = () => {
try {
const doc = { version: 1, appearance: Object.assign({}, value, { presets: undefined }) }
const doc = { version: 2, appearance: Object.assign({}, value, { presets: undefined }) }
const blob = new Blob([JSON.stringify(doc, null, 2)], { type: 'application/json' })
const a = g.document.createElement('a')
a.href = URL.createObjectURL(blob)
@@ -327,82 +510,119 @@
const doc = JSON.parse(String(rd.result || '{}'))
const src = doc && (doc.appearance || doc)
if (!src || typeof src !== 'object') return
set(normalize({ canvas: { appearance: src } }))
upd((a) => {
const n = normalize({ canvas: { appearance: src } })
a.seeds = n.seeds; a.overrides = n.overrides; a.overridesDark = n.overridesDark
a.sizes = n.sizes
})
} catch (e) { /* 忽略坏文件 */ }
}
rd.readAsText(file)
} catch (e) { /* ignore */ }
}
const themeGroup = Group('主题', [
Row('主题', el('select', {
className: 'iwb-input iwb-appear-select', value: 'theme:' + theme,
onChange: (e) => onThemePick(e.target.value),
}, themeOptions.concat(presetOptions).map((o) => el('option', { key: o.value, value: o.value }, o.label))),
'切换配色模式或直接套用预设'),
Row('主题色', el(ColorPicker, { value: value.accent, onChange: (v) => set({ accent: v }) }), '按钮 / 端口 / 选中框'),
Row('预设', el('div', { className: 'iwb-appear-btns' },
el('button', {
className: 'iwb-btn iwb-btn-sm',
onClick: () => {
const name = (g.prompt ? g.prompt('预设名称', '我的配色') : '') || ''
if (!name) return
const snap = Object.assign({}, value, { presets: undefined })
set({ presets: value.presets.concat([{ name: String(name).slice(0, 20), values: snap }]).slice(0, 12) })
},
}, '保存当前为预设'),
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: exportPresets }, '导出'),
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => importRef.current && importRef.current.click() }, '导入'),
el('input', { ref: importRef, type: 'file', accept: 'application/json', style: { display: 'none' }, onChange: (e) => { const f = e.target.files && e.target.files[0]; if (f) importFile(f) } }))),
], null)
const canvasGroup = Group('画布', [
Row('网格样式', Seg(value.gridStyle, (v) => set({ gridStyle: v, gridOn: v !== 'blank' }), [
{ value: 'dots', label: '点' }, { value: 'lines', label: '线' }, { value: 'blank', label: '空白' },
]), '空白 = 不显示背景网格'),
Row('底色(亮)', el(ColorPicker, { value: value.canvasBgLight, onChange: (v) => set({ canvasBgLight: v }) })),
Row('底色(暗)', el(ColorPicker, { value: value.canvasBgDark, onChange: (v) => set({ canvasBgDark: v }) })),
Row(value.gridStyle === 'lines' ? '网格线色(亮)' : '网格点(亮)', el(ColorPicker, { value: value.gridDotLight, onChange: (v) => set({ gridDotLight: v }) })),
Row(value.gridStyle === 'lines' ? '网格线色(暗)' : '网格点(暗)', el(ColorPicker, { value: value.gridDotDark, onChange: (v) => set({ gridDotDark: v }) })),
Row(value.gridStyle === 'lines' ? '网格线粗细' : '网格点大小', el('input', { type: 'range', min: 0.8, max: 2.6, step: 0.1, value: value.gridSize, className: 'iwb-appear-range', onChange: (e) => set({ gridSize: Number(e.target.value) }) }),
String(value.gridSize) + 'px'),
Row('网格间距', el('input', { type: 'range', min: 16, max: 40, step: 1, value: value.gridGap, className: 'iwb-appear-range', onChange: (e) => set({ gridGap: Number(e.target.value) }) }),
String(value.gridGap) + 'px'),
], ['canvasBgLight', 'canvasBgDark', 'gridDotLight', 'gridDotDark', 'gridSize', 'gridGap', 'gridOn', 'gridStyle'])
const edgeGroup = Group('连线', [
Row('默认色(亮)', el(ColorPicker, { value: value.edgeColorLight, onChange: (v) => set({ edgeColorLight: v }) }), warnOf(value.edgeColorLight, value.canvasBgLight)),
Row('默认色(暗)', el(ColorPicker, { value: value.edgeColorDark, onChange: (v) => set({ edgeColorDark: v }) }), warnOf(value.edgeColorDark, value.canvasBgDark)),
Row('粗细', el('input', { type: 'range', min: 1, max: 4, step: 0.1, value: value.edgeWidth, className: 'iwb-appear-range', onChange: (e) => set({ edgeWidth: Number(e.target.value) }) }), String(value.edgeWidth) + 'px'),
Row('线型', el('select', { className: 'iwb-input iwb-appear-select', value: value.edgeDash, onChange: (e) => set({ edgeDash: e.target.value }) },
el('option', { value: 'solid' }, '实线'), el('option', { value: 'dashed' }, '虚线'), el('option', { value: 'dotted' }, '点线'))),
Row('高亮色(亮)', el(ColorPicker, { value: value.edgeHotLight, onChange: (v) => set({ edgeHotLight: v }) }), '仅选中/悬停/临时连线'),
Row('高亮色(暗)', el(ColorPicker, { value: value.edgeHotDark, onChange: (v) => set({ edgeHotDark: v }) })),
], ['edgeColorLight', 'edgeColorDark', 'edgeWidth', 'edgeDash', 'edgeHotLight', 'edgeHotDark'])
const nodeGroup = Group('节点', [
Row('节点底色(亮)', el(ColorPicker, { value: value.nodeBgLight, onChange: (v) => set({ nodeBgLight: v }) })),
Row('节点底色(暗)', el(ColorPicker, { value: value.nodeBgDark, onChange: (v) => set({ nodeBgDark: v }) })),
Row('边框色(亮)', el(ColorPicker, { value: value.nodeBorderLight, onChange: (v) => set({ nodeBorderLight: v }) })),
Row('边框色(暗)', el(ColorPicker, { value: value.nodeBorderDark, onChange: (v) => set({ nodeBorderDark: v }) })),
Row('节点圆角', el('input', { type: 'range', min: 6, max: 20, step: 1, value: value.nodeRadius, className: 'iwb-appear-range', onChange: (e) => set({ nodeRadius: Number(e.target.value) }) }), String(value.nodeRadius) + 'px'),
Row('选中描边', el('input', { type: 'range', min: 1, max: 4, step: 0.5, value: value.nodeSelWidth, className: 'iwb-appear-range', onChange: (e) => set({ nodeSelWidth: Number(e.target.value) }) }), String(value.nodeSelWidth) + 'px'),
], ['nodeBgLight', 'nodeBgDark', 'nodeBorderLight', 'nodeBorderDark', 'nodeRadius', 'nodeSelWidth'])
const autoGroup = Group('自动配色', [
Row('自动配色', el('label', { className: 'iwb-check' },
el('input', { type: 'checkbox', checked: value.auto, onChange: (e) => set({ auto: e.target.checked }) }),
'按 4 个种子自动派生整套配色'), '关闭后可逐项手动调节'),
Row('中性基调', el(ColorPicker, { value: value.seedNeutral, onChange: (v) => set({ seedNeutral: v, auto: true }) }), '整机灰阶的基准'),
Row('画布深浅', el('input', { type: 'range', min: 0, max: 60, step: 1, value: value.depth, className: 'iwb-appear-range', onChange: (e) => set({ depth: Number(e.target.value), auto: true }) }), String(value.depth)),
Row('强调色', el(ColorPicker, { value: value.seedAccent, onChange: (v) => set({ seedAccent: v, auto: true }) }), '按钮 / 端口 / 选中框'),
], ['auto', 'seedNeutral', 'seedAccent', 'depth'])
const summary = [
['底色', followVal('bg')], ['面板', followVal('panel')],
['节点', followVal('nodeBg')], ['画布', followVal('canvasBg')],
['文字', ov.text || followVal('text')], ['强调', ov.brand || followVal('brand')],
]
return el('div', { className: 'iwb-appear' },
autoGroup, themeGroup, value.auto ? el('div', { className: 'iwb-appear-note-only' }, '自动配色已开启;关闭后可逐项微调(画布 / 连线 / 节点)。') : canvasGroup, value.auto ? null : edgeGroup, value.auto ? null : nodeGroup,
el('div', { className: 'iwb-row', style: { marginTop: 12 } },
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => set(Object.assign({}, DEFAULTS, { presets: value.presets })) }, '全部恢复默认')))
el('div', { className: 'iwb-appear-mode' },
Seg(theme, (v) => { setTheme(v); applyTheme(v) }, [
{ value: 'light', label: '亮色' }, { value: 'dark', label: '暗色' }, { value: 'system', label: '跟随系统' },
]),
el('span', { className: 'iwb-appear-modehint' }, '正在微调:' + (isDark ? '暗色' : '亮色'))),
Group('配色来源', [
el('div', { className: 'iwb-appear-row' },
el('span', { className: 'iwb-appear-label' }, '中性基调'),
el('div', { className: 'iwb-appear-ctrl' }, el(ColorPicker, { value: value.seeds.neutral, onChange: (v) => setSeed('neutral', v) })),
el('span', { className: 'iwb-appear-note' }, '整机灰阶基准')),
el('div', { className: 'iwb-appear-row' },
el('span', { className: 'iwb-appear-label' }, '强调色'),
el('div', { className: 'iwb-appear-ctrl' }, el(ColorPicker, { value: value.seeds.accent, onChange: (v) => setSeed('accent', v) })),
el('span', { className: 'iwb-appear-note' }, '按钮 / 端口 / 选中框')),
el('div', { className: 'iwb-appear-row' },
el('span', { className: 'iwb-appear-label' }, '画布深浅'),
el('div', { className: 'iwb-appear-ctrl' },
el('input', { type: 'range', min: 0, max: 60, step: 1, value: value.seeds.depth, className: 'iwb-appear-range', onChange: (e) => setSeed('depth', Number(e.target.value)) })),
el('span', { className: 'iwb-appear-note' }, String(value.seeds.depth))),
el('div', { className: 'iwb-appear-swaches' }, summary.map(([n, c]) => el('span', { key: n, className: 'iwb-appear-sw' },
el('i', { style: { background: c } }), n))),
], '改这里,下面没动过的项会自动跟着变'),
Group('界面微调', [
ColorRow('底色', 'bg'),
ColorRow('面板 / 卡片', 'panel'),
ColorRow('边框', 'border'),
ColorRow('分隔线', 'border2'),
], '只改你动过的项'),
Group('字体与对比度', [
TextRow('界面主文字', 'text', 'bg'),
TextRow('界面次文字', 'text2', 'bg'),
TextRow('节点主文字', 'nodeText', 'nodeBg'),
TextRow('节点次文字', 'nodeText2', 'nodeBg'),
el('div', { className: 'iwb-appear-row' },
el('span', { className: 'iwb-appear-label' }, '对比度修正'),
el('div', { className: 'iwb-appear-ctrl' },
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: fixText }, '把 4 项文字夹到 4.5:1')),
el('span', { className: 'iwb-appear-note' }, '徽标 <4.5 时点这里')),
], '正文 AA = 4.5:1,低于 3 会难读'),
Group('画布', [
el('div', { className: 'iwb-appear-row' },
el('span', { className: 'iwb-appear-label' }, '网格样式'),
el('div', { className: 'iwb-appear-ctrl' }, Seg(value.sizes.gridStyle, (v) => upd((a) => { a.sizes.gridStyle = v; a.sizes.gridOn = v !== 'blank' }), [
{ value: 'dots', label: '点' }, { value: 'lines', label: '线' }, { value: 'blank', label: '空白' },
]))),
ColorRow('底色', 'canvasBg'),
ColorRow('网格点 / 线', 'gridDot'),
SizeRow('网格大小', 'gridSize', 0.8, 2.6, 0.1, 'px'),
SizeRow('网格间距', 'gridGap', 16, 40, 1, 'px'),
], null),
Group('连线', [
ColorRow('默认色', 'edge'),
ColorRow('高亮色', 'edgeHot'),
SizeRow('粗细', 'edgeWidth', 1, 4, 0.1, 'px'),
el('div', { className: 'iwb-appear-row' },
el('span', { className: 'iwb-appear-label' }, '线型'),
el('div', { className: 'iwb-appear-ctrl' },
el('select', { className: 'iwb-input iwb-appear-select', value: value.sizes.edgeDash, onChange: (e) => setSize('edgeDash', e.target.value) },
el('option', { value: 'solid' }, '实线'), el('option', { value: 'dashed' }, '虚线'), el('option', { value: 'dotted' }, '点线')))),
], null),
Group('节点', [
ColorRow('节点底色', 'nodeBg'),
SizeRow('圆角', 'nodeRadius', 6, 20, 1, 'px'),
SizeRow('选中描边', 'nodeSelWidth', 1, 4, 0.5, 'px'),
], '节点文字在上面「字体与对比度」里'),
Group('预设', [
el('div', { className: 'iwb-appear-presets' },
PRESETS.map((p) => el('button', { key: p.name, className: 'iwb-btn iwb-btn-sm', onClick: () => usePreset(p) }, p.name)),
value.presets.map((p, i) => el('span', { key: 'c' + i, className: 'iwb-appear-preset' },
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => usePreset(p) }, p.name),
el('button', {
className: 'iwb-appear-del', title: '删除这个预设',
onClick: () => upd((a) => { a.presets = a.presets.filter((_, j) => j !== i) }),
}, '✕')))),
el('div', { className: 'iwb-appear-btns' },
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: savePreset }, '保存当前为预设'),
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: exportPresets }, '导出'),
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => importRef.current && importRef.current.click() }, '导入'),
el('input', { ref: importRef, type: 'file', accept: 'application/json', style: { display: 'none' }, onChange: (e) => { const f = e.target.files && e.target.files[0]; if (f) importFile(f) } }),
el('button', {
className: 'iwb-btn iwb-btn-sm',
onClick: () => upd((a) => { a.seeds = cleanSeeds(null); a.overrides = {}; a.overridesDark = {}; a.sizes = cleanSizes(null) }),
}, '全部恢复默认')),
], '预设存的是「种子 + 覆盖 + 尺寸」整份快照'),
)
}
g.IWBUIApearance = Object.freeze({ DEFAULTS, PRESETS, DASH, normalize, apply, contrast, ColorPicker, AppearanceTab })
g.IWBUIApearance = Object.freeze({
DEFAULTS, PRESETS, DASH, OV_TOKEN, normalize, apply, followTokens, contrast, ColorPicker, AppearanceTab,
})
})(typeof window !== 'undefined' ? window : globalThis)