-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
145 lines (131 loc) · 4.38 KB
/
Copy pathscript.js
File metadata and controls
145 lines (131 loc) · 4.38 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
144
145
function scrollToTop() {
document.body.scrollTo({
left: 0,
top: 0,
behavior: "smooth",
});
document.documentElement.scrollTo({
left: 0,
top: 0,
behavior: "smooth",
});
}
function getTheme() {
const stored = localStorage.getItem("theme");
if (stored === "light" || stored === "dark") return stored;
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
}
function applyTheme(theme) {
document.documentElement.setAttribute("data-theme", theme);
const toggleUses = document.querySelectorAll(
"#theme-toggle use, #theme-toggle-mobile use",
);
toggleUses.forEach((use) => {
use.setAttribute(
"href",
theme === "dark" ? "#icon-light-mode" : "#icon-dark-mode",
);
});
const iframe = document.getElementById("iframe-background");
if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage({ type: "theme", theme }, "*");
}
}
function toggleTheme() {
const stored = localStorage.getItem("theme");
if (stored === "light" || stored === "dark") {
localStorage.setItem("theme", "auto");
} else {
const systemIsDark = window.matchMedia(
"(prefers-color-scheme: dark)",
).matches;
localStorage.setItem("theme", systemIsDark ? "light" : "dark");
}
applyTheme(getTheme());
}
function updateTitlesAndTexts(lang) {
document.querySelectorAll("[data-title-zh]").forEach((el) => {
const title =
lang === "zh" ? el.dataset.titleZh : el.dataset.titleEn || el.dataset.titleZh;
el.setAttribute("title", title || "");
});
document.querySelectorAll("[data-text-zh]").forEach((el) => {
const text =
lang === "zh" ? el.dataset.textZh : el.dataset.textEn || el.dataset.textZh;
el.textContent = text || "";
});
}
function updateNavLinks(lang) {
document.querySelectorAll(".nav-lang-aware").forEach((link) => {
const base = link.dataset.hrefBase;
if (base) {
link.href = base.replace(/\.html$/, `-${lang}.html`);
}
});
}
document.addEventListener("DOMContentLoaded", () => {
applyTheme(getTheme());
const pageLang = document.documentElement.dataset.pageLang || "zh";
updateTitlesAndTexts(pageLang);
updateNavLinks(pageLang);
const tocContent = document.getElementById("toc-content");
const rightSidebar = document.getElementById("right-sidebar");
const mobileBtn = document.getElementById("toc-mobile-btn");
const mobileTocContent = document.getElementById("toc-content-mobile");
if (
tocContent &&
(!tocContent.innerHTML.trim() || tocContent.innerText.trim() === "")
) {
if (rightSidebar) rightSidebar.style.display = "none";
if (mobileBtn) mobileBtn.style.display = "none";
document.documentElement.style.setProperty("--right-sidebar-width", "0px");
} else if (mobileTocContent && tocContent) {
mobileTocContent.innerHTML = tocContent.innerHTML;
mobileTocContent.querySelectorAll("a").forEach((link) => {
link.addEventListener("click", () => {
const wrapper = document.getElementById("toc-mobile-wrapper");
if (wrapper) wrapper.classList.remove("active");
});
});
}
});
const titleContainer = document.getElementById("title-container");
const iframeBackground = document.getElementById("iframe-background");
var currentTopContentScrollStatus = false;
window.addEventListener(
"scroll",
() => {
let newScrollStatus = titleContainer.getBoundingClientRect().bottom < 0;
if (newScrollStatus == currentTopContentScrollStatus) return;
currentTopContentScrollStatus = newScrollStatus;
if (currentTopContentScrollStatus) {
iframeBackground.style.display = "none";
} else {
iframeBackground.style.display = "";
}
},
true,
);
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (e) => {
const stored = localStorage.getItem("theme");
if (!stored || stored === "auto") {
applyTheme(e.matches ? "dark" : "light");
}
});
function toggleToc() {
const wrapper = document.getElementById("toc-mobile-wrapper");
if (wrapper) wrapper.classList.toggle("active");
}
document.addEventListener("click", (e) => {
const toc = document.getElementById("toc-mobile-wrapper");
const btn = document.getElementById("toc-mobile-btn");
if (window.innerWidth < 1200 && toc && toc.classList.contains("active")) {
if (!toc.contains(e.target) && !btn.contains(e.target)) {
toc.classList.remove("active");
}
}
});