1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 16:46:39 -04:00

slightly faster attachment of clipboard.js to DOM

This commit is contained in:
2021-08-16 09:22:51 -04:00
parent 6e27cecba1
commit 96d5ad5ff1

View File

@ -14,25 +14,25 @@ if (ClipboardJS.isSupported()) {
// insert button as a sibling to Hugo's code fence // insert button as a sibling to Hugo's code fence
highlightDiv.insertBefore(button, highlightDiv.firstChild); highlightDiv.insertBefore(button, highlightDiv.firstChild);
});
new ClipboardJS(button, { new ClipboardJS("button.copy-button", {
text: (trigger) => { text: (trigger) => {
// actual code element will (should) have class "language-*", even if plaintext // actual code element will (should) have class "language-*", even if plaintext
const fenceElement = trigger.parentElement.querySelector('code[class^="language-"]'); // eslint-disable-line quotes const fenceElement = trigger.parentElement.querySelector('code[class^="language-"]'); // eslint-disable-line quotes
return fenceElement ? trimNewlines(fenceElement.innerText) : false; return fenceElement ? trimNewlines(fenceElement.innerText) : false;
}, },
}).on("success", (e) => { }).on("success", (e) => {
// show a subtle indication of success // show a subtle indication of success
e.trigger.innerText = "✓"; e.trigger.innerText = "✓";
// reset button to original text after 2 seconds // reset button to original text after 2 seconds
setTimeout(() => { setTimeout(() => {
e.trigger.innerText = copyTerm; e.trigger.innerText = copyTerm;
}, 2000); }, 2000);
// text needed to be auto-selected to copy, unselect immediately // text needed to be auto-selected to copy, unselect immediately
e.clearSelection(); e.clearSelection();
});
}); });
} }