diff options
| author | Jieiku | 2022-08-23 13:41:41 -0700 |
|---|---|---|
| committer | Jieiku | 2022-08-23 13:41:41 -0700 |
| commit | c7b914505b6505b013610cc71e3b50d68c4a5473 (patch) | |
| tree | d2abebfa75cedd1b7351b51f9d5c909834e8d05d /static/codecopy.js | |
| parent | 6e6c0cbbb5d6136788d35ee5a354d36079c3475a (diff) | |
| download | abridge-c7b914505b6505b013610cc71e3b50d68c4a5473.tar.gz abridge-c7b914505b6505b013610cc71e3b50d68c4a5473.tar.bz2 abridge-c7b914505b6505b013610cc71e3b50d68c4a5473.zip | |
Optimizations, Update Readme YellowLabTools
Diffstat (limited to 'static/codecopy.js')
| -rw-r--r-- | static/codecopy.js | 131 |
1 files changed, 66 insertions, 65 deletions
diff --git a/static/codecopy.js b/static/codecopy.js index f4d9f2b..c1f3a75 100644 --- a/static/codecopy.js +++ b/static/codecopy.js @@ -1,75 +1,76 @@ // https://aaronluna.dev/blog/add-copy-button-to-code-blocks-hugo-chroma/ // Further modified for Zola by Jieiku: https://github.com/Jieiku +(function() { + // create copy button + function ccb(highlightDiv) { + const button = document.createElement("button"); + button.className = "cc "; + button.type = "button"; + button.innerHTML = "Copy"; + button.addEventListener("click", () => + ccc(button, highlightDiv) + ); -// create copy button -function ccb(highlightDiv) { - const button = document.createElement("button"); - button.className = "cc "; - button.type = "button"; - button.innerHTML = "Copy"; - button.addEventListener("click", () => - ccc(button, highlightDiv) - ); - - // addCopyButtonToDom - const wrapper = document.createElement("div"); - wrapper.className = "ccw"; - wrapper.appendChild(button); - highlightDiv.parentNode.insertBefore(wrapper, highlightDiv); - wrapper.appendChild(highlightDiv); -} - -// copy code clipboard -async function ccc(button, highlightDiv) { - //look for table, if so do it different - const { length } = highlightDiv.querySelectorAll("table"); - let codeToCopy = ""; - if (length > 0) { - const items = highlightDiv.querySelectorAll(":last-child > tr > td:last-child"); - items.forEach(item => { - codeToCopy = codeToCopy + item.innerText; - //console.log(codeToCopy); - }); - } else { - codeToCopy = highlightDiv.querySelector(":last-child > code").innerText; + // addCopyButtonToDom + const wrapper = document.createElement("div"); + wrapper.className = "ccw"; + wrapper.appendChild(button); + highlightDiv.parentNode.insertBefore(wrapper, highlightDiv); + wrapper.appendChild(highlightDiv); } - try { - result = await navigator.permissions.query({ name: "clipboard-write" }); - if (result.state == "granted" || result.state == "prompt") { - await navigator.clipboard.writeText(codeToCopy); + + // copy code clipboard + async function ccc(button, highlightDiv) { + //look for table, if so do it different + const { length } = highlightDiv.querySelectorAll("table"); + let codeToCopy = ""; + if (length > 0) { + const items = highlightDiv.querySelectorAll(":last-child > tr > td:last-child"); + items.forEach(item => { + codeToCopy = codeToCopy + item.innerText; + //console.log(codeToCopy); + }); } else { + codeToCopy = highlightDiv.querySelector(":last-child > code").innerText; + } + try { + result = await navigator.permissions.query({ name: "clipboard-write" }); + if (result.state == "granted" || result.state == "prompt") { + await navigator.clipboard.writeText(codeToCopy); + } else { + cce(codeToCopy, highlightDiv);//copyCodeBlockExecCommand + } + } catch (_) { cce(codeToCopy, highlightDiv);//copyCodeBlockExecCommand + } finally { + //codeWasCopied + //button.blur(); + button.innerHTML = "Copied"; + setTimeout(function () { + button.innerHTML = "Copy"; + }, 2000); } - } catch (_) { - cce(codeToCopy, highlightDiv);//copyCodeBlockExecCommand - } finally { - //codeWasCopied - //button.blur(); - button.innerHTML = "Copied"; - setTimeout(function () { - button.innerHTML = "Copy"; - }, 2000); } -} -//copyCodeBlockExecCommand -function cce(codeToCopy, highlightDiv) { - const textArea = document.createElement("textArea"); - textArea.contentEditable = "true"; - textArea.readOnly = "false"; - textArea.className = "copyable-text-area"; - 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); -} + //copyCodeBlockExecCommand + function cce(codeToCopy, highlightDiv) { + const textArea = document.createElement("textArea"); + textArea.contentEditable = "true"; + textArea.readOnly = "false"; + textArea.className = "copyable-text-area"; + 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); + } -document - .querySelectorAll("pre") - .forEach((highlightDiv) => ccb(highlightDiv)); + document + .querySelectorAll("pre") + .forEach((highlightDiv) => ccb(highlightDiv)); +})();
\ No newline at end of file |
