Skip to content

Commit

Permalink
Added support for Sorcery.defer
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaydek Michels-Gualtieri committed Nov 4, 2020
1 parent 3a36192 commit bb4a848
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/runtime/Sorcery.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
interface ISorcery {
__deferers: (() => void)[]
init(): void
toggleDebugMode(): boolean
toggleDarkMode(): boolean
defer(): void
toggleDebugMode(): void
toggleDarkMode(): void
}

const Sorcery: ISorcery = {
__deferers: [],

init() {
const media = "matchMedia" in window && window.matchMedia("(prefers-color-scheme: dark)")
if (media) {
Expand All @@ -21,36 +25,42 @@ const Sorcery: ISorcery = {
// EventTarget in newer browsers.
//
// https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/addListener
media.addListener(() => {
const handleMedia = () => {
Sorcery.toggleDarkMode()
})
}
media.addListener(handleMedia)
Sorcery.__deferers.push(() => media.removeListener(handleMedia))
}

document.addEventListener("keydown", e => {
const handleKeyDown = (e: KeyboardEvent) => {
if (!e.ctrlKey && e.key.toLowerCase() === "d") {
Sorcery.toggleDarkMode()
} else if (e.ctrlKey && e.key.toLowerCase() === "d") {
Sorcery.toggleDebugMode()
}
})
}
document.addEventListener("keydown", handleKeyDown)
Sorcery.__deferers.push(() => document.removeEventListener("keydown", handleKeyDown))
},
defer() {
for (let x = 0; x < Sorcery.__deferers.length; x++) {
Sorcery.__deferers[x]()
}
},
toggleDebugMode() {
const mode = document.body.hasAttribute("data-debug")
if (!mode) {
const hasAttribute = document.body.hasAttribute("data-debug")
if (!hasAttribute) {
document.body.setAttribute("data-debug", "true")
} else {
document.body.removeAttribute("data-debug")
}
return !mode
},
toggleDarkMode() {
const mode = document.body.hasAttribute("data-theme")
if (!mode) {
const hasAttribute = document.body.hasAttribute("data-theme")
if (!hasAttribute) {
document.body.setAttribute("data-theme", "dark")
} else {
document.body.removeAttribute("data-theme")
}
return !mode
},
}

Expand Down

0 comments on commit bb4a848

Please sign in to comment.