1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
<script lang="ts">
import { flashair } from '../flashair';
import type { FlashAirFileEntry } from '../flashair';
import { imageCache } from '../cache';
import { autoCacheService } from '../cache';
interface Props {
file: FlashAirFileEntry | undefined;
}
let { file }: Props = $props();
let thumbnailBlobUrl = $state<string | undefined>(undefined);
let imageAspectRatio = $state<string>('3 / 2');
let fullObjectUrl = $state<string | undefined>(undefined);
let progress = $state(0);
let downloading = $state(false);
let loadError = $state<string | undefined>(undefined);
let currentAbort: AbortController | undefined;
/**
* Plain (non-reactive) mirror of fullObjectUrl so we can revoke it
* without reading the $state variable inside $effect (which would
* add it as a tracked dependency and cause an infinite loop).
*/
let rawObjectUrl: string | undefined;
let rawThumbnailUrl: string | undefined;
// --- Zoom & pan state ---
let zoomLevel = $state(1); // user zoom: 1 = fit, >1 = zoomed in
let panX = $state(0);
let panY = $state(0);
let containerEl: HTMLDivElement | undefined;
let containerW = $state(0);
let containerH = $state(0);
// Native image dimensions (set once the full-res img element loads)
let imgNaturalW = $state(0);
let imgNaturalH = $state(0);
function handleImageLoad(e: Event) {
const img = e.currentTarget as HTMLImageElement;
imgNaturalW = img.naturalWidth;
imgNaturalH = img.naturalHeight;
}
/**
* The scale factor that makes the native-size image "fit" inside the
* container (same logic as object-contain). When imgNatural* are not
* yet known we fall back to 1 so nothing explodes.
*/
let baseScale = $derived(
imgNaturalW > 0 && imgNaturalH > 0 && containerW > 0 && containerH > 0
? Math.min(containerW / imgNaturalW, containerH / imgNaturalH)
: 1
);
// Touch tracking for pinch-to-zoom and pan
let lastTouchDist = 0;
let lastTouchMidX = 0;
let lastTouchMidY = 0;
let isPinching = false;
let isPanning = false;
let lastPanX = 0;
let lastPanY = 0;
const MIN_ZOOM = 1;
// Allow zooming up to 3× beyond native 1:1 pixel density.
// The effective CSS scale = baseScale * zoomLevel; zoomLevel=1 always means
// "fit". MAX_ZOOM is recomputed per-image in clampZoom().
const ZOOM_PAST_NATIVE = 3;
function resetZoom() {
zoomLevel = 1;
panX = 0;
panY = 0;
}
function clampPan() {
if (zoomLevel <= 1) {
panX = 0;
panY = 0;
return;
}
if (containerW === 0 || containerH === 0) return;
// The rendered image size at the current zoom level.
const renderedW = imgNaturalW > 0 ? imgNaturalW * baseScale * zoomLevel : containerW * zoomLevel;
const renderedH = imgNaturalH > 0 ? imgNaturalH * baseScale * zoomLevel : containerH * zoomLevel;
const maxPanX = Math.max(0, (renderedW - containerW) / 2);
const maxPanY = Math.max(0, (renderedH - containerH) / 2);
panX = Math.max(-maxPanX, Math.min(maxPanX, panX));
panY = Math.max(-maxPanY, Math.min(maxPanY, panY));
}
function handleWheel(e: WheelEvent) {
e.preventDefault();
const maxZoom = baseScale > 0 ? ZOOM_PAST_NATIVE / baseScale : 10;
const delta = e.deltaY > 0 ? 0.9 : 1.1;
const newZoom = Math.max(MIN_ZOOM, Math.min(maxZoom, zoomLevel * delta));
if (containerEl !== undefined) {
const rect = containerEl.getBoundingClientRect();
const cursorX = e.clientX - rect.left - rect.width / 2;
const cursorY = e.clientY - rect.top - rect.height / 2;
const factor = newZoom / zoomLevel;
panX = cursorX - factor * (cursorX - panX);
panY = cursorY - factor * (cursorY - panY);
}
zoomLevel = newZoom;
clampPan();
}
function touchDist(t1: Touch, t2: Touch): number {
const dx = t1.clientX - t2.clientX;
const dy = t1.clientY - t2.clientY;
return Math.sqrt(dx * dx + dy * dy);
}
function handleTouchStart(e: TouchEvent) {
if (e.touches.length === 2) {
e.preventDefault();
isPinching = true;
isPanning = false;
const t0 = e.touches[0] as Touch;
const t1 = e.touches[1] as Touch;
lastTouchDist = touchDist(t0, t1);
lastTouchMidX = (t0.clientX + t1.clientX) / 2;
lastTouchMidY = (t0.clientY + t1.clientY) / 2;
} else if (e.touches.length === 1 && zoomLevel > 1) {
isPanning = true;
isPinching = false;
const t = e.touches[0] as Touch;
lastPanX = t.clientX;
lastPanY = t.clientY;
}
}
function handleTouchMove(e: TouchEvent) {
if (isPinching && e.touches.length === 2) {
e.preventDefault();
const t0 = e.touches[0] as Touch;
const t1 = e.touches[1] as Touch;
const dist = touchDist(t0, t1);
const midX = (t0.clientX + t1.clientX) / 2;
const midY = (t0.clientY + t1.clientY) / 2;
const maxZoom = baseScale > 0 ? ZOOM_PAST_NATIVE / baseScale : 10;
const factor = dist / lastTouchDist;
const newZoom = Math.max(MIN_ZOOM, Math.min(maxZoom, zoomLevel * factor));
if (containerEl !== undefined) {
const rect = containerEl.getBoundingClientRect();
const cx = midX - rect.left - rect.width / 2;
const cy = midY - rect.top - rect.height / 2;
const sf = newZoom / zoomLevel;
panX = cx - sf * (cx - panX) + (midX - lastTouchMidX);
panY = cy - sf * (cy - panY) + (midY - lastTouchMidY);
}
zoomLevel = newZoom;
clampPan();
lastTouchDist = dist;
lastTouchMidX = midX;
lastTouchMidY = midY;
} else if (isPanning && e.touches.length === 1 && zoomLevel > 1) {
e.preventDefault();
const t = e.touches[0] as Touch;
panX += t.clientX - lastPanX;
panY += t.clientY - lastPanY;
clampPan();
lastPanX = t.clientX;
lastPanY = t.clientY;
}
}
function handleTouchEnd(e: TouchEvent) {
if (e.touches.length < 2) {
isPinching = false;
}
if (e.touches.length === 0) {
isPanning = false;
}
if (e.touches.length === 1 && zoomLevel > 1) {
isPanning = true;
const t = e.touches[0] as Touch;
lastPanX = t.clientX;
lastPanY = t.clientY;
}
}
// Track container size via ResizeObserver
$effect(() => {
const el = containerEl;
if (el === undefined) return;
const ro = new ResizeObserver((entries) => {
const entry = entries[0];
if (entry !== undefined) {
containerW = entry.contentRect.width;
containerH = entry.contentRect.height;
}
});
ro.observe(el);
return () => ro.disconnect();
});
// Bind touch listeners with { passive: false } so we can preventDefault
$effect(() => {
const el = containerEl;
if (el === undefined) return;
el.addEventListener('touchstart', handleTouchStart, { passive: false });
el.addEventListener('touchmove', handleTouchMove, { passive: false });
el.addEventListener('touchend', handleTouchEnd, { passive: true });
return () => {
el.removeEventListener('touchstart', handleTouchStart);
el.removeEventListener('touchmove', handleTouchMove);
el.removeEventListener('touchend', handleTouchEnd);
};
});
/**
* The path currently being loaded. Used to detect stale async results
* without relying on the AbortController (which the $effect cleanup
* may fire prematurely if Svelte re-schedules the effect).
*/
let activePath: string | undefined;
$effect(() => {
const currentFile = file;
if (currentFile === undefined) {
activePath = undefined;
cleanup();
return;
}
// Track which path we are loading so async callbacks can detect staleness.
activePath = currentFile.path;
// Reset zoom on file change
resetZoom();
// Revoke previous thumbnail blob URL (plain let, not $state)
if (rawThumbnailUrl !== undefined) {
URL.revokeObjectURL(rawThumbnailUrl);
rawThumbnailUrl = undefined;
}
// Kick off async loaders — they handle most $state writes internally.
// resetZoom() above writes to $state (zoomLevel, panX, panY) but those
// are never read in this $effect body, so they don't add dependencies.
loadThumbnail(currentFile);
loadFullImage(currentFile);
return () => {
if (currentAbort !== undefined) {
currentAbort.abort();
currentAbort = undefined;
}
// If we were downloading and got aborted (e.g. user navigated away),
// make sure to resume the auto-cache service.
if (downloading) {
downloading = false;
autoCacheService.resumeAfterUserDownload();
}
};
});
function cleanup() {
if (rawObjectUrl !== undefined) {
URL.revokeObjectURL(rawObjectUrl);
rawObjectUrl = undefined;
}
if (rawThumbnailUrl !== undefined) {
URL.revokeObjectURL(rawThumbnailUrl);
rawThumbnailUrl = undefined;
}
fullObjectUrl = undefined;
thumbnailBlobUrl = undefined;
imageAspectRatio = '3 / 2';
imgNaturalW = 0;
imgNaturalH = 0;
progress = 0;
downloading = false;
loadError = undefined;
resetZoom();
}
async function loadThumbnail(entry: FlashAirFileEntry) {
// Reset thumbnail state at the start of each load
thumbnailBlobUrl = undefined;
imageAspectRatio = '3 / 2';
const url = flashair.thumbnailUrl(entry.path);
if (url === undefined) return;
// Try cache first
const cached = await imageCache.get('thumbnail', entry.path);
// Check staleness after await
if (activePath !== entry.path) return;
if (cached !== undefined) {
const blobUrl = URL.createObjectURL(cached.blob);
rawThumbnailUrl = blobUrl;
thumbnailBlobUrl = blobUrl;
if (cached.meta !== undefined && cached.meta.width !== undefined && cached.meta.height !== undefined && cached.meta.width > 0 && cached.meta.height > 0) {
imageAspectRatio = `${String(cached.meta.width)} / ${String(cached.meta.height)}`;
}
return;
}
try {
const { blob, meta } = await flashair.fetchThumbnail(entry.path);
// Check staleness after await
if (activePath !== entry.path) return;
// Store in cache (fire-and-forget)
void imageCache.put('thumbnail', entry.path, blob, meta, entry.date.getTime());
const blobUrl = URL.createObjectURL(blob);
rawThumbnailUrl = blobUrl;
thumbnailBlobUrl = blobUrl;
if (meta.width !== undefined && meta.height !== undefined && meta.width > 0 && meta.height > 0) {
imageAspectRatio = `${String(meta.width)} / ${String(meta.height)}`;
}
} catch {
// Thumbnail fetch failed — not critical, full image will load
}
}
async function loadFullImage(entry: FlashAirFileEntry) {
// Reset state at the start of each load
if (rawObjectUrl !== undefined) {
URL.revokeObjectURL(rawObjectUrl);
rawObjectUrl = undefined;
}
fullObjectUrl = undefined;
imgNaturalW = 0;
imgNaturalH = 0;
progress = 0;
loadError = undefined;
if (currentAbort !== undefined) {
currentAbort.abort();
}
const abort = new AbortController();
currentAbort = abort;
// Try cache first — before setting downloading=true to avoid flicker
const cached = await imageCache.get('full', entry.path);
// Use activePath for staleness: the abort signal may have been tripped by
// a Svelte effect re-schedule even though the user didn't change images.
if (activePath !== entry.path) return;
if (cached !== undefined) {
const objectUrl = URL.createObjectURL(cached.blob);
rawObjectUrl = objectUrl;
fullObjectUrl = objectUrl;
progress = 1;
downloading = false;
autoCacheService.markCached(entry.path);
return;
}
downloading = true;
progress = 0;
loadError = undefined;
autoCacheService.pauseForUserDownload();
const url = flashair.fileUrl(entry.path);
const totalBytes = entry.size;
try {
const res = await fetch(url, { signal: abort.signal });
if (!res.ok) {
throw new Error(`${res.status} ${res.statusText}`);
}
const reader = res.body?.getReader();
if (reader === undefined) {
const blob = await res.blob();
if (abort.signal.aborted || activePath !== entry.path) return;
// Store in cache (fire-and-forget)
void imageCache.put('full', entry.path, blob, undefined, entry.date.getTime());
autoCacheService.markCached(entry.path);
const objectUrl = URL.createObjectURL(blob);
rawObjectUrl = objectUrl;
fullObjectUrl = objectUrl;
progress = 1;
downloading = false;
autoCacheService.resumeAfterUserDownload();
return;
}
const chunks: Uint8Array[] = [];
let received = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
received += value.byteLength;
progress = totalBytes > 0 ? received / totalBytes : 0;
}
if (abort.signal.aborted || activePath !== entry.path) return;
const blob = new Blob(chunks);
// Store in cache (fire-and-forget)
void imageCache.put('full', entry.path, blob);
autoCacheService.markCached(entry.path);
const objectUrl = URL.createObjectURL(blob);
rawObjectUrl = objectUrl;
fullObjectUrl = objectUrl;
progress = 1;
} catch (e) {
if (abort.signal.aborted || activePath !== entry.path) return;
loadError = e instanceof Error ? e.message : String(e);
} finally {
if (!abort.signal.aborted && activePath === entry.path) {
downloading = false;
autoCacheService.resumeAfterUserDownload();
}
}
}
let progressPercent = $derived(Math.round(progress * 100));
let showThumbnail = $derived(fullObjectUrl === undefined && thumbnailBlobUrl !== undefined);
let imageTransform = $derived(
`translate(${String(panX)}px, ${String(panY)}px) scale(${String(baseScale * zoomLevel)})`
);
</script>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
class="h-full flex items-center justify-center bg-base-300 relative overflow-hidden touch-none"
bind:this={containerEl}
onwheel={handleWheel}
>
{#if file === undefined}
<div class="text-base-content/40 text-center p-8">
<p class="text-lg">Select a photo to preview</p>
</div>
{:else if loadError !== undefined}
<div class="text-center p-8">
<p class="text-error mb-2">Failed to load image</p>
<p class="text-sm text-base-content/60">{loadError}</p>
</div>
{:else}
{#if downloading && progress < 1}
<div class="absolute inset-0 z-10 flex items-center justify-center bg-base-300/80 backdrop-blur-sm">
<div class="bg-base-100 rounded-box p-4 shadow-xl max-w-xs w-full mx-4">
<div class="flex items-center gap-2">
<progress class="progress progress-primary flex-1" value={progressPercent} max="100"></progress>
<span class="text-xs font-mono text-base-content/70 w-10 text-right">{progressPercent}%</span>
</div>
</div>
</div>
{/if}
{#if fullObjectUrl !== undefined}
{#key fullObjectUrl}
<img
src={fullObjectUrl}
alt={file.filename}
class="will-change-transform shrink-0"
style:width={imgNaturalW > 0 ? `${String(imgNaturalW)}px` : 'auto'}
style:height={imgNaturalH > 0 ? `${String(imgNaturalH)}px` : 'auto'}
style:max-width="none"
style:transform={imageTransform}
style:transform-origin="center center"
draggable="false"
onload={handleImageLoad}
/>
{/key}
{:else if showThumbnail}
<div
class="w-full max-h-full overflow-hidden"
style:aspect-ratio={imageAspectRatio}
>
<img
src={thumbnailBlobUrl}
alt={file.filename}
class="w-full h-full object-cover blur-lg"
draggable="false"
/>
</div>
{:else}
<span class="loading loading-spinner loading-lg"></span>
{/if}
{/if}
</div>
|