|
| 1 | +/** |
| 2 | + * Copy code functionality for code blocks |
| 3 | + * This script adds click handlers to copy buttons in code blocks |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Initialize copy code functionality |
| 8 | + * This should be called after the DOM is loaded |
| 9 | + */ |
| 10 | +export function initCodeCopy(): void { |
| 11 | + // Remove any existing event listeners to prevent duplicates |
| 12 | + const existingButtons = document.querySelectorAll('.copy-code-btn'); |
| 13 | + existingButtons.forEach((button) => { |
| 14 | + if (button instanceof HTMLElement) { |
| 15 | + button.removeEventListener('click', handleCopyClick); |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | + // Find all copy buttons and add new event listeners |
| 20 | + const copyButtons = document.querySelectorAll('.copy-code-btn'); |
| 21 | + |
| 22 | + // Add click handler to each button |
| 23 | + copyButtons.forEach((button) => { |
| 24 | + if (button instanceof HTMLElement) { |
| 25 | + button.addEventListener('click', handleCopyClick); |
| 26 | + } |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Handle click on copy button |
| 32 | + */ |
| 33 | +function handleCopyClick(event: Event): void { |
| 34 | + event.preventDefault(); |
| 35 | + event.stopPropagation(); |
| 36 | + |
| 37 | + const button = event.currentTarget as HTMLElement; |
| 38 | + const codeContent = button.getAttribute('data-code'); |
| 39 | + |
| 40 | + if (!codeContent) { |
| 41 | + console.error('No code content found to copy'); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Copy to clipboard using both modern and fallback methods |
| 46 | + try { |
| 47 | + // Modern method |
| 48 | + navigator.clipboard |
| 49 | + .writeText(codeContent) |
| 50 | + .then(() => showSuccessMessage(button)) |
| 51 | + .catch((err) => { |
| 52 | + console.error('Clipboard API failed:', err); |
| 53 | + fallbackCopy(codeContent, button); |
| 54 | + }); |
| 55 | + } catch (err) { |
| 56 | + console.error('Clipboard API not supported:', err); |
| 57 | + fallbackCopy(codeContent, button); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Fallback copy method using textarea |
| 63 | + */ |
| 64 | +function fallbackCopy(text: string, button: HTMLElement): void { |
| 65 | + // Create a temporary textarea element |
| 66 | + const textarea = document.createElement('textarea'); |
| 67 | + textarea.value = text; |
| 68 | + |
| 69 | + // Make the textarea out of viewport |
| 70 | + textarea.style.position = 'fixed'; |
| 71 | + textarea.style.left = '-999999px'; |
| 72 | + textarea.style.top = '-999999px'; |
| 73 | + |
| 74 | + document.body.appendChild(textarea); |
| 75 | + textarea.focus(); |
| 76 | + textarea.select(); |
| 77 | + |
| 78 | + let success = false; |
| 79 | + try { |
| 80 | + // Execute the copy command |
| 81 | + success = document.execCommand('copy'); |
| 82 | + } catch (err) { |
| 83 | + console.error('Fallback copy failed:', err); |
| 84 | + } |
| 85 | + |
| 86 | + // Remove the textarea |
| 87 | + document.body.removeChild(textarea); |
| 88 | + |
| 89 | + if (success) { |
| 90 | + showSuccessMessage(button); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Show success message |
| 96 | + */ |
| 97 | +function showSuccessMessage(button: HTMLElement): void { |
| 98 | + // Show success message |
| 99 | + const successMessage = button.nextElementSibling; |
| 100 | + if ( |
| 101 | + successMessage && |
| 102 | + successMessage.classList.contains('copy-success-message') |
| 103 | + ) { |
| 104 | + // Show success message |
| 105 | + successMessage.classList.remove('hidden'); |
| 106 | + |
| 107 | + // Hide after 2 seconds |
| 108 | + setTimeout(() => { |
| 109 | + successMessage.classList.add('hidden'); |
| 110 | + }, 2000); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Add event listener to initialize copy functionality when DOM is loaded |
| 116 | + * and also when DOM content changes (for dynamic content) |
| 117 | + */ |
| 118 | +if (typeof window !== 'undefined') { |
| 119 | + // Initialize on page load |
| 120 | + if (document.readyState === 'loading') { |
| 121 | + document.addEventListener('DOMContentLoaded', initCodeCopy); |
| 122 | + } else { |
| 123 | + initCodeCopy(); |
| 124 | + } |
| 125 | + |
| 126 | + // Re-initialize periodically to catch dynamically added code blocks |
| 127 | + setInterval(initCodeCopy, 2000); |
| 128 | + |
| 129 | + // Also initialize when user interacts with the page |
| 130 | + document.addEventListener('click', () => { |
| 131 | + setTimeout(initCodeCopy, 100); |
| 132 | + }); |
| 133 | +} |
0 commit comments