Add code copy button
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
4017705026
commit
47b613cf43
3 changed files with 35 additions and 2 deletions
32
templates/js/code_copy.js
Normal file
32
templates/js/code_copy.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
const copyButtonLabel = "Copy Code";
|
||||
|
||||
// use a class selector if available
|
||||
let blocks = document.querySelectorAll("pre");
|
||||
|
||||
blocks.forEach((block) => {
|
||||
// only add button if browser supports Clipboard API
|
||||
if (navigator.clipboard) {
|
||||
let button = document.createElement("button");
|
||||
|
||||
button.innerText = copyButtonLabel;
|
||||
block.appendChild(button);
|
||||
|
||||
button.addEventListener("click", async () => {
|
||||
await copyCode(block);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
async function copyCode(block, button) {
|
||||
let code = block.querySelector("code");
|
||||
let text = code.innerText;
|
||||
|
||||
await navigator.clipboard.writeText(text);
|
||||
|
||||
// visual feedback that task is completed
|
||||
button.innerText = "Code Copied";
|
||||
|
||||
setTimeout(() => {
|
||||
button.innerText = copyButtonLabel;
|
||||
}, 700);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue