mirror of
https://github.com/BBaoVanC/bobatheme.git
synced 2026-09-22 00:34:55 -05:00
Instead of looping through all the code blocks at onload, just put an onClick tag in the HTML template, which calls a global function. This also improves the semantics since we aren't abusing href to be javascript. Also improves the writeText function call, since that method actually returns a Promise which we should be waiting on. It technically worked before because Promise starts executing instantly in JS, without being awaited.
23 lines
936 B
JavaScript
23 lines
936 B
JavaScript
// code block copy to clipboard
|
|
async function copy_to_clipboard(button) {
|
|
const codeBlock = button.parentElement.parentElement;
|
|
|
|
// lang will not be unset because we default it to textoriginalCopyText
|
|
// clone it so it doesn't change the actual DOM element
|
|
const codeElem = codeBlock.querySelector("code[data-lang]").cloneNode(true);
|
|
// bashsession: remove command output lines
|
|
codeElem.querySelectorAll(".go").forEach(e => e.parentNode.removeChild(e));
|
|
// bashsession: remove prompt symbol
|
|
codeElem.querySelectorAll(".gp").forEach(e => e.parentNode.removeChild(e));
|
|
const rawCode = codeElem.innerText;
|
|
|
|
await navigator.clipboard.writeText(rawCode);
|
|
|
|
// TODO: maybe we could add a fancier indicator, like a flash or something
|
|
const originalCopyText = button.innerHTML;
|
|
button.innerHTML = "Copied!";
|
|
setTimeout(() => {
|
|
button.innerHTML = originalCopyText;
|
|
}, 3000);
|
|
}
|