// ============================================================ // 🌓 主题切换 // ============================================================ function getTheme() { return localStorage.getItem('forumlify-theme') || 'light'; } function setTheme(theme) { document.documentElement.setAttribute('data-theme', theme); localStorage.setItem('forumlify-theme', theme); updateThemeUI(theme); } function toggleTheme(e) { if (e) { e.preventDefault(); e.stopPropagation(); } const current = getTheme(); const next = current === 'light' ? 'dark' : 'light'; setTheme(next); } function updateThemeUI(theme) { const icon = document.getElementById('themeIcon'); const label = document.getElementById('themeLabel'); if (!icon) return; // 确保 t 函数存在,否则用默认文字 const translate = typeof t === 'function' ? t : function(key) { return key; }; if (theme === 'dark') { icon.innerHTML = ``; if (label) label.textContent = translate('亮色模式'); } else { icon.innerHTML = ``; if (label) label.textContent = translate('暗色模式'); } } function applyTheme() { const theme = getTheme(); document.documentElement.setAttribute('data-theme', theme); updateThemeUI(theme); } // 确保 DOM 加载完成后执行 document.addEventListener('DOMContentLoaded', function() { applyTheme(); });