summaryrefslogtreecommitdiff
path: root/themes/blowfish/assets/js/code.js
diff options
context:
space:
mode:
authorChristoph Cullmann <cullmann@kde.org>2024-04-28 17:33:09 +0200
committerChristoph Cullmann <cullmann@kde.org>2024-04-28 17:33:09 +0200
commite77051ccc4b47951bfa4fde2be436b1bb2fb113b (patch)
treef0b75ee3521da9c8cd39dac4359212348f70e4e8 /themes/blowfish/assets/js/code.js
parent4b355837824ac2422d371acef790f0f4249255c7 (diff)
use https://github.com/nunocoracao/blowfish.git
Diffstat (limited to 'themes/blowfish/assets/js/code.js')
-rw-r--r--themes/blowfish/assets/js/code.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/themes/blowfish/assets/js/code.js b/themes/blowfish/assets/js/code.js
new file mode 100644
index 0000000..88ae18d
--- /dev/null
+++ b/themes/blowfish/assets/js/code.js
@@ -0,0 +1,66 @@
+var scriptBundle = document.getElementById("script-bundle");
+var copyText = scriptBundle && scriptBundle.getAttribute("data-copy")? scriptBundle.getAttribute("data-copy") : "Copy";
+var copiedText = scriptBundle && scriptBundle.getAttribute("data-copied")? scriptBundle.getAttribute("data-copied") : "Copied";
+
+function createCopyButton(highlightDiv) {
+ const button = document.createElement("button");
+ button.className = "copy-button";
+ button.type = "button";
+ button.ariaLabel = copyText;
+ button.innerText = copyText;
+ button.addEventListener("click", () => copyCodeToClipboard(button, highlightDiv));
+ addCopyButtonToDom(button, highlightDiv);
+}
+
+async function copyCodeToClipboard(button, highlightDiv) {
+ const codeToCopy = highlightDiv.querySelector(":last-child").innerText;
+ try {
+ result = await navigator.permissions.query({ name: "clipboard-write" });
+ if (result.state == "granted" || result.state == "prompt") {
+ await navigator.clipboard.writeText(codeToCopy);
+ } else {
+ copyCodeBlockExecCommand(codeToCopy, highlightDiv);
+ }
+ } catch (_) {
+ copyCodeBlockExecCommand(codeToCopy, highlightDiv);
+ } finally {
+ codeWasCopied(button);
+ }
+}
+
+function copyCodeBlockExecCommand(codeToCopy, highlightDiv) {
+ const textArea = document.createElement("textArea");
+ textArea.contentEditable = "true";
+ textArea.readOnly = "false";
+ textArea.className = "copy-textarea";
+ textArea.value = codeToCopy;
+ highlightDiv.insertBefore(textArea, highlightDiv.firstChild);
+ const range = document.createRange();
+ range.selectNodeContents(textArea);
+ const sel = window.getSelection();
+ sel.removeAllRanges();
+ sel.addRange(range);
+ textArea.setSelectionRange(0, 999999);
+ document.execCommand("copy");
+ highlightDiv.removeChild(textArea);
+}
+
+function codeWasCopied(button) {
+ button.blur();
+ button.innerText = copiedText;
+ setTimeout(function () {
+ button.innerText = copyText;
+ }, 2000);
+}
+
+function addCopyButtonToDom(button, highlightDiv) {
+ highlightDiv.insertBefore(button, highlightDiv.firstChild);
+ const wrapper = document.createElement("div");
+ wrapper.className = "highlight-wrapper";
+ highlightDiv.parentNode.insertBefore(wrapper, highlightDiv);
+ wrapper.appendChild(highlightDiv);
+}
+
+window.addEventListener("DOMContentLoaded", (event) => {
+ document.querySelectorAll(".highlight").forEach((highlightDiv) => createCopyButton(highlightDiv));
+});