2024-01-15 21:10:43 +00:00
|
|
|
import Storage from "store2"
|
|
|
|
import { TOGGLE_COLOR_THEMES, THEME, COLOR_THEME_AUTO } from "./config.js"
|
2022-12-21 10:41:23 +00:00
|
|
|
;(() => {
|
|
|
|
applyTheme()
|
|
|
|
})()
|
|
|
|
|
2024-08-28 07:31:33 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
2022-06-02 12:01:56 +00:00
|
|
|
const colorThemeToggle = document.getElementById("gblog-color-theme")
|
|
|
|
|
|
|
|
colorThemeToggle.onclick = function () {
|
|
|
|
let lstore = Storage.namespace(THEME)
|
2022-12-21 10:29:15 +00:00
|
|
|
let currentColorTheme = lstore.get("color-theme") || COLOR_THEME_AUTO
|
2022-06-02 12:01:56 +00:00
|
|
|
let nextColorTheme = toggle(TOGGLE_COLOR_THEMES, currentColorTheme)
|
|
|
|
|
|
|
|
lstore.set("color-theme", TOGGLE_COLOR_THEMES[nextColorTheme])
|
|
|
|
applyTheme(false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-12-21 10:41:23 +00:00
|
|
|
function applyTheme(init = true) {
|
2022-06-02 12:01:56 +00:00
|
|
|
if (Storage.isFake()) return
|
|
|
|
|
|
|
|
let lstore = Storage.namespace(THEME)
|
|
|
|
let html = document.documentElement
|
|
|
|
let currentColorTheme = TOGGLE_COLOR_THEMES.includes(lstore.get("color-theme"))
|
|
|
|
? lstore.get("color-theme")
|
|
|
|
: COLOR_THEME_AUTO
|
|
|
|
|
|
|
|
html.setAttribute("class", "color-toggle-" + currentColorTheme)
|
|
|
|
|
|
|
|
if (currentColorTheme === COLOR_THEME_AUTO) {
|
|
|
|
html.removeAttribute("color-theme")
|
|
|
|
} else {
|
|
|
|
html.setAttribute("color-theme", currentColorTheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!init) {
|
|
|
|
// Reload required to re-initialise e.g. Mermaid with the new theme
|
|
|
|
// and re-parse the Mermaid code blocks.
|
|
|
|
location.reload()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggle(list = [], value) {
|
|
|
|
let current = list.indexOf(value)
|
|
|
|
let max = list.length - 1
|
|
|
|
let next = 0
|
|
|
|
|
|
|
|
if (current < max) {
|
|
|
|
next = current + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return next
|
|
|
|
}
|