/* ui-appearance.js —— 外观(界面主题 / 画布 / 连线 / 节点 / 主题色)与取色器(v3.25.22) * * 设计边界(与用户确认过): * · 开放:画布底色、网格、连线(颜色/粗细/线型/高亮)、节点(底色/边框/圆角/选中描边)、主题色 * · 不开放:界面背景 / 面板底色 / 文字颜色 —— 它们被大量 color-mix 派生,开放后极易变成不可读 * · 所有颜色项**亮/暗各存一套**;尺寸类(粗细、圆角、间距)共用一套 * · 全部最终落到 CSS 变量上(改一个变量全局生效,不需要改渲染逻辑) */ ;(function (global) { 'use strict' const g = /** @type {any} */ (global || {}) 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, 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 || '')) const normHex = (v) => { const s = String(v || '').trim() if (!isHex(s)) return '' if (s.length === 4) return ('#' + s[1] + s[1] + s[2] + s[2] + s[3] + s[3]).toLowerCase() return s.toLowerCase() } const clamp = (v, lo, hi, def) => { const n = Number(v) if (!Number.isFinite(n)) return def 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] 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, 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'), 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) : [], } } function apply (cfg, dark) { try { const a = normalize(cfg) 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') set('--iwb-grid-on', a.gridOn ? '1' : '0') 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') } catch (e) { /* 样式写失败不影响功能 */ } } function hexToRgb (hex) { const h = normHex(hex) || DEFAULTS.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) { const p = (n) => Math.max(0, Math.min(255, Math.round(n))).toString(16).padStart(2, '0') return '#' + p(r) + p(gg) + p(b) } function rgbToHsv (r, gg, b) { const R = r / 255, G = gg / 255, B = b / 255 const max = Math.max(R, G, B), min = Math.min(R, G, B) const d = max - min let h = 0 if (d) { if (max === R) h = ((G - B) / d) % 6 else if (max === G) h = (B - R) / d + 2 else h = (R - G) / d + 4 h *= 60 if (h < 0) h += 360 } return { h: h, s: max ? d / max : 0, v: max } } function hsvToRgb (h, s, v) { const c = v * s const x = c * (1 - Math.abs(((h / 60) % 2) - 1)) const m = v - c let R = 0, G = 0, B = 0 if (h < 60) { R = c; G = x } else if (h < 120) { R = x; G = c } else if (h < 180) { G = c; B = x } else if (h < 240) { G = x; B = c } else if (h < 300) { R = x; B = c } else { R = c; B = x } return { r: (R + m) * 255, g: (G + m) * 255, b: (B + m) * 255 } } const lum = (rgb) => { const f = (v) => { const c = v / 255; return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4) } return 0.2126 * f(rgb.r) + 0.7152 * f(rgb.g) + 0.0722 * f(rgb.b) } function contrast (a, b) { const l1 = lum(hexToRgb(a)), l2 = lum(hexToRgb(b)) const hi = Math.max(l1, l2), lo = Math.min(l1, l2) return (hi + 0.05) / (lo + 0.05) } const readableOn = (bg) => (contrast(bg, '#1c2333') >= 3 ? '#1c2333' : '#ffffff') function ColorPicker (props) { const value = normHex(props && props.value) || DEFAULTS.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) }) const boxRef = React.useRef(null) React.useEffect(() => { const c = hexToRgb(value) setHsv((prev) => { const next = rgbToHsv(c.r, c.g, c.b) return { h: next.s === 0 ? prev.h : next.h, s: next.s, v: next.v } }) }, [value]) const emit = (next) => { const c = hsvToRgb(next.h, next.s, next.v); onChange(rgbToHex(c.r, c.g, c.b)) } const pick = (e) => { const box = boxRef.current if (!box || !box.getBoundingClientRect) return const r = box.getBoundingClientRect() const x = Math.max(0, Math.min(1, (e.clientX - r.left) / r.width)) const y = Math.max(0, Math.min(1, (e.clientY - r.top) / r.height)) const next = { h: hsv.h, s: x, v: 1 - y } setHsv(next); emit(next) } const hue = (e) => { const next = { h: Number(e.target.value) || 0, s: hsv.s || 0.6, v: hsv.v || 1 }; setHsv(next); emit(next) } const rgb = hexToRgb(value) const setChannel = (k) => (e) => { const n = Math.max(0, Math.min(255, Number(e.target.value) || 0)) const c = { ...rgb }; c[k] = n onChange(rgbToHex(c.r, c.g, c.b)) } const hueColor = 'hsl(' + Math.round(hsv.h) + ',100%,50%)' return el('div', { className: 'iwb-cpick' }, el('button', { type: 'button', className: 'iwb-cpick-swatch', title: '点击取色', onClick: () => setOpen((v) => !v), style: { background: value, color: readableOn(value) }, }, el('span', { className: 'iwb-cpick-hex' }, value.toUpperCase())), open ? el('div', { className: 'iwb-cpick-pop', onPointerDown: (e) => e.stopPropagation() }, el('div', { className: 'iwb-cpick-box', ref: boxRef, onPointerDown: (e) => { e.currentTarget.setPointerCapture && e.currentTarget.setPointerCapture(e.pointerId); pick(e) }, onPointerMove: (e) => { if (e.buttons === 1) pick(e) }, style: { background: 'linear-gradient(to top, #000, rgba(0,0,0,0)), linear-gradient(to right, #fff, ' + hueColor + ')' }, }, el('div', { className: 'iwb-cpick-dot', style: { left: (hsv.s * 100) + '%', top: ((1 - hsv.v) * 100) + '%', background: value } })), el('input', { type: 'range', min: 0, max: 360, value: Math.round(hsv.h), onChange: hue, className: 'iwb-cpick-hue', style: { background: 'linear-gradient(to right,#f00,#ff0,#0f0,#0ff,#00f,#f0f,#f00)' } }), el('div', { className: 'iwb-cpick-rgb' }, ['r', 'g', 'b'].map((k) => el('label', { key: k, className: 'iwb-cpick-chan' }, el('input', { type: 'number', min: 0, max: 255, value: rgb[k], onChange: setChannel(k) }), el('span', null, k.toUpperCase())))), el('div', { className: 'iwb-cpick-foot' }, el('input', { className: 'iwb-input iwb-cpick-input', value: value, onChange: (e) => { const v = normHex(e.target.value); if (v) onChange(v) }, title: '支持 #rgb / #rrggbb' }), g.EyeDropper ? el('button', { type: 'button', className: 'iwb-btn iwb-btn-sm', title: '从屏幕取色', onClick: () => { try { new g.EyeDropper().open().then((r) => { const v = normHex(r && r.sRGBHex); if (v) onChange(v) }).catch(() => {}) } catch (e) { /* ignore */ } } }, '吸管') : null, el('button', { type: 'button', className: 'iwb-btn iwb-btn-sm', onClick: () => setOpen(false) }, '完成')), ) : null, ) } 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 || '')), ) } 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' } }, ] function AppearanceTab (props) { const draft = (props && props.draft) || {} const setDraft = (props && props.setDraft) || function () {} const theme = (props && props.theme) || 'light' 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 setMany = (patch) => set(patch) const resetGroup = (keys) => { const p = {}; for (const k of keys) p[k] = DEFAULTS[k]; set(p) } const warnEdge = (edge, bg) => { const r = contrast(edge, bg) return r < 1.5 ? el('div', { className: 'iwb-hint iwb-hint-warn' }, '连线与画布底色对比度仅 ' + r.toFixed(1) + ':1,可能看不清') : null } const importRef = React.useRef(null) const exportPresets = () => { try { const doc = { version: 1, 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) a.download = 'iwb-appearance.json' a.click() setTimeout(() => URL.revokeObjectURL(a.href), 3000) } catch (e) { /* ignore */ } } const importFile = (file) => { try { const rd = new g.FileReader() rd.onload = () => { try { const doc = JSON.parse(String(rd.result || '{}')) const src = doc && (doc.appearance || doc) if (!src || typeof src !== 'object') return set(normalize({ canvas: { appearance: src } })) } catch (e) { /* 忽略坏文件 */ } } rd.readAsText(file) } catch (e) { /* ignore */ } } return el('div', null, el('div', { className: 'iwb-section-sub' }, '界面主题'), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '配色模式'), el('select', { className: 'iwb-input', title: '界面配色(亮色保证文字清晰)', value: theme, onChange: (e) => { const t = e.target.value; const v = (t === 'dark' || t === 'system') ? t : 'light'; setTheme(v); applyTheme(v) }, }, el('option', { value: 'light' }, '亮色(默认:节点灰底 + 内容白)'), el('option', { value: 'dark' }, '暗色'), el('option', { value: 'system' }, '跟随系统'), )), el('div', { className: 'iwb-section-sub' }, '预设'), el('div', { className: 'iwb-appear-presets' }, PRESETS.map((p) => el('button', { key: p.name, className: 'iwb-btn iwb-btn-sm', onClick: () => setMany(Object.assign({}, DEFAULTS, p.values)), title: '套用「' + p.name + '」' }, p.name)), value.presets.map((p, i) => el('button', { key: 'c' + i, className: 'iwb-btn iwb-btn-sm', onClick: () => set(normalize({ canvas: { appearance: p.values } })), title: '套用自定义预设' }, p.name || ('自定义 ' + (i + 1)))), ), el('div', { className: 'iwb-row' }, 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); e.target.value = '' } }), ), el('div', { className: 'iwb-section-sub' }, '画布'), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '底色(亮)'), el(ColorPicker, { value: value.canvasBgLight, onChange: (v) => set({ canvasBgLight: v }) })), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '底色(暗)'), el(ColorPicker, { value: value.canvasBgDark, onChange: (v) => set({ canvasBgDark: v }) })), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '网格点(亮)'), el(ColorPicker, { value: value.gridDotLight, onChange: (v) => set({ gridDotLight: v }) })), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '网格点(暗)'), el(ColorPicker, { value: value.gridDotDark, onChange: (v) => set({ gridDotDark: v }) })), el(NumberRow, { label: '网格点大小', value: value.gridSize, min: 0.8, max: 2.6, step: 0.1, unit: 'px', onChange: (v) => set({ gridSize: v }) }), el(NumberRow, { label: '网格间距', value: value.gridGap, min: 16, max: 40, step: 1, unit: 'px', onChange: (v) => set({ gridGap: v }) }), el('label', { className: 'iwb-check' }, el('input', { type: 'checkbox', checked: value.gridOn, onChange: (e) => set({ gridOn: e.target.checked }) }, null), ' 显示网格'), el('div', { className: 'iwb-row' }, el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => resetGroup(['canvasBgLight', 'canvasBgDark', 'gridDotLight', 'gridDotDark', 'gridSize', 'gridGap', 'gridOn']) }, '恢复本组默认')), el('div', { className: 'iwb-section-sub' }, '连线'), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '默认色(亮)'), el(ColorPicker, { value: value.edgeColorLight, onChange: (v) => set({ edgeColorLight: v }) })), warnEdge(value.edgeColorLight, value.canvasBgLight), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '默认色(暗)'), el(ColorPicker, { value: value.edgeColorDark, onChange: (v) => set({ edgeColorDark: v }) })), warnEdge(value.edgeColorDark, value.canvasBgDark), el(NumberRow, { label: '默认粗细', value: value.edgeWidth, min: 1, max: 4, step: 0.1, unit: 'px', onChange: (v) => set({ edgeWidth: v }) }), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '线型'), el('select', { className: 'iwb-input', value: value.edgeDash, onChange: (e) => set({ edgeDash: e.target.value }) }, el('option', { value: 'solid' }, '实线'), el('option', { value: 'dashed' }, '虚线'), el('option', { value: 'dotted' }, '点线'), )), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '高亮色(亮)'), el(ColorPicker, { value: value.edgeHotLight, onChange: (v) => set({ edgeHotLight: v }) })), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '高亮色(暗)'), el(ColorPicker, { value: value.edgeHotDark, onChange: (v) => set({ edgeHotDark: v }) })), el('div', { className: 'iwb-hint' }, '高亮色只用于选中节点的上下游连线、悬停连线与临时连线;默认连线永远用上面的「默认色」。'), el('div', { className: 'iwb-row' }, el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => resetGroup(['edgeColorLight', 'edgeColorDark', 'edgeWidth', 'edgeDash', 'edgeHotLight', 'edgeHotDark']) }, '恢复本组默认')), el('div', { className: 'iwb-section-sub' }, '节点'), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '节点底色(亮)'), el(ColorPicker, { value: value.nodeBgLight, onChange: (v) => set({ nodeBgLight: v }) })), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '节点底色(暗)'), el(ColorPicker, { value: value.nodeBgDark, onChange: (v) => set({ nodeBgDark: v }) })), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '边框色(亮)'), el(ColorPicker, { value: value.nodeBorderLight, onChange: (v) => set({ nodeBorderLight: v }) })), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '边框色(暗)'), el(ColorPicker, { value: value.nodeBorderDark, onChange: (v) => set({ nodeBorderDark: v }) })), el(NumberRow, { label: '节点圆角', value: value.nodeRadius, min: 6, max: 20, step: 1, unit: 'px', onChange: (v) => set({ nodeRadius: v }) }), el(NumberRow, { label: '选中描边', value: value.nodeSelWidth, min: 1, max: 4, step: 0.5, unit: 'px', onChange: (v) => set({ nodeSelWidth: v }) }), el('div', { className: 'iwb-row' }, el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => resetGroup(['nodeBgLight', 'nodeBgDark', 'nodeBorderLight', 'nodeBorderDark', 'nodeRadius', 'nodeSelWidth']) }, '恢复本组默认')), el('div', { className: 'iwb-section-sub' }, '主题色'), el('div', { className: 'iwb-row' }, el('span', { className: 'iwb-label' }, '按钮 / 端口 / 选中框'), el(ColorPicker, { value: value.accent, onChange: (v) => set({ accent: v }) })), el('div', { className: 'iwb-hint' }, '主题色同时用于亮色与暗色主题;作用在按钮、端口、选中框与进度动画上。连线用上面的「连线」分组。'), el('div', { className: 'iwb-row', style: { marginTop: 12 } }, el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => setMany(Object.assign({}, DEFAULTS, { presets: value.presets })) }, '全部恢复默认')), ) } g.IWBUIApearance = Object.freeze({ DEFAULTS, PRESETS, DASH, normalize, apply, contrast, ColorPicker, AppearanceTab }) })(typeof window !== 'undefined' ? window : globalThis)