🚧 feat: add basic copy button to codeblocks

This commit is contained in:
welpo
2023-07-06 02:45:54 +02:00
parent 700037afe5
commit 8e1473bba9
2 changed files with 44 additions and 1 deletions

19
static/js/copy_button.js Normal file
View File

@@ -0,0 +1,19 @@
function changeIcon(copyDiv, className) {
copyDiv.classList.add(className);
setTimeout(() => copyDiv.classList.remove(className), 2500);
}
document.querySelectorAll("pre").forEach((block) => {
const copyDiv = document.createElement("div");
copyDiv.className = "copy-code";
block.prepend(copyDiv);
copyDiv.addEventListener("click", function () {
const code = block.innerText;
navigator.clipboard.writeText(code).then(() => {
changeIcon(copyDiv, "checked");
}, () => {
changeIcon(copyDiv, "error");
});
});
});