forked from halo-dev/theme-earth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
91 lines (77 loc) · 2.84 KB
/
main.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
import "./styles/tailwind.css";
import "./styles/main.scss";
import Alpine from "alpinejs";
import * as tocbot from "tocbot";
import dropdown from "./alpine-data/dropdown";
import colorSchemeSwitcher from "./alpine-data/color-scheme-switcher";
import upvote from "./alpine-data/upvote";
import share from "./alpine-data/share";
window.Alpine = Alpine;
Alpine.data("dropdown", dropdown);
Alpine.data("colorSchemeSwitcher", colorSchemeSwitcher);
// @ts-ignore
Alpine.data("upvote", upvote);
// @ts-ignore
Alpine.data("share", share);
Alpine.start();
const onScroll = () => {
const headerMenu = document.getElementById("header-menu");
if (window.scrollY > 0) {
headerMenu?.classList.add("menu-sticky");
} else {
headerMenu?.classList.remove("menu-sticky");
}
};
window.addEventListener("scroll", onScroll);
export function generateToc() {
const content = document.getElementById("content");
const titles = content?.querySelectorAll("h1, h2, h3, h4");
if (!titles || titles.length === 0) {
const tocContainer = document.querySelector(".toc-container");
tocContainer?.remove();
return;
}
tocbot.init({
tocSelector: ".toc",
contentSelector: "#content",
headingSelector: "h1, h2, h3, h4",
extraListClasses: "space-y-1 dark:border-slate-500",
extraLinkClasses:
"group flex items-center justify-between rounded py-1 px-1.5 transition-all hover:bg-gray-100 text-sm opacity-80 dark:hover:bg-slate-700 dark:text-slate-50",
collapseDepth: 6,
headingsOffset: 100,
scrollSmooth: true,
scrollSmoothOffset: -100,
// @ts-ignore
tocScrollOffset: 50,
});
}
type ColorSchemeType = "system" | "dark" | "light";
export let currentColorScheme: ColorSchemeType = "system";
export function initColorScheme(defaultColorScheme: ColorSchemeType, enableChangeColorScheme: boolean) {
let colorScheme = defaultColorScheme;
if (enableChangeColorScheme) {
colorScheme = (localStorage.getItem("color-scheme") as ColorSchemeType) || defaultColorScheme;
}
currentColorScheme = colorScheme;
setColorScheme(colorScheme, false);
}
export function setColorScheme(colorScheme: ColorSchemeType, store: boolean) {
if (colorScheme === "system") {
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
document.documentElement.classList.add(prefersDark ? "dark" : "light");
document.documentElement.classList.remove(prefersDark ? "light" : "dark");
} else {
document.documentElement.classList.add(colorScheme);
document.documentElement.classList.remove(colorScheme === "dark" ? "light" : "dark");
}
currentColorScheme = colorScheme;
if (store) {
localStorage.setItem("color-scheme", colorScheme);
}
}
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", function () {
if (currentColorScheme === "system") {
setColorScheme("system", false);
}
});