|
1 | 1 | <script lang="ts"> |
2 | 2 | import * as Dialog from '$lib/components/ui/dialog'; |
3 | | - import { FileText, Image, Music, FileIcon } from '@lucide/svelte'; |
| 3 | + import { FileText, Image, Music, FileIcon, Eye } from '@lucide/svelte'; |
| 4 | + import { convertPDFToImage } from '$lib/utils/pdf-processing'; |
| 5 | + import { Button } from '$lib/components/ui/button'; |
4 | 6 |
|
5 | 7 | interface Props { |
6 | 8 | open: boolean; |
|
62 | 64 | let isPdf = $derived(displayType === 'application/pdf'); |
63 | 65 | let isAudio = $derived(displayType.startsWith('audio/') || displayType === 'audio'); |
64 | 66 |
|
| 67 | + // PDF preview state |
| 68 | + let pdfViewMode = $state<'text' | 'pages'>('pages'); // Default to pages view |
| 69 | + let pdfImages = $state<string[]>([]); |
| 70 | + let pdfImagesLoading = $state(false); |
| 71 | + let pdfImagesError = $state<string | null>(null); |
| 72 | +
|
65 | 73 | let IconComponent = $derived(() => { |
66 | 74 | if (isImage) return Image; |
67 | 75 | if (isText || isPdf) return FileText; |
|
78 | 86 |
|
79 | 87 | return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; |
80 | 88 | } |
| 89 | +
|
| 90 | + async function loadPdfImages() { |
| 91 | + if (!isPdf || pdfImages.length > 0 || pdfImagesLoading) return; |
| 92 | + |
| 93 | + if (import.meta.env.DEV) console.log('Loading PDF images...', { isPdf, attachment, uploadedFile }); |
| 94 | + |
| 95 | + pdfImagesLoading = true; |
| 96 | + pdfImagesError = null; |
| 97 | + |
| 98 | + try { |
| 99 | + let file: File | null = null; |
| 100 | + |
| 101 | + if (uploadedFile?.file) { |
| 102 | + if (import.meta.env.DEV) console.log('Using uploaded file:', uploadedFile.file); |
| 103 | + file = uploadedFile.file; |
| 104 | + } else if (attachment?.type === 'pdfFile') { |
| 105 | + if (import.meta.env.DEV) console.log('Processing stored PDF attachment:', attachment); |
| 106 | + |
| 107 | + // Check if we have pre-processed images |
| 108 | + if ((attachment as any).images && Array.isArray((attachment as any).images)) { |
| 109 | + if (import.meta.env.DEV) console.log('Using pre-processed PDF images:', (attachment as any).images.length); |
| 110 | + pdfImages = (attachment as any).images; |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + // Convert base64 back to File for processing |
| 115 | + if ((attachment as any).base64Data) { |
| 116 | + const base64Data = (attachment as any).base64Data; |
| 117 | + const byteCharacters = atob(base64Data); |
| 118 | + const byteNumbers = new Array(byteCharacters.length); |
| 119 | + for (let i = 0; i < byteCharacters.length; i++) { |
| 120 | + byteNumbers[i] = byteCharacters.charCodeAt(i); |
| 121 | + } |
| 122 | + const byteArray = new Uint8Array(byteNumbers); |
| 123 | + file = new File([byteArray], displayName, { type: 'application/pdf' }); |
| 124 | + if (import.meta.env.DEV) console.log('Created file from base64 data, size:', file.size); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if (file) { |
| 129 | + if (import.meta.env.DEV) console.log('Converting PDF to images...'); |
| 130 | + const images = await convertPDFToImage(file); |
| 131 | + if (import.meta.env.DEV) console.log('PDF conversion successful, got', images.length, 'images'); |
| 132 | + pdfImages = images; |
| 133 | + } else { |
| 134 | + throw new Error('No PDF file available for conversion'); |
| 135 | + } |
| 136 | + } catch (error) { |
| 137 | + if (import.meta.env.DEV) console.error('Failed to convert PDF to images:', error); |
| 138 | + pdfImagesError = error instanceof Error ? error.message : 'Failed to load PDF images'; |
| 139 | + // Don't automatically fallback to text view, let user choose |
| 140 | + } finally { |
| 141 | + pdfImagesLoading = false; |
| 142 | + } |
| 143 | + } |
| 144 | +
|
| 145 | + // Load PDF images when dialog opens and it's a PDF |
| 146 | + $effect(() => { |
| 147 | + if (open && isPdf && pdfViewMode === 'pages') { |
| 148 | + loadPdfImages(); |
| 149 | + } |
| 150 | + }); |
81 | 151 | </script> |
82 | 152 |
|
83 | 153 | <Dialog.Root bind:open> |
84 | | - <Dialog.Content class="grid max-h-[90vh] max-w-4xl overflow-hidden sm:w-auto sm:max-w-6xl"> |
| 154 | + <Dialog.Content class="grid max-h-[90vh] !p-10 max-w-5xl overflow-hidden sm:w-auto sm:max-w-6xl"> |
85 | 155 | <Dialog.Header class="flex-shrink-0"> |
86 | | - <div class="flex items-center space-x-4"> |
| 156 | + <div class="flex items-center justify-between"> |
87 | 157 | <div class="flex items-center gap-3"> |
88 | 158 | {#if IconComponent} |
89 | 159 | <IconComponent class="text-muted-foreground h-5 w-5" /> |
|
101 | 171 | </div> |
102 | 172 | </div> |
103 | 173 | </div> |
| 174 | + |
| 175 | + {#if isPdf} |
| 176 | + <div class="flex items-center gap-2"> |
| 177 | + <Button |
| 178 | + variant={pdfViewMode === 'text' ? 'default' : 'outline'} |
| 179 | + size="sm" |
| 180 | + onclick={() => pdfViewMode = 'text'} |
| 181 | + disabled={pdfImagesLoading} |
| 182 | + > |
| 183 | + <FileText class="h-4 w-4 mr-1" /> |
| 184 | + Text |
| 185 | + </Button> |
| 186 | + <Button |
| 187 | + variant={pdfViewMode === 'pages' ? 'default' : 'outline'} |
| 188 | + size="sm" |
| 189 | + onclick={() => { pdfViewMode = 'pages'; loadPdfImages(); }} |
| 190 | + disabled={pdfImagesLoading} |
| 191 | + > |
| 192 | + {#if pdfImagesLoading} |
| 193 | + <div class="h-4 w-4 mr-1 animate-spin rounded-full border-2 border-current border-t-transparent"></div> |
| 194 | + {:else} |
| 195 | + <Eye class="h-4 w-4 mr-1" /> |
| 196 | + {/if} |
| 197 | + Pages |
| 198 | + </Button> |
| 199 | + </div> |
| 200 | + {/if} |
104 | 201 | </div> |
105 | 202 | </Dialog.Header> |
106 | 203 |
|
107 | 204 | <div class="flex-1 overflow-auto"> |
108 | 205 | {#if isImage && displayPreview} |
109 | | - <div class="flex items-center justify-center p-4"> |
| 206 | + <div class="flex items-center justify-center"> |
110 | 207 | <img |
111 | 208 | src={displayPreview} |
112 | 209 | alt={displayName} |
113 | 210 | class="max-h-full rounded-lg object-contain shadow-lg" |
114 | 211 | /> |
115 | 212 | </div> |
116 | | - {:else if (isText || isPdf) && displayTextContent} |
117 | | - <div class="p-4"> |
118 | | - <div |
119 | | - class="bg-muted max-h-[60vh] overflow-auto whitespace-pre-wrap break-words rounded-lg p-4 font-mono text-sm" |
120 | | - > |
121 | | - {displayTextContent} |
| 213 | + {:else if isPdf && pdfViewMode === 'pages'} |
| 214 | + {#if pdfImagesLoading} |
| 215 | + <div class="flex items-center justify-center p-8"> |
| 216 | + <div class="text-center"> |
| 217 | + <div class="h-8 w-8 mx-auto mb-4 animate-spin rounded-full border-4 border-primary border-t-transparent"></div> |
| 218 | + <p class="text-muted-foreground">Converting PDF to images...</p> |
| 219 | + </div> |
122 | 220 | </div> |
| 221 | + {:else if pdfImagesError} |
| 222 | + <div class="flex items-center justify-center p-8"> |
| 223 | + <div class="text-center"> |
| 224 | + <FileText class="text-muted-foreground mx-auto mb-4 h-16 w-16" /> |
| 225 | + <p class="text-muted-foreground mb-4">Failed to load PDF images</p> |
| 226 | + <p class="text-muted-foreground text-sm">{pdfImagesError}</p> |
| 227 | + <Button class="mt-4" onclick={() => { pdfViewMode = 'text'; }}>View as Text</Button> |
| 228 | + </div> |
| 229 | + </div> |
| 230 | + {:else if pdfImages.length > 0} |
| 231 | + <div class="max-h-[70vh] overflow-auto space-y-4"> |
| 232 | + {#each pdfImages as image, index} |
| 233 | + <div class="text-center"> |
| 234 | + <p class="text-muted-foreground mb-2 text-sm">Page {index + 1}</p> |
| 235 | + <img |
| 236 | + src={image} |
| 237 | + alt="PDF Page {index + 1}" |
| 238 | + class="mx-auto max-w-full rounded-lg shadow-lg" |
| 239 | + /> |
| 240 | + </div> |
| 241 | + {/each} |
| 242 | + </div> |
| 243 | + {:else} |
| 244 | + <div class="flex items-center justify-center p-8"> |
| 245 | + <div class="text-center"> |
| 246 | + <FileText class="text-muted-foreground mx-auto mb-4 h-16 w-16" /> |
| 247 | + <p class="text-muted-foreground mb-4">No PDF pages available</p> |
| 248 | + </div> |
| 249 | + </div> |
| 250 | + {/if} |
| 251 | + {:else if (isText || (isPdf && pdfViewMode === 'text')) && displayTextContent} |
| 252 | + <div |
| 253 | + class="bg-muted max-h-[60vh] overflow-auto whitespace-pre-wrap break-words rounded-lg p-4 font-mono text-sm" |
| 254 | + > |
| 255 | + {displayTextContent} |
123 | 256 | </div> |
124 | | - {:else if isAudio && attachment?.type === 'audioFile'} |
| 257 | + {:else if isAudio} |
125 | 258 | <div class="flex items-center justify-center p-8"> |
126 | | - <div class="text-center"> |
| 259 | + <div class="w-full max-w-md text-center"> |
127 | 260 | <Music class="text-muted-foreground mx-auto mb-4 h-16 w-16" /> |
128 | | - |
129 | | - <p class="text-muted-foreground mb-4">Audio file preview not available</p> |
| 261 | + |
| 262 | + {#if attachment?.type === 'audioFile'} |
| 263 | + <audio |
| 264 | + controls |
| 265 | + class="w-full mb-4" |
| 266 | + src="data:{(attachment as any).mimeType};base64,{(attachment as any).base64Data}" |
| 267 | + > |
| 268 | + Your browser does not support the audio element. |
| 269 | + </audio> |
| 270 | + {:else if uploadedFile?.preview} |
| 271 | + <audio |
| 272 | + controls |
| 273 | + class="w-full mb-4" |
| 274 | + src={uploadedFile.preview} |
| 275 | + > |
| 276 | + Your browser does not support the audio element. |
| 277 | + </audio> |
| 278 | + {:else} |
| 279 | + <p class="text-muted-foreground mb-4">Audio preview not available</p> |
| 280 | + {/if} |
| 281 | + |
| 282 | + <p class="text-muted-foreground text-sm"> |
| 283 | + {displayName} |
| 284 | + </p> |
130 | 285 | </div> |
131 | 286 | </div> |
132 | 287 | {:else} |
|
0 commit comments