Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (123 loc) · 4.36 KB
/
Copy pathscript.js
File metadata and controls
143 lines (123 loc) · 4.36 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* =========================================================
1 THEME
========================================================= */
const root = document.documentElement;
const toggle = document.getElementById("theme-toggle");
const toggleIcon = toggle.querySelector("use");
const themeColor = document.querySelector('meta[name="theme-color"]');
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
let followsSystem = true;
try {
const savedTheme = localStorage.getItem("deepak-theme");
followsSystem = savedTheme !== "light" && savedTheme !== "dark";
} catch {
// Without storage, follow the system until the visitor chooses a theme.
}
const currentTheme = () =>
root.dataset.theme === "system"
? (prefersDark.matches ? "dark" : "light")
: root.dataset.theme;
const syncTheme = () => {
themeColor.content = getComputedStyle(root).getPropertyValue("--bg").trim();
const isDark = currentTheme() === "dark";
toggle.setAttribute("aria-label", `Switch to ${isDark ? "light" : "dark"} theme`);
toggleIcon.setAttribute("href", isDark ? "#i-moon" : "#i-sun");
};
toggle.addEventListener("click", () => {
const next = currentTheme() === "dark" ? "light" : "dark";
root.dataset.theme = next;
followsSystem = false;
try {
localStorage.setItem("deepak-theme", next);
} catch {
// The theme still changes for this page view when storage is unavailable.
}
syncTheme();
});
prefersDark.addEventListener("change", () => {
if (!followsSystem) return;
syncTheme();
});
syncTheme();
/* =========================================================
2 SCROLL REVEAL
========================================================= */
const sections = [...document.querySelectorAll(".chapter")];
const chapterInners = sections
.map((section) => section.querySelector(".chapter-inner"))
.filter(Boolean);
chapterInners[0]?.classList.add("is-visible");
root.classList.add("reveal-ready");
const reveal = new IntersectionObserver((entries, observer) => {
entries.forEach(({ isIntersecting, target }) => {
if (!isIntersecting) return;
target.classList.add("is-visible");
observer.unobserve(target);
});
}, { rootMargin: "0px 0px -30px 0px" });
chapterInners.forEach((inner) => reveal.observe(inner));
/* =========================================================
3 ACTIVE CHAPTER (dot nav)
========================================================= */
const links = [...document.querySelectorAll(".chapter-nav a")];
let activeUpdatePending = false;
let trackedSectionId = null;
let sectionStartedAt = null;
const finishSectionTiming = () => {
if (!trackedSectionId || sectionStartedAt === null) return;
const durationSeconds = Math.round((performance.now() - sectionStartedAt) / 1000);
sectionStartedAt = null;
if (durationSeconds < 1) return;
window.umami?.track("section-time", {
section: trackedSectionId,
duration_seconds: durationSeconds,
});
};
const startSectionTiming = () => {
if (document.visibilityState !== "visible" || !trackedSectionId || sectionStartedAt !== null) return;
sectionStartedAt = performance.now();
};
const updateActiveChapter = () => {
activeUpdatePending = false;
const activationLine = window.innerHeight * 0.27;
let currentSection = sections[0];
for (const section of sections) {
if (section.getBoundingClientRect().top > activationLine) break;
currentSection = section;
}
if (!currentSection) return;
if (currentSection.getBoundingClientRect().bottom <= activationLine) {
finishSectionTiming();
trackedSectionId = null;
} else {
if (currentSection.id !== trackedSectionId) {
finishSectionTiming();
trackedSectionId = currentSection.id;
}
startSectionTiming();
}
links.forEach((link) => {
if (link.hash === `#${currentSection.id}`) {
link.setAttribute("aria-current", "location");
} else {
link.removeAttribute("aria-current");
}
});
};
const scheduleActiveUpdate = () => {
if (activeUpdatePending) return;
activeUpdatePending = true;
window.requestAnimationFrame(updateActiveChapter);
};
window.addEventListener("scroll", scheduleActiveUpdate, { passive: true });
window.addEventListener("resize", scheduleActiveUpdate);
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") {
finishSectionTiming();
} else {
updateActiveChapter();
}
});
window.addEventListener("pagehide", finishSectionTiming);
window.addEventListener("pageshow", updateActiveChapter);
updateActiveChapter();