@widgetic/file-browser 0.5.40 → 0.5.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/core/FileBrowser.svelte +157 -62
- package/dist/components/core/FileBrowser.svelte.d.ts +5 -1
- package/dist/components/core/FileUploader.svelte +49 -15
- package/dist/components/core/RecentFiles.svelte +23 -11
- package/dist/components/core/RecentFiles.svelte.d.ts +1 -0
- package/dist/components/core/SearchDropdown.svelte +124 -26
- package/dist/components/core/UploadPanel.svelte +1 -1
- package/dist/components/items/FileItem.svelte +12 -2
- package/dist/components/items/FolderItem.svelte +8 -1
- package/dist/components/modals/index.d.ts +0 -1
- package/dist/components/modals/index.js +0 -1
- package/dist/components/previews/FilePreview.svelte +226 -35
- package/dist/components/previews/FilePreview.svelte.d.ts +7 -3
- package/dist/components/previews/FontPreview.svelte +4 -1
- package/dist/components/previews/FontPreview.svelte.d.ts +2 -0
- package/dist/components/previews/ImagePreview.svelte +60 -31
- package/dist/components/previews/ImagePreview.svelte.d.ts +3 -0
- package/dist/components/previews/PreviewHeaderBar.svelte +33 -25
- package/dist/components/previews/PreviewHeaderBar.svelte.d.ts +2 -0
- package/dist/components/ui/DragSelect.svelte +85 -67
- package/dist/components/ui/DragSelect.svelte.d.ts +1 -0
- package/dist/data/demoContent.json +13 -11
- package/dist/i18n/messages.d.ts +0 -1
- package/dist/i18n/messages.js +0 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/stores/navigationStore.d.ts +5 -0
- package/dist/stores/navigationStore.js +40 -67
- package/dist/utils/escapeSelectionAction.d.ts +18 -0
- package/dist/utils/escapeSelectionAction.js +18 -0
- package/dist/utils/imagePreviewCache.d.ts +16 -0
- package/dist/utils/imagePreviewCache.js +78 -0
- package/dist/utils/imagePreviewVisualState.d.ts +19 -0
- package/dist/utils/imagePreviewVisualState.js +22 -0
- package/dist/utils/menuKeydownBubble.d.ts +9 -0
- package/dist/utils/menuKeydownBubble.js +11 -0
- package/dist/utils/previewNavigation.d.ts +4 -0
- package/dist/utils/previewNavigation.js +17 -0
- package/dist/utils/recentFiles.d.ts +6 -0
- package/dist/utils/recentFiles.js +21 -0
- package/dist/utils/recentFilesLimit.d.ts +8 -0
- package/dist/utils/recentFilesLimit.js +17 -0
- package/dist/utils/searchEscapeAction.d.ts +10 -0
- package/dist/utils/searchEscapeAction.js +12 -0
- package/dist/utils/searchItems.d.ts +2 -4
- package/dist/utils/searchItems.js +12 -5
- package/dist/utils/searchNavigation.d.ts +12 -0
- package/dist/utils/searchNavigation.js +42 -0
- package/dist/utils/uploadDisplayProgress.d.ts +4 -0
- package/dist/utils/uploadDisplayProgress.js +9 -0
- package/dist/utils/uploadListReconcile.d.ts +11 -0
- package/dist/utils/uploadListReconcile.js +30 -0
- package/dist/utils/uploadPanelProgress.d.ts +5 -0
- package/dist/utils/uploadPanelProgress.js +17 -0
- package/dist/utils/userUploadsStorage.d.ts +8 -0
- package/dist/utils/userUploadsStorage.js +32 -0
- package/package.json +3 -3
- package/dist/components/modals/SearchModal.svelte +0 -296
- package/dist/components/modals/SearchModal.svelte.d.ts +0 -27
|
@@ -7,7 +7,12 @@
|
|
|
7
7
|
// Svelte imports
|
|
8
8
|
import { onMount } from 'svelte';
|
|
9
9
|
import { createEventDispatcher } from 'svelte';
|
|
10
|
-
const dispatch = createEventDispatcher
|
|
10
|
+
const dispatch = createEventDispatcher<{
|
|
11
|
+
navigate: FileSystemItem;
|
|
12
|
+
select: FileSystemItem;
|
|
13
|
+
close: void;
|
|
14
|
+
fileUnavailable: { id: string };
|
|
15
|
+
}>();
|
|
11
16
|
|
|
12
17
|
// Design System Components imports
|
|
13
18
|
import { BaseModal, PDFViewer, EmptyState } from '@widgetic/design-system/components';
|
|
@@ -30,11 +35,14 @@
|
|
|
30
35
|
|
|
31
36
|
// Utils
|
|
32
37
|
import { getFileTypeOf } from '../../utils/fileKinds';
|
|
38
|
+
import { countPreviewNavigationPeers } from '../../utils/previewNavigation';
|
|
33
39
|
import { toSafeFontFamilyName } from '../../utils/safeText';
|
|
34
40
|
import { previewKeyAction } from '../../utils/previewKeys';
|
|
41
|
+
import { getCachedImageNaturalSize, preloadImageUrl, probeDecodedImage } from '../../utils/imagePreviewCache';
|
|
42
|
+
import { IMAGE_SLOW_LOADER_DELAY_MS, imageRevealDelayMs } from '../../utils/imagePreviewVisualState';
|
|
35
43
|
|
|
36
44
|
// Stores
|
|
37
|
-
import { missingFiles } from '../../stores/missingFilesStore';
|
|
45
|
+
import { addMissingFile, missingFiles } from '../../stores/missingFilesStore';
|
|
38
46
|
import { validSelections } from '../../stores/persistentSelectStore';
|
|
39
47
|
import { type FileFilterType, isPreviewableTextFile } from '../../utils/fileTypes';
|
|
40
48
|
import { useI18n } from '../../i18n';
|
|
@@ -48,11 +56,15 @@
|
|
|
48
56
|
export let file: FileSystemItem;
|
|
49
57
|
export let open = false;
|
|
50
58
|
export let allowSelecting: boolean = false;
|
|
59
|
+
/** Editor picker mode — shows Select in the preview header and Space-to-select. */
|
|
60
|
+
export let showAddToEditor = false;
|
|
51
61
|
export let currentFiles: FileSystemItem[] = [];
|
|
52
62
|
export let currentFilter: FileFilterType = 'all';
|
|
53
63
|
export let target: string = 'file-preview-modal'; // target for Portal
|
|
54
64
|
/** Horizontal inset inside the dialog shell (Figma pad 24 each side + the 1px border of the card on each side). */
|
|
55
65
|
const DIALOG_HORIZONTAL_PAD = 50;
|
|
66
|
+
/** Narrowest card for a picture: the header (name, previous, next, Select, close) needs this much room. */
|
|
67
|
+
const MIN_IMAGE_DIALOG_WIDTH = 320;
|
|
56
68
|
/** Header (title + meta) + gap to body (Figma 36 + 16). */
|
|
57
69
|
const HEADER_BLOCK = 52;
|
|
58
70
|
/** Shell vertical padding (p-6 top + bottom, and the 1px border of the card on each side). */
|
|
@@ -194,17 +206,79 @@
|
|
|
194
206
|
// A file the library lost, one without an address, or a picture that does not load: the card says so (Figma "Preview Dialog / Error")
|
|
195
207
|
let imageFailed = false;
|
|
196
208
|
$: showsErrorState = !hasValidUrl || $missingFiles.has(file.id) || imageFailed;
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
209
|
+
// (`modalDimensions`, the card's size, is declared below the block that sizes the frame of a known picture.)
|
|
210
|
+
// What the person sees when a picture opens (round 26, the timing rules live in `imagePreviewVisualState.ts`):
|
|
211
|
+
// - a picture the browser already holds is shown at once, with no spinner;
|
|
212
|
+
// - any other picture keeps an empty frame while it loads, and only if it is STILL not there after
|
|
213
|
+
// `IMAGE_SLOW_LOADER_DELAY_MS` does the spinner show;
|
|
214
|
+
// - a loaded picture fades in after the card has eased to its size (at once when the card was sized long ago).
|
|
215
|
+
// Every flag below names the FILE it is true for, and every timer checks that name when it fires, so a late timer or a late
|
|
216
|
+
// "load" of the file the person just left can never show an empty frame or a spinner on the file that is open now.
|
|
204
217
|
let imageRevealForId = '';
|
|
218
|
+
let imageBitmapReadyForId = '';
|
|
219
|
+
let slowLoaderForId = '';
|
|
220
|
+
let frameSizedForId = '';
|
|
221
|
+
let frameSizedAtMs = 0;
|
|
205
222
|
let imageRevealTimer: ReturnType<typeof setTimeout> | undefined;
|
|
223
|
+
let slowLoaderTimer: ReturnType<typeof setTimeout> | undefined;
|
|
206
224
|
|
|
207
|
-
function
|
|
225
|
+
function clearImageRevealTimers() {
|
|
226
|
+
if (imageRevealTimer) clearTimeout(imageRevealTimer);
|
|
227
|
+
if (slowLoaderTimer) clearTimeout(slowLoaderTimer);
|
|
228
|
+
imageRevealTimer = undefined;
|
|
229
|
+
slowLoaderTimer = undefined;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** A new file starts with nothing shown, nothing loaded and a fresh slow-loader clock. */
|
|
233
|
+
function resetImageRevealState(fileId: string) {
|
|
234
|
+
clearImageRevealTimers();
|
|
235
|
+
imageRevealForId = '';
|
|
236
|
+
imageBitmapReadyForId = '';
|
|
237
|
+
slowLoaderForId = '';
|
|
238
|
+
frameSizedForId = '';
|
|
239
|
+
slowLoaderTimer = setTimeout(() => {
|
|
240
|
+
slowLoaderTimer = undefined;
|
|
241
|
+
// Still on this file and its picture is still not there: now (and only now) the spinner shows
|
|
242
|
+
if (file.id === fileId && imageRevealForId !== fileId) slowLoaderForId = fileId;
|
|
243
|
+
}, IMAGE_SLOW_LOADER_DELAY_MS);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Shows the picture of this file, if it has loaded, once its card has had the time to ease to the picture's size. */
|
|
247
|
+
function revealImageWhenReady(fileId: string) {
|
|
248
|
+
if (file.id !== fileId || imageRevealForId === fileId || imageBitmapReadyForId !== fileId) return;
|
|
249
|
+
if (imageRevealTimer) clearTimeout(imageRevealTimer);
|
|
250
|
+
// A card that never got a size (the picture reported none) has no move to wait for
|
|
251
|
+
const waitMs = frameSizedForId === fileId ? imageRevealDelayMs(frameSizedAtMs, performance.now()) : 0;
|
|
252
|
+
imageRevealTimer = setTimeout(() => {
|
|
253
|
+
imageRevealTimer = undefined;
|
|
254
|
+
if (file.id !== fileId || imageBitmapReadyForId !== fileId) return;
|
|
255
|
+
imageRevealForId = fileId;
|
|
256
|
+
slowLoaderForId = '';
|
|
257
|
+
if (slowLoaderTimer) clearTimeout(slowLoaderTimer);
|
|
258
|
+
slowLoaderTimer = undefined;
|
|
259
|
+
}, waitMs);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** The picture of the file that is open has arrived (the "load" event of `ImagePreview`). */
|
|
263
|
+
function handleImageBitmapLoaded(event: CustomEvent<{ src: string }>) {
|
|
264
|
+
// The picture of a file the person has already left says nothing about the one that is open
|
|
265
|
+
if (!fileUrl || event.detail?.src !== fileUrl) return;
|
|
266
|
+
const id = file.id;
|
|
267
|
+
imageLoaded = true;
|
|
268
|
+
imageBitmapReadyForId = id;
|
|
269
|
+
if (frameSizedForId !== id) {
|
|
270
|
+
// No size was known before the picture came (not in the library's data, no "dimensions" event): the picture's own size is
|
|
271
|
+
const natural = getCachedImageNaturalSize(fileUrl);
|
|
272
|
+
if (natural) fitImageFrame(natural.width, natural.height);
|
|
273
|
+
}
|
|
274
|
+
revealImageWhenReady(id);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function fitImageFrame(
|
|
278
|
+
width: number,
|
|
279
|
+
height: number,
|
|
280
|
+
options?: { revealImmediately?: boolean }
|
|
281
|
+
) {
|
|
208
282
|
const frame = hostFrame();
|
|
209
283
|
const element = document.querySelector('.widgetic-breakpoints');
|
|
210
284
|
let margin = 24;
|
|
@@ -216,19 +290,60 @@
|
|
|
216
290
|
const maxHeight = Math.max(120, frame.height - TOTAL_VERTICAL_SPACE - 48);
|
|
217
291
|
const scale = Math.min(1, maxWidth / width, maxHeight / height);
|
|
218
292
|
const pictureWidth = Math.max(100, Math.round(width * scale));
|
|
293
|
+
// A tall, thin picture gets a card as wide as the header needs (the picture stays centred in it), never wider than the box.
|
|
294
|
+
const cardWidth = Math.min(
|
|
295
|
+
Math.max(MIN_IMAGE_DIALOG_WIDTH, pictureWidth + DIALOG_HORIZONTAL_PAD),
|
|
296
|
+
Math.max(pictureWidth + DIALOG_HORIZONTAL_PAD, frame.width - 2 * margin)
|
|
297
|
+
);
|
|
219
298
|
const next = {
|
|
220
|
-
width: `${
|
|
299
|
+
width: `${cardWidth}px`,
|
|
221
300
|
height: `${Math.max(100, Math.round(height * scale) + TOTAL_VERTICAL_SPACE)}px`
|
|
222
301
|
};
|
|
223
302
|
const id = file.id;
|
|
224
303
|
fittedImageForId = id;
|
|
225
304
|
fittedImage = next;
|
|
226
|
-
|
|
305
|
+
frameSizedForId = id;
|
|
306
|
+
frameSizedAtMs = performance.now();
|
|
227
307
|
if (imageRevealTimer) clearTimeout(imageRevealTimer);
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
308
|
+
imageRevealTimer = undefined;
|
|
309
|
+
if (options?.revealImmediately) {
|
|
310
|
+
// The picture was decoded before: the card takes its size and the picture is there at once, with no spinner
|
|
311
|
+
imageBitmapReadyForId = id;
|
|
312
|
+
imageRevealForId = id;
|
|
313
|
+
slowLoaderForId = '';
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
// The card eases to this size first. The picture stays hidden until it has loaded AND the card has made that move
|
|
317
|
+
// (if it loaded already, it appears when the move is over; if not, `handleImageBitmapLoaded` shows it when it arrives).
|
|
318
|
+
imageRevealForId = '';
|
|
319
|
+
revealImageWhenReady(id);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function revealCachedImageIfReady() {
|
|
323
|
+
if (!open || fileType !== 'image' || !hasValidUrl || !fileUrl) return;
|
|
324
|
+
const id = file.id;
|
|
325
|
+
// This file's card has its size already: the "load" of the picture, or the end of the card's move, finishes the job
|
|
326
|
+
if (fittedImageForId === id) return;
|
|
327
|
+
|
|
328
|
+
// The browser may still hold the picture decoded (opened before, or loaded as a neighbour): then it is shown at once
|
|
329
|
+
const cached = probeDecodedImage(fileUrl);
|
|
330
|
+
if (file.width && file.height) {
|
|
331
|
+
fitImageFrame(file.width, file.height, { revealImmediately: !!cached });
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (cached) {
|
|
335
|
+
fitImageFrame(cached.width, cached.height, { revealImmediately: true });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function preloadNeighborPreviewImages() {
|
|
340
|
+
if (!open || fileType !== 'image') return;
|
|
341
|
+
for (const direction of ['prev', 'next'] as const) {
|
|
342
|
+
const neighbor = findNextAvailableFile(currentIndex, direction);
|
|
343
|
+
if (!neighbor?.url) continue;
|
|
344
|
+
if (getFileTypeOf(neighbor) !== 'image') continue;
|
|
345
|
+
void preloadImageUrl(neighbor.url);
|
|
346
|
+
}
|
|
232
347
|
}
|
|
233
348
|
|
|
234
349
|
$: currentIndex = currentFiles.findIndex((f: FileSystemItem) => f.id === file.id);
|
|
@@ -253,7 +368,7 @@
|
|
|
253
368
|
isMissing: $missingFiles.has(candidateFile.id)
|
|
254
369
|
});
|
|
255
370
|
|
|
256
|
-
if (!$missingFiles.has(candidateFile.id)) {
|
|
371
|
+
if (candidateFile.id !== file.id && !$missingFiles.has(candidateFile.id)) {
|
|
257
372
|
if(showLogs) console.log('Found available file:', candidateFile.name);
|
|
258
373
|
return candidateFile;
|
|
259
374
|
}
|
|
@@ -262,8 +377,39 @@
|
|
|
262
377
|
return null;
|
|
263
378
|
}
|
|
264
379
|
|
|
380
|
+
$: showNavigation =
|
|
381
|
+
allowSelecting &&
|
|
382
|
+
countPreviewNavigationPeers(currentFiles, file, $missingFiles) > 0;
|
|
383
|
+
|
|
384
|
+
// A new file starts with its own reveal state: nothing shown, nothing loaded, a fresh slow-loader clock. This block sits
|
|
385
|
+
// ABOVE the one that sizes the card on purpose, and that one reads `imageStateForFileId`, so when the file changes the reset
|
|
386
|
+
// always runs first (a picture that is warm is then shown by the sizing block, and is not wiped by the reset).
|
|
387
|
+
let imageStateForFileId = '';
|
|
388
|
+
$: if (file.id !== imageStateForFileId) {
|
|
389
|
+
imageStateForFileId = file.id;
|
|
390
|
+
resetImageRevealState(file.id);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Size the shell from catalogued dimensions or a warm decoder cache before the bitmap loads. */
|
|
394
|
+
$: if (open && file?.id && fileUrl && fileType === 'image' && imageStateForFileId === file.id) {
|
|
395
|
+
revealCachedImageIfReady();
|
|
396
|
+
preloadNeighborPreviewImages();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// The card's size. It stays BELOW the block above on purpose: that block sets `fittedImage` and `fittedImageForId`
|
|
400
|
+
// inside a function, so Svelte cannot order the two statements by their assignments. A statement placed above it reads
|
|
401
|
+
// the frame of the previous file in the same update (the small loading card) and does not run again, so a file that
|
|
402
|
+
// shares its picture with the one before (no new load event, no later "dimensions" event) kept the small card.
|
|
403
|
+
$: modalDimensions = showsErrorState
|
|
404
|
+
? getModalDimensions('error')
|
|
405
|
+
: fileType === 'video' && fittedVideo && fittedVideoForId === file.id
|
|
406
|
+
? fittedVideo
|
|
407
|
+
: fileType === 'image' && fittedImage && fittedImageForId === file.id
|
|
408
|
+
? fittedImage
|
|
409
|
+
: getModalDimensions(fileType);
|
|
410
|
+
|
|
265
411
|
function handleKeydown(event: KeyboardEvent) {
|
|
266
|
-
if (!open || !allowSelecting) return;
|
|
412
|
+
if (!open || !allowSelecting || !showNavigation) return;
|
|
267
413
|
|
|
268
414
|
// The arrow keys follow the reading direction: the left arrow is "previous" in English and "next" in Arabic.
|
|
269
415
|
const keyAction = previewKeyAction(event.key, $textDirection);
|
|
@@ -295,6 +441,7 @@
|
|
|
295
441
|
window.addEventListener('keydown', handleKeydown);
|
|
296
442
|
return () => {
|
|
297
443
|
window.removeEventListener('keydown', handleKeydown);
|
|
444
|
+
clearImageRevealTimers();
|
|
298
445
|
};
|
|
299
446
|
});
|
|
300
447
|
|
|
@@ -343,11 +490,26 @@
|
|
|
343
490
|
$: isPDF = fileType === 'document' && file.name.toLowerCase().endsWith('.pdf');
|
|
344
491
|
$: isPreviewableText = fileType === 'document' && isPreviewableTextFile(file.name);
|
|
345
492
|
|
|
493
|
+
let reportedUnavailableForId = '';
|
|
494
|
+
|
|
495
|
+
function reportFileUnavailable(fileId: string) {
|
|
496
|
+
if (reportedUnavailableForId === fileId) return;
|
|
497
|
+
reportedUnavailableForId = fileId;
|
|
498
|
+
addMissingFile(fileId);
|
|
499
|
+
dispatch('fileUnavailable', { id: fileId });
|
|
500
|
+
}
|
|
501
|
+
|
|
346
502
|
// Autoplay when component mounts
|
|
347
503
|
onMount(() => {
|
|
348
504
|
|
|
349
505
|
if(showLogs) console.log('FilePreview: onMount: file:', file);
|
|
350
506
|
|
|
507
|
+
if (fileType === 'font' && file.url) {
|
|
508
|
+
void checkFileExists(file.url).then((ok) => {
|
|
509
|
+
if (!ok) reportFileUnavailable(file.id);
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
|
|
351
513
|
// Autoplay media when component mounts
|
|
352
514
|
if (videoElement || audioElement) {
|
|
353
515
|
const mediaElement = videoElement || audioElement;
|
|
@@ -400,8 +562,10 @@
|
|
|
400
562
|
|
|
401
563
|
// Update imageLoaded state when file changes
|
|
402
564
|
$: if (fileUrl && fileType === 'image') {
|
|
403
|
-
|
|
404
|
-
|
|
565
|
+
const urlBeingChecked = fileUrl;
|
|
566
|
+
checkImageLoaded(urlBeingChecked).then(loaded => {
|
|
567
|
+
// The answer for a file the person has already left must not be taken for the one that is open
|
|
568
|
+
if (urlBeingChecked === fileUrl) imageLoaded = loaded;
|
|
405
569
|
});
|
|
406
570
|
}
|
|
407
571
|
|
|
@@ -450,9 +614,16 @@
|
|
|
450
614
|
!pdfLoadError &&
|
|
451
615
|
!(fileType === 'image' && imageRevealForId !== file.id);
|
|
452
616
|
|
|
453
|
-
$: canGoPrevious =
|
|
454
|
-
|
|
455
|
-
|
|
617
|
+
$: canGoPrevious = showNavigation && findNextAvailableFile(currentIndex, 'prev') !== null;
|
|
618
|
+
$: canGoNext = showNavigation && findNextAvailableFile(currentIndex, 'next') !== null;
|
|
619
|
+
|
|
620
|
+
// The spinner: only for a picture that is STILL loading after `IMAGE_SLOW_LOADER_DELAY_MS` (the timer of the file that is
|
|
621
|
+
// open sets `slowLoaderForId`), never for one that was cached or arrived sooner, and never over an error card.
|
|
622
|
+
$: showSlowImageLoader =
|
|
623
|
+
fileType === 'image' &&
|
|
624
|
+
slowLoaderForId === file.id &&
|
|
625
|
+
imageRevealForId !== file.id &&
|
|
626
|
+
!showsErrorState;
|
|
456
627
|
|
|
457
628
|
function goPreviousFile() {
|
|
458
629
|
const prevFile = findNextAvailableFile(currentIndex, 'prev');
|
|
@@ -477,20 +648,22 @@
|
|
|
477
648
|
class="file-preview-modal"
|
|
478
649
|
bind:open
|
|
479
650
|
bare
|
|
651
|
+
ariaLabel={file.name}
|
|
480
652
|
width={modalDimensions.width}
|
|
481
653
|
height={modalDimensions.height}
|
|
482
654
|
animateSize={fileType === 'image' || fileType === 'video'}
|
|
483
655
|
{target}
|
|
484
656
|
showScroll={false}
|
|
485
657
|
on:close={handleClose}
|
|
486
|
-
enableSpaceKey={allowSelecting && !isMissing}
|
|
658
|
+
enableSpaceKey={allowSelecting && showAddToEditor && !isMissing}
|
|
487
659
|
isSelected={isSelected}
|
|
488
660
|
modalType="filePreview"
|
|
489
661
|
modalData={{
|
|
490
662
|
file,
|
|
491
663
|
currentFiles,
|
|
492
664
|
currentFilter,
|
|
493
|
-
allowSelecting
|
|
665
|
+
allowSelecting,
|
|
666
|
+
showAddToEditor
|
|
494
667
|
}}
|
|
495
668
|
on:restore={(event) => {
|
|
496
669
|
if (event.detail) {
|
|
@@ -498,6 +671,7 @@
|
|
|
498
671
|
currentFiles = event.detail.currentFiles;
|
|
499
672
|
currentFilter = event.detail.currentFilter;
|
|
500
673
|
allowSelecting = event.detail.allowSelecting;
|
|
674
|
+
showAddToEditor = event.detail.showAddToEditor ?? showAddToEditor;
|
|
501
675
|
|
|
502
676
|
// Re-trigger file type checks and URL validation
|
|
503
677
|
fileType = getFileTypeOf(file);
|
|
@@ -523,7 +697,9 @@
|
|
|
523
697
|
meta={previewMetaLine}
|
|
524
698
|
showMeta={showPreviewMeta}
|
|
525
699
|
{allowSelecting}
|
|
700
|
+
showSelectInPreview={allowSelecting && showAddToEditor}
|
|
526
701
|
{isSelected}
|
|
702
|
+
{showNavigation}
|
|
527
703
|
{canGoPrevious}
|
|
528
704
|
{canGoNext}
|
|
529
705
|
on:previous={goPreviousFile}
|
|
@@ -537,7 +713,8 @@
|
|
|
537
713
|
<FontPreview
|
|
538
714
|
src={fileUrl}
|
|
539
715
|
{fontName}
|
|
540
|
-
previewText={file.previewText || undefined}
|
|
716
|
+
previewText={file.previewText || undefined}
|
|
717
|
+
on:unavailable={() => reportFileUnavailable(file.id)}
|
|
541
718
|
/>
|
|
542
719
|
{:else if fileType === 'image' || fileType === 'svg'}
|
|
543
720
|
{#if fileType === 'svg'}
|
|
@@ -552,30 +729,44 @@
|
|
|
552
729
|
{:else}
|
|
553
730
|
<!-- Until the picture's size is known the slot is the loading card's body (156px); then it is as tall as the picture -->
|
|
554
731
|
<div
|
|
555
|
-
class="image-preview-slot relative
|
|
556
|
-
style={fittedImageForId === file.id ? '' : 'height: 9.75rem; overflow: hidden;'}
|
|
732
|
+
class="image-preview-slot relative flex w-full shrink-0 justify-center"
|
|
733
|
+
style={fittedImageForId === file.id ? 'min-height: 0' : 'min-height: 9.75rem; overflow: hidden;'}
|
|
557
734
|
>
|
|
558
|
-
<div
|
|
735
|
+
<div
|
|
736
|
+
class="image-preview-frame inline-flex max-w-full justify-center {imageRevealForId === file.id ? 'opacity-100' : 'opacity-0'}"
|
|
737
|
+
>
|
|
559
738
|
<ImagePreview
|
|
560
739
|
hash={file.hash}
|
|
561
740
|
src={fileUrl}
|
|
562
741
|
alt={file.name}
|
|
563
742
|
width={file.width}
|
|
564
743
|
height={file.height}
|
|
744
|
+
maxContentWidth={hostFrame().width - 100}
|
|
745
|
+
maxContentHeight={hostFrame().height - TOTAL_VERTICAL_SPACE - 48}
|
|
565
746
|
initiallyLoaded={imageLoaded}
|
|
566
|
-
on:load={
|
|
567
|
-
on:error={() =>
|
|
747
|
+
on:load={handleImageBitmapLoaded}
|
|
748
|
+
on:error={() => {
|
|
749
|
+
imageFailed = true;
|
|
750
|
+
reportFileUnavailable(file.id);
|
|
751
|
+
}}
|
|
568
752
|
on:dimensions={(event) => {
|
|
569
753
|
const { width, height } = event.detail;
|
|
570
754
|
if (!width || !height) return;
|
|
571
|
-
|
|
755
|
+
// The picture was decoded before this view started: show it at once (the cache is filled by the bitmap load
|
|
756
|
+
// in ImagePreview, with the natural size; the size of this event is the fitted one and is not stored).
|
|
757
|
+
const wasWarm = !!getCachedImageNaturalSize(fileUrl);
|
|
758
|
+
fitImageFrame(width, height, { revealImmediately: wasWarm });
|
|
572
759
|
}}
|
|
573
760
|
/>
|
|
574
761
|
</div>
|
|
575
|
-
{#if
|
|
576
|
-
<div
|
|
762
|
+
{#if showSlowImageLoader}
|
|
763
|
+
<div
|
|
764
|
+
class="image-preview-loader absolute inset-0 flex flex-col items-center justify-center"
|
|
765
|
+
role="status"
|
|
766
|
+
aria-label={$t('preview.loading')}
|
|
767
|
+
>
|
|
577
768
|
<IconLoader2 class="size-8 animate-spin stroke-[1.333] text-primary motion-reduce:animate-none" />
|
|
578
|
-
<
|
|
769
|
+
<span class="sr-only">{$t('preview.loading')}</span>
|
|
579
770
|
</div>
|
|
580
771
|
{/if}
|
|
581
772
|
</div>
|
|
@@ -17,13 +17,17 @@ declare const FilePreview: $$__sveltets_2_IsomorphicComponent<{
|
|
|
17
17
|
file: FileSystemItem;
|
|
18
18
|
open?: boolean;
|
|
19
19
|
allowSelecting?: boolean;
|
|
20
|
+
/** Editor picker mode — shows Select in the preview header and Space-to-select. */ showAddToEditor?: boolean;
|
|
20
21
|
currentFiles?: FileSystemItem[];
|
|
21
22
|
currentFilter?: FileFilterType;
|
|
22
23
|
target?: string;
|
|
23
24
|
}, {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
close: CustomEvent<
|
|
25
|
+
navigate: CustomEvent<FileSystemItem>;
|
|
26
|
+
select: CustomEvent<FileSystemItem>;
|
|
27
|
+
close: CustomEvent<void>;
|
|
28
|
+
fileUnavailable: CustomEvent<{
|
|
29
|
+
id: string;
|
|
30
|
+
}>;
|
|
27
31
|
} & {
|
|
28
32
|
[evt: string]: CustomEvent<any>;
|
|
29
33
|
}, {}, {}, string>;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { onMount } from 'svelte';
|
|
2
|
+
import { createEventDispatcher, onMount } from 'svelte';
|
|
3
3
|
import ErrorPreview from './ErrorPreview.svelte';
|
|
4
|
+
|
|
5
|
+
const dispatch = createEventDispatcher<{ unavailable: void }>();
|
|
4
6
|
import { toCssUrlLiteral, toSafeFontFamilyName } from '../../utils/safeText';
|
|
5
7
|
import { useI18n } from '../../i18n';
|
|
6
8
|
|
|
@@ -27,6 +29,7 @@
|
|
|
27
29
|
} catch (error) {
|
|
28
30
|
console.error('FontPreview: Error loading font from url:', src, error);
|
|
29
31
|
hasError = true;
|
|
32
|
+
dispatch('unavailable');
|
|
30
33
|
}
|
|
31
34
|
});
|
|
32
35
|
</script>
|
|
@@ -16,6 +16,8 @@ declare const FontPreview: $$__sveltets_2_IsomorphicComponent<{
|
|
|
16
16
|
fontName: string;
|
|
17
17
|
/** The sentence shown in the font. Left out, a pangram in the page's language ("The quick brown fox jumps over the lazy dog"). */ previewText?: string | undefined;
|
|
18
18
|
}, {
|
|
19
|
+
unavailable: CustomEvent<void>;
|
|
20
|
+
} & {
|
|
19
21
|
[evt: string]: CustomEvent<any>;
|
|
20
22
|
}, {}, {}, string>;
|
|
21
23
|
type FontPreview = InstanceType<typeof FontPreview>;
|
|
@@ -5,12 +5,17 @@
|
|
|
5
5
|
import { createEventDispatcher } from 'svelte';
|
|
6
6
|
import { tweened } from 'svelte/motion';
|
|
7
7
|
import { cubicOut } from 'svelte/easing';
|
|
8
|
+
import { rememberImageNaturalSize } from '../../utils/imagePreviewCache';
|
|
8
9
|
export let hash: string | undefined = undefined;
|
|
9
10
|
export let src: string | undefined = undefined;
|
|
10
11
|
export let alt: string = '';
|
|
11
12
|
export let initiallyLoaded: boolean = false;
|
|
12
13
|
export let width: number | undefined = undefined;
|
|
13
14
|
export let height: number | undefined = undefined;
|
|
15
|
+
/** Max picture width inside the preview host (falls back to window when omitted). */
|
|
16
|
+
export let maxContentWidth: number | undefined = undefined;
|
|
17
|
+
/** Max picture height inside the preview host (falls back to window when omitted). */
|
|
18
|
+
export let maxContentHeight: number | undefined = undefined;
|
|
14
19
|
|
|
15
20
|
let imageLoaded = initiallyLoaded;
|
|
16
21
|
let isImageCached = false;
|
|
@@ -38,21 +43,34 @@
|
|
|
38
43
|
let domObserver: MutationObserver | null = null;
|
|
39
44
|
let elementStates: {timestamp: number, mainImage: string, thumbhash: string}[] = [];
|
|
40
45
|
|
|
46
|
+
function pictureMaxBounds() {
|
|
47
|
+
const maxWidth =
|
|
48
|
+
maxContentWidth ??
|
|
49
|
+
(typeof window !== 'undefined' ? Math.max(160, window.innerWidth - 100) : 1024);
|
|
50
|
+
const maxHeight =
|
|
51
|
+
maxContentHeight ??
|
|
52
|
+
(typeof window !== 'undefined' ? Math.max(120, window.innerHeight - 200) : 768);
|
|
53
|
+
return { maxWidth, maxHeight };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function scaleToFitBounds(pictureWidth: number, pictureHeight: number) {
|
|
57
|
+
const { maxWidth, maxHeight } = pictureMaxBounds();
|
|
58
|
+
const scale = Math.min(1, maxWidth / pictureWidth, maxHeight / pictureHeight);
|
|
59
|
+
return {
|
|
60
|
+
width: Math.max(1, Math.round(pictureWidth * scale)),
|
|
61
|
+
height: Math.max(1, Math.round(pictureHeight * scale)),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
41
65
|
// Calculate initial dimensions with proper scaling
|
|
42
66
|
$: {
|
|
43
67
|
if (width && height) {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const scaleWidth = maxWidth / width;
|
|
48
|
-
const scaleHeight = maxHeight / height;
|
|
49
|
-
const scale = Math.min(1, scaleWidth, scaleHeight);
|
|
50
|
-
|
|
51
|
-
containerWidth = `${Math.round(width * scale)}px`;
|
|
52
|
-
containerHeight = `${Math.round(height * scale)}px`;
|
|
68
|
+
const fitted = scaleToFitBounds(width, height);
|
|
69
|
+
containerWidth = `${fitted.width}px`;
|
|
70
|
+
containerHeight = `${fitted.height}px`;
|
|
53
71
|
} else {
|
|
54
|
-
containerWidth =
|
|
55
|
-
containerHeight =
|
|
72
|
+
containerWidth = '10rem';
|
|
73
|
+
containerHeight = '10rem';
|
|
56
74
|
}
|
|
57
75
|
}
|
|
58
76
|
|
|
@@ -104,22 +122,13 @@
|
|
|
104
122
|
}
|
|
105
123
|
|
|
106
124
|
function updateDimensions(imgWidth: number, imgHeight: number) {
|
|
107
|
-
//
|
|
108
|
-
const actualWidth = width
|
|
109
|
-
const actualHeight = height
|
|
110
|
-
|
|
111
|
-
// Get viewport dimensions with some padding
|
|
112
|
-
const maxWidth = window.innerWidth - 100; // 50px padding each side
|
|
113
|
-
const maxHeight = window.innerHeight - 200; // 100px padding top/bottom
|
|
114
|
-
|
|
115
|
-
// Calculate scale to fit within bounds while maintaining aspect ratio
|
|
116
|
-
const scaleWidth = maxWidth / actualWidth;
|
|
117
|
-
const scaleHeight = maxHeight / actualHeight;
|
|
118
|
-
const scale = Math.min(1, scaleWidth, scaleHeight);
|
|
125
|
+
// Loaded bitmap dimensions beat stale catalog metadata (wrong aspect → grey bands in the preview).
|
|
126
|
+
const actualWidth = imgWidth > 0 ? imgWidth : (width ?? imgWidth);
|
|
127
|
+
const actualHeight = imgHeight > 0 ? imgHeight : (height ?? imgHeight);
|
|
119
128
|
|
|
120
|
-
|
|
121
|
-
const finalWidth =
|
|
122
|
-
const finalHeight =
|
|
129
|
+
const fitted = scaleToFitBounds(actualWidth, actualHeight);
|
|
130
|
+
const finalWidth = fitted.width;
|
|
131
|
+
const finalHeight = fitted.height;
|
|
123
132
|
|
|
124
133
|
containerWidth = `${finalWidth}px`;
|
|
125
134
|
containerHeight = `${finalHeight}px`;
|
|
@@ -135,7 +144,22 @@
|
|
|
135
144
|
}
|
|
136
145
|
}
|
|
137
146
|
|
|
138
|
-
function handleImageLoad() {
|
|
147
|
+
function handleImageLoad(event: Event, loadedSrc: string) {
|
|
148
|
+
// A picture that is no longer the one shown (the person moved on before it arrived) must neither size the card nor
|
|
149
|
+
// fill the cache under the address of the picture that is shown now.
|
|
150
|
+
if (loadedSrc !== src) return;
|
|
151
|
+
const loadedImage = event.currentTarget as HTMLImageElement | null;
|
|
152
|
+
if (loadedImage?.naturalWidth && loadedImage?.naturalHeight) {
|
|
153
|
+
updateDimensions(loadedImage.naturalWidth, loadedImage.naturalHeight);
|
|
154
|
+
// Remembered after the "dimensions" event, so the preview can tell a picture it had not seen before from one it had
|
|
155
|
+
// (the first view eases the card before it shows the picture; a view of a known picture shows it at once).
|
|
156
|
+
if (src) {
|
|
157
|
+
rememberImageNaturalSize(src, loadedImage.naturalWidth, loadedImage.naturalHeight);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// The picture itself has arrived: the preview shows it (and takes its spinner away) when its card has the right size.
|
|
161
|
+
dispatch('load', { src: loadedSrc });
|
|
162
|
+
|
|
139
163
|
if (imageLoaded || isTransitioning) {
|
|
140
164
|
if (debugMode) console.log(`[DEBUG] Image load handler skipped - already loaded: ${imageLoaded}, transitioning: ${isTransitioning}`);
|
|
141
165
|
return;
|
|
@@ -170,7 +194,9 @@
|
|
|
170
194
|
}
|
|
171
195
|
|
|
172
196
|
// The file cannot be fetched or is not a picture: tell the preview, so it can say so instead of loading for ever.
|
|
173
|
-
function handleImageError() {
|
|
197
|
+
function handleImageError(failedSrc: string) {
|
|
198
|
+
// The failure of a picture that is no longer shown says nothing about the one that is
|
|
199
|
+
if (failedSrc !== src) return;
|
|
174
200
|
console.warn('ImagePreview: the image could not be loaded:', src);
|
|
175
201
|
dispatch('error');
|
|
176
202
|
}
|
|
@@ -499,7 +525,10 @@
|
|
|
499
525
|
});
|
|
500
526
|
</script>
|
|
501
527
|
|
|
502
|
-
<div
|
|
528
|
+
<div
|
|
529
|
+
bind:this={previewRoot}
|
|
530
|
+
class="image-preview-container inline-flex max-w-full items-center justify-center overflow-hidden rounded-xl bg-muted"
|
|
531
|
+
>
|
|
503
532
|
<div
|
|
504
533
|
class="image-parent-container relative flex items-center justify-center"
|
|
505
534
|
style="width: {containerWidth}; height: {containerHeight};"
|
|
@@ -519,8 +548,8 @@
|
|
|
519
548
|
loading="eager"
|
|
520
549
|
decoding="async"
|
|
521
550
|
class="preview-image h-full w-full object-contain"
|
|
522
|
-
onload={handleImageLoad}
|
|
523
|
-
onerror={handleImageError}
|
|
551
|
+
onload={(event) => handleImageLoad(event, item.src)}
|
|
552
|
+
onerror={() => handleImageError(item.src)}
|
|
524
553
|
/>
|
|
525
554
|
</div>
|
|
526
555
|
{/each}
|
|
@@ -18,8 +18,11 @@ declare const ImagePreview: $$__sveltets_2_IsomorphicComponent<{
|
|
|
18
18
|
initiallyLoaded?: boolean;
|
|
19
19
|
width?: number | undefined;
|
|
20
20
|
height?: number | undefined;
|
|
21
|
+
/** Max picture width inside the preview host (falls back to window when omitted). */ maxContentWidth?: number | undefined;
|
|
22
|
+
/** Max picture height inside the preview host (falls back to window when omitted). */ maxContentHeight?: number | undefined;
|
|
21
23
|
}, {
|
|
22
24
|
dimensions: CustomEvent<any>;
|
|
25
|
+
load: CustomEvent<any>;
|
|
23
26
|
error: CustomEvent<any>;
|
|
24
27
|
} & {
|
|
25
28
|
[evt: string]: CustomEvent<any>;
|