Update highlightjs-copy.min.js

This commit is contained in:
foxfire52 2024-10-25 14:14:47 +02:00 committed by GitHub
parent 51d6f28511
commit e9ecdfe5dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -15,13 +15,25 @@ class CopyButtonPlugin {
el.parentElement.classList.add("hljs-copy-wrapper");
el.parentElement.appendChild(button);
el.parentElement.style.setProperty("--hljs-theme-background", window.getComputedStyle(el).backgroundColor);
button.onclick = function () {
if (!navigator.clipboard) return;
button.onclick = async () => {
if (!navigator.clipboard) {
console.error("navigator.clipboard: Clipboard API unavailable.")
return;
}
let newText = text;
if (hook && typeof hook === "function") {
newText = hook(text, el) || text
}
navigator.clipboard.writeText(newText).then(function () {
try {
console.warn("newText type: ", typeof newText);
console.warn("Current Text: " + newText);
await navigator.clipboard.writeText(newText);
} catch (e) {
console.error("Clipboard API writeText failed!!")
console.error(e);
fallback_copy(newText);
}
button.innerHTML = "Copied!";
button.dataset.copied = true;
let alert = Object.assign(document.createElement("div"), {
@ -36,9 +48,11 @@ class CopyButtonPlugin {
el.parentElement.removeChild(alert);
alert = null
}, 2e3)
}).then(function () {
if (typeof callback === "function") return callback(newText, el)
})
}
if (typeof callback === "function") return callback(newText, el);
}
}