feat(webui): appearance customization — theme/accent/background/fonts/a11y (phase 2)

Wires the Phase 1 store into a full appearance layer.

ThemeContext is rewritten to load/save UiAppearance from the server (with a
localStorage anti-flash cache + a one-time migration of the legacy per-browser
theme keys), apply ~12 dimensions to <html>, and theme the login page pre-auth.

New Settings controls (外观 / 数据与格式): free-hex accent (+ presets + apply
scope), dark intensity (soft / OLED black), sidebar style, background
(solid / gradient / uploaded image with opacity + blur), font presets, UI
scale, reduce-motion, high-contrast, default-collapsed sidebar, 12h/24h time
format (wired into logs + overview timestamps).

- index.html gains a pre-paint bootstrap script that applies the cached theme
  before the bundle mounts, eliminating the dark/OLED/scale FOUC.
- server saveUiConfig is now a section-level merge so appearance-only and
  layout-only saves never clobber each other.
- reduce-motion uses Framer MotionConfig; the background renders on a fixed
  layer revealed by transparent app roots; custom-accent hex and ui-scale are
  validated/clamped before use (localStorage-tamper hardening).

Verified across 2 adversarial review rounds (FOUC + CSS-injection + impure
state-updater + ref-staleness all fixed).
This commit is contained in:
DrSmoothl committed 2026-06-13 01:15:00 +08:00
1 parent 0b5087b410
commit ac1ffc46d3
9 files changed
+1028 -308

No files matched your search

+22 -8
View File
@@ -303,16 +303,20 @@ export function normalizeAppearance(value: unknown, imageState: ServerImageState
};
}
export function normalizeLayout(value: unknown): UiLayout {
const layout = isObject(value) ? value : {};
return {
overviewBlocks: normalizeLayoutItems(layout.overviewBlocks, DEFAULT_OVERVIEW_BLOCKS),
navItems: normalizeLayoutItems(layout.navItems, DEFAULT_NAV_ITEMS),
};
}
export function normalizeUiConfig(value: unknown, imageState: ServerImageState = DEFAULT_IMAGE_STATE): UiConfig {
const v = isObject(value) ? value : {};
const layout = isObject(v.layout) ? v.layout : {};
return {
version: UI_CONFIG_VERSION,
appearance: normalizeAppearance(v.appearance, imageState),
layout: {
overviewBlocks: normalizeLayoutItems(layout.overviewBlocks, DEFAULT_OVERVIEW_BLOCKS),
navItems: normalizeLayoutItems(layout.navItems, DEFAULT_NAV_ITEMS),
},
layout: normalizeLayout(v.layout),
};
}
@@ -379,9 +383,19 @@ export function loadUiConfig(): UiConfig {
*/
export function saveUiConfig(incoming: unknown): UiConfig {
const current = loadUiConfig();
// On save, the current on-disk state is the truth for the server-managed
// image fields — a client payload cannot forge `hasImage` etc.
const next = normalizeUiConfig(incoming, current.appearance.background);
const v = isObject(incoming) ? incoming : {};
// Section-level merge: a payload may carry just `appearance` (the theme
// editor) or just `layout` (the layout editor) without clobbering the
// other. A missing/non-object section keeps the current on-disk value.
// The current on-disk state is the truth for the server-managed image
// fields — a client payload cannot forge `hasImage` etc.
const next: UiConfig = {
version: UI_CONFIG_VERSION,
appearance: isObject(v.appearance)
? normalizeAppearance(v.appearance, current.appearance.background)
: current.appearance,
layout: isObject(v.layout) ? normalizeLayout(v.layout) : current.layout,
};
atomicWrite(next);
cached = next;
return next;