@classic-homes/files-svelte 0.5.0 → 0.6.1

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.
@@ -1,11 +1,12 @@
1
1
  <script lang="ts">
2
- import type { Snippet } from 'svelte';
2
+ import type { ComponentProps, Snippet } from 'svelte';
3
3
  import { tick, untrack } from 'svelte';
4
4
  import { MediaQuery } from 'svelte/reactivity';
5
5
  import {
6
6
  Alert,
7
7
  Button,
8
8
  Dialog,
9
+ DropdownMenu,
9
10
  EmptyState,
10
11
  Icon,
11
12
  LiveRegion,
@@ -20,14 +21,18 @@
20
21
  filterEntries,
21
22
  isAbortError,
22
23
  isFilesError,
24
+ joinPath,
23
25
  normalizePath,
24
26
  parentPath,
27
+ pathSegments,
28
+ sortEntries,
25
29
  writeCapabilities,
26
30
  type FileEntry,
27
31
  type FileRoot,
28
32
  type FileSort,
29
33
  type FilesSource,
30
34
  } from '@classic-homes/files-core';
35
+ import FileBranch from './FileBranch.svelte';
31
36
  import FileBreadcrumbs from './FileBreadcrumbs.svelte';
32
37
  import FileErrorState from './FileErrorState.svelte';
33
38
  import FileList from './FileList.svelte';
@@ -51,9 +56,27 @@
51
56
  * The location changed (drive, folder or both), in one call. Use this to
52
57
  * keep the location in the URL: on a drive change `onrootchange` and
53
58
  * `onpathchange` fire back to back, and a URL built from the current page
54
- * in each would lose the first change.
59
+ * in each would lose the first change. In pane mode it also fires when a
60
+ * file opens, changes or closes, with `file` set to the open file's name.
55
61
  */
56
- onnavigate?: (location: { rootId: string; path: string }) => void;
62
+ onnavigate?: (location: { rootId: string; path: string; file?: string }) => void;
63
+ /**
64
+ * How a file opens: `'dialog'` (a modal over the list) or `'pane'` (in place
65
+ * of the list, with the folder tree beside it focused on the open folder's
66
+ * branch). Default `'dialog'`.
67
+ */
68
+ preview?: 'dialog' | 'pane';
69
+ /**
70
+ * Name of the open file in the current folder. Bindable. Setting it opens
71
+ * that file (a deep link); `undefined` closes it. A name that doesn't resolve
72
+ * to a file in the folder is cleared and reported through `onfilechange`
73
+ * (and, in pane mode, `onnavigate` without `file`).
74
+ */
75
+ file?: string;
76
+ /** The user opened, switched or closed a file: its name, or `undefined`. */
77
+ onfilechange?: (file: string | undefined) => void;
78
+ /** Draw the rounded border. Set false when the browser fills its container edge to edge. Default true. */
79
+ frame?: boolean;
57
80
  /** Document viewers for previews, as lazy imports keyed by format. */
58
81
  viewers?: ViewerRegistry;
59
82
  /** Entries per page. Default 200. */
@@ -102,6 +125,10 @@
102
125
  onrootchange,
103
126
  onpathchange,
104
127
  onnavigate,
128
+ preview = 'dialog',
129
+ file = $bindable(),
130
+ onfilechange,
131
+ frame = true,
105
132
  viewers = {},
106
133
  pageSize = 200,
107
134
  autoLoad = false,
@@ -125,6 +152,7 @@
125
152
  // The folder pane sits beside the list from md up; phones get a sheet instead.
126
153
  const wide = new MediaQuery('(min-width: 768px)', false);
127
154
  let foldersOpen = $state(false);
155
+ let filesOpen = $state(false);
128
156
  const filterId = `${uid}-filter`;
129
157
  const hiddenId = `${uid}-hidden`;
130
158
 
@@ -158,6 +186,11 @@
158
186
  let announcement = $state('');
159
187
  let online = $state(typeof navigator === 'undefined' ? true : navigator.onLine !== false);
160
188
  let list = $state<FileList>();
189
+ let previewer = $state<FilePreview>();
190
+ let sideBranch = $state<FileBranch>();
191
+ let sectionEl = $state<HTMLElement>();
192
+ /** Root + folder whose first page is in `entries` (resolves `file` against it). */
193
+ let listedKey = $state<string>();
161
194
  /** Focus the list once the next listing lands (keyboard navigation). */
162
195
  let focusAfterLoad = false;
163
196
 
@@ -177,6 +210,20 @@
177
210
  const searching = $derived(!!source.search && filter.trim() !== '');
178
211
  const shown = $derived(searching ? (searchResults ?? []) : filterEntries(entries, filter));
179
212
  const hasMore = $derived(!searching && !!nextCursor);
213
+ // Pane mode: the open file and its neighbours, in display order.
214
+ const docOpen = $derived(
215
+ preview === 'pane' && previewOpen && !!previewEntry && rootId !== undefined
216
+ );
217
+ /** The entries in the list's order: the left pane's branch and previous / next follow it. */
218
+ const docEntries = $derived(sort ? sortEntries(shown, sort) : shown);
219
+ const docFiles = $derived(docEntries.filter((e) => e.type === 'file'));
220
+ const docIndex = $derived(
221
+ previewEntry ? docFiles.findIndex((e) => e.path === previewEntry?.path) : -1
222
+ );
223
+ const prevFile = $derived(docIndex > 0 ? docFiles[docIndex - 1] : undefined);
224
+ const nextFile = $derived(
225
+ docIndex >= 0 && docIndex < docFiles.length - 1 ? docFiles[docIndex + 1] : undefined
226
+ );
180
227
  const nf = $derived(new Intl.NumberFormat(locale));
181
228
 
182
229
  const context: FileBrowserContext = {
@@ -256,6 +303,7 @@
256
303
  entries = [];
257
304
  total = undefined;
258
305
  nextCursor = null;
306
+ listedKey = undefined;
259
307
  announcement = `Loading ${folderName}…`;
260
308
  });
261
309
 
@@ -266,6 +314,7 @@
266
314
  total = page.total;
267
315
  nextCursor = page.nextCursor ?? null;
268
316
  status = 'ready';
317
+ listedKey = locationKey(id, folder);
269
318
  announce();
270
319
  if (focusAfterLoad) {
271
320
  focusAfterLoad = false;
@@ -372,6 +421,122 @@
372
421
  };
373
422
  });
374
423
 
424
+ const locationKey = (id: string | undefined, folder: string) => `${id ?? ''}\u0000${folder}`;
425
+
426
+ /** Show a file. `notify` reports it as the user's choice; a `file` prop change doesn't. */
427
+ function show(entry: FileEntry, notify: boolean) {
428
+ const same = previewOpen && previewEntry?.path === entry.path;
429
+ previewEntry = entry;
430
+ previewOpen = true;
431
+ selected = entry.path;
432
+ if (same) return;
433
+ file = entry.name;
434
+ onpreview?.(entry);
435
+ if (!notify) return;
436
+ onfilechange?.(entry.name);
437
+ if (preview === 'pane' && rootId !== undefined)
438
+ onnavigate?.({ rootId, path: currentPath, file: entry.name });
439
+ }
440
+
441
+ /** Hide the open file. In pane mode focus inside the preview goes back to the list. */
442
+ function hidePreview() {
443
+ if (!previewOpen) return;
444
+ const refocus = preview === 'pane' && !!sectionEl && sectionEl.contains(document.activeElement);
445
+ previewOpen = false;
446
+ filesOpen = false;
447
+ // The dialog keeps its entry while it animates out.
448
+ if (preview === 'pane') previewEntry = undefined;
449
+ if (refocus) void tick().then(() => list?.focus());
450
+ }
451
+
452
+ function closePreview(notify: boolean) {
453
+ const had = file !== undefined;
454
+ hidePreview();
455
+ file = undefined;
456
+ if (!notify || !had) return;
457
+ onfilechange?.(undefined);
458
+ if (preview === 'pane' && rootId !== undefined) onnavigate?.({ rootId, path: currentPath });
459
+ }
460
+
461
+ /** Leaving the folder closes its file; the caller reports the new location. */
462
+ function leaveFile() {
463
+ const had = file !== undefined;
464
+ closePreview(false);
465
+ if (had) onfilechange?.(undefined);
466
+ }
467
+
468
+ // `file` from outside (a deep link, Back/Forward): open or close to match. A
469
+ // location change closes the open file; a new `file` then resolves in the new folder.
470
+ let lastLocation: string | undefined;
471
+ let resolveController: AbortController | undefined;
472
+ $effect(() => {
473
+ const want = file;
474
+ const id = rootId;
475
+ const folder = currentPath;
476
+ const listed = listedKey;
477
+ const key = locationKey(id, folder);
478
+ untrack(() => {
479
+ if (lastLocation !== undefined && key !== lastLocation) hidePreview();
480
+ lastLocation = key;
481
+ const openName = previewOpen ? previewEntry?.name : undefined;
482
+ if (want === openName) return;
483
+ resolveController?.abort();
484
+ if (want === undefined) {
485
+ hidePreview();
486
+ return;
487
+ }
488
+ if (id === undefined || listed !== key) return; // wait for this folder's listing
489
+ resolveFile(want, id, folder);
490
+ });
491
+ });
492
+
493
+ /**
494
+ * `file` named something that isn't a file in this folder: clear the binding
495
+ * and say so, so the caller drops a stale `?file=` from its URL.
496
+ */
497
+ function unresolved(name: string) {
498
+ if (file !== name) return;
499
+ file = undefined;
500
+ onfilechange?.(undefined);
501
+ if (preview === 'pane' && rootId !== undefined) onnavigate?.({ rootId, path: currentPath });
502
+ }
503
+
504
+ /** Errors from `info()` that settle it: the name isn't a file here. Others are transient. */
505
+ const CONCLUSIVE = new Set(['not_found', 'invalid_path', 'is_directory', 'not_a_directory']);
506
+
507
+ function resolveFile(name: string, id: string, folder: string) {
508
+ if (name.includes('/')) return unresolved(name);
509
+ const hit = entries.find((e) => e.name === name);
510
+ if (hit) {
511
+ if (hit.type === 'file') show(hit, false);
512
+ else unresolved(name);
513
+ return;
514
+ }
515
+ if (!nextCursor) return unresolved(name); // the whole folder is listed: it isn't here
516
+ // Beyond the first page: ask for it directly.
517
+ let target: string;
518
+ try {
519
+ target = joinPath(folder, name);
520
+ } catch {
521
+ return unresolved(name);
522
+ }
523
+ const controller = new AbortController();
524
+ resolveController = controller;
525
+ source.info(id, target, controller.signal).then(
526
+ (entry) => {
527
+ if (controller.signal.aborted || file !== name) return;
528
+ if (entry.type === 'file') show(entry, false);
529
+ else unresolved(name);
530
+ },
531
+ (err) => {
532
+ if (controller.signal.aborted || isAbortError(err)) return;
533
+ // A file that isn't there clears the link; anything else (network, 401…) is reported.
534
+ if (isFilesError(err) && CONCLUSIVE.has(err.code)) unresolved(name);
535
+ else report(err);
536
+ }
537
+ );
538
+ }
539
+
375
540
  function navigate(next: string, focusChild?: string) {
376
541
  let target: string;
377
542
  try {
@@ -379,6 +544,7 @@
379
544
  } catch {
380
545
  target = '';
381
546
  }
547
+ leaveFile();
382
548
  filter = '';
383
549
  sort = null;
384
550
  selected = focusChild;
@@ -399,6 +565,7 @@
399
565
 
400
566
  function changeRoot(id: string, nextPath = '') {
401
567
  if (id === rootId) return;
568
+ leaveFile();
402
569
  rootId = id;
403
570
  filter = '';
404
571
  sort = null;
@@ -424,7 +591,10 @@
424
591
 
425
592
  // A sheet left open on a phone must not reappear when the view narrows again.
426
593
  $effect(() => {
427
- if (wide.current) foldersOpen = false;
594
+ if (wide.current) {
595
+ foldersOpen = false;
596
+ filesOpen = false;
597
+ }
428
598
  });
429
599
 
430
600
  function refresh() {
@@ -436,14 +606,63 @@
436
606
  focusAfterLoad = true;
437
607
  navigate(entry.path);
438
608
  } else if (entry.type === 'file') {
439
- previewEntry = entry;
440
- previewOpen = true;
441
- onpreview?.(entry);
609
+ show(entry, true);
610
+ // The preview replaces the list that had focus.
611
+ if (preview === 'pane') void tick().then(() => previewer?.focus());
442
612
  }
443
613
  }
444
614
 
615
+ /**
616
+ * The branch beside (or, on phones, over) an open document: a folder on it
617
+ * closes the document. The current folder just closes it; any other folder
618
+ * opens, and an ancestor selects the folder the user came from.
619
+ */
620
+ function branchFolder(target: string) {
621
+ const inSheet = filesOpen;
622
+ filesOpen = false;
623
+ if (target === currentPath) {
624
+ closePreview(true);
625
+ // From the phone sheet focus isn't in the browser: put it on the list.
626
+ if (inSheet) void tick().then(() => list?.focus());
627
+ return;
628
+ }
629
+ let from: string | undefined;
630
+ try {
631
+ from = pathSegments(currentPath).find((s) => parentPath(s.path) === target)?.path;
632
+ } catch {
633
+ from = undefined;
634
+ }
635
+ focusAfterLoad = true;
636
+ navigate(target, from);
637
+ }
638
+
639
+ function branchFile(entry: FileEntry) {
640
+ filesOpen = false;
641
+ show(entry, true);
642
+ }
643
+
644
+ /** Arrowing onto a file in the branch switches the document as it goes. */
645
+ function branchStep(entry: FileEntry) {
646
+ if (!filesOpen) show(entry, true);
647
+ }
648
+
649
+ /** The side pane's "Clear": the full folder again, the open document kept. */
650
+ function clearSideFilter() {
651
+ filter = '';
652
+ // The strip (and its button) goes: keep focus in the tree, on the open file.
653
+ void tick().then(() => sideBranch?.focus());
654
+ }
655
+
445
656
  function onkeydown(event: KeyboardEvent) {
446
657
  if (event.defaultPrevented) return;
658
+ if (docOpen) {
659
+ // Esc in a field (the viewer's page box) only leaves the field.
660
+ if (event.key === 'Escape' && !isTextInput(event.target)) {
661
+ event.preventDefault();
662
+ closePreview(true);
663
+ }
664
+ return;
665
+ }
447
666
  if (previewOpen) return;
448
667
  const typing = isTextInput(event.target);
449
668
  if (event.key === '/' && !typing && !event.ctrlKey && !event.metaKey && !event.altKey) {
@@ -487,8 +706,27 @@
487
706
  };
488
707
  });
489
708
 
490
- const showPane = $derived(navigationPane && wide.current && roots.length > 0);
709
+ // Two panes: the folder tree beside the list, or the folder's branch beside a document.
710
+ const split = $derived(wide.current && roots.length > 0 && (navigationPane || docOpen));
491
711
  const rootOptions = $derived(roots.map((r) => ({ value: r.id, label: r.label })));
712
+ // Phones open the folder tree (which lists every drive) from a button beside the breadcrumbs.
713
+ const showFoldersButton = $derived(navigationPane && !wide.current && roots.length > 0);
714
+ // The drive picker, only where no folder tree lists the drives.
715
+ const showRootPicker = $derived(roots.length > 1 && !navigationPane);
716
+ /** Phones: the header's secondary controls, behind one ⋯ button. */
717
+ // The component's own item type: the package's exported `DropdownMenuItem` lacks `checked`.
718
+ const moreItems = $derived<ComponentProps<typeof DropdownMenu>['items']>([
719
+ {
720
+ label: 'Show hidden files',
721
+ checked: showHidden,
722
+ onSelect: () => (showHidden = !showHidden),
723
+ },
724
+ { label: 'Refresh', icon: 'refresh', onSelect: refresh },
725
+ ]);
726
+ /** The open folder as a path, for the phone "Files" button's tooltip. */
727
+ const folderTrail = $derived(
728
+ [rootLabel, ...(currentPath ? currentPath.split('/') : [])].join(' / ')
729
+ );
492
730
  const listLabel = $derived(`${folderName} contents`);
493
731
 
494
732
  const countText = $derived.by(() => {
@@ -514,8 +752,13 @@
514
752
 
515
753
  <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
516
754
  <section
755
+ bind:this={sectionEl}
517
756
  aria-label={label}
518
- class={cn('flex min-h-0 flex-col rounded-lg border border-border bg-background', className)}
757
+ class={cn(
758
+ 'flex min-h-0 flex-col bg-background',
759
+ frame && 'rounded-lg border border-border',
760
+ className
761
+ )}
519
762
  {onkeydown}
520
763
  data-file-browser
521
764
  >
@@ -535,101 +778,156 @@
535
778
  class="py-12"
536
779
  />
537
780
  {:else}
538
- <div class="flex flex-col gap-3 border-b border-border p-3">
539
- <div class="flex flex-col gap-2 sm:flex-row sm:items-center">
540
- {#if roots.length > 1 && !showPane}
541
- <div class="sm:w-64">
542
- <Select
543
- label="Drive"
544
- value={rootId}
545
- options={rootOptions}
546
- onValueChange={changeRoot}
547
- hint={root?.description ?? undefined}
781
+ <!-- The open document's header carries the name; the branch beside it carries navigation. -->
782
+ {#if !docOpen}
783
+ <!-- Phones: two rows (Folders + breadcrumbs; filter + toolbar + ⋯ menu). From md up: one row. -->
784
+ <div
785
+ class="flex flex-col gap-2 border-b border-border p-3 md:flex-row md:flex-wrap md:items-center md:gap-x-4 md:gap-y-2"
786
+ data-file-browser-header
787
+ >
788
+ <div
789
+ class="flex min-w-0 flex-wrap items-center gap-2 md:flex-[1_1_16rem] md:flex-nowrap"
790
+ data-file-browser-location
791
+ >
792
+ {#if showFoldersButton}
793
+ <Button
794
+ variant="outline"
795
+ size="sm"
796
+ class="shrink-0 px-2 sm:px-3"
797
+ onclick={() => (foldersOpen = true)}
798
+ aria-haspopup="dialog"
799
+ aria-label="Folders"
800
+ title="Folders"
801
+ >
802
+ <Icon name="sidebar" size={16} />
803
+ <span class="hidden sm:inline">Folders</span>
804
+ </Button>
805
+ {/if}
806
+ {#if showRootPicker}
807
+ <div class="w-full sm:w-64 sm:shrink-0">
808
+ <Select
809
+ label="Drive"
810
+ value={rootId}
811
+ options={rootOptions}
812
+ onValueChange={changeRoot}
813
+ hint={root?.description ?? undefined}
814
+ />
815
+ </div>
816
+ {/if}
817
+ {#if root}
818
+ <FileBreadcrumbs
819
+ rootLabel={root.label}
820
+ rootTitle={root.description ?? undefined}
821
+ path={currentPath}
822
+ onnavigate={(p) => navigate(p)}
823
+ class={cn('min-w-0', wide.current && root.description ? 'flex-[0_1_auto]' : 'flex-1')}
548
824
  />
549
- </div>
550
- {/if}
551
- {#if root}
552
- <FileBreadcrumbs
553
- rootLabel={root.label}
554
- path={currentPath}
555
- onnavigate={(p) => navigate(p)}
556
- class="min-w-0 flex-1"
557
- />
558
- {/if}
559
- </div>
560
- {#if root?.description && (roots.length === 1 || showPane)}
561
- <p class="-mt-1 text-xs text-muted-foreground">{root.description}</p>
562
- {/if}
563
-
564
- <div class="flex flex-wrap items-center gap-x-4 gap-y-2">
565
- <div class="relative min-w-[12rem] flex-1">
566
- <label for={filterId} class="sr-only"
567
- >{source.search ? 'Search files' : 'Filter files in this folder'}</label
568
- >
569
- <Icon
570
- name={source.search ? 'search' : 'filter'}
571
- size={16}
572
- class="pointer-events-none absolute left-2.5 top-1/2 -translate-y-1/2 text-muted-foreground"
573
- />
574
- <input
575
- id={filterId}
576
- type="search"
577
- bind:value={filter}
578
- placeholder={source.search ? 'Search files' : 'Filter this folder'}
579
- autocomplete="off"
580
- aria-keyshortcuts="/"
581
- class="flex h-9 w-full rounded-md border border-input bg-background py-1 pl-8 pr-10 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
582
- />
583
- <kbd
584
- class="pointer-events-none absolute right-2 top-1/2 hidden -translate-y-1/2 rounded border border-border px-1.5 text-xs text-muted-foreground sm:block"
585
- aria-hidden="true">/</kbd
586
- >
587
- </div>
588
- <div class="flex items-center gap-2">
589
- <Switch id={hiddenId} checked={showHidden} onchange={(v) => (showHidden = v)} />
590
- <label for={hiddenId} class="text-sm">Show hidden files</label>
825
+ {#if wide.current && root.description && !showRootPicker}
826
+ <!-- Secondary: takes only the room the breadcrumbs leave, truncating. -->
827
+ <p
828
+ class="min-w-0 flex-[1_1_0] truncate text-xs text-muted-foreground"
829
+ title={root.description}
830
+ data-file-browser-description
831
+ >
832
+ {root.description}
833
+ </p>
834
+ {/if}
835
+ {/if}
591
836
  </div>
592
- {#if toolbar && caps.any}
593
- <div class="flex items-center gap-2" data-file-browser-toolbar>
594
- {@render toolbar(context)}
837
+
838
+ <div
839
+ class="flex flex-wrap items-center gap-2 md:flex-[0_1_auto] md:gap-x-4"
840
+ data-file-browser-controls
841
+ >
842
+ <div class="relative min-w-[10rem] flex-1 md:w-56 md:flex-none">
843
+ <label for={filterId} class="sr-only"
844
+ >{source.search ? 'Search files' : 'Filter files in this folder'}</label
845
+ >
846
+ <Icon
847
+ name={source.search ? 'search' : 'filter'}
848
+ size={16}
849
+ class="pointer-events-none absolute left-2.5 top-1/2 -translate-y-1/2 text-muted-foreground"
850
+ />
851
+ <input
852
+ id={filterId}
853
+ type="search"
854
+ bind:value={filter}
855
+ placeholder={source.search ? 'Search files' : 'Filter this folder'}
856
+ autocomplete="off"
857
+ aria-keyshortcuts="/"
858
+ class="flex h-9 w-full rounded-md border border-input bg-background py-1 pl-8 pr-10 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
859
+ />
860
+ <kbd
861
+ class="pointer-events-none absolute right-2 top-1/2 hidden -translate-y-1/2 rounded border border-border px-1.5 text-xs text-muted-foreground sm:block"
862
+ aria-hidden="true">/</kbd
863
+ >
595
864
  </div>
596
- {/if}
597
- {#if navigationPane && !wide.current && roots.length > 0}
598
- <Button
599
- variant="outline"
600
- size="sm"
601
- onclick={() => (foldersOpen = true)}
602
- aria-haspopup="dialog"
603
- >
604
- <Icon name="sidebar" size={16} />
605
- Folders
606
- </Button>
607
- {/if}
608
- <Button variant="ghost" size="icon" onclick={refresh} aria-label="Refresh">
609
- <Icon name="refresh" size={16} />
610
- </Button>
865
+ {#if wide.current}
866
+ <div class="flex items-center gap-2">
867
+ <Switch id={hiddenId} checked={showHidden} onchange={(v) => (showHidden = v)} />
868
+ <label for={hiddenId} class="whitespace-nowrap text-sm">Show hidden files</label>
869
+ </div>
870
+ {/if}
871
+ {#if toolbar && caps.any}
872
+ <div class="flex items-center gap-2" data-file-browser-toolbar>
873
+ {@render toolbar(context)}
874
+ </div>
875
+ {/if}
876
+ {#if wide.current}
877
+ <Button variant="ghost" size="icon" onclick={refresh} aria-label="Refresh">
878
+ <Icon name="refresh" size={16} />
879
+ </Button>
880
+ {:else}
881
+ <DropdownMenu
882
+ items={moreItems}
883
+ align="end"
884
+ triggerVariant="ghost"
885
+ triggerSize="icon"
886
+ triggerClass="shrink-0"
887
+ triggerLabel="More options"
888
+ >
889
+ {#snippet trigger()}
890
+ <Icon name="more-horizontal" size={16} />
891
+ {/snippet}
892
+ </DropdownMenu>
893
+ {/if}
894
+ </div>
611
895
  </div>
612
- </div>
896
+ {/if}
613
897
 
614
- {#if showPane}
898
+ {#if split}
615
899
  <ResizablePanes
616
900
  bind:size={paneSize}
617
901
  min={12}
618
902
  max={50}
619
903
  persistKey={paneKey}
620
- handleLabel="Resize folder pane"
904
+ handleLabel={docOpen ? 'Resize file list' : 'Resize folder pane'}
621
905
  class="min-h-0 flex-1"
622
906
  startClass="bg-muted/20"
623
907
  >
624
908
  {#snippet start()}
625
- <div class="h-full overflow-auto px-1" data-file-tree-scroll>
626
- {@render tree()}
627
- </div>
909
+ {#if docOpen}
910
+ <div class="h-full overflow-auto" data-file-side-list>
911
+ {@render sideList()}
912
+ </div>
913
+ {:else}
914
+ <div class="h-full overflow-auto px-1" data-file-tree-scroll>
915
+ {@render tree()}
916
+ </div>
917
+ {/if}
628
918
  {/snippet}
629
919
  {#snippet end()}
630
- <div class="flex h-full min-h-0 flex-col">{@render listArea()}</div>
920
+ <div class="flex h-full min-h-0 flex-col">
921
+ {#if docOpen}
922
+ {@render docPane()}
923
+ {:else}
924
+ {@render listArea()}
925
+ {/if}
926
+ </div>
631
927
  {/snippet}
632
928
  </ResizablePanes>
929
+ {:else if docOpen}
930
+ {@render docPane()}
633
931
  {:else}
634
932
  {@render listArea()}
635
933
  {/if}
@@ -699,6 +997,85 @@
699
997
  </div>
700
998
  {/snippet}
701
999
 
1000
+ {#snippet docPane()}
1001
+ {#if previewEntry && rootId !== undefined}
1002
+ <FilePreview
1003
+ bind:this={previewer}
1004
+ {source}
1005
+ {rootId}
1006
+ {rootLabel}
1007
+ entry={previewEntry}
1008
+ {viewers}
1009
+ maxBytes={maxPreviewBytes}
1010
+ {locale}
1011
+ ondownload={(_, err) => {
1012
+ if (err) report(err);
1013
+ }}
1014
+ onerror={report}
1015
+ onclose={() => closePreview(true)}
1016
+ onprev={prevFile ? () => prevFile && show(prevFile, true) : undefined}
1017
+ onnext={nextFile ? () => nextFile && show(nextFile, true) : undefined}
1018
+ start={wide.current ? undefined : filesButton}
1019
+ class="min-h-0 flex-1"
1020
+ />
1021
+ {/if}
1022
+ {/snippet}
1023
+
1024
+ {#snippet filesButton()}
1025
+ <Button
1026
+ variant="outline"
1027
+ size="sm"
1028
+ onclick={() => (filesOpen = true)}
1029
+ aria-haspopup="dialog"
1030
+ title={`Files in ${folderTrail}`}
1031
+ >
1032
+ <Icon name="list" size={16} />
1033
+ Files
1034
+ </Button>
1035
+ {/snippet}
1036
+
1037
+ {#snippet sideList()}
1038
+ {#if filter.trim()}
1039
+ <!-- The filter box hides while a document is open: say why the list is short. -->
1040
+ <div
1041
+ class="sticky top-0 z-10 flex items-center gap-2 border-b border-border bg-background px-3 py-1.5 text-xs text-muted-foreground"
1042
+ data-file-side-filter
1043
+ >
1044
+ <Icon name={source.search ? 'search' : 'filter'} size={14} class="shrink-0" />
1045
+ <span class="min-w-0 flex-1 truncate" title={filter.trim()}>
1046
+ <span class="sr-only">{source.search ? 'Search results for' : 'Filtered by'}</span>
1047
+ “{filter.trim()}”
1048
+ </span>
1049
+ <Button
1050
+ variant="ghost"
1051
+ size="sm"
1052
+ class="h-7 shrink-0 px-2"
1053
+ aria-label={source.search ? 'Clear search' : 'Clear filter'}
1054
+ onclick={clearSideFilter}
1055
+ >
1056
+ Clear
1057
+ </Button>
1058
+ </div>
1059
+ {/if}
1060
+ <FileBranch
1061
+ bind:this={sideBranch}
1062
+ {rootLabel}
1063
+ path={currentPath}
1064
+ entries={docEntries}
1065
+ current={previewEntry?.path}
1066
+ loading={status !== 'ready' || (searching && searchStatus === 'loading')}
1067
+ {loadingMore}
1068
+ {hasMore}
1069
+ onloadmore={loadMore}
1070
+ onfolder={branchFolder}
1071
+ onfile={branchFile}
1072
+ onstep={branchStep}
1073
+ emptyText={filter.trim() ? `Nothing matches “${filter.trim()}”` : 'This folder is empty'}
1074
+ label={`Files in ${folderName}`}
1075
+ class={wide.current ? 'px-1' : undefined}
1076
+ />
1077
+ {/snippet}
1078
+
702
1079
  {#snippet tree()}
703
1080
  <FileTree
704
1081
  {source}
@@ -724,9 +1101,20 @@
724
1101
  </Sheet>
725
1102
  {/if}
726
1103
 
727
- {#if previewEntry && rootId !== undefined}
1104
+ {#if preview === 'pane' && !wide.current && docOpen}
1105
+ <Sheet bind:open={filesOpen} side="left" size="sm" title="Files">
1106
+ <div data-file-side-list class="-mx-2">
1107
+ {@render sideList()}
1108
+ </div>
1109
+ </Sheet>
1110
+ {/if}
1111
+
1112
+ {#if preview === 'dialog' && previewEntry && rootId !== undefined}
728
1113
  <Dialog
729
1114
  bind:open={previewOpen}
1115
+ onOpenChange={(open) => {
1116
+ if (!open) closePreview(true);
1117
+ }}
730
1118
  title={previewEntry.name}
731
1119
  size="full"
732
1120
  class="h-[100dvh] max-h-[100dvh] max-w-[100vw] gap-0 rounded-none sm:h-[90dvh] sm:max-h-[90dvh] sm:max-w-5xl sm:rounded-lg [&_.dialog-content]:flex [&_.dialog-content]:flex-col [&_.dialog-content]:p-0"