|
15 | 15 | import githubLightCss from 'highlight.js/styles/github.css?inline'; |
16 | 16 | import { mode } from 'mode-watcher'; |
17 | 17 | import { remarkLiteralHtml } from '$lib/markdown/literal-html'; |
| 18 | + import CodePreviewDialog from './CodePreviewDialog.svelte'; |
18 | 19 |
|
19 | 20 | interface Props { |
20 | 21 | content: string; |
|
25 | 26 |
|
26 | 27 | let containerRef = $state<HTMLDivElement>(); |
27 | 28 | let processedHtml = $state(''); |
| 29 | + let previewDialogOpen = $state(false); |
| 30 | + let previewCode = $state(''); |
| 31 | + let previewLanguage = $state('text'); |
| 32 | +
|
| 33 | + const previewableLanguages = new Set(['html', 'htm', 'javascript', 'js', 'svelte']); |
28 | 34 |
|
29 | 35 | function loadHighlightTheme(isDark: boolean) { |
30 | 36 | if (!browser) return; |
|
117 | 123 |
|
118 | 124 | const rawCode = codeElement.textContent || ''; |
119 | 125 | const codeId = `code-${Date.now()}-${index}`; |
| 126 | + const normalizedLanguage = language.toLowerCase(); |
120 | 127 |
|
121 | 128 | codeElement.setAttribute('data-code-id', codeId); |
122 | 129 | codeElement.setAttribute('data-raw-code', rawCode); |
| 130 | + codeElement.setAttribute('data-language', normalizedLanguage); |
123 | 131 |
|
124 | 132 | const wrapper = document.createElement('div'); |
125 | 133 | wrapper.className = 'code-block-wrapper'; |
|
138 | 146 | copyButton.setAttribute('type', 'button'); |
139 | 147 |
|
140 | 148 | copyButton.innerHTML = ` |
141 | | - <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy-icon lucide-copy"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg> |
142 | | - `; |
| 149 | + <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-copy-icon lucide-copy"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg> |
| 150 | + `; |
| 151 | +
|
| 152 | + const actions = document.createElement('div'); |
| 153 | + actions.className = 'code-block-actions'; |
| 154 | +
|
| 155 | + actions.appendChild(copyButton); |
| 156 | +
|
| 157 | + if (previewableLanguages.has(normalizedLanguage)) { |
| 158 | + const previewButton = document.createElement('button'); |
| 159 | + previewButton.className = 'preview-code-btn'; |
| 160 | + previewButton.setAttribute('data-code-id', codeId); |
| 161 | + previewButton.setAttribute('title', 'Preview code'); |
| 162 | + previewButton.setAttribute('type', 'button'); |
| 163 | +
|
| 164 | + previewButton.innerHTML = ` |
| 165 | + <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-eye lucide-eye-icon"><path d="M2.062 12.345a1 1 0 0 1 0-.69C3.5 7.73 7.36 5 12 5s8.5 2.73 9.938 6.655a1 1 0 0 1 0 .69C20.5 16.27 16.64 19 12 19s-8.5-2.73-9.938-6.655"/><circle cx="12" cy="12" r="3"/></svg> |
| 166 | + `; |
| 167 | +
|
| 168 | + actions.appendChild(previewButton); |
| 169 | + } |
143 | 170 |
|
144 | 171 | header.appendChild(languageLabel); |
145 | | - header.appendChild(copyButton); |
| 172 | + header.appendChild(actions); |
146 | 173 | wrapper.appendChild(header); |
147 | 174 |
|
148 | 175 | const clonedPre = pre.cloneNode(true) as HTMLElement; |
|
180 | 207 | } |
181 | 208 | } |
182 | 209 |
|
183 | | - function setupCopyButtons() { |
184 | | - if (!containerRef) return; |
| 210 | + function getCodeInfoFromTarget(target: HTMLElement) { |
| 211 | + const wrapper = target.closest('.code-block-wrapper'); |
185 | 212 |
|
186 | | - const copyButtons = containerRef.querySelectorAll('.copy-code-btn'); |
| 213 | + if (!wrapper) { |
| 214 | + console.error('No wrapper found'); |
| 215 | + return null; |
| 216 | + } |
187 | 217 |
|
188 | | - for (const button of copyButtons) { |
189 | | - button.addEventListener('click', async (e) => { |
190 | | - e.preventDefault(); |
191 | | - e.stopPropagation(); |
| 218 | + const codeElement = wrapper.querySelector<HTMLElement>('code[data-code-id]'); |
192 | 219 |
|
193 | | - const target = e.currentTarget as HTMLButtonElement; |
194 | | - const codeId = target.getAttribute('data-code-id'); |
| 220 | + if (!codeElement) { |
| 221 | + console.error('No code element found in wrapper'); |
| 222 | + return null; |
| 223 | + } |
195 | 224 |
|
196 | | - if (!codeId) { |
197 | | - console.error('No code ID found on button'); |
198 | | - return; |
199 | | - } |
| 225 | + const rawCode = codeElement.getAttribute('data-raw-code'); |
200 | 226 |
|
201 | | - // Find the code element within the same wrapper |
202 | | - const wrapper = target.closest('.code-block-wrapper'); |
203 | | - if (!wrapper) { |
204 | | - console.error('No wrapper found'); |
205 | | - return; |
206 | | - } |
| 227 | + if (rawCode === null) { |
| 228 | + console.error('No raw code found'); |
| 229 | + return null; |
| 230 | + } |
207 | 231 |
|
208 | | - const codeElement = wrapper.querySelector('code[data-code-id]'); |
209 | | - if (!codeElement) { |
210 | | - console.error('No code element found in wrapper'); |
211 | | - return; |
212 | | - } |
| 232 | + const language = codeElement.getAttribute('data-language') || 'text'; |
213 | 233 |
|
214 | | - const rawCode = codeElement.getAttribute('data-raw-code'); |
215 | | - if (!rawCode) { |
216 | | - console.error('No raw code found'); |
217 | | - return; |
218 | | - } |
| 234 | + return { rawCode, language }; |
| 235 | + } |
219 | 236 |
|
220 | | - try { |
221 | | - await copyCodeToClipboard(rawCode); |
222 | | - } catch (error) { |
223 | | - console.error('Failed to copy code:', error); |
224 | | - } |
225 | | - }); |
| 237 | + async function handleCopyClick(event: Event) { |
| 238 | + event.preventDefault(); |
| 239 | + event.stopPropagation(); |
| 240 | +
|
| 241 | + const target = event.currentTarget as HTMLButtonElement | null; |
| 242 | +
|
| 243 | + if (!target) { |
| 244 | + return; |
| 245 | + } |
| 246 | +
|
| 247 | + const info = getCodeInfoFromTarget(target); |
| 248 | +
|
| 249 | + if (!info) { |
| 250 | + return; |
| 251 | + } |
| 252 | +
|
| 253 | + try { |
| 254 | + await copyCodeToClipboard(info.rawCode); |
| 255 | + } catch (error) { |
| 256 | + console.error('Failed to copy code:', error); |
| 257 | + } |
| 258 | + } |
| 259 | +
|
| 260 | + function handlePreviewClick(event: Event) { |
| 261 | + event.preventDefault(); |
| 262 | + event.stopPropagation(); |
| 263 | +
|
| 264 | + const target = event.currentTarget as HTMLButtonElement | null; |
| 265 | +
|
| 266 | + if (!target) { |
| 267 | + return; |
| 268 | + } |
| 269 | +
|
| 270 | + const info = getCodeInfoFromTarget(target); |
| 271 | +
|
| 272 | + if (!info) { |
| 273 | + return; |
| 274 | + } |
| 275 | +
|
| 276 | + previewCode = info.rawCode; |
| 277 | + previewLanguage = info.language; |
| 278 | + previewDialogOpen = true; |
| 279 | + } |
| 280 | +
|
| 281 | + function setupCodeBlockActions() { |
| 282 | + if (!containerRef) return; |
| 283 | +
|
| 284 | + const wrappers = containerRef.querySelectorAll<HTMLElement>('.code-block-wrapper'); |
| 285 | +
|
| 286 | + for (const wrapper of wrappers) { |
| 287 | + const copyButton = wrapper.querySelector<HTMLButtonElement>('.copy-code-btn'); |
| 288 | + const previewButton = wrapper.querySelector<HTMLButtonElement>('.preview-code-btn'); |
| 289 | +
|
| 290 | + if (copyButton && copyButton.dataset.listenerBound !== 'true') { |
| 291 | + copyButton.dataset.listenerBound = 'true'; |
| 292 | + copyButton.addEventListener('click', handleCopyClick); |
| 293 | + } |
| 294 | +
|
| 295 | + if (previewButton && previewButton.dataset.listenerBound !== 'true') { |
| 296 | + previewButton.dataset.listenerBound = 'true'; |
| 297 | + previewButton.addEventListener('click', handlePreviewClick); |
| 298 | + } |
| 299 | + } |
| 300 | + } |
| 301 | +
|
| 302 | + function handlePreviewDialogOpenChange(open: boolean) { |
| 303 | + previewDialogOpen = open; |
| 304 | +
|
| 305 | + if (!open) { |
| 306 | + previewCode = ''; |
| 307 | + previewLanguage = 'text'; |
226 | 308 | } |
227 | 309 | } |
228 | 310 |
|
|
243 | 325 |
|
244 | 326 | $effect(() => { |
245 | 327 | if (containerRef && processedHtml) { |
246 | | - setupCopyButtons(); |
| 328 | + setupCodeBlockActions(); |
247 | 329 | } |
248 | 330 | }); |
249 | 331 | </script> |
|
253 | 335 | {@html processedHtml} |
254 | 336 | </div> |
255 | 337 |
|
| 338 | +<CodePreviewDialog |
| 339 | + open={previewDialogOpen} |
| 340 | + code={previewCode} |
| 341 | + language={previewLanguage} |
| 342 | + onOpenChange={handlePreviewDialogOpenChange} |
| 343 | +/> |
| 344 | + |
256 | 345 | <style> |
257 | 346 | /* Base typography styles */ |
258 | 347 | div :global(p:not(:last-child)) { |
|
472 | 561 | letter-spacing: 0.05em; |
473 | 562 | } |
474 | 563 |
|
475 | | - div :global(.copy-code-btn) { |
| 564 | + div :global(.code-block-actions) { |
| 565 | + display: flex; |
| 566 | + align-items: center; |
| 567 | + gap: 0.5rem; |
| 568 | + } |
| 569 | +
|
| 570 | + div :global(.copy-code-btn), |
| 571 | + div :global(.preview-code-btn) { |
476 | 572 | display: flex; |
477 | 573 | align-items: center; |
478 | 574 | justify-content: center; |
|
483 | 579 | transition: all 0.2s ease; |
484 | 580 | } |
485 | 581 |
|
486 | | - div :global(.copy-code-btn:hover) { |
| 582 | + div :global(.copy-code-btn:hover), |
| 583 | + div :global(.preview-code-btn:hover) { |
487 | 584 | transform: scale(1.05); |
488 | 585 | } |
489 | 586 |
|
490 | | - div :global(.copy-code-btn:active) { |
| 587 | + div :global(.copy-code-btn:active), |
| 588 | + div :global(.preview-code-btn:active) { |
491 | 589 | transform: scale(0.95); |
492 | 590 | } |
493 | 591 |
|
|
0 commit comments