From e77051ccc4b47951bfa4fde2be436b1bb2fb113b Mon Sep 17 00:00:00 2001 From: Christoph Cullmann Date: Sun, 28 Apr 2024 17:33:09 +0200 Subject: use https://github.com/nunocoracao/blowfish.git --- themes/blowfish/assets/js/code.js | 66 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 themes/blowfish/assets/js/code.js (limited to 'themes/blowfish/assets/js/code.js') 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)); +}); -- cgit v1.2.3