mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 06:47:49 +00:00
Update js with latest improvements from second edition
This commit is contained in:
@@ -1,17 +1,23 @@
|
|||||||
window.onload = function () {
|
window.onload = function () {
|
||||||
var container = document.querySelector('#toc-aside');
|
let container = document.querySelector('#toc-aside');
|
||||||
|
|
||||||
if (container != null) {
|
if (container != null) {
|
||||||
resize_toc(container);
|
resize_toc(container);
|
||||||
toc_scroll_position(container);
|
toc_scroll_position(container);
|
||||||
window.onscroll = function () { toc_scroll_position(container) };
|
window.onscroll = function () { toc_scroll_position(container) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let theme = localStorage.getItem("theme");
|
||||||
|
if (theme != null) {
|
||||||
|
setTimeout(() => {
|
||||||
|
set_giscus_theme(theme)
|
||||||
|
}, 500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function resize_toc(container) {
|
function resize_toc(container) {
|
||||||
var containerHeight = container.clientHeight;
|
let containerHeight = container.clientHeight;
|
||||||
|
|
||||||
var resize = function () {
|
let resize = function () {
|
||||||
if (containerHeight > document.documentElement.clientHeight - 100) {
|
if (containerHeight > document.documentElement.clientHeight - 100) {
|
||||||
container.classList.add('coarse');
|
container.classList.add('coarse');
|
||||||
} else {
|
} else {
|
||||||
@@ -20,7 +26,7 @@ function resize_toc(container) {
|
|||||||
};
|
};
|
||||||
resize();
|
resize();
|
||||||
|
|
||||||
var resizeId;
|
let resizeId;
|
||||||
window.onresize = function () {
|
window.onresize = function () {
|
||||||
clearTimeout(resizeId);
|
clearTimeout(resizeId);
|
||||||
resizeId = setTimeout(resize, 300);
|
resizeId = setTimeout(resize, 300);
|
||||||
@@ -32,7 +38,6 @@ function toc_scroll_position(container) {
|
|||||||
// skip computation if ToC is not visible
|
// skip computation if ToC is not visible
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var items = container.querySelectorAll("li")
|
|
||||||
|
|
||||||
// remove active class for all items
|
// remove active class for all items
|
||||||
for (item of container.querySelectorAll("li")) {
|
for (item of container.querySelectorAll("li")) {
|
||||||
@@ -40,15 +45,15 @@ function toc_scroll_position(container) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// look for active item
|
// look for active item
|
||||||
var site_offset = document.documentElement.scrollTop;
|
let site_offset = document.documentElement.scrollTop;
|
||||||
var current_toc_item = null;
|
let current_toc_item = null;
|
||||||
for (item of container.querySelectorAll("li")) {
|
for (item of container.querySelectorAll("li")) {
|
||||||
if (item.offsetParent === null) {
|
if (item.offsetParent === null) {
|
||||||
// skip items that are not visible
|
// skip items that are not visible
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
var anchor = item.firstElementChild.getAttribute("href");
|
let anchor = item.firstElementChild.getAttribute("href");
|
||||||
var heading = document.querySelector(anchor);
|
let heading = document.querySelector(anchor);
|
||||||
if (heading.offsetTop <= (site_offset + document.documentElement.clientHeight / 3)) {
|
if (heading.offsetTop <= (site_offset + document.documentElement.clientHeight / 3)) {
|
||||||
current_toc_item = item;
|
current_toc_item = item;
|
||||||
} else {
|
} else {
|
||||||
@@ -63,27 +68,32 @@ function toc_scroll_position(container) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function toggle_lights() {
|
function toggle_lights() {
|
||||||
var body = document.querySelector("body");
|
if (document.documentElement.getAttribute("data-theme") === "dark") {
|
||||||
var comment_form = document.querySelector("iframe.utterances-frame");
|
set_theme("light")
|
||||||
if (body != null) {
|
} else if (document.documentElement.getAttribute("data-theme") === "light") {
|
||||||
if (body.classList.contains("dark")) {
|
set_theme("dark")
|
||||||
body.classList.replace("dark", "light");
|
|
||||||
if (comment_form != null) {
|
|
||||||
comment_form.contentWindow.postMessage(
|
|
||||||
{ type: "set-theme", theme: "github-light" },
|
|
||||||
"https://utteranc.es/"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
body.classList.remove("light");
|
set_theme(window.matchMedia("(prefers-color-scheme: dark)").matches ? "light" : "dark")
|
||||||
body.classList.add("dark");
|
}
|
||||||
if (comment_form != null) {
|
}
|
||||||
comment_form.contentWindow.postMessage(
|
|
||||||
{ type: "set-theme", theme: "github-dark" },
|
function set_theme(theme) {
|
||||||
"https://utteranc.es/"
|
document.documentElement.setAttribute("data-theme", theme)
|
||||||
)
|
set_giscus_theme(theme)
|
||||||
}
|
localStorage.setItem("theme", theme)
|
||||||
}
|
}
|
||||||
console.log(body)
|
|
||||||
|
function clear_theme_override() {
|
||||||
|
document.documentElement.removeAttribute("data-theme");
|
||||||
|
set_giscus_theme("preferred_color_scheme")
|
||||||
|
localStorage.removeItem("theme")
|
||||||
|
}
|
||||||
|
|
||||||
|
function set_giscus_theme(theme) {
|
||||||
|
let comment_form = document.querySelector("iframe.giscus-frame");
|
||||||
|
if (comment_form != null) {
|
||||||
|
comment_form.contentWindow.postMessage({
|
||||||
|
giscus: { setConfig: { theme: theme } }
|
||||||
|
}, "https://giscus.app")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user