Files
writing-agent/web/theme-init.js
T
chenmoranzhi e2413b0677 主题与字号从页面顶栏搬进设置页的「外观」,顶部导航加上预设入口。
顶栏那套汉堡菜单在每页都要单独挂载、样式也重复,收进设置页后只剩一处改;顺手把「整组都没有曾用型号」的配置组整组收起,不再留空卡片。
2026-10-26 01:09:43 +08:00

43 lines
1.6 KiB
JavaScript

/* Apply the workspace theme + UI font scale before first paint.
* The controls live in 设置 → 外观 (see settings.js); pages only read storage. */
(function () {
function normalizeTheme(value) {
return /^(paper|modern|dark|green)$/.test(value || "") ? value : "modern";
}
function normalizeScale(value) {
var legacy = { sm: 88, md: 100, lg: 112, xl: 125 };
var percent = legacy[value] != null ? legacy[value] : Number(value);
if (!isFinite(percent)) percent = 100;
if (percent > 0 && percent <= 2.5) percent = Math.round(percent * 100);
return Math.min(150, Math.max(80, Math.round(percent / 5) * 5));
}
function applyTheme(theme) {
var next = normalizeTheme(theme);
document.documentElement.setAttribute("data-color-theme", next);
document.documentElement.setAttribute("data-theme", next);
return next;
}
function applyScale(scale) {
var next = normalizeScale(scale);
document.documentElement.setAttribute("data-ui-font-scale", String(next));
document.documentElement.style.setProperty("--ui-font-scale", String(next / 100));
return next;
}
try {
applyTheme(localStorage.getItem("wa-color-theme") || "modern");
applyScale(localStorage.getItem("wa-ui-font-scale") || "100");
} catch (_) {
/* Storage may be unavailable; CSS defaults remain usable. */
}
/* Keep tabs in sync when 设置 · 外观 changes the stored values. */
window.addEventListener("storage", function (event) {
if (event.key === "wa-color-theme") applyTheme(event.newValue);
if (event.key === "wa-ui-font-scale") applyScale(event.newValue);
});
})();