hugo-WonderMod/assets/js/papermod.js

106 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-07-21 18:00:36 -04:00
import * as params from '@params';
function scrollToTop() {
2022-07-29 10:15:27 -04:00
const topButton = document.getElementById("top-link");
2022-07-29 09:59:19 -04:00
document.addEventListener('scroll', () => {
if (document.body.scrollTop > 600 || document.documentElement.scrollTop > 600) {
topButton.style.visibility = "visible";
topButton.style.opacity = "1";
2022-07-21 18:00:36 -04:00
} else {
2022-07-29 09:59:19 -04:00
topButton.style.visibility = "hidden";
topButton.style.opacity = "0";
2022-07-21 18:00:36 -04:00
}
2022-07-29 09:59:19 -04:00
})
2022-07-21 18:00:36 -04:00
}
function themeToggle() {
2022-07-29 10:15:27 -04:00
const themeButton = document.getElementById("theme-toggle");
2022-07-24 19:41:59 -04:00
themeButton.addEventListener("click", () => {
2022-07-21 18:00:36 -04:00
if (document.body.className.includes("dark")) {
document.body.classList.remove('dark');
localStorage.setItem("pref-theme", 'light');
} else {
document.body.classList.add('dark');
localStorage.setItem("pref-theme", 'dark');
}
})
}
function showCodeCopyButtons() {
document.querySelectorAll('pre > code').forEach((codeblock) => {
const container = codeblock.parentNode.parentNode;
const copybutton = document.createElement('button');
copybutton.classList.add('copy-code');
copybutton.innerHTML = 'copy';
function copyingDone() {
copybutton.innerHTML = 'copied!';
setTimeout(() => {
copybutton.innerHTML = 'copy';
}, 2000);
}
copybutton.addEventListener('click', (cb) => {
if ('clipboard' in navigator) {
navigator.clipboard.writeText(codeblock.textContent);
copyingDone();
return;
}
const range = document.createRange();
range.selectNodeContents(codeblock);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
try {
document.execCommand('copy');
copyingDone();
} catch (e) { };
selection.removeRange(range);
});
if (container.classList.contains("highlight")) {
container.appendChild(copybutton);
} else if (container.parentNode.firstChild == container) {
// td containing LineNos
} else if (codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "TABLE") {
// table containing LineNos and code
codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.appendChild(copybutton);
} else {
// code blocks not having highlight as parent class
codeblock.parentNode.appendChild(copybutton);
}
});
}
2022-07-27 21:17:31 -04:00
function openToc() {
2022-07-29 10:15:27 -04:00
const toc = document.getElementById("toc");
2022-07-27 21:17:31 -04:00
2022-07-29 09:59:19 -04:00
if (!toc) return;
2022-07-28 18:07:11 -04:00
if (window.innerWidth > 1500 && window.innerHeight > 800) {
2022-07-27 21:17:31 -04:00
toc.open = true;
}
}
2022-07-28 18:07:11 -04:00
function progressBar() {
2022-07-29 10:15:27 -04:00
const bar = document.getElementById("progressBar");
2022-07-28 18:07:11 -04:00
2022-07-29 09:59:19 -04:00
if (!bar) return;
2022-07-28 18:07:11 -04:00
document.addEventListener('scroll', () => {
2022-07-29 10:15:27 -04:00
let scrollPercent = document.documentElement.scrollTop / (document.documentElement.scrollHeight - window.innerHeight) * 100;
if (scrollPercent >= 99) { scrollPercent = 0 };
bar.style.setProperty("--scrollAmount", scrollPercent + '%');
2022-07-28 18:07:11 -04:00
})
}
2022-07-29 09:59:19 -04:00
2022-07-21 18:00:36 -04:00
if (params.scrollToTop) scrollToTop();
if (params.themeToggle) themeToggle();
2022-07-28 18:07:11 -04:00
if (params.showCodeCopyButtons) showCodeCopyButtons();
2022-07-29 09:59:19 -04:00
openToc();
progressBar();