|
1 |
| -document.addEventListener('DOMContentLoaded', initColorScheme); |
| 1 | +document.addEventListener('DOMContentLoaded', initColorScheme) |
2 | 2 |
|
3 | 3 | const ColorScheme = {
|
4 | 4 | auto: 'auto',
|
5 | 5 | light: 'light',
|
6 | 6 | dark: 'dark',
|
7 |
| -}; |
| 7 | +} |
8 | 8 |
|
9 |
| -const localStorageKey = 'developer.setting.preferredColorScheme'; |
| 9 | +const localStorageKey = 'developer.setting.preferredColorScheme' |
10 | 10 |
|
11 | 11 | const supportsAutoColorScheme = (() => {
|
12 |
| - return typeof window.matchMedia !== 'undefined' && |
| 12 | + return ( |
| 13 | + typeof window.matchMedia !== 'undefined' && |
13 | 14 | ['light', 'dark', 'no-preference'].some(
|
14 |
| - (scheme) => window.matchMedia(`(prefers-color-scheme: ${scheme})`).matches |
15 |
| - ); |
16 |
| -})(); |
| 15 | + (scheme) => |
| 16 | + window.matchMedia(`(prefers-color-scheme: ${scheme})`).matches, |
| 17 | + ) |
| 18 | + ) |
| 19 | +})() |
17 | 20 |
|
18 | 21 | if (!supportsAutoColorScheme) {
|
19 |
| - document.getElementById('scheme-auto-wrapper')?.remove(); |
| 22 | + document.getElementById('scheme-auto-wrapper')?.remove() |
20 | 23 | }
|
21 | 24 |
|
22 | 25 | function initColorScheme() {
|
23 |
| - setColorScheme(getStoredScheme()); |
24 |
| - |
25 |
| - const toggle = getToggle(); |
| 26 | + const toggle = getToggle() |
26 | 27 | if (toggle && toggle.value !== getStoredScheme()) {
|
27 |
| - toggle.value = getStoredScheme(); |
| 28 | + toggle.value = getStoredScheme() |
28 | 29 | }
|
29 | 30 |
|
30 | 31 | getToggleForm()?.addEventListener('change', (e) => {
|
31 |
| - setColorScheme(e.target.value); |
32 |
| - }); |
| 32 | + setColorScheme(e.target.value) |
| 33 | + }) |
33 | 34 | }
|
34 | 35 |
|
35 | 36 | function systemPrefersLight() {
|
36 |
| - return window.matchMedia('(prefers-color-scheme: light)'); |
| 37 | + return window.matchMedia('(prefers-color-scheme: light)') |
37 | 38 | }
|
38 | 39 |
|
39 | 40 | function systemPrefersDark() {
|
40 |
| - return window.matchMedia('(prefers-color-scheme: dark)'); |
| 41 | + return window.matchMedia('(prefers-color-scheme: dark)') |
41 | 42 | }
|
42 | 43 |
|
43 | 44 | function isSystemDark() {
|
44 |
| - return systemPrefersDark().matches; |
| 45 | + return systemPrefersDark().matches |
45 | 46 | }
|
46 | 47 |
|
47 | 48 | function setColorScheme(value) {
|
48 | 49 | if (!Object.values(ColorScheme).includes(value)) {
|
49 |
| - value = supportsAutoColorScheme ? ColorScheme.auto : ColorScheme.light; |
| 50 | + value = supportsAutoColorScheme ? ColorScheme.auto : ColorScheme.light |
50 | 51 | }
|
51 | 52 |
|
52 | 53 | if (value === ColorScheme.auto) {
|
53 |
| - updateScheme(isSystemDark() ? ColorScheme.dark : ColorScheme.light, value); |
| 54 | + updateScheme(isSystemDark() ? ColorScheme.dark : ColorScheme.light, value) |
54 | 55 | } else {
|
55 |
| - updateScheme(value); |
| 56 | + updateScheme(value) |
56 | 57 | }
|
57 | 58 | }
|
58 | 59 |
|
59 | 60 | function updateScheme(scheme, storedValue = scheme) {
|
60 | 61 | if (getStoredScheme() !== storedValue) {
|
61 |
| - saveScheme(storedValue); |
| 62 | + saveScheme(storedValue) |
62 | 63 | }
|
63 | 64 |
|
64 | 65 | if (getCurrentScheme() !== scheme) {
|
65 |
| - document.body.setAttribute('data-color-scheme', scheme); |
| 66 | + document.body.setAttribute('data-color-scheme', scheme) |
66 | 67 | }
|
67 | 68 |
|
68 |
| - const toggle = getToggle(); |
| 69 | + const toggle = getToggle() |
69 | 70 | if (toggle && toggle.value !== storedValue) {
|
70 |
| - toggle.value = storedValue; |
| 71 | + toggle.value = storedValue |
71 | 72 | }
|
72 | 73 | }
|
73 | 74 |
|
74 | 75 | systemPrefersLight().addEventListener('change', (e) => {
|
75 | 76 | if (e.matches && getStoredScheme() === ColorScheme.auto) {
|
76 |
| - const current = getCurrentScheme(); |
| 77 | + const current = getCurrentScheme() |
77 | 78 | if (current !== ColorScheme.light) {
|
78 |
| - document.body.setAttribute('data-color-scheme', ColorScheme.light); |
| 79 | + document.body.setAttribute('data-color-scheme', ColorScheme.light) |
79 | 80 | }
|
80 | 81 | }
|
81 |
| -}); |
| 82 | +}) |
82 | 83 |
|
83 | 84 | systemPrefersDark().addEventListener('change', (e) => {
|
84 | 85 | if (e.matches && getStoredScheme() === ColorScheme.auto) {
|
85 |
| - const current = getCurrentScheme(); |
| 86 | + const current = getCurrentScheme() |
86 | 87 | if (current !== ColorScheme.dark) {
|
87 |
| - document.body.setAttribute('data-color-scheme', ColorScheme.dark); |
| 88 | + document.body.setAttribute('data-color-scheme', ColorScheme.dark) |
88 | 89 | }
|
89 | 90 | }
|
90 |
| -}); |
| 91 | +}) |
91 | 92 |
|
92 | 93 | window.addEventListener('storage', () => {
|
93 |
| - setColorScheme(getStoredScheme()); |
94 |
| -}); |
| 94 | + setColorScheme(getStoredScheme()) |
| 95 | +}) |
95 | 96 |
|
96 | 97 | window.addEventListener('pageshow', () => {
|
97 |
| - setColorScheme(getStoredScheme()); |
98 |
| -}); |
| 98 | + setColorScheme(getStoredScheme()) |
| 99 | +}) |
99 | 100 |
|
100 | 101 | function getToggleForm() {
|
101 |
| - return document.getElementById('color-scheme-toggle'); |
| 102 | + return document.getElementById('color-scheme-toggle') |
102 | 103 | }
|
103 | 104 |
|
104 | 105 | function getToggle() {
|
105 |
| - return getToggleForm()?.elements['color-scheme-preference']; |
| 106 | + return getToggleForm()?.elements['color-scheme-preference'] |
106 | 107 | }
|
107 | 108 |
|
108 | 109 | function getCurrentScheme() {
|
109 |
| - return document.body.getAttribute('data-color-scheme'); |
| 110 | + return document.body.getAttribute('data-color-scheme') |
110 | 111 | }
|
111 | 112 |
|
112 | 113 | function getStoredScheme() {
|
113 | 114 | try {
|
114 |
| - return localStorage.getItem(localStorageKey); |
| 115 | + return localStorage.getItem(localStorageKey) |
115 | 116 | } catch {
|
116 |
| - return null; |
| 117 | + return null |
117 | 118 | }
|
118 | 119 | }
|
119 | 120 |
|
120 | 121 | function saveScheme(value) {
|
121 | 122 | try {
|
122 |
| - localStorage.setItem(localStorageKey, value); |
123 |
| - } catch { |
124 |
| - } |
| 123 | + localStorage.setItem(localStorageKey, value) |
| 124 | + } catch {} |
125 | 125 | }
|
0 commit comments