site.lyte.dev/static/global.mjs

92 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-05-23 11:59:29 -05:00
import { Theme } from "./theme.mjs";
2023-07-10 11:15:32 -05:00
// import "./preload.mjs";
2022-03-09 10:23:26 -06:00
2023-05-23 11:59:29 -05:00
document.querySelectorAll(".js-only").forEach((a) =>
a.classList.remove("js-only")
);
document.querySelectorAll(".js-disabled-only").forEach((a) => a.remove());
2022-03-09 10:23:26 -06:00
2023-05-23 11:59:29 -05:00
const theme = new Theme();
document.querySelectorAll(".theme-toggler").forEach((a) =>
a.addEventListener("click", () => theme.cycle())
);
2020-12-15 16:30:13 -06:00
const initAlign = () => {
const cur = localStorage.getItem("align");
const prev = cur == "center" ? "left" : "center";
2023-05-23 11:59:29 -05:00
document.body.classList.remove("align-" + prev);
2024-05-26 10:05:00 -05:00
document.body.classList.add("align-" + cur);
};
if (localStorage.getItem("align") === null) {
localStorage.setItem("align", "center");
2020-12-15 16:30:13 -06:00
}
2022-03-09 10:23:26 -06:00
const toggleAlign = (ev) => {
localStorage.setItem(
"align",
localStorage.getItem("align") == "center" ? "left" : "center",
);
initAlign();
if (!!ev.target) ev.preventDefault();
return false;
};
2023-05-23 11:59:29 -05:00
document.querySelectorAll(".align-toggler").forEach((a) =>
a.addEventListener("click", toggleAlign)
);
2022-03-09 10:23:26 -06:00
window.addEventListener("load", (_ev) => {
2023-06-26 14:57:21 -05:00
const selector = [1, 2, 3, 4, 5, 6].map((n) => `main > h${n}[id]`).join(",");
2023-05-23 11:59:29 -05:00
document.querySelectorAll(selector).forEach((el) => {
const anchorLink = document.createElement("a");
anchorLink.classList.add("anchor-link");
anchorLink.textContent = "§";
anchorLink.href = `#${el.id}`;
el.appendChild(anchorLink);
});
});
2022-03-09 10:23:26 -06:00
function initCodeCopyButtons() {
const codeBlocks = document.querySelectorAll("pre.chroma");
codeBlocks.forEach((block) => {
const code = block.querySelectorAll("code")[0];
2023-06-26 14:35:12 -05:00
// const buttonContainer = document.createElement("p");
// buttonContainer.classList.add("copy-code-button-container");
const button = document.createElement("button");
2023-06-26 14:35:12 -05:00
// buttonContainer.appendChild(button);
button.classList.add("copy-code-button");
button.textContent = "Copy";
button.addEventListener("click", (_ev) => {
2023-05-11 14:40:11 -05:00
button.disabled = true;
navigator.clipboard.writeText(code.textContent).catch(() => {
const lastText = button.textContent;
button.textContent = "Error Copying";
setTimeout(() => {
button.disabled = false;
button.textContent = lastText;
}, 500);
}).then(() => {
const lastText = button.textContent;
button.textContent = "Copied to clipbooard!";
setTimeout(() => {
button.disabled = false;
button.textContent = lastText;
}, 3000);
});
});
2023-06-26 14:35:12 -05:00
block.parentNode.appendChild(button);
});
2020-12-15 16:30:13 -06:00
}
2022-03-09 10:23:26 -06:00
2023-07-06 17:02:38 -05:00
window.addEventListener("load", initAlign);
2024-07-09 11:59:15 -05:00
// window.addEventListener("load", initCodeCopyButtons);
2023-07-06 17:02:38 -05:00
window.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll(".nojs").forEach(e => {
e.classList.remove("nojs")
})
})