-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (80 loc) · 2.4 KB
/
script.js
File metadata and controls
96 lines (80 loc) · 2.4 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
$(document).ready(function() {
$(window).resize(refreshAsides);
// Since we may not have the height correct for the images, adjust the asides
// too when an image is loaded.
$('img').on('load', function() {
refreshAsides();
});
// On the off chance the browser supports the new font loader API, use it.
if (document.fontloader) {
document.fontloader.notifyWhenFontsReady(function() {
refreshAsides();
});
}
// Lame. Just do another refresh after a second when the font is *probably*
// loaded to hack around the fact that the metrics changed a bit.
window.setTimeout(refreshAsides, 200);
refreshAsides();
loadInitialTheme();
});
function refreshAsides() {
// Don't position them if they're inline.
if ($(document).width() < 800) return;
// Vertically position the asides next to the span they annotate.
$("aside").each(function() {
var aside = $(this);
// Find the span the aside should be anchored next to.
var name = aside.attr("name");
var span = $("span[name='" + name.replace("'", "\\'") + "']");
if (span == null) {
window.console.log("Could not find span for '" + name + "'");
return;
}
if (span.position()) {
aside.offset({top: span.position().top - 3});
}
});
}
function loadInitialTheme() {
const theme = localStorage.getItem("theme");
if (theme === "dark") {
toggleTheme();
return;
}
const userPrefersDarkTheme = window.matchMedia(
"(prefers-color-scheme: dark)"
).matches;
if (userPrefersDarkTheme) {
toggleTheme();
return;
}
}
function toggleTheme() {
document.body.classList.toggle("dark");
if (document.body.classList.contains("dark")) {
document
.querySelector(".theme-toggler")
.setAttribute("title", "light theme");
localStorage.setItem("theme", "dark");
} else {
document
.querySelector(".theme-toggler")
.setAttribute("title", "dark theme");
localStorage.removeItem("theme");
}
}
function getCurentTheme() {
return localStorage.getItem("theme") === "dark" ? "dark" : "light";
}
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", function (e) {
const prefersDarkTheme = e.matches;
const prefersLightTheme = !prefersDarkTheme;
if (
(prefersDarkTheme && getCurentTheme() !== "dark") ||
(prefersLightTheme && getCurentTheme() !== "light")
) {
toggleTheme();
}
});