|
| 1 | +/* Subscribe to the document$ observable provided by MkDocs Material */ |
| 2 | +document$.subscribe(function() { |
| 3 | + const codeElements = document.querySelectorAll("table td:first-child code"); |
| 4 | + |
| 5 | + codeElements.forEach(code => { |
| 6 | + if (code.closest(".copy-to-clipboard-wrapper")) return; |
| 7 | + |
| 8 | + const td = code.closest("td"); |
| 9 | + if (!td) return; |
| 10 | + |
| 11 | + td.classList.add("copy-to-clipboard-td"); |
| 12 | + |
| 13 | + const wrapper = document.createElement("span"); |
| 14 | + wrapper.classList.add("copy-to-clipboard-wrapper"); |
| 15 | + |
| 16 | + code.parentNode.insertBefore(wrapper, code); |
| 17 | + wrapper.appendChild(code); |
| 18 | + |
| 19 | + const button = document.createElement("button"); |
| 20 | + button.type = "button"; |
| 21 | + button.setAttribute("aria-label", "Copy to clipboard"); |
| 22 | + button.setAttribute("title", "Copy to clipboard"); |
| 23 | + button.classList.add("copy-to-clipboard-button"); |
| 24 | + button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 21H8V7h11m0-2H8a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h11a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2m-3-4H4a2 2 0 0 0-2 2v14h2V3h12V1Z"/></svg>'; |
| 25 | + |
| 26 | + wrapper.appendChild(button); |
| 27 | + |
| 28 | + button.addEventListener("click", (e) => { |
| 29 | + e.preventDefault(); |
| 30 | + e.stopPropagation(); |
| 31 | + |
| 32 | + const textToCopy = code.textContent.trim(); |
| 33 | + |
| 34 | + const fallbackCopy = () => { |
| 35 | + const textArea = document.createElement("textarea"); |
| 36 | + textArea.value = textToCopy; |
| 37 | + textArea.style.position = "fixed"; |
| 38 | + textArea.style.left = "-9999px"; |
| 39 | + textArea.style.top = "0"; |
| 40 | + document.body.appendChild(textArea); |
| 41 | + textArea.focus(); |
| 42 | + textArea.select(); |
| 43 | + try { |
| 44 | + document.execCommand('copy'); |
| 45 | + return true; |
| 46 | + } catch (err) { |
| 47 | + console.error('Fallback copy failed', err); |
| 48 | + return false; |
| 49 | + } |
| 50 | + document.body.removeChild(textArea); |
| 51 | + }; |
| 52 | + |
| 53 | + if (navigator.clipboard && window.isSecureContext) { |
| 54 | + navigator.clipboard.writeText(textToCopy) |
| 55 | + .then(() => showSuccess(button)) |
| 56 | + .catch(() => { |
| 57 | + if (fallbackCopy()) showSuccess(button); |
| 58 | + }); |
| 59 | + } else if (fallbackCopy()) { |
| 60 | + showSuccess(button); |
| 61 | + } |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + function showSuccess(button) { |
| 66 | + const originalHTML = button.innerHTML; |
| 67 | + |
| 68 | + button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M21 7 9 19l-5.5-5.5 1.41-1.41L9 16.17 19.59 5.59 21 7Z"/></svg>'; |
| 69 | + button.classList.add("copy-success"); |
| 70 | + |
| 71 | + setTimeout(() => { |
| 72 | + button.innerHTML = originalHTML; |
| 73 | + button.classList.remove("copy-success"); |
| 74 | + }, 1500); |
| 75 | + } |
| 76 | +}); |
0 commit comments