Add code copy button
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Isaac Mills 2024-04-23 08:44:25 -04:00
parent 90d78f9abb
commit 7f6548e805
Signed by: fnmain
GPG key ID: B67D7410F33A0F61
2 changed files with 13 additions and 17 deletions

View file

@ -1,7 +1,4 @@
.content pre button {
position: absolute;
top: 5px;
right: 5px;}.content code .a {
.content code .a {
color: #483D8B;
}
.content code .b {
@ -155,7 +152,6 @@
color: #483D8B;
}
.content pre {
position: relative;
border: 1px solid #999;
color: #000000;
background-color: #FFFFFF;

View file

@ -1,21 +1,21 @@
const copyButtonLabel = "Copy Code";
// use a class selector if available
let blocks = document.querySelectorAll("pre");
if (navigator.clipboard) {
let blocks = document.querySelectorAll("pre");
blocks.forEach((block) => {
// only add button if browser supports Clipboard API
if (navigator.clipboard) {
let button = document.createElement("button");
blocks.forEach((block) => {
// only add button if browser supports Clipboard API
let button = document.createElement("button");
button.innerText = copyButtonLabel;
block.appendChild(button);
button.innerText = copyButtonLabel;
block.parentNode.insertBefore(button, block);
button.addEventListener("click", async (event) => {
await copyCode(block, event.target);
});
}
});
button.addEventListener("click", async (event) => {
await copyCode(block, event.target);
});
});
}
async function copyCode(block, button) {
let code = block.querySelector("code");