|
| 1 | +/** |
| 2 | + * Copy-to-clipboard functionality for copyable text elements. |
| 3 | + * Uses the Clipboard API with visual feedback. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Initialize all copyable elements on the page. |
| 8 | + */ |
| 9 | +export function initCopyButtons() { |
| 10 | + const copyables = document.querySelectorAll("[data-copyable]"); |
| 11 | + copyables.forEach(initCopyable); |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Initialize a single copyable element. |
| 16 | + * @param {HTMLElement} element - The copyable container element |
| 17 | + */ |
| 18 | +function initCopyable(element) { |
| 19 | + // Prevent duplicate initialization |
| 20 | + if (element.hasAttribute("data-copy-initialized")) { |
| 21 | + return; |
| 22 | + } |
| 23 | + element.setAttribute("data-copy-initialized", "true"); |
| 24 | + |
| 25 | + const button = element.querySelector("[data-copy-button]"); |
| 26 | + const textElement = element.querySelector("[data-copy-text]"); |
| 27 | + const copyIcon = element.querySelector("[data-copy-icon]"); |
| 28 | + const checkIcon = element.querySelector("[data-check-icon]"); |
| 29 | + |
| 30 | + if (!button || !textElement) return; |
| 31 | + |
| 32 | + // Store timeout ID for cleanup on rapid clicks |
| 33 | + let resetTimeout = null; |
| 34 | + |
| 35 | + button.addEventListener("click", async () => { |
| 36 | + const text = textElement.textContent?.trim() || ""; |
| 37 | + |
| 38 | + // Clear any pending timeout from previous click |
| 39 | + if (resetTimeout) { |
| 40 | + clearTimeout(resetTimeout); |
| 41 | + resetTimeout = null; |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + await navigator.clipboard.writeText(text); |
| 46 | + showSuccessFeedback(copyIcon, checkIcon, (timeoutId) => { |
| 47 | + resetTimeout = timeoutId; |
| 48 | + }); |
| 49 | + } catch (err) { |
| 50 | + console.error("Failed to copy text:", err); |
| 51 | + // Fallback for older browsers |
| 52 | + const success = fallbackCopy(text); |
| 53 | + if (success) { |
| 54 | + showSuccessFeedback(copyIcon, checkIcon, (timeoutId) => { |
| 55 | + resetTimeout = timeoutId; |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Show success feedback by toggling icons. |
| 64 | + * @param {HTMLElement|null} copyIcon - The copy icon element |
| 65 | + * @param {HTMLElement|null} checkIcon - The check icon element |
| 66 | + * @param {function} onTimeout - Callback to store the timeout ID |
| 67 | + */ |
| 68 | +function showSuccessFeedback(copyIcon, checkIcon, onTimeout) { |
| 69 | + if (copyIcon && checkIcon) { |
| 70 | + copyIcon.classList.add("hidden"); |
| 71 | + checkIcon.classList.remove("hidden"); |
| 72 | + |
| 73 | + // Reset after 2 seconds |
| 74 | + const timeoutId = setTimeout(() => { |
| 75 | + copyIcon.classList.remove("hidden"); |
| 76 | + checkIcon.classList.add("hidden"); |
| 77 | + }, 2000); |
| 78 | + |
| 79 | + if (onTimeout) { |
| 80 | + onTimeout(timeoutId); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Fallback copy method for browsers without Clipboard API. |
| 87 | + * @param {string} text - The text to copy |
| 88 | + * @returns {boolean} - Whether the copy was successful |
| 89 | + */ |
| 90 | +function fallbackCopy(text) { |
| 91 | + const textarea = document.createElement("textarea"); |
| 92 | + textarea.value = text; |
| 93 | + textarea.style.position = "fixed"; |
| 94 | + textarea.style.opacity = "0"; |
| 95 | + document.body.appendChild(textarea); |
| 96 | + textarea.select(); |
| 97 | + let success = false; |
| 98 | + try { |
| 99 | + success = document.execCommand("copy"); |
| 100 | + } catch (err) { |
| 101 | + console.error("Fallback copy failed:", err); |
| 102 | + } |
| 103 | + document.body.removeChild(textarea); |
| 104 | + return success; |
| 105 | +} |
0 commit comments