|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect } from 'react'; |
| 4 | + |
| 5 | +function addCopyButton(block) { |
| 6 | + // Check if button already exists |
| 7 | + if (block.querySelector('[data-copy-button]')) { |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + const codeElement = block.querySelector('code'); |
| 12 | + if (!codeElement) return; |
| 13 | + |
| 14 | + // Create button container |
| 15 | + const buttonContainer = document.createElement('div'); |
| 16 | + buttonContainer.setAttribute('data-copy-button', 'true'); |
| 17 | + buttonContainer.style.position = 'absolute'; |
| 18 | + buttonContainer.style.top = '5px'; |
| 19 | + buttonContainer.style.right = '5px'; |
| 20 | + buttonContainer.style.zIndex = '10'; |
| 21 | + |
| 22 | + // Create button |
| 23 | + const button = document.createElement('button'); |
| 24 | + button.setAttribute('aria-label', 'Copy code'); |
| 25 | + button.style.opacity = '0'; |
| 26 | + button.style.transition = 'opacity 0.2s'; |
| 27 | + button.style.width = '36px'; |
| 28 | + button.style.height = '36px'; |
| 29 | + button.style.display = 'inline-flex'; |
| 30 | + button.style.alignItems = 'center'; |
| 31 | + button.style.justifyContent = 'center'; |
| 32 | + button.style.borderRadius = '6px'; |
| 33 | + button.style.cursor = 'pointer'; |
| 34 | + |
| 35 | + // Set button styles based on dark mode |
| 36 | + const isDark = document.documentElement.classList.contains('dark'); |
| 37 | + if (isDark) { |
| 38 | + button.style.backgroundColor = 'rgba(30, 30, 30, 0.8)'; |
| 39 | + button.style.border = '1px solid rgba(255, 255, 255, 0.1)'; |
| 40 | + button.style.color = 'rgba(255, 255, 255, 0.9)'; |
| 41 | + } else { |
| 42 | + button.style.backgroundColor = 'rgba(255, 255, 255, 0.8)'; |
| 43 | + button.style.border = '1px solid rgba(0, 0, 0, 0.1)'; |
| 44 | + button.style.color = 'rgba(0, 0, 0, 0.9)'; |
| 45 | + } |
| 46 | + button.style.backdropFilter = 'blur(8px)'; |
| 47 | + |
| 48 | + // Copy icon SVG |
| 49 | + const copyIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); |
| 50 | + copyIcon.setAttribute('xmlns', 'http://www.w3.org/2000/svg'); |
| 51 | + copyIcon.setAttribute('width', '14'); |
| 52 | + copyIcon.setAttribute('height', '14'); |
| 53 | + copyIcon.setAttribute('viewBox', '0 0 24 24'); |
| 54 | + copyIcon.setAttribute('fill', 'none'); |
| 55 | + copyIcon.setAttribute('stroke', 'currentColor'); |
| 56 | + copyIcon.setAttribute('stroke-width', '2'); |
| 57 | + copyIcon.setAttribute('stroke-linecap', 'round'); |
| 58 | + copyIcon.setAttribute('stroke-linejoin', 'round'); |
| 59 | + copyIcon.style.color = 'inherit'; |
| 60 | + |
| 61 | + const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect'); |
| 62 | + rect.setAttribute('width', '14'); |
| 63 | + rect.setAttribute('height', '14'); |
| 64 | + rect.setAttribute('x', '8'); |
| 65 | + rect.setAttribute('y', '8'); |
| 66 | + rect.setAttribute('rx', '2'); |
| 67 | + rect.setAttribute('ry', '2'); |
| 68 | + |
| 69 | + const path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); |
| 70 | + path.setAttribute('d', 'M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2'); |
| 71 | + |
| 72 | + copyIcon.appendChild(rect); |
| 73 | + copyIcon.appendChild(path); |
| 74 | + button.appendChild(copyIcon); |
| 75 | + |
| 76 | + // Copy functionality |
| 77 | + let isCopied = false; |
| 78 | + button.addEventListener('click', async () => { |
| 79 | + if (isCopied) return; |
| 80 | + |
| 81 | + const codeText = codeElement.textContent || codeElement.innerText || ''; |
| 82 | + if (!codeText) return; |
| 83 | + |
| 84 | + try { |
| 85 | + await navigator.clipboard.writeText(codeText); |
| 86 | + isCopied = true; |
| 87 | + |
| 88 | + // Change icon to checkmark |
| 89 | + const checkIcon = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); |
| 90 | + checkIcon.setAttribute('xmlns', 'http://www.w3.org/2000/svg'); |
| 91 | + checkIcon.setAttribute('width', '14'); |
| 92 | + checkIcon.setAttribute('height', '14'); |
| 93 | + checkIcon.setAttribute('viewBox', '0 0 24 24'); |
| 94 | + checkIcon.setAttribute('fill', 'none'); |
| 95 | + checkIcon.setAttribute('stroke', 'currentColor'); |
| 96 | + checkIcon.setAttribute('stroke-width', '2'); |
| 97 | + checkIcon.setAttribute('stroke-linecap', 'round'); |
| 98 | + checkIcon.setAttribute('stroke-linejoin', 'round'); |
| 99 | + |
| 100 | + const polyline = document.createElementNS('http://www.w3.org/2000/svg', 'polyline'); |
| 101 | + polyline.setAttribute('points', '20 6 9 17 4 12'); |
| 102 | + checkIcon.appendChild(polyline); |
| 103 | + |
| 104 | + button.innerHTML = ''; |
| 105 | + button.appendChild(checkIcon); |
| 106 | + |
| 107 | + setTimeout(() => { |
| 108 | + isCopied = false; |
| 109 | + button.innerHTML = ''; |
| 110 | + button.appendChild(copyIcon.cloneNode(true)); |
| 111 | + }, 2000); |
| 112 | + } catch (err) { |
| 113 | + console.error('Failed to copy code:', err); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + // Show button on hover |
| 118 | + block.addEventListener('mouseenter', () => { |
| 119 | + button.style.opacity = '1'; |
| 120 | + }); |
| 121 | + block.addEventListener('mouseleave', () => { |
| 122 | + if (!isCopied) { |
| 123 | + button.style.opacity = '0'; |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + // Update button hover style |
| 128 | + button.addEventListener('mouseenter', () => { |
| 129 | + const isCurrentlyDark = document.documentElement.classList.contains('dark'); |
| 130 | + if (isCurrentlyDark) { |
| 131 | + button.style.backgroundColor = 'rgba(255, 255, 255, 0.1)'; |
| 132 | + } else { |
| 133 | + button.style.backgroundColor = 'rgba(0, 0, 0, 0.05)'; |
| 134 | + } |
| 135 | + }); |
| 136 | + button.addEventListener('mouseleave', () => { |
| 137 | + const isCurrentlyDark = document.documentElement.classList.contains('dark'); |
| 138 | + if (isCurrentlyDark) { |
| 139 | + button.style.backgroundColor = 'rgba(30, 30, 30, 0.8)'; |
| 140 | + } else { |
| 141 | + button.style.backgroundColor = 'rgba(255, 255, 255, 0.8)'; |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + buttonContainer.appendChild(button); |
| 146 | + block.style.position = 'relative'; |
| 147 | + block.appendChild(buttonContainer); |
| 148 | +} |
| 149 | + |
| 150 | +export default function CodeBlockEnhancer() { |
| 151 | + useEffect(() => { |
| 152 | + // Function to add copy buttons to all code blocks |
| 153 | + const enhanceCodeBlocks = () => { |
| 154 | + const codeBlocks = document.querySelectorAll('.nextra-code'); |
| 155 | + codeBlocks.forEach(addCopyButton); |
| 156 | + }; |
| 157 | + |
| 158 | + // Run immediately |
| 159 | + enhanceCodeBlocks(); |
| 160 | + |
| 161 | + // Also run after a short delay to catch dynamically loaded content |
| 162 | + const timeoutId = setTimeout(enhanceCodeBlocks, 100); |
| 163 | + |
| 164 | + // Use MutationObserver to watch for new code blocks |
| 165 | + const observer = new MutationObserver(() => { |
| 166 | + enhanceCodeBlocks(); |
| 167 | + }); |
| 168 | + |
| 169 | + // Observe the document body for changes |
| 170 | + observer.observe(document.body, { |
| 171 | + childList: true, |
| 172 | + subtree: true, |
| 173 | + }); |
| 174 | + |
| 175 | + return () => { |
| 176 | + clearTimeout(timeoutId); |
| 177 | + observer.disconnect(); |
| 178 | + }; |
| 179 | + }, []); |
| 180 | + |
| 181 | + return null; |
| 182 | +} |
| 183 | + |
0 commit comments