Files
dsh-theme-pack/scripts/build.mjs
T

307 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 生成器:调色板 → 全部产物。
* node scripts/build.mjs
* 产出:
* lib/themes.json 16 套主题的 token 覆盖表(唯一可机读成果)
* lib/client.js 客户端插件 bundle(lazy CJS,注册主题 + 设置页皮肤选择器)
* preview/<id>.html 每套主题的 GUI 预览页(供 Edge headless 截图)
* preview/gallery.html 16 合 1 总览页
*/
import { writeFileSync, mkdirSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { PALETTES } from '../src/palettes.mjs';
import { themeDefOf } from '../src/tokens.mjs';
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
const out = (rel, content) => {
const p = join(root, rel);
mkdirSync(dirname(p), { recursive: true });
writeFileSync(p, content);
console.log('wrote', rel);
};
const THEMES = PALETTES.map(themeDefOf);
// ── 1) themes.json ──────────────────────────────────────────
out('lib/themes.json', JSON.stringify(THEMES, null, 2));
// ── 2) client.js(lazy CJS bundle) ─────────────────────────
const zhDict = { 'themepack.title': '主题皮肤(16 套)' };
const enDict = { 'themepack.title': 'Theme skins (16)' };
for (const t of THEMES) {
zhDict['themepack.' + t.id] = t.label.zh;
enDict['themepack.' + t.id] = t.label.en;
}
const pickerCss = [
'.dtp-group{border-bottom:1px solid var(--dsw-alias-border-l2);flex-direction:column;gap:8px;padding:16px 0;display:flex}',
'.dtp-title{color:var(--dsw-alias-label-primary);font-size:14px;font-weight:400;line-height:22px}',
'.dtp-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(152px,1fr));gap:8px}',
'.dtp-cube{box-sizing:border-box;border:1px solid var(--dsw-alias-border-l2);border-radius:12px;background:0 0;color:var(--dsw-alias-label-primary);cursor:pointer;font:inherit;display:flex;flex-direction:column;align-items:flex-start;gap:8px;padding:12px 14px;font-size:13px;line-height:18px}',
'.dtp-cube:hover:not(.dtp-selected){background:var(--dsw-alias-interactive-bg-hover)}',
'.dtp-selected{border-color:var(--dsw-alias-state-business-primary);background:var(--dsw-alias-bg-module-platform)}',
'.dtp-dots{display:flex;gap:5px}',
'.dtp-dot{width:14px;height:14px;border-radius:50%;border:1px solid var(--dsw-alias-border-l3)}',
'.dtp-name{color:var(--dsw-alias-label-secondary)}',
].join('');
const clientJs = `/* dsh-theme-pack client bundle — generated by scripts/build.mjs, do not edit by hand. */
window.__ModuleLoader__.load({
id: "dsh-theme-pack",
factory: (require) => {
var module = { exports: {} };
var exports = module.exports;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const { jsx, jsxs } = require("react/jsx-runtime");
const { defineStore } = require("@deepseek-ai/dsh-client-runtime/client");
const THEMES = ${JSON.stringify(THEMES)};
const CSS = ${JSON.stringify(pickerCss)};
const TAG_ID = "dsh-theme-pack/picker.css";
if (typeof document !== "undefined" && document.querySelector("style[data-plugin-css=" + JSON.stringify(TAG_ID) + "]") === null) {
const tag = document.createElement("style");
tag.dataset.plugin = "dsh-theme-pack";
tag.dataset.pluginCss = TAG_ID;
tag.textContent = CSS;
document.head.appendChild(tag);
}
const zh = ${JSON.stringify(zhDict)};
const en = ${JSON.stringify(enDict)};
function createRowStore() {
return defineStore({
init: () => ({ preference: "system", revision: -1 }),
actions: {
sync: (d, preference, revision) => {
if (revision <= d.revision) return;
d.preference = preference;
d.revision = revision;
}
}
});
}
function ThemePackRow({ t, setTheme, useStore }) {
const preference = useStore((s) => s.preference);
return jsxs("div", { className: "dtp-group", children: [
jsx("div", { className: "dtp-title", children: t("themepack.title") }),
jsx("div", { className: "dtp-grid", children: THEMES.map((th) => jsxs("button", {
type: "button",
className: "dtp-cube" + (preference === th.id ? " dtp-selected" : ""),
"aria-pressed": preference === th.id,
title: th.label.en,
onClick: () => { setTheme(th.id); },
children: [
jsx("span", { className: "dtp-dots", children: th.swatches.map((c) => jsx("span", { className: "dtp-dot", style: { background: c } }, c)) }),
jsx("span", { className: "dtp-name", children: t("themepack." + th.id) })
]
}, th.id)) })
] });
}
function apply(ctx) {
for (const th of THEMES) {
ctx.effect(() => ctx.theme.register({ id: th.id, colorScheme: th.colorScheme, tokens: th.tokens }), "theme-pack: register " + th.id);
}
ctx.effect(() => ctx.locale.register("settings.themePack", { zh, en }), "theme-pack: dictionaries");
const store = createRowStore();
let bound;
const sync = (snapshot) => { if (bound) bound.sync(snapshot.preference, snapshot.revision); };
ctx.effect(() => {
const off = ctx.on("theme/change", sync);
return () => { off(); };
}, "theme-pack: theme/change mirror");
const injected = (actions) => {
bound = actions;
sync(ctx.theme.getTheme());
return { setTheme: (id) => { ctx.theme.setTheme(id); } };
};
ctx.slots.inject("settings.general.item", () => ctx.slots.register({
name: "settings.general.item",
id: "theme-pack",
order: 11,
store,
locale: "settings.themePack",
inject: injected
}, ThemePackRow));
}
const inject = ["slots", "locale", "theme"];
exports.apply = apply;
exports.inject = inject;
return module.exports;
}
});
`;
out('lib/client.js', clientJs);
// ── 3) 预览页 ───────────────────────────────────────────────
const bodyStyle = (t) => Object.entries(t.tokens).map(([k, v]) => `${k}: ${v}`).join('; ');
const pageCss = `
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
background: var(--dsw-alias-bg-base);
color: var(--dsw-alias-label-primary);
font-size: 14px; line-height: 1.7;
}
.app { display: flex; height: 100vh; }
.sidebar {
width: 264px; flex-shrink: 0; display: flex; flex-direction: column;
background: var(--dsw-specific-sidebar-fill);
border-right: 1px solid var(--dsw-alias-border-l1);
padding: 16px 12px;
}
.logo { font-size: 17px; font-weight: 600; color: var(--dsw-alias-brand-text); padding: 4px 10px 16px; }
.newchat {
border: 1px solid var(--dsw-alias-border-l2); border-radius: 12px;
background: var(--dsw-alias-button-elevated-fill); color: var(--dsw-alias-label-primary);
font: inherit; padding: 9px 12px; text-align: left; cursor: pointer; margin-bottom: 14px;
}
.sess { padding: 8px 10px; border-radius: 10px; color: var(--dsw-alias-label-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sess:hover { background: var(--dsw-specific-sidebar-nav-item-hover); }
.sess.active { background: var(--dsw-specific-sidebar-nav-item-active); color: var(--dsw-alias-label-primary); box-shadow: inset 2px 0 0 var(--dsw-alias-state-business-primary); }
.sidefoot { margin-top: auto; padding: 8px 10px; color: var(--dsw-alias-label-tertiary); border-top: 1px solid var(--dsw-alias-border-l1); }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
height: 52px; flex-shrink: 0; display: flex; align-items: center; gap: 10px;
padding: 0 24px; border-bottom: 1px solid var(--dsw-alias-border-l1);
color: var(--dsw-alias-label-primary); font-weight: 500;
}
.model { font-size: 12px; font-weight: 400; color: var(--dsw-alias-label-caption); border: 1px solid var(--dsw-alias-border-l2); border-radius: 8px; padding: 1px 8px; }
.chat { flex: 1; overflow: hidden; padding: 24px 24px 8px; display: flex; flex-direction: column; align-items: center; }
.col { width: 100%; max-width: 780px; }
.msg-user { display: flex; justify-content: flex-end; margin-bottom: 18px; }
.bubble {
background: var(--dsw-specific-bubble); color: var(--dsw-alias-label-primary);
border-radius: 16px; padding: 10px 16px; max-width: 70%;
}
.msg-ai { color: var(--dsw-alias-label-primary); }
.msg-ai p { margin-bottom: 10px; }
.msg-ai ul { margin: 0 0 10px 22px; color: var(--dsw-alias-label-secondary); }
code.inline {
background: var(--dsw-alias-markdown-inline-code); border-radius: 6px;
padding: 1px 6px; font-family: Consolas, "Cascadia Mono", monospace; font-size: 13px;
color: var(--dsw-alias-label-primary-bluish);
}
.ok { color: var(--dsw-alias-state-success-primary); }
.warn { color: var(--dsw-alias-state-warn-label); }
.err { color: var(--dsw-alias-state-error-primary); }
.code { border: 1px solid var(--dsw-alias-border-l1); border-radius: 14px; overflow: hidden; margin: 6px 0 12px; }
.codebanner {
display: flex; justify-content: space-between; align-items: center;
background: var(--dsw-alias-markdown-code-block-banner);
border-bottom: 1px solid var(--dsw-alias-border-l1);
padding: 6px 14px; font-size: 12px; color: var(--dsw-alias-label-caption);
}
.code pre {
background: var(--dsw-alias-markdown-code-block); padding: 14px 16px;
font-family: Consolas, "Cascadia Mono", monospace; font-size: 13px; line-height: 1.65;
color: var(--dsw-alias-label-secondary); overflow: hidden;
}
.kw { color: var(--syn-kw); } .str { color: var(--syn-str); } .fn { color: var(--syn-fn); }
.num { color: var(--syn-num); } .com { color: var(--syn-com); font-style: italic; }
.cap { font-size: 12px; color: var(--dsw-alias-label-caption); }
.composer { flex-shrink: 0; padding: 8px 24px 20px; display: flex; justify-content: center; }
.input {
width: 100%; max-width: 780px; display: flex; align-items: center; gap: 10px;
background: var(--dsw-specific-input-major); border: 1px solid var(--dsw-alias-border-l2);
border-radius: 24px; padding: 12px 12px 12px 20px;
box-shadow: 0 4px 20px rgba(0,0,0,0.08);
}
.ph { flex: 1; color: var(--dsw-alias-label-caption); }
.send {
width: 34px; height: 34px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
background: var(--dsw-alias-button-primary-fill); color: var(--dsw-alias-label-primary-foreground);
font-weight: 600;
}
.themetag {
position: fixed; right: 18px; bottom: 18px; z-index: 9;
background: var(--dsw-alias-tooltip-bg); color: var(--dsw-alias-label-primary);
border: 1px solid var(--dsw-alias-border-l2); border-radius: 10px;
padding: 6px 12px; font-size: 12px;
}
.themetag b { color: var(--dsw-alias-state-business-primary); font-weight: 600; }
`;
const codeSample = `<span class="com">// 调色板 → DSH 语义 token 覆盖表</span>
<span class="kw">const</span> themes = palettes.<span class="fn">map</span>((p) =&gt; ({
id: p.id, <span class="com">// 例:'tokyo-night'</span>
colorScheme: <span class="str">'dark'</span>,
tokens: <span class="fn">tokensFor</span>(p), <span class="com">// --dsw-alias-* 全集</span>
swatches: <span class="num">4</span>,
}));
<span class="kw">export default</span> themes;`;
function pageHtml(t, p) {
const synVars = `--syn-kw: ${p.syn.kw}; --syn-str: ${p.syn.str}; --syn-fn: ${p.syn.fn}; --syn-num: ${p.syn.num}; --syn-com: ${p.syn.com}`;
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>${t.label.zh} — DSH 主题预览</title>
<style>${pageCss}</style>
</head>
<body style="color-scheme: ${t.colorScheme}; ${bodyStyle(t)}; ${synVars}">
<div class="app">
<aside class="sidebar">
<div class="logo">DeepSeek</div>
<button class="newchat">+ 开启新会话</button>
<div class="sess">期中试卷讲评提纲</div>
<div class="sess active">DSH 主题包设计</div>
<div class="sess">导数压轴题归类整理</div>
<div class="sess">家长会发言稿润色</div>
<div class="sidefoot">⚙ 设置</div>
</aside>
<div class="main">
<div class="topbar">DSH 主题包设计 <span class="model">k3-256k</span></div>
<div class="chat"><div class="col">
<div class="msg-user"><div class="bubble">把 16 套主题各出一张预览图,深色主题的代码块要好看。</div></div>
<div class="msg-ai">
<p>已按 <code class="inline">--dsw-alias-*</code> 语义层完成映射,规则:</p>
<ul>
<li>背景四层与侧栏取自调色板原色,层级不乱;</li>
<li>状态色 <span class="ok">成功</span> / <span class="warn">警告</span> / <span class="err">错误</span> 沿用主题官方色。</li>
</ul>
<div class="code">
<div class="codebanner"><span>javascript</span><span>复制</span></div>
<pre>${codeSample}</pre>
</div>
<p class="cap">配色数据源:src/palettes.mjs · 映射器:src/tokens.mjs</p>
</div>
</div></div>
<div class="composer"><div class="input"><span class="ph">给 DeepSeek 发送消息…</span><span class="send">↑</span></div></div>
</div>
</div>
<div class="themetag"><b>${t.label.zh}</b> · ${t.label.en} · ${t.colorScheme === 'dark' ? '深色' : '浅色'}</div>
</body>
</html>`;
}
for (let i = 0; i < THEMES.length; i++) out(`preview/${THEMES[i].id}.html`, pageHtml(THEMES[i], PALETTES[i]));
// ── 4) 总览页(16 个缩放 iframe) ───────────────────────────
// 总览图直接引用已渲染的 PNG(headless 截图时 iframe 常未加载完,img 可靠)。
const cells = THEMES.map((t) => `
<figure>
<img src="../previews/${t.id}.png" alt="${t.id}">
<figcaption>${t.label.zh}<small>${t.id}</small></figcaption>
</figure>`).join('\n');
out('preview/gallery.html', `<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="utf-8"><title>DSH 主题包 16 套总览</title>
<style>
body { margin: 0; background: #111; font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif; }
h1 { color: #eee; font-size: 20px; font-weight: 600; padding: 20px 28px 4px; }
.sub { color: #999; font-size: 13px; padding: 0 28px 16px; }
.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; padding: 0 28px 28px; }
figure { margin: 0; }
img { width: 100%; display: block; border-radius: 10px; border: 1px solid #333; }
figcaption { color: #ddd; font-size: 13px; padding: 6px 2px 0; display: flex; justify-content: space-between; }
figcaption small { color: #777; }
</style></head>
<body>
<h1>DSH 主题皮肤包 · 16 套</h1>
<div class="sub">11 色系 × 明暗变体 = 16 套:Catppuccin / Gruvbox / Everforest / Rosé Pine / Solarized / 神奈川 / 东京夜 / 夜猫子 / Nord / Dracula / One Dark</div>
<div class="grid">${cells}
</div>
</body></html>`);
console.log(`done: ${THEMES.length} themes`);