43 lines
1.6 KiB
JavaScript
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);
|
|
});
|
|
})();
|