40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
(function () {
|
|
const body = document.body;
|
|
if (!body) return;
|
|
|
|
const THEME_KEY = 'echo-theme';
|
|
const THEMES = new Set(['minimal', 'dopamine', 'midnight', 'mint', 'sunset', 'aurora', 'graphite']);
|
|
|
|
const normalizeTheme = (value) => {
|
|
const theme = String(value || '').trim().toLowerCase();
|
|
if (THEMES.has(theme)) return theme;
|
|
return 'dopamine';
|
|
};
|
|
|
|
const applyTheme = (theme) => {
|
|
const normalized = normalizeTheme(theme);
|
|
body.setAttribute('data-theme', normalized);
|
|
localStorage.setItem(THEME_KEY, normalized);
|
|
return normalized;
|
|
};
|
|
|
|
let user = null;
|
|
try {
|
|
user = JSON.parse(body.dataset.user || 'null');
|
|
} catch {
|
|
user = null;
|
|
}
|
|
|
|
const fromUser = user && user.theme ? normalizeTheme(user.theme) : '';
|
|
const fromBody = normalizeTheme(body.dataset.theme || 'dopamine');
|
|
const fromLocal = normalizeTheme(localStorage.getItem(THEME_KEY) || '');
|
|
const initial = fromUser || fromLocal || fromBody || 'dopamine';
|
|
applyTheme(initial);
|
|
|
|
window.EchoTheme = {
|
|
applyTheme,
|
|
normalizeTheme,
|
|
themes: Array.from(THEMES),
|
|
};
|
|
})();
|