Files
aigc-home/calliope-web/src/lib/theme.ts
T

124 lines
3.9 KiB
TypeScript

import { tick } from "svelte";
import { writable } from "svelte/store";
/** Approved HEL-726 themes. Colours live only in static/themes.css. */
export const THEMES = [
{
id: "night",
name: "夜片场",
english: "Nocturne",
tone: "深色 · 默认",
description:
"墨黑棚里一盏钨丝灯:作品最亮,金色只给“去做”。夜间与沉浸创作。",
},
{
id: "day",
name: "日光棚",
english: "Daylight",
tone: "浅色",
description:
"开窗的白天摄影棚:暖白纸面、墨色文字、琥珀主键。白天办公与长时间审稿。",
},
{
id: "grade",
name: "调色间",
english: "Grading",
tone: "中性深灰",
description:
"调色室的中性灰:不给作品染色,场景色降饱和、主键为白。判断色彩、久盯不累。",
},
{
id: "bluehour",
name: "蓝调时刻",
english: "Blue Hour",
tone: "深蓝",
description: "日落后的靛蓝天色配珊瑚主光,氛围最浓。提案、展示与灵感发散。",
},
] as const;
export type ThemeId = (typeof THEMES)[number]["id"];
export const THEME_KEY = "aigc-theme";
export const theme = writable<ThemeId>("night");
export const themeStorageUnavailable = writable(false);
export function isTheme(value: unknown): value is ThemeId {
return THEMES.some((item) => item.id === value);
}
let themeRequest = 0;
let transition: ViewTransition | undefined;
let fallbackTimer: ReturnType<typeof setTimeout> | undefined;
/** HEL-726: 520ms reveal, 280ms fallback, immediate under reduced motion.
* Only root tokens and the store change; no editor/component is remounted. */
export function setTheme(id: ThemeId, event?: MouseEvent) {
if (typeof document === "undefined" || !isTheme(id)) return;
const root = document.documentElement;
const request = ++themeRequest;
transition?.skipTransition();
clearTimeout(fallbackTimer);
root.classList.remove("theme-anim");
if (root.dataset.theme === id) return;
const apply = () => {
if (request !== themeRequest) return;
root.dataset.theme = id;
theme.set(id);
try {
localStorage.setItem(THEME_KEY, id);
themeStorageUnavailable.set(false);
} catch {
themeStorageUnavailable.set(true);
}
};
const reduced =
root.dataset.reduce === "1" ||
matchMedia("(prefers-reduced-motion: reduce)").matches;
if (reduced) {
apply();
return;
}
if (document.startViewTransition && root.dataset.motion !== "calm") {
if (event && event.detail !== 0) {
root.style.setProperty("--theme-x", event.clientX + "px");
root.style.setProperty("--theme-y", event.clientY + "px");
}
transition = document.startViewTransition(async () => {
apply();
await tick();
});
void transition.ready.catch(() => {});
void transition.finished.catch(() => {});
} else {
root.classList.add("theme-anim");
apply();
fallbackTimer = setTimeout(() => root.classList.remove("theme-anim"), 340);
}
}
export function initTheme() {
const current = document.documentElement.dataset.theme;
theme.set(isTheme(current) ? current : "night");
try {
localStorage.getItem(THEME_KEY);
} catch {
themeStorageUnavailable.set(true);
}
const sync = (event: StorageEvent) => {
if (event.key !== THEME_KEY && event.key !== null) return;
const id = isTheme(event.newValue) ? event.newValue : "night";
document.documentElement.dataset.theme = id;
theme.set(id);
};
const finishMotion = () => {
transition?.skipTransition();
clearTimeout(fallbackTimer);
document.documentElement.classList.remove("theme-anim");
};
window.addEventListener("aigc-motion-change", finishMotion);
window.addEventListener("storage", sync);
return () => {
finishMotion();
window.removeEventListener("aigc-motion-change", finishMotion);
window.removeEventListener("storage", sync);
};
}