@classic-homes/files-svelte 0.2.0 → 0.3.0

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.
@@ -47,6 +47,13 @@
47
47
  onrootchange?: (rootId: string) => void;
48
48
  /** The user opened another folder. */
49
49
  onpathchange?: (path: string) => void;
50
+ /**
51
+ * The location changed (drive, folder or both), in one call. Use this to
52
+ * keep the location in the URL: on a drive change `onrootchange` and
53
+ * `onpathchange` fire back to back, and a URL built from the current page
54
+ * in each would lose the first change.
55
+ */
56
+ onnavigate?: (location: { rootId: string; path: string }) => void;
50
57
  /** Document viewers for previews, as lazy imports keyed by format. */
51
58
  viewers?: ViewerRegistry;
52
59
  /** Entries per page. Default 200. */
@@ -94,6 +101,7 @@
94
101
  path = $bindable(''),
95
102
  onrootchange,
96
103
  onpathchange,
104
+ onnavigate,
97
105
  viewers = {},
98
106
  pageSize = 200,
99
107
  autoLoad = false,
@@ -211,6 +219,7 @@
211
219
  rootId = list[0].id;
212
220
  if (current !== undefined) path = '';
213
221
  onrootchange?.(list[0].id);
222
+ onnavigate?.({ rootId: list[0].id, path: untrack(() => currentPath) });
214
223
  }
215
224
  },
216
225
  (err) => {
@@ -368,6 +377,7 @@
368
377
  }
369
378
  path = target;
370
379
  onpathchange?.(target);
380
+ if (rootId !== undefined) onnavigate?.({ rootId, path: target });
371
381
  }
372
382
 
373
383
  function goUp() {
@@ -387,15 +397,25 @@
387
397
  path = nextPath;
388
398
  onpathchange?.(nextPath);
389
399
  }
400
+ onnavigate?.({ rootId: id, path: nextPath });
390
401
  }
391
402
 
392
403
  /** From the folder tree: a folder in this drive, or another drive (and folder). */
393
404
  function treeNavigate(id: string, next: string) {
394
- foldersOpen = false;
405
+ if (foldersOpen) {
406
+ // Phone sheet: it closes, so continue in the list once it loads.
407
+ foldersOpen = false;
408
+ focusAfterLoad = true;
409
+ }
395
410
  if (id !== rootId) changeRoot(id, next);
396
411
  else navigate(next);
397
412
  }
398
413
 
414
+ // A sheet left open on a phone must not reappear when the view narrows again.
415
+ $effect(() => {
416
+ if (wide.current) foldersOpen = false;
417
+ });
418
+
399
419
  function refresh() {
400
420
  attempt++;
401
421
  }
@@ -666,7 +686,16 @@
666
686
  {/snippet}
667
687
 
668
688
  {#snippet tree()}
669
- <FileTree {source} {roots} {rootId} path={currentPath} {showHidden} onnavigate={treeNavigate} />
689
+ <FileTree
690
+ {source}
691
+ {roots}
692
+ {rootId}
693
+ path={currentPath}
694
+ {showHidden}
695
+ refreshKey={attempt}
696
+ onerror={report}
697
+ onnavigate={treeNavigate}
698
+ />
670
699
  {/snippet}
671
700
 
672
701
  {#snippet actions(entry: FileEntry)}
@@ -13,6 +13,16 @@ interface Props {
13
13
  onrootchange?: (rootId: string) => void;
14
14
  /** The user opened another folder. */
15
15
  onpathchange?: (path: string) => void;
16
+ /**
17
+ * The location changed (drive, folder or both), in one call. Use this to
18
+ * keep the location in the URL: on a drive change `onrootchange` and
19
+ * `onpathchange` fire back to back, and a URL built from the current page
20
+ * in each would lose the first change.
21
+ */
22
+ onnavigate?: (location: {
23
+ rootId: string;
24
+ path: string;
25
+ }) => void;
16
26
  /** Document viewers for previews, as lazy imports keyed by format. */
17
27
  viewers?: ViewerRegistry;
18
28
  /** Entries per page. Default 200. */
@@ -31,6 +31,13 @@
31
31
  showHidden?: boolean;
32
32
  /** A folder (or a drive, with `path` `''`) was chosen. */
33
33
  onnavigate?: (rootId: string, path: string) => void;
34
+ /**
35
+ * Change it to reload the expanded folders in place (e.g. after a refresh
36
+ * or a write). FileBrowser passes its refresh counter.
37
+ */
38
+ refreshKey?: unknown;
39
+ /** A folder failed to load (a 401 included), for logging or re-login. */
40
+ onerror?: (error: unknown) => void;
34
41
  /** Accessible name of the tree. Default "Folders". */
35
42
  label?: string;
36
43
  class?: string;
@@ -43,14 +50,21 @@
43
50
  path = '',
44
51
  showHidden = false,
45
52
  onnavigate,
53
+ refreshKey,
54
+ onerror,
46
55
  label = 'Folders',
47
56
  class: className,
48
57
  }: Props = $props();
49
58
 
59
+ /** Folders listed per folder; past this the tree says to open the folder instead. */
60
+ const MAX_FOLDERS = 5000;
61
+
50
62
  type Status = 'loading' | 'ready' | 'error';
51
63
  interface Children {
52
64
  status: Status;
53
65
  folders: FileEntry[];
66
+ /** More than MAX_FOLDERS subfolders: only the first are listed. */
67
+ more?: boolean;
54
68
  }
55
69
 
56
70
  /** `${rootId}|${path}` → its subfolders (`|` can't occur in SMB names). */
@@ -71,15 +85,20 @@
71
85
  let focusedKey = $state<string>();
72
86
  let tree = $state<HTMLElement>();
73
87
 
74
- /** Load every subfolder (directories come first, so stop at the first file). */
75
- async function load(r: string, p: string) {
88
+ /**
89
+ * Load every subfolder (sources list directories first, so stop at the first
90
+ * file). `force` reloads a loaded folder, keeping its folders on screen.
91
+ */
92
+ async function load(r: string, p: string, force = false) {
76
93
  const key = keyOf(r, p);
77
94
  const state = children.get(key);
78
- if (state && state.status !== 'error') return;
95
+ if (!force && state && state.status !== 'error') return;
96
+ controllers.get(key)?.abort();
79
97
  const controller = new AbortController();
80
98
  controllers.set(key, controller);
81
99
  children.set(key, { status: 'loading', folders: state?.folders ?? [] });
82
100
  const folders: FileEntry[] = [];
101
+ let more = false;
83
102
  try {
84
103
  let cursor: string | undefined;
85
104
  for (;;) {
@@ -92,26 +111,66 @@
92
111
  const dirs = page.entries.filter((e) => e.type === 'dir');
93
112
  folders.push(...dirs);
94
113
  if (!page.nextCursor || dirs.length < page.entries.length) break;
114
+ if (folders.length >= MAX_FOLDERS) {
115
+ more = true;
116
+ break;
117
+ }
95
118
  cursor = page.nextCursor;
96
119
  }
97
- if (!controller.signal.aborted) children.set(key, { status: 'ready', folders });
120
+ if (!controller.signal.aborted) children.set(key, { status: 'ready', folders, more });
98
121
  } catch (err) {
99
122
  if (controller.signal.aborted || isAbortError(err)) return;
100
123
  children.set(key, { status: 'error', folders: [] });
124
+ onerror?.(err);
101
125
  } finally {
102
126
  if (controllers.get(key) === controller) controllers.delete(key);
103
127
  }
104
128
  }
105
129
 
106
- // Start over when the source or the hidden-files setting changes.
130
+ /** (Re)load every expanded folder of a drive that is still listed. */
131
+ function loadExpanded(force: boolean) {
132
+ for (const key of expanded) {
133
+ const i = key.indexOf('|');
134
+ const r = key.slice(0, i);
135
+ if (roots.some((root) => root.id === r)) void load(r, key.slice(i + 1), force);
136
+ }
137
+ }
138
+
139
+ function abortAll() {
140
+ for (const c of controllers.values()) c.abort();
141
+ controllers.clear();
142
+ }
143
+
144
+ // Effects below compare values: a parent that spreads a props object reruns
145
+ // every prop read on any change, and that must not wipe the cache.
146
+ const UNSET = Symbol();
147
+ let loadedFor: { source: FilesSource; showHidden: boolean } | undefined;
148
+ let lastRefresh: unknown = UNSET;
149
+
150
+ // Start over when the source or the hidden-files setting changes, reloading
151
+ // what was expanded (otherwise those folders would wait forever).
107
152
  $effect(() => {
108
- void source;
109
- void showHidden;
110
- return () => {
111
- for (const c of controllers.values()) c.abort();
112
- controllers.clear();
113
- children.clear();
114
- };
153
+ const src = source;
154
+ const hidden = showHidden;
155
+ untrack(() => {
156
+ if (loadedFor?.source === src && loadedFor.showHidden === hidden) return;
157
+ if (loadedFor) {
158
+ abortAll();
159
+ children.clear();
160
+ }
161
+ loadedFor = { source: src, showHidden: hidden };
162
+ loadExpanded(false);
163
+ });
164
+ });
165
+ $effect(() => abortAll);
166
+
167
+ // Refresh: reload the expanded folders in place (no "Loading…" flash).
168
+ $effect(() => {
169
+ const key = refreshKey;
170
+ untrack(() => {
171
+ if (lastRefresh !== UNSET && key !== lastRefresh) loadExpanded(true);
172
+ lastRefresh = key;
173
+ });
115
174
  });
116
175
 
117
176
  // Reveal the open folder: expand its drive and every ancestor, and load them.
@@ -144,10 +203,12 @@
144
203
  hidden: boolean;
145
204
  setsize: number;
146
205
  posinset: number;
206
+ /** A failed folder's "try again" row: a treeitem, so the keyboard reaches it. */
207
+ retry?: () => void;
147
208
  }
148
209
  type Row =
149
210
  | { kind: 'item'; item: Item }
150
- | { kind: 'note'; key: string; level: number; text: string; retry?: () => void };
211
+ | { kind: 'note'; key: string; level: number; text: string };
151
212
 
152
213
  const rows = $derived.by<Row[]>(() => {
153
214
  const out: Row[] = [];
@@ -159,11 +220,19 @@
159
220
  }
160
221
  if (state.status === 'error') {
161
222
  out.push({
162
- kind: 'note',
163
- key: `${keyOf(r, p)}:error`,
164
- level,
165
- text: "Couldn't open. Try again",
166
- retry: () => void load(r, p),
223
+ kind: 'item',
224
+ item: {
225
+ key: `${keyOf(r, p)}:error`,
226
+ rootId: r,
227
+ path: p,
228
+ name: "Couldn't open this folder. Try again",
229
+ level,
230
+ isRoot: false,
231
+ hidden: false,
232
+ setsize: 1,
233
+ posinset: 1,
234
+ retry: () => void load(r, p),
235
+ },
167
236
  });
168
237
  return;
169
238
  }
@@ -185,6 +254,14 @@
185
254
  });
186
255
  if (expanded.has(key)) walk(r, f.path, level + 1);
187
256
  });
257
+ if (state.more) {
258
+ out.push({
259
+ kind: 'note',
260
+ key: `${keyOf(r, p)}:more`,
261
+ level,
262
+ text: `Only the first ${state.folders.length.toLocaleString()} folders are listed. Open the folder to see them all.`,
263
+ });
264
+ }
188
265
  };
189
266
  roots.forEach((root, i) => {
190
267
  const key = keyOf(root.id, '');
@@ -214,8 +291,9 @@
214
291
  : (items.find((i) => i.key === currentKey) ?? items[0])?.key
215
292
  );
216
293
 
217
- /** Known to have no subfolders (loaded and empty). */
294
+ /** Known to have no subfolders (loaded and empty), or a retry row. */
218
295
  function isLeaf(item: Item): boolean {
296
+ if (item.retry) return true;
219
297
  const state = children.get(item.key);
220
298
  return state?.status === 'ready' && state.folders.length === 0;
221
299
  }
@@ -231,6 +309,13 @@
231
309
  }
232
310
 
233
311
  function choose(item: Item) {
312
+ if (item.retry) {
313
+ // The row turns into "Loading…": keep focus on its folder.
314
+ const parent = items.find((i) => i.key === keyOf(item.rootId, item.path));
315
+ void focusItem(parent);
316
+ item.retry();
317
+ return;
318
+ }
234
319
  focusedKey = item.key;
235
320
  onnavigate?.(item.rootId, item.path);
236
321
  }
@@ -331,16 +416,7 @@
331
416
  style:padding-left="{row.level * 0.875 + 0.5}rem"
332
417
  class="py-1 text-xs text-muted-foreground"
333
418
  >
334
- {#if row.retry}
335
- <button
336
- type="button"
337
- class="underline hover:text-foreground"
338
- tabindex="-1"
339
- onclick={row.retry}>{row.text}</button
340
- >
341
- {:else}
342
- {row.text}
343
- {/if}
419
+ {row.text}
344
420
  </li>
345
421
  {:else}
346
422
  {@const item = row.item}
@@ -361,7 +437,8 @@
361
437
  'flex cursor-pointer items-center gap-1 rounded-sm py-1 pr-2 outline-none',
362
438
  'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring',
363
439
  current ? 'bg-chart-1/15 font-medium text-foreground' : 'hover:bg-muted/60',
364
- item.hidden && 'opacity-70'
440
+ // Hidden folders: muted and italic, not faded (opacity fails contrast).
441
+ item.hidden && 'italic text-muted-foreground'
365
442
  )}
366
443
  style:padding-left="{(item.level - 1) * 0.875 + 0.25}rem"
367
444
  title={item.name}
@@ -384,12 +461,16 @@
384
461
  >
385
462
  <Icon name={open ? 'chevron-down' : 'chevron-right'} size={14} />
386
463
  </span>
387
- {#if item.isRoot}
464
+ {#if item.retry}
465
+ <Icon name="refresh" size={14} class="shrink-0 text-muted-foreground" />
466
+ {:else if item.isRoot}
388
467
  <Icon name="home" size={16} class="shrink-0 text-muted-foreground" />
389
468
  {:else}
390
469
  <FileIcon type="dir" size={16} />
391
470
  {/if}
392
- <span class="truncate">{item.name}</span>
471
+ <span class={cn('truncate', item.retry && 'text-xs text-muted-foreground underline')}
472
+ >{item.name}</span
473
+ >
393
474
  </li>
394
475
  {/if}
395
476
  {/each}
@@ -11,6 +11,13 @@ interface Props {
11
11
  showHidden?: boolean;
12
12
  /** A folder (or a drive, with `path` `''`) was chosen. */
13
13
  onnavigate?: (rootId: string, path: string) => void;
14
+ /**
15
+ * Change it to reload the expanded folders in place (e.g. after a refresh
16
+ * or a write). FileBrowser passes its refresh counter.
17
+ */
18
+ refreshKey?: unknown;
19
+ /** A folder failed to load (a 401 included), for logging or re-login. */
20
+ onerror?: (error: unknown) => void;
14
21
  /** Accessible name of the tree. Default "Folders". */
15
22
  label?: string;
16
23
  class?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/files-svelte",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Svelte network-drive file browser for Classic Theme: FileBrowser plus FileList, FileBreadcrumbs, FileIcon, FilePreview and FileDetails, with previews through the document viewers",
5
5
  "type": "module",
6
6
  "svelte": "./dist/lib/index.js",