|
8 | 8 |
|
9 | 9 | Prism.manual = true; |
10 | 10 |
|
| 11 | +function initThemeToggle() { |
| 12 | + const toggle = document.querySelector('[data-theme-toggle]'); |
| 13 | + |
| 14 | + if (!toggle) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const validThemes = ['light', 'dark', 'auto']; |
| 19 | + const body = document.body; |
| 20 | + const iconUse = toggle.querySelector('use'); |
| 21 | + const prefersDark = window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : null; |
| 22 | + |
| 23 | + function isDark(theme) { |
| 24 | + if (theme === 'dark') { |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + return theme === 'auto' && prefersDark && prefersDark.matches; |
| 29 | + } |
| 30 | + |
| 31 | + function normalizeTheme(theme) { |
| 32 | + return validThemes.includes(theme) ? theme : 'auto'; |
| 33 | + } |
| 34 | + |
| 35 | + function getCurrentTheme() { |
| 36 | + if (body.classList.contains('dark')) { |
| 37 | + return 'dark'; |
| 38 | + } |
| 39 | + |
| 40 | + if (body.classList.contains('light')) { |
| 41 | + return 'light'; |
| 42 | + } |
| 43 | + |
| 44 | + return 'auto'; |
| 45 | + } |
| 46 | + |
| 47 | + function updateToggleState(theme) { |
| 48 | + const darkMode = isDark(theme); |
| 49 | + const title = darkMode ? toggle.dataset.titleLight : toggle.dataset.titleDark; |
| 50 | + const icon = darkMode ? 'sun' : 'moon'; |
| 51 | + const iconHref = toggle.dataset.iconSprite + '#' + icon; |
| 52 | + |
| 53 | + toggle.setAttribute('title', title); |
| 54 | + toggle.setAttribute('aria-label', title); |
| 55 | + |
| 56 | + if (iconUse) { |
| 57 | + iconUse.setAttribute('xlink:href', iconHref); |
| 58 | + iconUse.setAttribute('href', iconHref); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + function applyTheme(theme) { |
| 63 | + const normalizedTheme = normalizeTheme(theme); |
| 64 | + body.classList.remove('light', 'dark', 'auto'); |
| 65 | + body.classList.add(normalizedTheme); |
| 66 | + updateToggleState(normalizedTheme); |
| 67 | + } |
| 68 | + |
| 69 | + function persistTheme(theme) { |
| 70 | + document.cookie = 'siteTheme=' + encodeURIComponent(theme) + '; path=/; max-age=31536000; SameSite=Lax'; |
| 71 | + } |
| 72 | + |
| 73 | + applyTheme(toggle.dataset.currentTheme || getCurrentTheme()); |
| 74 | + |
| 75 | + toggle.addEventListener('click', function () { |
| 76 | + const currentTheme = getCurrentTheme(); |
| 77 | + const nextTheme = isDark(currentTheme) ? 'light' : 'dark'; |
| 78 | + applyTheme(nextTheme); |
| 79 | + persistTheme(nextTheme); |
| 80 | + }); |
| 81 | + |
| 82 | + if (prefersDark && typeof prefersDark.addEventListener === 'function') { |
| 83 | + prefersDark.addEventListener('change', function () { |
| 84 | + if (getCurrentTheme() === 'auto') { |
| 85 | + updateToggleState('auto'); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | +} |
| 90 | + |
11 | 91 | $(function () { |
| 92 | + initThemeToggle(); |
| 93 | + |
12 | 94 | $(".post-body").each(function () { |
13 | 95 | Prism.highlightAllUnder(this); |
14 | 96 | }); |
|
0 commit comments