|
| 1 | +function createElementAndClass(element, classes) { |
| 2 | + result = document.createElement(element); |
| 3 | + result.className = classes; |
| 4 | + return result; |
| 5 | +} |
| 6 | + |
| 7 | +function createCopyButton(highlightDiv) { |
| 8 | + const codeBlockActionsCopyButton = createElementAndClass("button", "pf-c-button pf-m-plain"); |
| 9 | + codeBlockActionsCopyButton.type = "button"; |
| 10 | + codeBlockActionsCopyButton.addEventListener("click", () => |
| 11 | + copyCodeToClipboard(codeBlockActionsCopyButton, highlightDiv) |
| 12 | + ); |
| 13 | + const codeBlockActionsCopyButtonIcon = createElementAndClass("i", "fas fa-copy"); |
| 14 | + codeBlockActionsCopyButton.appendChild(codeBlockActionsCopyButtonIcon); |
| 15 | + addCopyButtonToDom(codeBlockActionsCopyButton, highlightDiv); |
| 16 | +} |
| 17 | + |
| 18 | +async function copyCodeToClipboard(button, highlightDiv) { |
| 19 | + const codeToCopy = highlightDiv.querySelector(":last-child > .highlight > code") |
| 20 | + .innerText; |
| 21 | + try { |
| 22 | + result = await navigator.permissions.query({ name: "clipboard-write" }); |
| 23 | + if (result.state == "granted" || result.state == "prompt") { |
| 24 | + await navigator.clipboard.writeText(codeToCopy); |
| 25 | + } else { |
| 26 | + copyCodeBlockExecCommand(codeToCopy, highlightDiv); |
| 27 | + } |
| 28 | + } catch (_) { |
| 29 | + copyCodeBlockExecCommand(codeToCopy, highlightDiv); |
| 30 | + } finally { |
| 31 | + |
| 32 | + codeWasCopied(button); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function copyCodeBlockExecCommand(codeToCopy, highlightDiv) { |
| 37 | + const textArea = document.createElement("textArea"); |
| 38 | + textArea.contentEditable = "true"; |
| 39 | + textArea.readOnly = "false"; |
| 40 | + textArea.className = "copyable-text-area"; |
| 41 | + textArea.value = codeToCopy; |
| 42 | + highlightDiv.insertBefore(textArea, highlightDiv.firstChild); |
| 43 | + const range = document.createRange(); |
| 44 | + range.selectNodeContents(textArea); |
| 45 | + const sel = window.getSelection(); |
| 46 | + sel.removeAllRanges(); |
| 47 | + sel.addRange(range); |
| 48 | + textArea.setSelectionRange(0, 999999); |
| 49 | + document.execCommand("copy"); |
| 50 | + highlightDiv.removeChild(textArea); |
| 51 | +} |
| 52 | + |
| 53 | +function codeWasCopied(button) { |
| 54 | + button.blur(); |
| 55 | + const icon = document.createElement("i"); |
| 56 | + icon.className = "fas fa-check"; |
| 57 | + button.replaceChild(icon, button.firstChild); |
| 58 | + setTimeout(function () { |
| 59 | + const icon = document.createElement("i"); |
| 60 | + icon.className = "fas fa-copy"; |
| 61 | + button.replaceChild(icon, button.firstChild); |
| 62 | + }, 2000); |
| 63 | +} |
| 64 | + |
| 65 | +function addCopyButtonToDom(button, highlightDiv) { |
| 66 | + dataLang = highlightDiv.querySelector("pre > code[data-lang]").attributes["data-lang"].value; |
| 67 | + highlightDiv.classList.add("pf-c-code-block__content") |
| 68 | + pre = highlightDiv.getElementsByTagName("pre"); |
| 69 | + pre[0].classList.add("pf-c-code-block__pre") |
| 70 | + code = pre[0].getElementsByTagName("code"); |
| 71 | + code[0].classList.add("pf-c-code-block__code") |
| 72 | + console.log(highlightDiv); |
| 73 | + highlightDiv.insertBefore(button, highlightDiv.firstChild); |
| 74 | + const codeBlock = createElementAndClass("div", "pf-c-code-block"); |
| 75 | + codeBlock.setAttribute("data-lang", dataLang) |
| 76 | + const codeBlockHeader = createElementAndClass("div", "pf-c-code-block__header"); |
| 77 | + const codeBlockActions = createElementAndClass("div", "pf-c-code-block__actions"); |
| 78 | + const codeBlockActionsCopy = createElementAndClass("div", "pf-c-code-block__actions-item"); |
| 79 | + codeBlockActionsCopy.appendChild(button); |
| 80 | + codeBlockActions.appendChild(codeBlockActionsCopy); |
| 81 | + codeBlockHeader.appendChild(codeBlockActions); |
| 82 | + codeBlock.appendChild(codeBlockHeader); |
| 83 | + highlightDiv.parentNode.insertBefore(codeBlock, highlightDiv); |
| 84 | + codeBlock.appendChild(highlightDiv); |
| 85 | + // const wrapper = document.createElement("div"); |
| 86 | + // wrapper.className = "highlight-wrapper"; |
| 87 | + // highlightDiv.parentNode.insertBefore(wrapper, highlightDiv); |
| 88 | + // wrapper.appendChild(highlightDiv); |
| 89 | +} |
| 90 | + |
| 91 | +document.querySelectorAll(".listingblock .content").forEach((highlightDiv) => createCopyButton(highlightDiv)); |
0 commit comments