mirror of
https://github.com/BBaoVanC/bobatheme.git
synced 2026-09-22 08:44:55 -05:00
js: Fix copy to clipboard button stuck as "Copied" when clicked twice
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
// 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
|
||||
button.innerHTML = "Copied!";
|
||||
setTimeout(() => {
|
||||
button.innerHTML = "{{ i18n "copy_to_clipboard" }}";
|
||||
}, 3000);
|
||||
}
|
||||
Reference in New Issue
Block a user