|
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'); |
28 | 32 |
|
29 | 33 | function loadHighlightTheme(isDark: boolean) { |
30 | 34 | if (!browser) return; |
|
117 | 121 |
|
118 | 122 | const rawCode = codeElement.textContent || ''; |
119 | 123 | const codeId = `code-${Date.now()}-${index}`; |
120 | | -
|
121 | 124 | codeElement.setAttribute('data-code-id', codeId); |
122 | 125 | codeElement.setAttribute('data-raw-code', rawCode); |
123 | 126 |
|
|
138 | 141 | copyButton.setAttribute('type', 'button'); |
139 | 142 |
|
140 | 143 | 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 | | - `; |
| 144 | + <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> |
| 145 | + `; |
| 146 | +
|
| 147 | + const actions = document.createElement('div'); |
| 148 | + actions.className = 'code-block-actions'; |
| 149 | +
|
| 150 | + actions.appendChild(copyButton); |
| 151 | +
|
| 152 | + if (language.toLowerCase() === 'html') { |
| 153 | + const previewButton = document.createElement('button'); |
| 154 | + previewButton.className = 'preview-code-btn'; |
| 155 | + previewButton.setAttribute('data-code-id', codeId); |
| 156 | + previewButton.setAttribute('title', 'Preview code'); |
| 157 | + previewButton.setAttribute('type', 'button'); |
| 158 | +
|
| 159 | + previewButton.innerHTML = ` |
| 160 | + <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> |
| 161 | + `; |
| 162 | +
|
| 163 | + actions.appendChild(previewButton); |
| 164 | + } |
143 | 165 |
|
144 | 166 | header.appendChild(languageLabel); |
145 | | - header.appendChild(copyButton); |
| 167 | + header.appendChild(actions); |
146 | 168 | wrapper.appendChild(header); |
147 | 169 |
|
148 | 170 | const clonedPre = pre.cloneNode(true) as HTMLElement; |
|
180 | 202 | } |
181 | 203 | } |
182 | 204 |
|
183 | | - function setupCopyButtons() { |
184 | | - if (!containerRef) return; |
| 205 | + function getCodeInfoFromTarget(target: HTMLElement) { |
| 206 | + const wrapper = target.closest('.code-block-wrapper'); |
185 | 207 |
|
186 | | - const copyButtons = containerRef.querySelectorAll('.copy-code-btn'); |
| 208 | + if (!wrapper) { |
| 209 | + console.error('No wrapper found'); |
| 210 | + return null; |
| 211 | + } |
187 | 212 |
|
188 | | - for (const button of copyButtons) { |
189 | | - button.addEventListener('click', async (e) => { |
190 | | - e.preventDefault(); |
191 | | - e.stopPropagation(); |
| 213 | + const codeElement = wrapper.querySelector<HTMLElement>('code[data-code-id]'); |
192 | 214 |
|
193 | | - const target = e.currentTarget as HTMLButtonElement; |
194 | | - const codeId = target.getAttribute('data-code-id'); |
| 215 | + if (!codeElement) { |
| 216 | + console.error('No code element found in wrapper'); |
| 217 | + return null; |
| 218 | + } |
195 | 219 |
|
196 | | - if (!codeId) { |
197 | | - console.error('No code ID found on button'); |
198 | | - return; |
199 | | - } |
| 220 | + const rawCode = codeElement.getAttribute('data-raw-code'); |
200 | 221 |
|
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 | | - } |
| 222 | + if (rawCode === null) { |
| 223 | + console.error('No raw code found'); |
| 224 | + return null; |
| 225 | + } |
207 | 226 |
|
208 | | - const codeElement = wrapper.querySelector('code[data-code-id]'); |
209 | | - if (!codeElement) { |
210 | | - console.error('No code element found in wrapper'); |
211 | | - return; |
212 | | - } |
| 227 | + const languageLabel = wrapper.querySelector<HTMLElement>('.code-language'); |
| 228 | + const language = languageLabel?.textContent?.trim() || 'text'; |
213 | 229 |
|
214 | | - const rawCode = codeElement.getAttribute('data-raw-code'); |
215 | | - if (!rawCode) { |
216 | | - console.error('No raw code found'); |
217 | | - return; |
218 | | - } |
| 230 | + return { rawCode, language }; |
| 231 | + } |
219 | 232 |
|
220 | | - try { |
221 | | - await copyCodeToClipboard(rawCode); |
222 | | - } catch (error) { |
223 | | - console.error('Failed to copy code:', error); |
224 | | - } |
225 | | - }); |
| 233 | + async function handleCopyClick(event: Event) { |
| 234 | + event.preventDefault(); |
| 235 | + event.stopPropagation(); |
| 236 | +
|
| 237 | + const target = event.currentTarget as HTMLButtonElement | null; |
| 238 | +
|
| 239 | + if (!target) { |
| 240 | + return; |
| 241 | + } |
| 242 | +
|
| 243 | + const info = getCodeInfoFromTarget(target); |
| 244 | +
|
| 245 | + if (!info) { |
| 246 | + return; |
| 247 | + } |
| 248 | +
|
| 249 | + try { |
| 250 | + await copyCodeToClipboard(info.rawCode); |
| 251 | + } catch (error) { |
| 252 | + console.error('Failed to copy code:', error); |
| 253 | + } |
| 254 | + } |
| 255 | +
|
| 256 | + function handlePreviewClick(event: Event) { |
| 257 | + event.preventDefault(); |
| 258 | + event.stopPropagation(); |
| 259 | +
|
| 260 | + const target = event.currentTarget as HTMLButtonElement | null; |
| 261 | +
|
| 262 | + if (!target) { |
| 263 | + return; |
| 264 | + } |
| 265 | +
|
| 266 | + const info = getCodeInfoFromTarget(target); |
| 267 | +
|
| 268 | + if (!info) { |
| 269 | + return; |
| 270 | + } |
| 271 | +
|
| 272 | + previewCode = info.rawCode; |
| 273 | + previewLanguage = info.language; |
| 274 | + previewDialogOpen = true; |
| 275 | + } |
| 276 | +
|
| 277 | + function setupCodeBlockActions() { |
| 278 | + if (!containerRef) return; |
| 279 | +
|
| 280 | + const wrappers = containerRef.querySelectorAll<HTMLElement>('.code-block-wrapper'); |
| 281 | +
|
| 282 | + for (const wrapper of wrappers) { |
| 283 | + const copyButton = wrapper.querySelector<HTMLButtonElement>('.copy-code-btn'); |
| 284 | + const previewButton = wrapper.querySelector<HTMLButtonElement>('.preview-code-btn'); |
| 285 | +
|
| 286 | + if (copyButton && copyButton.dataset.listenerBound !== 'true') { |
| 287 | + copyButton.dataset.listenerBound = 'true'; |
| 288 | + copyButton.addEventListener('click', handleCopyClick); |
| 289 | + } |
| 290 | +
|
| 291 | + if (previewButton && previewButton.dataset.listenerBound !== 'true') { |
| 292 | + previewButton.dataset.listenerBound = 'true'; |
| 293 | + previewButton.addEventListener('click', handlePreviewClick); |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | +
|
| 298 | + function handlePreviewDialogOpenChange(open: boolean) { |
| 299 | + previewDialogOpen = open; |
| 300 | +
|
| 301 | + if (!open) { |
| 302 | + previewCode = ''; |
| 303 | + previewLanguage = 'text'; |
226 | 304 | } |
227 | 305 | } |
228 | 306 |
|
|
243 | 321 |
|
244 | 322 | $effect(() => { |
245 | 323 | if (containerRef && processedHtml) { |
246 | | - setupCopyButtons(); |
| 324 | + setupCodeBlockActions(); |
247 | 325 | } |
248 | 326 | }); |
249 | 327 | </script> |
|
253 | 331 | {@html processedHtml} |
254 | 332 | </div> |
255 | 333 |
|
| 334 | +<CodePreviewDialog |
| 335 | + open={previewDialogOpen} |
| 336 | + code={previewCode} |
| 337 | + language={previewLanguage} |
| 338 | + onOpenChange={handlePreviewDialogOpenChange} |
| 339 | +/> |
| 340 | + |
256 | 341 | <style> |
257 | 342 | /* Base typography styles */ |
258 | 343 | div :global(p:not(:last-child)) { |
|
472 | 557 | letter-spacing: 0.05em; |
473 | 558 | } |
474 | 559 |
|
475 | | - div :global(.copy-code-btn) { |
| 560 | + div :global(.code-block-actions) { |
| 561 | + display: flex; |
| 562 | + align-items: center; |
| 563 | + gap: 0.5rem; |
| 564 | + } |
| 565 | +
|
| 566 | + div :global(.copy-code-btn), |
| 567 | + div :global(.preview-code-btn) { |
476 | 568 | display: flex; |
477 | 569 | align-items: center; |
478 | 570 | justify-content: center; |
|
483 | 575 | transition: all 0.2s ease; |
484 | 576 | } |
485 | 577 |
|
486 | | - div :global(.copy-code-btn:hover) { |
| 578 | + div :global(.copy-code-btn:hover), |
| 579 | + div :global(.preview-code-btn:hover) { |
487 | 580 | transform: scale(1.05); |
488 | 581 | } |
489 | 582 |
|
490 | | - div :global(.copy-code-btn:active) { |
| 583 | + div :global(.copy-code-btn:active), |
| 584 | + div :global(.preview-code-btn:active) { |
491 | 585 | transform: scale(0.95); |
492 | 586 | } |
493 | 587 |
|
|
0 commit comments