馃悕馃悕馃悕
1
2function checkForParentTheme(element, theme) {
3 let parent = element.parentElement;
4 while (parent) {
5 const parentTheme = parent.dataset.theme;
6
7 if (parentTheme) return parentTheme !== theme;
8
9 parent = parent.parentElement;
10 }
11
12 return false;
13}
14
15function getOppositeTheme(theme) {
16 if (theme === "blackboard") return "whiteboard";
17 if (theme === "whiteboard") return "blackboard";
18 return theme;
19}
20
21export function main(target, initialTheme = null) {
22 const storedTheme = localStorage.getItem("theme");
23
24 let theme = storedTheme || "blackboard";
25
26 if (initialTheme === "toggle") {
27 theme = getOppositeTheme(theme);
28 } else {
29 theme = initialTheme || theme;
30 }
31
32 target.dataset.theme = theme;
33
34 if (target === document.body) {
35 localStorage.setItem("theme", theme);
36 }
37
38 if (checkForParentTheme(target, theme)) {
39 target.dataset.themeChanged = "";
40 } else {
41 delete target.dataset.themeChanged;
42 }
43
44 return { replace: false };
45}
46