From 4dd3a8c7853d606104d11f5f1b2a5f995b451c24 Mon Sep 17 00:00:00 2001 From: super-agent Date: Wed, 18 Nov 2026 15:03:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(theme):=20=E6=81=A2=E5=A4=8D=E4=B8=BB?= =?UTF-8?q?=E9=A2=98=E6=A1=A5=EF=BC=8C=E5=B7=A5=E4=BD=9C=E5=8F=B0=E9=A1=B5?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E8=B7=9F=E9=9A=8F=20DSH=20=E6=B7=B1=E6=B5=85?= =?UTF-8?q?=E8=89=B2=20(REQ-20261117-0013)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:Gitea 0.5.x 线缺了 qiudl 0.4.x 分支的整套主题桥能力,升级后所有工作台 产物页都不再跟随 DSH 主题(浅色/深色/跟随系统全部失效),页面各自锁死自己的配色。 这是升级引入的真实回归,比单个页面的配色问题严重得多。 移植内容(来自 qiudl 0.4.9-rc.2,theme.ts 无外部依赖,纯函数 + 浏览器脚本): - 新增 src/theme.ts(191 行):THEME_BRIDGE_JS / injectThemeBridge / 前缀判定 - 模板路由补 /api/worktable/template/theme.js(独立打开/嵌套 iframe 显式引用) - 站点路由对 .html/.htm 自动注入主题桥(幂等:已含 data-wt-theme-bridge 则原样) - 新增 04_test/theme-bridge.test.mjs(7 个回归用例,含接线防漏) 顺带修两处写死项(同属「不跟随主题」根因): - dshell.css::root{color-scheme:dark} → color-scheme:light dark + 浅/深两套 兜底 token(历史事故:写死深色会让所有页面锁死深色) - dshell.css:正文字体改走 var(--dsw-font-family, ...) 版本 0.5.3-local.3 -> 0.5.3-local.4 验证:build 通过;client-factory-gate 10/10;主题桥测试 7/7;兼容回归 105/105; release-prep 全过(结构断言、独立安装 import、split anchor dom 14 场景、 sidebar popup、真实 Chromium split margin 5 场景、server home 3 场景) --- 01_content/dsh.plugin.json | 2 +- 01_content/lib/client.js | 2 +- 01_content/lib/index.js | 172 +++++++++++++++++++++++++++- 01_content/package.json | 2 +- 01_content/src/index.ts | 13 ++- 01_content/src/theme.ts | 191 ++++++++++++++++++++++++++++++++ 01_content/template/dshell.css | 34 +++++- 01_content/template/dshell.html | 3 + 04_test/theme-bridge.test.mjs | 136 +++++++++++++++++++++++ PATCH.md | 3 +- 10 files changed, 544 insertions(+), 14 deletions(-) create mode 100644 01_content/src/theme.ts create mode 100644 04_test/theme-bridge.test.mjs diff --git a/01_content/dsh.plugin.json b/01_content/dsh.plugin.json index 26a809e..bc952af 100644 --- a/01_content/dsh.plugin.json +++ b/01_content/dsh.plugin.json @@ -1,7 +1,7 @@ { "name": "dsh-worktable", "description": "工作台:DSH 侧边栏的 agent 级项目容器,收纳如 TravelAtlas 等自装项目", - "version": "0.5.3-local.3", + "version": "0.5.3-local.4", "entry": { "name": "dsh-worktable", "inject": [] diff --git a/01_content/lib/client.js b/01_content/lib/client.js index fd6889e..3829d77 100644 --- a/01_content/lib/client.js +++ b/01_content/lib/client.js @@ -9175,7 +9175,7 @@ var CHANGELOG_V030 = `\u66F4\u65B0\u516C\u544A \xB7 v0.5.1 \u30103.1 \u6587\u5B57\u4E0E\u7EC6\u8282\u6253\u78E8\u3011\u5168\u5C40\u5B57\u4F53\u4E0E\u5B57\u53F7\u7EDF\u4E00\u4F18\u5316\u3001\u4E0B\u62C9\u9762\u677F\u73BB\u7483\u5316\u4E0E\u5BF9\u9F50\u3001\u83DC\u5355\u70B9\u9009\u540E\u4FDD\u6301\u6253\u5F00\u4FBF\u4E8E\u8FDE\u7EED\u9884\u89C8\u3001\u6309\u94AE\u63CF\u8FB9\u4E0E\u60AC\u505C\u53CD\u9988\u7B49\u4EA4\u4E92\u7EC6\u8282\uFF1B\u540C\u65F6\u4FEE\u590D\u4E86\u591A\u9879\u4F53\u9A8C\u95EE\u9898\uFF08\u7167\u7247\u4E0A\u4F20\u6E05\u6670\u5EA6\u3001\u80CC\u666F\u7F51\u683C\u7EBF\u5728\u7167\u7247\u6A21\u5F0F\u4E0B\u4E0D\u751F\u6548\u3001\u6D45\u8272\u4E3B\u9898\u4E0B\u5DE5\u4F5C\u72B6\u6001\u5149\u6548\u4E0D\u53EF\u89C1\u7B49\uFF09\u3002`; // src/client/updateCheck.ts -var LOCAL_VERSION = false ? "dev" : "0.5.3-local.3"; +var LOCAL_VERSION = false ? "dev" : "0.5.3-local.4"; var UPDATE_REPO = "Aisland-SJL/dsh-worktable"; var K_UPDATE_CHECK = "dsh.worktable.updateCheck.v1"; var K_LAST_CHECK = "dsh.worktable.lastUpdateCheck.v1"; diff --git a/01_content/lib/index.js b/01_content/lib/index.js index d8ee5b7..5e97075 100644 --- a/01_content/lib/index.js +++ b/01_content/lib/index.js @@ -117,17 +117,174 @@ function createPreviewFiles(now = () => Date.now()) { }; } +// src/theme.ts +var THEME_VAR_PREFIXES = ["--dsw-", "--dsh-", "--ds-"]; +var THEME_BRIDGE_ID = "wt-theme-bridge"; +var THEME_JS_PATH = "/api/worktable/template/theme.js"; +var THEME_BRIDGE_JS = `(function () { + var PREFIXES = ${JSON.stringify(THEME_VAR_PREFIXES)}; + if (window.__wtThemeBridgeInstalled) return; + window.__wtThemeBridgeInstalled = true; + function ok(name) { + if (!name || name.slice(0, 2) !== '--') return false; + for (var i = 0; i < PREFIXES.length; i++) if (name.slice(0, PREFIXES[i].length) === PREFIXES[i]) return true; + return false; + } + /** \u5411\u4E0A\u627E\u5230\u6700\u5916\u5C42\u540C\u6E90\u7956\u5148\u7A97\u53E3\uFF1B\u8DE8\u57DF\u6216\u5230\u9876\u5C31\u505C\u3002 */ + function hostWindow() { + var w = window; + try { + for (var i = 0; i < 12; i++) { + var p = w.parent; + if (!p || p === w) break; + if (!p.document || !p.document.documentElement) break; + w = p; + } + } catch (e) { /* \u8DE8\u57DF\uFF1A\u505C\u5728\u5F53\u524D\u5C42 */ } + return w === window ? null : w; + } + function hostVars(host) { + var out = []; + try { + var el = host.document.body || host.document.documentElement; + if (!el) return out; + var cs = host.getComputedStyle(el); + for (var i = 0; i < cs.length; i++) { + var n = cs[i]; + if (!ok(n)) continue; + var v = cs.getPropertyValue(n); + if (v && v.trim()) out.push([n, v.trim()]); + } + out.dark = el.hasAttribute && el.hasAttribute('data-ds-dark-theme'); + } catch (e) { /* \u8BFB\u4E0D\u5230\u5C31\u9000\u56DE\u7CFB\u7EDF\u504F\u597D */ } + return out; + } + function systemDark() { + try { return !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches); } catch (e) { return false; } + } + /** \u9875\u9762\u81EA\u5E26\u4E3B\u9898\u5F00\u5173\uFF1F\uFF08\u9875\u9762\u4F18\u5148\uFF0C\u6865\u4E0D\u62A2\uFF09 + * 1) \u8FD0\u884C\u65F6\u663E\u5F0F\uFF1Awindow.__wtThemeBridgeManual = true + * 2) \u6807\u8BB0\uFF1A + * 3) \u8001\u5DE5\u4F5C\u53F0\u9875\u517C\u5BB9\uFF1A \u6216 localStorage['dwb.theme']\uFF08\u65E5\u8BB0\u5DE5\u4F5C\u53F0 / ai\u9012\u5F52\u5B66\u4E60\u7528\u8FD9\u5957\uFF09 + * \u663E\u5F0F\u8DDF\u968F\uFF1A\uFF08\u6216 \uFF09\uFF1D**\u5FFD\u7565**\u4E0A\u9762 3) \u7684 + * legacy \u5224\u5B9A\uFF0C\u5F3A\u5236\u8DDF\u5BBF\u4E3B\u2014\u2014\u540C\u6E90\u9875\u9762\u5171\u7528\u4E00\u4E2A localStorage \u952E\u65F6\uFF0C\u8FD9\u6761\u662F"\u6211\u53EA\u60F3\u8DDF\u5BBF\u4E3B"\u7684\u552F\u4E00\u529E\u6CD5\u3002 */ + function pageManual() { + try { + if (window.__wtThemeBridgeManual) return true; + var el = document.documentElement; + if (el) { + var mode = el.getAttribute && el.getAttribute('data-wt-theme-mode'); + if (mode === 'manual') return true; + if (mode === 'auto' || mode === 'host') return false; + if (el.hasAttribute && el.hasAttribute('data-wt-theme-key')) return false; + if (el.hasAttribute && el.hasAttribute('data-wb-theme')) return true; + } + var v = null; + try { v = localStorage.getItem('dwb.theme'); } catch (e) { v = null; } + if (v === 'dark' || v === 'light') return true; + } catch (e) { /* \u8BFB\u4E0D\u5230\u5C31\u6309\u975E\u624B\u52A8\u5904\u7406 */ } + return false; + } + var applied = []; + /** \u6E05\u6389\u6865\u5199\u8FC7\u7684\u5185\u8054 token\uFF1A\u5207\u5230\u624B\u52A8\u4E3B\u9898\u65F6\u5FC5\u987B\u8BA9\u4F4D\u7ED9\u9875\u9762\u81EA\u5DF1\u7684\u6837\u5F0F\u8868\u503C\u3002 */ + function clearApplied() { + var root = document.documentElement; + for (var i = 0; i < applied.length; i++) { + try { root.style.removeProperty(applied[i]); } catch (e) {} + try { if (document.body) document.body.style.removeProperty(applied[i]); } catch (e) {} + } + applied = []; + } + function apply() { + if (pageManual()) { clearApplied(); return; } + var host = hostWindow(); + var vars = host ? hostVars(host) : []; + var dark = host ? !!vars.dark : systemDark(); + var root = document.documentElement; + for (var i = 0; i < vars.length; i++) root.style.setProperty(vars[i][0], vars[i][1]); + root.style.colorScheme = dark ? 'dark' : 'light'; + if (document.body) { + for (var j = 0; j < vars.length; j++) document.body.style.setProperty(vars[j][0], vars[j][1]); + if (dark) document.body.setAttribute('data-ds-dark-theme', ''); + else document.body.removeAttribute('data-ds-dark-theme'); + } + applied = vars.map(function (p) { return p[0]; }); + } + apply(); + if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', apply, { once: true }); + // \u9875\u9762\u81EA\u5DF1\u5207\u4E3B\u9898\uFF08data-wb-theme / data-wt-theme-mode\uFF09\u65F6\u7ACB\u523B\u91CD\u65B0\u5224\u5B9A\uFF0C\u522B\u7B49\u8F6E\u8BE2 + try { + var MO0 = window.MutationObserver; + if (MO0) new MO0(function () { apply(); }).observe(document.documentElement, { attributes: true, attributeFilter: ['data-wb-theme', 'data-wt-theme-mode'] }); + } catch (e) { /* \u5FFD\u7565 */ } + var host = hostWindow(); + if (host) { + try { + var MO = host.MutationObserver || window.MutationObserver; + var mo = new MO(function () { apply(); }); + var opts = { attributes: true, attributeFilter: ['style', 'class', 'data-ds-dark-theme'] }; + mo.observe(host.document.documentElement, opts); + if (host.document.body) mo.observe(host.document.body, opts); + host.document.addEventListener('DOMContentLoaded', function () { try { mo.observe(host.document.body, opts); } catch (e) {} apply(); }); + } catch (e) { /* \u89C2\u6D4B\u5931\u8D25\u9760\u4E0B\u9762\u7684\u515C\u5E95\u8F6E\u8BE2 */ } + } + try { + var mq = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)'); + if (mq) { if (mq.addEventListener) mq.addEventListener('change', apply); else if (mq.addListener) mq.addListener(apply); } + } catch (e) { /* \u5FFD\u7565 */ } + window.__wtThemeBridgeApply = apply; + setInterval(apply, 5000); // \u515C\u5E95\uFF1A\u5BBF\u4E3B\u6362\u4E3B\u9898\u4E5F\u53EF\u80FD\u53EA\u6362\u6837\u5F0F\u8868\u3001\u4E0D\u52A8\u5C5E\u6027 +})();`; +function injectThemeBridge(html, external = false) { + const src = String(html ?? ""); + if (!src || src.includes(THEME_BRIDGE_ID)) return src; + const tag = external ? `` : ``; + const head = /<\/head\s*>/i.exec(src); + if (head) return src.slice(0, head.index) + tag + src.slice(head.index); + const body = /<\/body\s*>/i.exec(src); + if (body) return src.slice(0, body.index) + tag + src.slice(body.index); + return tag + src; +} + // template/dshell.css var dshell_default = `/* dsh-worktable \u539F\u751F\u76AE\u80A4 \xB7 DSH \u8BBE\u8BA1\u7CFB\u7EDF\u7EC4\u4EF6\u5E93 \u7528\u6CD5\uFF1A - \u6240\u6709\u989C\u8272\u8D70 DSH \u4E3B\u9898\u53D8\u91CF\uFF08--dsw-alias-*\uFF09\uFF0C\u81EA\u52A8\u9002\u914D\u660E\u6697\u4E3B\u9898\u3002 */ -:root { color-scheme: dark; } + \u989C\u8272\u4E00\u5F8B\u8D70 DSH \u4E3B\u9898\u53D8\u91CF\uFF08--dsw-alias-*\uFF09\uFF0C\u81EA\u52A8\u9002\u914D\u660E\u6697\u4E3B\u9898\u3002 + \u4E3B\u9898\u6765\u6E90\u4F18\u5148\u7EA7\uFF1A + 1) \u5DE5\u4F5C\u53F0\u5185\uFF1A\u4E3B\u9898\u6865\uFF08\u7AD9\u70B9\u8DEF\u7531\u81EA\u52A8\u6CE8\u5165\uFF0C\u6216\u663E\u5F0F\u5F15 /api/worktable/template/theme.js\uFF09 + \u628A\u5BBF\u4E3B body \u4E0A\u7684 --dsw-*/--dsh-*/--ds-* \u955C\u50CF\u6210\u9875\u9762\u5185\u8054\u6837\u5F0F \u2192 \u8DDF\u968F DSH \u7684 \u6D45\u8272/\u6DF1\u8272/\u8DDF\u968F\u7CFB\u7EDF\uFF1B + 2) \u72EC\u7ACB\u6253\u5F00\uFF1A\u7528\u4E0B\u9762 :root \u7684\u515C\u5E95\uFF0C\u5E76\u6309\u7CFB\u7EDF\u6DF1\u6D45\u8272\u5207\u6362\u3002 + \u6CE8\u610F\uFF1A\u4E0D\u8981\u5728\u8FD9\u91CC\u5199\u6B7B\u6DF1\u8272\uFF08\u65E7\u7248 :root{color-scheme:dark} \u4F1A\u8BA9\u6240\u6709\u9875\u9762\u9501\u6B7B\u6DF1\u8272\uFF0C + 2026-11 \u4ECE 0.5.x \u7EBF\u6062\u590D\u65F6\u518D\u6B21\u8E29\u5230\uFF0CREQ-20261117-0013 \u4E00\u5E76\u4FEE\u6389\uFF09\u3002 */ +:root { + color-scheme: light dark; + --dsw-alias-bg-base: #ffffff; + --dsw-alias-fill-l1: rgba(16, 24, 40, .03); + --dsw-alias-border-l1: #e4e7ec; + --dsw-alias-border-l2: #d0d5dd; + --dsw-alias-label-primary: #101828; + --dsw-alias-label-secondary: #475467; + --dsw-alias-label-tertiary: #98a2b3; + --dsw-alias-state-accent-primary: #2f6feb; +} +@media (prefers-color-scheme: dark) { + :root { + --dsw-alias-bg-base: #0b0e14; + --dsw-alias-fill-l1: rgba(255, 255, 255, .02); + --dsw-alias-border-l1: #262b36; + --dsw-alias-border-l2: #3a4150; + --dsw-alias-label-primary: #e6e8eb; + --dsw-alias-label-secondary: #9aa4b2; + --dsw-alias-label-tertiary: #6b7280; + --dsw-alias-state-accent-primary: #4f8ef7; + } +} * { box-sizing: border-box; } html, body { margin: 0; padding: 0; } body { background: var(--dsw-alias-bg-base, #0b0e14); color: var(--dsw-alias-label-primary, #e6e8eb); - font-family: -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif; + font-family: var(--dsw-font-family, -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif); font-size: 13px; line-height: 1.6; } @@ -195,7 +352,7 @@ body { `; // template/dshell.html -var dshell_default2 = '\n\n\n\n \n \n \u6211\u7684\u7A97\u53E3\n \n\n\n
\n \n

\u7A97\u53E3\u6807\u9898

\n

\u4E00\u53E5\u8BDD\u8BF4\u660E\u8FD9\u4E2A\u7A97\u53E3\u505A\u4EC0\u4E48\u3002

\n\n \n
\n \u5DF2\u5B8C\u6210\n \u8FDB\u884C\u4E2D\n \u672A\u5F00\u59CB\n
\n\n \n
\n \n \n \n
\n\n \n
\n
\n
\u603B\u6570
\n
128
\n
+12.4%
\n
\n
\n
\u8FDB\u884C\u4E2D
\n
7
\n
\n
\n
\u5DF2\u5B8C\u6210
\n
121
\n
\n
\n\n \n
\n
\n \u6761\u76EE\u4E00\uFF1A\u793A\u4F8B\u5185\u5BB9\u6807\u9898\n \u6628\u5929\n
\n
\n \u6761\u76EE\u4E8C\uFF1A\u793A\u4F8B\u5185\u5BB9\u6807\u9898\n \u5DF2\u53D1\u5E03\n
\n
\n\n \n
\n

\u8BE6\u60C5

\n
\n
\u5B57\u6BB5 A\u503C A
\n
\u5B57\u6BB5 B\u503C B
\n
\n
\n
\n
\n\n \n
\n \n \n
\n
\n\n\n'; +var dshell_default2 = '\n\n\n\n \n \n \n \u6211\u7684\u7A97\u53E3\n \n\n\n
\n \n

\u7A97\u53E3\u6807\u9898

\n

\u4E00\u53E5\u8BDD\u8BF4\u660E\u8FD9\u4E2A\u7A97\u53E3\u505A\u4EC0\u4E48\u3002

\n\n \n
\n \u5DF2\u5B8C\u6210\n \u8FDB\u884C\u4E2D\n \u672A\u5F00\u59CB\n
\n\n \n
\n \n \n \n
\n\n \n
\n
\n
\u603B\u6570
\n
128
\n
+12.4%
\n
\n
\n
\u8FDB\u884C\u4E2D
\n
7
\n
\n
\n
\u5DF2\u5B8C\u6210
\n
121
\n
\n
\n\n \n
\n
\n \u6761\u76EE\u4E00\uFF1A\u793A\u4F8B\u5185\u5BB9\u6807\u9898\n \u6628\u5929\n
\n
\n \u6761\u76EE\u4E8C\uFF1A\u793A\u4F8B\u5185\u5BB9\u6807\u9898\n \u5DF2\u53D1\u5E03\n
\n
\n\n \n
\n

\u8BE6\u60C5

\n
\n
\u5B57\u6BB5 A\u503C A
\n
\u5B57\u6BB5 B\u503C B
\n
\n
\n
\n
\n\n \n
\n \n \n
\n
\n\n\n'; // src/index.ts function baseDshHome() { @@ -225,7 +382,7 @@ function resolveDshHomeSafe() { cachedDshHome = baseDshHome(); return cachedDshHome; } -var PLUGIN_VERSION = false ? "dev" : "0.5.3-local.3"; +var PLUGIN_VERSION = false ? "dev" : "0.5.3-local.4"; var name = "dsh-worktable"; var inject = ["webServer", "sessions"]; var HEALTH_PATH = "/api/worktable/health"; @@ -638,6 +795,9 @@ function apply(ctx) { if (rel === "/dshell.css") { res.writeHead(200, { "content-type": "text/css; charset=utf-8", "cache-control": "no-store" }); res.end(dshell_default); + } else if (rel === "/theme.js") { + res.writeHead(200, { "content-type": "text/javascript; charset=utf-8", "cache-control": "no-store" }); + res.end(THEME_BRIDGE_JS); } else { res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" }); res.end(dshell_default2); @@ -701,7 +861,7 @@ function apply(ctx) { "referrer-policy": "no-referrer", "x-content-type-options": "nosniff" }); - res.end(data); + res.end(ext === "html" || ext === "htm" ? Buffer.from(injectThemeBridge(data.toString("utf8")), "utf8") : data); } catch (err) { json(res, 404, { error: String(err) }); } diff --git a/01_content/package.json b/01_content/package.json index a7f93c4..cc2ad29 100644 --- a/01_content/package.json +++ b/01_content/package.json @@ -1,7 +1,7 @@ { "name": "dsh-worktable", "description": "工作台 Worktable:DeepSeek Harness 侧边栏的 agent 级项目容器(应用抽屉)", - "version": "0.5.3-local.3", + "version": "0.5.3-local.4", "repository": { "type": "git", "url": "https://github.com/Aisland-SJL/dsh-worktable.git", diff --git a/01_content/src/index.ts b/01_content/src/index.ts index f89e310..415c964 100644 --- a/01_content/src/index.ts +++ b/01_content/src/index.ts @@ -8,6 +8,7 @@ import { homedir } from 'node:os' import { fileURLToPath, pathToFileURL } from 'node:url' import { requestRejection, rejectionText } from './requestAuth' import { createPreviewFiles, PREVIEW_PREFIX, PREVIEW_CSP } from './previewFiles' +import { injectThemeBridge, THEME_BRIDGE_JS } from './theme' /** * 基础数据目录解析:不加载任何官方包(loadPkg 的兜底只能用它,禁止反向调用包加载函数——否则成环)。 @@ -397,6 +398,11 @@ export function apply(ctx: Context) { if (rel === '/dshell.css') { res.writeHead(200, { 'content-type': 'text/css; charset=utf-8', 'cache-control': 'no-store' }) res.end(dshellCss) + } else if (rel === '/theme.js') { + // REQ-20261117-0013:主题桥显式引用入口(站点路由已自动注入内联版; + // 独立打开 / 嵌套 iframe 时手动引这一份)。移植自 qiudl 0.4.x 分支。 + res.writeHead(200, { 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'no-store' }) + res.end(THEME_BRIDGE_JS) } else { res.writeHead(200, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' }) res.end(dshellHtml) @@ -433,7 +439,12 @@ export function apply(ctx: Context) { const ext = (abs.split('.').pop() || '').toLowerCase() res.writeHead(200, { 'content-type': FILE_TYPES[ext] ?? 'application/octet-stream', 'cache-control': 'no-store', 'content-security-policy': PREVIEW_CSP, 'referrer-policy': 'no-referrer', 'x-content-type-options': 'nosniff' }) - res.end(data) + // REQ-20261117-0013:产物 HTML 自动挂主题桥,让所有工作台页跟随 DSH 主题 + // (浅色/深色/跟随系统)。幂等:已含 data-wt-theme-bridge 标记则原样返回; + // 非 HTML 不受影响。移植自 qiudl 0.4.x 分支(0.5.x 线缺失该能力)。 + res.end(ext === 'html' || ext === 'htm' + ? Buffer.from(injectThemeBridge(data.toString('utf8')), 'utf8') + : data) } catch (err) { json(res, 404, { error: String(err) }) } diff --git a/01_content/src/theme.ts b/01_content/src/theme.ts new file mode 100644 index 0000000..39554e6 --- /dev/null +++ b/01_content/src/theme.ts @@ -0,0 +1,191 @@ +/** + * 工作台主题桥(theme bridge)。 + * + * 问题:产物页是**独立的 document**,DSH 宿主写在 `body` / `body[data-ds-dark-theme]` 上的 + * `--dsw-*` token 不会跨文档继承;`dshell.css` 只能靠 `var(--x, 兜底)` 兜底, + * 于是所有工作台页在浅色主题下依旧渲染成深色(2026-10-15 用户报"页面缺少主题跟随")。 + * + * 方案(同源 iframe 直接读宿主文档的 computed style,实测可枚举自定义属性): + * 1) **站点路由自动注入**(`injectThemeBridge`):`/api/worktable/site/**` 下所有 `.html` + * 响应都会带上一段内联桥,页面作者零改动,嵌套 iframe 也各拿一份; + * 2) **显式引入**:`/api/worktable/template/theme.js`(同一份脚本),供独立打开、 + * 或经 file/其它托管方式打开的页面使用; + * 3) 纯函数 `shouldMirrorVar` / `pickThemeVars` 同时给测试与浏览器脚本用,避免判定漂移。 + * + * 桥的行为:把宿主 body 上 `--dsw-* / --dsh-* / --ds-*` 的自定义属性镜像到自己的 + * `html`/`body` 内联样式(内联优先级高于 dshell.css 的 `:root` 兜底),并同步 + * `color-scheme` 与 `data-ds-dark-theme` 标记;跟随宿主属性变化(MutationObserver)、 + * 系统深浅色变化(matchMedia)与低频轮询兜底。拿不到宿主(独立打开)时退回系统偏好。 + * + * **页面优先**:页面自带主题开关时不抢——`window.__wtThemeBridgeManual = true`、 + * ``,或老工作台页的 `` / + * `localStorage['dwb.theme']`(日记工作台、ai递归学习用这套)。判定为手动时会**清掉** + * 桥写过的内联 token,让页面自己的样式表值生效;页面切回跟随(清掉标记)后调 + * `window.__wtThemeBridgeApply()` 即可恢复镜像。 + */ + +/** 需要镜像的自定义属性前缀:颜色/材质 token(--dsw-)、字体与字号 token(--dsh-)、代码字体(--ds-)。 */ +export const THEME_VAR_PREFIXES = ['--dsw-', '--dsh-', '--ds-'] as const + +/** 桥脚本的 DOM 标记(幂等注入与测试断言共用)。 */ +export const THEME_BRIDGE_ID = 'wt-theme-bridge' + +/** 主题脚本路由(显式引入用)。 */ +export const THEME_JS_PATH = '/api/worktable/template/theme.js' + +export type ThemeVar = [name: string, value: string] + +/** 该自定义属性是否属于需要镜像的主题 token。 */ +export function shouldMirrorVar(name: string): boolean { + const n = String(name ?? '').trim() + if (!n.startsWith('--')) return false + return THEME_VAR_PREFIXES.some((p) => n.startsWith(p)) +} + +/** 从 [名, 值] 列表里挑出要镜像的 token(去空值、去非法名)。 */ +export function pickThemeVars(pairs: ReadonlyArray | null | undefined): ThemeVar[] { + const out: ThemeVar[] = [] + for (const pair of pairs ?? []) { + if (!Array.isArray(pair) || pair.length < 2) continue + const name = String(pair[0] ?? '').trim() + const value = String(pair[1] ?? '').trim() + if (!shouldMirrorVar(name) || !value) continue + out.push([name, value]) + } + return out +} + +/** 浏览器端桥脚本(单份真相:内联注入与 /template/theme.js 都用它)。 */ +export const THEME_BRIDGE_JS = `(function () { + var PREFIXES = ${JSON.stringify(THEME_VAR_PREFIXES)}; + if (window.__wtThemeBridgeInstalled) return; + window.__wtThemeBridgeInstalled = true; + function ok(name) { + if (!name || name.slice(0, 2) !== '--') return false; + for (var i = 0; i < PREFIXES.length; i++) if (name.slice(0, PREFIXES[i].length) === PREFIXES[i]) return true; + return false; + } + /** 向上找到最外层同源祖先窗口;跨域或到顶就停。 */ + function hostWindow() { + var w = window; + try { + for (var i = 0; i < 12; i++) { + var p = w.parent; + if (!p || p === w) break; + if (!p.document || !p.document.documentElement) break; + w = p; + } + } catch (e) { /* 跨域:停在当前层 */ } + return w === window ? null : w; + } + function hostVars(host) { + var out = []; + try { + var el = host.document.body || host.document.documentElement; + if (!el) return out; + var cs = host.getComputedStyle(el); + for (var i = 0; i < cs.length; i++) { + var n = cs[i]; + if (!ok(n)) continue; + var v = cs.getPropertyValue(n); + if (v && v.trim()) out.push([n, v.trim()]); + } + out.dark = el.hasAttribute && el.hasAttribute('data-ds-dark-theme'); + } catch (e) { /* 读不到就退回系统偏好 */ } + return out; + } + function systemDark() { + try { return !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches); } catch (e) { return false; } + } + /** 页面自带主题开关?(页面优先,桥不抢) + * 1) 运行时显式:window.__wtThemeBridgeManual = true + * 2) 标记: + * 3) 老工作台页兼容: 或 localStorage['dwb.theme'](日记工作台 / ai递归学习用这套) + * 显式跟随:(或 )=**忽略**上面 3) 的 + * legacy 判定,强制跟宿主——同源页面共用一个 localStorage 键时,这条是"我只想跟宿主"的唯一办法。 */ + function pageManual() { + try { + if (window.__wtThemeBridgeManual) return true; + var el = document.documentElement; + if (el) { + var mode = el.getAttribute && el.getAttribute('data-wt-theme-mode'); + if (mode === 'manual') return true; + if (mode === 'auto' || mode === 'host') return false; + if (el.hasAttribute && el.hasAttribute('data-wt-theme-key')) return false; + if (el.hasAttribute && el.hasAttribute('data-wb-theme')) return true; + } + var v = null; + try { v = localStorage.getItem('dwb.theme'); } catch (e) { v = null; } + if (v === 'dark' || v === 'light') return true; + } catch (e) { /* 读不到就按非手动处理 */ } + return false; + } + var applied = []; + /** 清掉桥写过的内联 token:切到手动主题时必须让位给页面自己的样式表值。 */ + function clearApplied() { + var root = document.documentElement; + for (var i = 0; i < applied.length; i++) { + try { root.style.removeProperty(applied[i]); } catch (e) {} + try { if (document.body) document.body.style.removeProperty(applied[i]); } catch (e) {} + } + applied = []; + } + function apply() { + if (pageManual()) { clearApplied(); return; } + var host = hostWindow(); + var vars = host ? hostVars(host) : []; + var dark = host ? !!vars.dark : systemDark(); + var root = document.documentElement; + for (var i = 0; i < vars.length; i++) root.style.setProperty(vars[i][0], vars[i][1]); + root.style.colorScheme = dark ? 'dark' : 'light'; + if (document.body) { + for (var j = 0; j < vars.length; j++) document.body.style.setProperty(vars[j][0], vars[j][1]); + if (dark) document.body.setAttribute('data-ds-dark-theme', ''); + else document.body.removeAttribute('data-ds-dark-theme'); + } + applied = vars.map(function (p) { return p[0]; }); + } + apply(); + if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', apply, { once: true }); + // 页面自己切主题(data-wb-theme / data-wt-theme-mode)时立刻重新判定,别等轮询 + try { + var MO0 = window.MutationObserver; + if (MO0) new MO0(function () { apply(); }).observe(document.documentElement, { attributes: true, attributeFilter: ['data-wb-theme', 'data-wt-theme-mode'] }); + } catch (e) { /* 忽略 */ } + var host = hostWindow(); + if (host) { + try { + var MO = host.MutationObserver || window.MutationObserver; + var mo = new MO(function () { apply(); }); + var opts = { attributes: true, attributeFilter: ['style', 'class', 'data-ds-dark-theme'] }; + mo.observe(host.document.documentElement, opts); + if (host.document.body) mo.observe(host.document.body, opts); + host.document.addEventListener('DOMContentLoaded', function () { try { mo.observe(host.document.body, opts); } catch (e) {} apply(); }); + } catch (e) { /* 观测失败靠下面的兜底轮询 */ } + } + try { + var mq = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)'); + if (mq) { if (mq.addEventListener) mq.addEventListener('change', apply); else if (mq.addListener) mq.addListener(apply); } + } catch (e) { /* 忽略 */ } + window.__wtThemeBridgeApply = apply; + setInterval(apply, 5000); // 兜底:宿主换主题也可能只换样式表、不动属性 +})();` + +/** + * 给产物 HTML 注入主题桥(幂等)。 + * 优先插到 `` 前;没有 head 就插到 `` 前;都没有就前置。 + * @param html 原始 HTML 文本 + * @param external true = 用 `` + : `` + const head = /<\/head\s*>/i.exec(src) + if (head) return src.slice(0, head.index) + tag + src.slice(head.index) + const body = /<\/body\s*>/i.exec(src) + if (body) return src.slice(0, body.index) + tag + src.slice(body.index) + return tag + src +} diff --git a/01_content/template/dshell.css b/01_content/template/dshell.css index d460833..37ae858 100644 --- a/01_content/template/dshell.css +++ b/01_content/template/dshell.css @@ -1,13 +1,41 @@ /* dsh-worktable 原生皮肤 · DSH 设计系统组件库 用法: - 所有颜色走 DSH 主题变量(--dsw-alias-*),自动适配明暗主题。 */ -:root { color-scheme: dark; } + 颜色一律走 DSH 主题变量(--dsw-alias-*),自动适配明暗主题。 + 主题来源优先级: + 1) 工作台内:主题桥(站点路由自动注入,或显式引 /api/worktable/template/theme.js) + 把宿主 body 上的 --dsw-*/--dsh-*/--ds-* 镜像成页面内联样式 → 跟随 DSH 的 浅色/深色/跟随系统; + 2) 独立打开:用下面 :root 的兜底,并按系统深浅色切换。 + 注意:不要在这里写死深色(旧版 :root{color-scheme:dark} 会让所有页面锁死深色, + 2026-11 从 0.5.x 线恢复时再次踩到,REQ-20261117-0013 一并修掉)。 */ +:root { + color-scheme: light dark; + --dsw-alias-bg-base: #ffffff; + --dsw-alias-fill-l1: rgba(16, 24, 40, .03); + --dsw-alias-border-l1: #e4e7ec; + --dsw-alias-border-l2: #d0d5dd; + --dsw-alias-label-primary: #101828; + --dsw-alias-label-secondary: #475467; + --dsw-alias-label-tertiary: #98a2b3; + --dsw-alias-state-accent-primary: #2f6feb; +} +@media (prefers-color-scheme: dark) { + :root { + --dsw-alias-bg-base: #0b0e14; + --dsw-alias-fill-l1: rgba(255, 255, 255, .02); + --dsw-alias-border-l1: #262b36; + --dsw-alias-border-l2: #3a4150; + --dsw-alias-label-primary: #e6e8eb; + --dsw-alias-label-secondary: #9aa4b2; + --dsw-alias-label-tertiary: #6b7280; + --dsw-alias-state-accent-primary: #4f8ef7; + } +} * { box-sizing: border-box; } html, body { margin: 0; padding: 0; } body { background: var(--dsw-alias-bg-base, #0b0e14); color: var(--dsw-alias-label-primary, #e6e8eb); - font-family: -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif; + font-family: var(--dsw-font-family, -apple-system, "Segoe UI", "Microsoft YaHei", sans-serif); font-size: 13px; line-height: 1.6; } diff --git a/01_content/template/dshell.html b/01_content/template/dshell.html index 25aa369..496c2b8 100644 --- a/01_content/template/dshell.html +++ b/01_content/template/dshell.html @@ -3,6 +3,9 @@ 样式表由插件提供(随主题自动适配),不要复制或改写它。 --> + 我的窗口 diff --git a/04_test/theme-bridge.test.mjs b/04_test/theme-bridge.test.mjs new file mode 100644 index 0000000..e673658 --- /dev/null +++ b/04_test/theme-bridge.test.mjs @@ -0,0 +1,136 @@ +// 工作台主题桥回归: +// 1) 纯函数(前缀判定 / 取值清洗)行为正确 +// 2) 注入幂等、有 head 插 head、无 head 退 body、再退前置;支持 external 模式 +// 3) 浏览器脚本含关键接线(宿主镜像、深色标记、变化跟随、兜底轮询) +// 4) 站点路由 / 模板路由 / dshell.css / dshell.html 的接线必须都在(防"改了桥但没接上") +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { createRequire } from 'node:module' +import { runInNewContext } from 'node:vm' +import { readFileSync } from 'node:fs' +import { fileURLToPath } from 'node:url' + +const require = createRequire(new URL('../01_content/package.json', import.meta.url)) +const { buildSync } = require('esbuild') +const code = buildSync({ + entryPoints: [fileURLToPath(new URL('../01_content/src/theme.ts', import.meta.url))], + bundle: true, write: false, platform: 'node', format: 'cjs', +}).outputFiles[0].text +const module = { exports: {} } +runInNewContext(code, { module, exports: module.exports, setInterval: () => 0, setTimeout: () => 0, clearTimeout: () => {} }) +const { THEME_VAR_PREFIXES, THEME_BRIDGE_ID, THEME_JS_PATH, shouldMirrorVar, pickThemeVars, injectThemeBridge, THEME_BRIDGE_JS } = module.exports + +const read = (rel) => readFileSync(fileURLToPath(new URL(rel, import.meta.url)), 'utf8') +const indexSrc = read('../01_content/src/index.ts') +const cssSrc = read('../01_content/template/dshell.css') +const htmlSrc = read('../01_content/template/dshell.html') + +test('shouldMirrorVar 只认主题 token 前缀', () => { + assert.deepEqual([...THEME_VAR_PREFIXES], ['--dsw-', '--dsh-', '--ds-']) + for (const ok of ['--dsw-alias-bg-base', '--dsh-content-font-size', '--ds-font-family-code']) { + assert.ok(shouldMirrorVar(ok), '应镜像:' + ok) + } + for (const no of ['color', '--x', '--dsw', 'dsw-alias-bg-base', '', null, undefined]) { + assert.ok(!shouldMirrorVar(no), '不应镜像:' + String(no)) + } +}) + +test('pickThemeVars 去空值、去非法名、保留顺序', () => { + const got = pickThemeVars([ + ['--dsw-alias-bg-base', ' #fff '], + ['color', '#000'], + ['--dsh-content-font-size', ''], + ['--ds-font-family-code', 'Menlo, monospace'], + ['--dsw-alias-label-primary', ' '], + ]) + // 跨 vm 上下文,数组原型不同,逐项按字符串比较 + assert.equal(got.length, 2) + assert.deepEqual([String(got[0][0]), String(got[0][1])], ['--dsw-alias-bg-base', '#fff']) + assert.deepEqual([String(got[1][0]), String(got[1][1])], ['--ds-font-family-code', 'Menlo, monospace']) + assert.equal(pickThemeVars(null).length, 0) + assert.equal(pickThemeVars([null, [], ['--dsw-a']]).length, 0) +}) + +test('injectThemeBridge:插 head、幂等、退化到 body/前置、external 模式', () => { + const page = 'thi' + const out = injectThemeBridge(page) + assert.ok(out.includes(THEME_BRIDGE_ID), '应写入桥标记') + assert.ok(out.indexOf(THEME_BRIDGE_ID) < out.indexOf(''), '应插在 之前') + assert.ok(out.includes('window.__wtThemeBridgeInstalled'), '应内联脚本本体') + assert.equal(injectThemeBridge(out), out, '二次注入必须原样返回(幂等)') + + const noHead = 'hi' + const out2 = injectThemeBridge(noHead) + assert.ok(out2.indexOf(THEME_BRIDGE_ID) < out2.indexOf(''), '无 head 时插在 前') + const bare = 'just text' + assert.ok(injectThemeBridge(bare).startsWith(' { + for (const needle of [ + 'window.__wtThemeBridgeInstalled', + 'w.parent', + 'getComputedStyle', + 'getPropertyValue', + "setAttribute('data-ds-dark-theme'", + "style.colorScheme", + 'MutationObserver', + 'matchMedia', + 'prefers-color-scheme: dark', + 'setInterval', + ]) { + assert.ok(THEME_BRIDGE_JS.includes(needle), '桥脚本缺少:' + needle) + } + assert.ok(THEME_BRIDGE_JS.includes(JSON.stringify(THEME_VAR_PREFIXES)), '桥脚本应内联同一份前缀表') +}) + +test('页面优先:自带主题开关时桥必须让位(并清掉自己写过的内联 token)', () => { + for (const needle of [ + 'function pageManual()', + '__wtThemeBridgeManual', + "getAttribute('data-wt-theme-mode')", + "mode === 'auto' || mode === 'host'", + "hasAttribute('data-wt-theme-key')", + "hasAttribute('data-wb-theme')", + "localStorage.getItem('dwb.theme')", + 'function clearApplied()', + 'style.removeProperty', + 'if (pageManual()) { clearApplied(); return; }', + "attributeFilter: ['data-wb-theme', 'data-wt-theme-mode']", + ]) { + assert.ok(THEME_BRIDGE_JS.includes(needle), '页面优先逻辑缺少:' + needle) + } + // 「显式跟随宿主」必须排在 legacy dwb.theme 判定之前:同源页面共用一个键时, + // 否则任何一个页面点过 ◐,所有页面都会被判成手动,想跟宿主的页面就被劫持了。 + const autoIdx = THEME_BRIDGE_JS.indexOf("mode === 'auto' || mode === 'host'") + const legacyIdx = THEME_BRIDGE_JS.indexOf("localStorage.getItem('dwb.theme')") + assert.ok(autoIdx > -1 && legacyIdx > -1 && autoIdx < legacyIdx, 'auto/host 短路必须早于 legacy 键判定') + // 兼容老工作台页:apply() 里必须先判定「页面自带主题」,再镜像宿主 + const callManual = THEME_BRIDGE_JS.indexOf('if (pageManual()) { clearApplied(); return; }') + const callHost = THEME_BRIDGE_JS.indexOf('host ? hostVars(host) : []') + assert.ok(callManual > -1 && callHost > -1, 'apply() 应同时含两处调用') + assert.ok(callManual < callHost, '应先判定手动再镜像宿主') +}) + +test('站点路由与模板路由接线齐全(产物 HTML 自动跟随主题)', () => { + assert.ok(indexSrc.includes("import { injectThemeBridge, THEME_BRIDGE_JS } from './theme'"), '服务端应导入主题桥') + assert.ok(/ext === 'html' \|\| ext === 'htm'[\s\S]{0,120}injectThemeBridge\(/.test(indexSrc), '站点路由应对 html 响应注入主题桥') + assert.ok(/rel === '\/theme\.js'/.test(indexSrc), '模板路由应下发 /theme.js') + assert.ok(/text\/javascript; charset=utf-8/.test(indexSrc), 'theme.js 的 content-type 应为 JS') +}) + +test('dshell.css 不再写死深色,深/浅两套兜底与主题桥分工明确', () => { + const css = cssSrc.replace(/\/\*[\s\S]*?\*\//g, '') // 去掉注释(注释里会引用旧写法) + assert.ok(!/:root\s*\{\s*color-scheme:\s*dark\s*;/.test(css), '不应再写死 color-scheme: dark') + assert.ok(css.includes('color-scheme: light dark'), '应声明 light dark 让独立打开时跟随系统') + assert.ok(css.includes('@media (prefers-color-scheme: dark)'), '应有系统深色兜底') + assert.ok(css.includes('--dsw-alias-bg-base: #ffffff'), '应有浅色兜底 token') + assert.ok(css.includes('--dsw-alias-bg-base: #0b0e14'), '应有深色兜底 token') + assert.ok(/font-family:\s*var\(--dsw-font-family/.test(css), '正文字体应跟随主题字体 token') + assert.ok(htmlSrc.includes('theme.js'), '模板应说明显式引入主题桥的方式') +}) diff --git a/PATCH.md b/PATCH.md index 8a52eec..67a2e93 100644 --- a/PATCH.md +++ b/PATCH.md @@ -1,6 +1,6 @@ # 本地补丁:工作台状态镜像到宿主侧 -基于上游 `Aisland-SJL/dsh-worktable` **v0.5.1**,本地版本号 `0.5.3-local.3`。 +基于上游 `Aisland-SJL/dsh-worktable` **v0.5.1**,本地版本号 `0.5.3-local.4`。 ## 为什么打这个补丁 @@ -88,3 +88,4 @@ dsh plugin --profile web add "https://gitea.ququclean.com/xiaoqu/dsh-worktable/r | 0.5.3-local.1 | REQ-20261117-0013:单标签窗格隐藏标签栏(原只对控制室生效),关闭入口移到窗格标题条,标题改显示当前标签名 | | 0.5.3-local.2 | REQ-20261117-0013:收起态 rail 限制 8 个图标,其余进「⋯」菜单(原先四类全平铺无上限) | | 0.5.3-local.3 | REQ-20261117-0013:状态文件路径改为优先取本机 `03_local/local.json` 的 `cloudStatePath`(指向独立 git 仓),恢复「云状态 + git 历史」;客户端兼容旧 ISO 串形态的 updatedAt | +| 0.5.3-local.4 | REQ-20261117-0013:恢复主题桥(0.5.x 线缺失 `src/theme.ts`、`/theme.js` 路由与产物 HTML 自动注入);修 dshell.css 写死 `color-scheme: dark`;正文字体改走 `--dsw-font-family` |