-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
app.ts
103 lines (95 loc) · 3.1 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* notion-enhancer
* (c) 2022 dragonwocky <[email protected]> (https://dragonwocky.me/)
* (https://notion-enhancer.github.io/) under the MIT license
*/
/// <reference lib="dom" />
import psyche, {
platformModifier,
registerHotkey,
} from "https://deno.land/x/[email protected]/client/psyche.min.mjs";
import { initRouter, onRouteChange } from "./_scripts/router.ts";
import { tiltElements } from "./_scripts/tilt.ts";
const eventListener = (
$: Element,
event: string,
listener: EventListenerOrEventListenerObject,
) => {
$.removeEventListener(event, listener);
$.addEventListener(event, listener);
};
const getMenu = () =>
document.querySelector<HTMLElement>("aside[aria-label='menu']"),
closeMenu = () => {
const $menu = getMenu();
if ($menu) $menu.style.removeProperty("--tw-translate-x");
},
toggleMenu = () => {
const $menu = getMenu();
if ($menu) {
const isOpen = $menu.style.getPropertyValue("--tw-translate-x");
isOpen ? closeMenu() : $menu.style.setProperty("--tw-translate-x", "0");
}
},
initMenu = () => {
const $menu = getMenu();
if (!$menu) return;
const $toggle = document.querySelector("[data-action='toggle-menu']"),
$close = [
document.querySelector("figure[role='banner']"),
document.querySelector("header"),
document.querySelector("article"),
...$menu.querySelectorAll("a"),
].filter(($) => $);
if ($toggle) eventListener($toggle, "click", toggleMenu);
for (const $ of $close) eventListener($!, "click", closeMenu);
};
const toggleTheme = () => {
document.documentElement.classList.toggle("dark");
const mode = document.documentElement.classList.contains("dark");
localStorage["theme"] = mode ? "dark" : "light";
},
initTheme = () => {
const mediaMode = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "",
storedMode = localStorage["theme"];
if (storedMode === "dark" || (!storedMode && mediaMode === "dark")) {
document.documentElement.classList.add("dark");
}
const $buttons = document.querySelectorAll("[data-action='toggle-theme']");
$buttons.forEach(($) => eventListener($, "click", toggleTheme));
};
const searchInstance = psyche({
theme: {
font: { sans: "Inter", mono: "Fira Code" },
light: { accent: "#a78bfa" },
dark: { accent: "#d8b4fe" },
scrollbarStyle: "square",
},
hotkeys: [{
kbd: `${platformModifier} + SHIFT + L`,
label: "to toggle theme",
}],
index: await fetch("/search.json").then((res) => res.json()),
}),
initSearch = () => {
const $buttons = document.querySelectorAll("[data-action='open-search']");
$buttons.forEach(($) => eventListener($, "click", searchInstance.open));
searchInstance.unregister();
searchInstance.register();
};
registerHotkey({
key: "l",
platformModifier: true,
shiftKey: true,
onkeydown: (event: KeyboardEvent) => {
event.preventDefault();
toggleTheme();
},
});
onRouteChange(initMenu);
onRouteChange(initTheme);
onRouteChange(initSearch);
onRouteChange(tiltElements);
initRouter();