@classic-homes/files-svelte 0.0.0-stage → 0.2.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.
- package/README.md +29 -2
- package/dist/lib/FileBreadcrumbs.svelte +103 -0
- package/dist/lib/FileBreadcrumbs.svelte.d.ts +16 -0
- package/dist/lib/FileBrowser.svelte +706 -0
- package/dist/lib/FileBrowser.svelte.d.ts +58 -0
- package/dist/lib/FileDetails.svelte +68 -0
- package/dist/lib/FileDetails.svelte.d.ts +11 -0
- package/dist/lib/FileErrorState.svelte +73 -0
- package/dist/lib/FileErrorState.svelte.d.ts +14 -0
- package/dist/lib/FileIcon.svelte +48 -0
- package/dist/lib/FileIcon.svelte.d.ts +17 -0
- package/dist/lib/FileList.svelte +393 -0
- package/dist/lib/FileList.svelte.d.ts +51 -0
- package/dist/lib/FilePreview.svelte +219 -0
- package/dist/lib/FilePreview.svelte.d.ts +26 -0
- package/dist/lib/FileTree.svelte +396 -0
- package/dist/lib/FileTree.svelte.d.ts +25 -0
- package/dist/lib/index.d.ts +11 -0
- package/dist/lib/index.js +11 -0
- package/dist/lib/types.d.ts +13 -0
- package/dist/lib/types.js +1 -0
- package/dist/lib/utils.d.ts +13 -0
- package/dist/lib/utils.js +28 -0
- package/package.json +63 -4
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import { tick, untrack } from 'svelte';
|
|
4
|
+
import { MediaQuery } from 'svelte/reactivity';
|
|
5
|
+
import {
|
|
6
|
+
Alert,
|
|
7
|
+
Button,
|
|
8
|
+
Dialog,
|
|
9
|
+
EmptyState,
|
|
10
|
+
Icon,
|
|
11
|
+
LiveRegion,
|
|
12
|
+
ResizablePanes,
|
|
13
|
+
Select,
|
|
14
|
+
Sheet,
|
|
15
|
+
Switch,
|
|
16
|
+
} from '@classic-homes/theme-svelte';
|
|
17
|
+
import type { ViewerRegistry } from '@classic-homes/viewer-svelte';
|
|
18
|
+
import {
|
|
19
|
+
describeFilesError,
|
|
20
|
+
filterEntries,
|
|
21
|
+
isAbortError,
|
|
22
|
+
isFilesError,
|
|
23
|
+
normalizePath,
|
|
24
|
+
parentPath,
|
|
25
|
+
writeCapabilities,
|
|
26
|
+
type FileEntry,
|
|
27
|
+
type FileRoot,
|
|
28
|
+
type FileSort,
|
|
29
|
+
type FilesSource,
|
|
30
|
+
} from '@classic-homes/files-core';
|
|
31
|
+
import FileBreadcrumbs from './FileBreadcrumbs.svelte';
|
|
32
|
+
import FileErrorState from './FileErrorState.svelte';
|
|
33
|
+
import FileList from './FileList.svelte';
|
|
34
|
+
import FilePreview from './FilePreview.svelte';
|
|
35
|
+
import FileTree from './FileTree.svelte';
|
|
36
|
+
import type { FileBrowserContext } from './types.js';
|
|
37
|
+
import { cn, isTextInput } from './utils.js';
|
|
38
|
+
|
|
39
|
+
interface Props {
|
|
40
|
+
/** Where the data comes from: `createChapiFilesSource()`, `createMemoryFilesSource()` or your own. */
|
|
41
|
+
source: FilesSource;
|
|
42
|
+
/** Current root. Bindable; defaults to the first root the caller can read. */
|
|
43
|
+
rootId?: string;
|
|
44
|
+
/** Current folder, root-relative (`''` = the root). Bindable. */
|
|
45
|
+
path?: string;
|
|
46
|
+
/** The user picked another root. Put it in the URL to make Back/Forward work. */
|
|
47
|
+
onrootchange?: (rootId: string) => void;
|
|
48
|
+
/** The user opened another folder. */
|
|
49
|
+
onpathchange?: (path: string) => void;
|
|
50
|
+
/** Document viewers for previews, as lazy imports keyed by format. */
|
|
51
|
+
viewers?: ViewerRegistry;
|
|
52
|
+
/** Entries per page. Default 200. */
|
|
53
|
+
pageSize?: number;
|
|
54
|
+
/** Load the next page on scroll instead of only with "Load more". Default false. */
|
|
55
|
+
autoLoad?: boolean;
|
|
56
|
+
/** "Show hidden files". Bindable. */
|
|
57
|
+
showHidden?: boolean;
|
|
58
|
+
/** Filter text. Bindable. Narrows the loaded entries (or searches, when the source can). */
|
|
59
|
+
filter?: string;
|
|
60
|
+
/** Larger files are not fetched for preview. Default 100 MB. */
|
|
61
|
+
maxPreviewBytes?: number;
|
|
62
|
+
/** A request got a 401: hand it to the app's auth (re-login). */
|
|
63
|
+
onunauthorized?: (error: unknown) => void;
|
|
64
|
+
/** Any listing or preview error, for logging. */
|
|
65
|
+
onerror?: (error: unknown) => void;
|
|
66
|
+
/** A file was opened for preview. */
|
|
67
|
+
onpreview?: (entry: FileEntry) => void;
|
|
68
|
+
/**
|
|
69
|
+
* Write toolbar (upload, new folder…). Rendered only when the source has a
|
|
70
|
+
* write method AND the root's `canWrite` is true. The pilot shows nothing.
|
|
71
|
+
*/
|
|
72
|
+
toolbar?: Snippet<[FileBrowserContext]>;
|
|
73
|
+
/** Per-row write actions, under the same rule as `toolbar`. */
|
|
74
|
+
rowActions?: Snippet<[FileEntry, FileBrowserContext]>;
|
|
75
|
+
/**
|
|
76
|
+
* Explorer-style folder tree beside the list (drives at the top, the open
|
|
77
|
+
* folder's parents expanded). On phones it opens from a "Folders" button.
|
|
78
|
+
* Default true.
|
|
79
|
+
*/
|
|
80
|
+
navigationPane?: boolean;
|
|
81
|
+
/** Starting width of the folder pane, in percent. Default 25. */
|
|
82
|
+
paneSize?: number;
|
|
83
|
+
/** Remember the pane width in localStorage under this key. */
|
|
84
|
+
paneKey?: string;
|
|
85
|
+
/** Accessible name of the browser region. Default "File browser". */
|
|
86
|
+
label?: string;
|
|
87
|
+
locale?: string | string[];
|
|
88
|
+
class?: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let {
|
|
92
|
+
source,
|
|
93
|
+
rootId = $bindable(),
|
|
94
|
+
path = $bindable(''),
|
|
95
|
+
onrootchange,
|
|
96
|
+
onpathchange,
|
|
97
|
+
viewers = {},
|
|
98
|
+
pageSize = 200,
|
|
99
|
+
autoLoad = false,
|
|
100
|
+
showHidden = $bindable(false),
|
|
101
|
+
filter = $bindable(''),
|
|
102
|
+
maxPreviewBytes,
|
|
103
|
+
onunauthorized,
|
|
104
|
+
onerror,
|
|
105
|
+
onpreview,
|
|
106
|
+
toolbar,
|
|
107
|
+
rowActions,
|
|
108
|
+
navigationPane = true,
|
|
109
|
+
paneSize = $bindable(25),
|
|
110
|
+
paneKey,
|
|
111
|
+
label = 'File browser',
|
|
112
|
+
locale,
|
|
113
|
+
class: className,
|
|
114
|
+
}: Props = $props();
|
|
115
|
+
|
|
116
|
+
const uid = $props.id();
|
|
117
|
+
// The folder pane sits beside the list from md up; phones get a sheet instead.
|
|
118
|
+
const wide = new MediaQuery('(min-width: 768px)', false);
|
|
119
|
+
let foldersOpen = $state(false);
|
|
120
|
+
const filterId = `${uid}-filter`;
|
|
121
|
+
const hiddenId = `${uid}-hidden`;
|
|
122
|
+
|
|
123
|
+
// Roots
|
|
124
|
+
let roots = $state.raw<FileRoot[]>([]);
|
|
125
|
+
let rootsStatus = $state<'loading' | 'ready' | 'error'>('loading');
|
|
126
|
+
let rootsError = $state.raw<unknown>();
|
|
127
|
+
let rootsAttempt = $state(0);
|
|
128
|
+
|
|
129
|
+
// Listing
|
|
130
|
+
let entries = $state.raw<FileEntry[]>([]);
|
|
131
|
+
let total = $state<number>();
|
|
132
|
+
let nextCursor = $state<string | null>(null);
|
|
133
|
+
let status = $state<'idle' | 'loading' | 'ready' | 'error'>('idle');
|
|
134
|
+
let listError = $state.raw<unknown>();
|
|
135
|
+
let loadingMore = $state(false);
|
|
136
|
+
let moreError = $state.raw<unknown>();
|
|
137
|
+
let attempt = $state(0);
|
|
138
|
+
let listController: AbortController | undefined;
|
|
139
|
+
|
|
140
|
+
// Server search (only when the source offers it)
|
|
141
|
+
let searchResults = $state.raw<FileEntry[] | null>(null);
|
|
142
|
+
let searchStatus = $state<'idle' | 'loading' | 'ready' | 'error'>('idle');
|
|
143
|
+
|
|
144
|
+
let selected = $state<string>();
|
|
145
|
+
let sort = $state<FileSort | null>(null);
|
|
146
|
+
let previewEntry = $state.raw<FileEntry>();
|
|
147
|
+
let previewOpen = $state(false);
|
|
148
|
+
let announcement = $state('');
|
|
149
|
+
let online = $state(typeof navigator === 'undefined' ? true : navigator.onLine !== false);
|
|
150
|
+
let list = $state<FileList>();
|
|
151
|
+
/** Focus the list once the next listing lands (keyboard navigation). */
|
|
152
|
+
let focusAfterLoad = false;
|
|
153
|
+
|
|
154
|
+
const currentPath = $derived.by(() => {
|
|
155
|
+
try {
|
|
156
|
+
return normalizePath(path);
|
|
157
|
+
} catch {
|
|
158
|
+
return path ?? '';
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
const root = $derived(roots.find((r) => r.id === rootId));
|
|
162
|
+
const rootLabel = $derived(root?.label ?? '');
|
|
163
|
+
const caps = $derived(writeCapabilities(source, root));
|
|
164
|
+
const folderName = $derived(
|
|
165
|
+
currentPath ? currentPath.slice(currentPath.lastIndexOf('/') + 1) : rootLabel
|
|
166
|
+
);
|
|
167
|
+
const searching = $derived(!!source.search && filter.trim() !== '');
|
|
168
|
+
const shown = $derived(searching ? (searchResults ?? []) : filterEntries(entries, filter));
|
|
169
|
+
const hasMore = $derived(!searching && !!nextCursor);
|
|
170
|
+
const nf = $derived(new Intl.NumberFormat(locale));
|
|
171
|
+
|
|
172
|
+
const context: FileBrowserContext = {
|
|
173
|
+
get source() {
|
|
174
|
+
return source;
|
|
175
|
+
},
|
|
176
|
+
get root() {
|
|
177
|
+
return root;
|
|
178
|
+
},
|
|
179
|
+
get path() {
|
|
180
|
+
return currentPath;
|
|
181
|
+
},
|
|
182
|
+
get capabilities() {
|
|
183
|
+
return caps;
|
|
184
|
+
},
|
|
185
|
+
refresh: () => refresh(),
|
|
186
|
+
navigate: (p: string) => navigate(p),
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
function report(err: unknown) {
|
|
190
|
+
onerror?.(err);
|
|
191
|
+
if (isFilesError(err) && err.code === 'unauthorized') onunauthorized?.(err);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Load roots whenever the source changes.
|
|
195
|
+
$effect(() => {
|
|
196
|
+
const src = source;
|
|
197
|
+
void rootsAttempt;
|
|
198
|
+
const controller = new AbortController();
|
|
199
|
+
untrack(() => {
|
|
200
|
+
rootsStatus = 'loading';
|
|
201
|
+
rootsError = undefined;
|
|
202
|
+
});
|
|
203
|
+
src.listRoots(controller.signal).then(
|
|
204
|
+
(list) => {
|
|
205
|
+
if (controller.signal.aborted) return;
|
|
206
|
+
roots = list;
|
|
207
|
+
rootsStatus = 'ready';
|
|
208
|
+
const current = untrack(() => rootId);
|
|
209
|
+
if (list.length && !list.some((r) => r.id === current)) {
|
|
210
|
+
// Unknown or missing root: start on the first one the caller can read.
|
|
211
|
+
rootId = list[0].id;
|
|
212
|
+
if (current !== undefined) path = '';
|
|
213
|
+
onrootchange?.(list[0].id);
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
(err) => {
|
|
217
|
+
if (controller.signal.aborted || isAbortError(err)) return;
|
|
218
|
+
rootsError = err;
|
|
219
|
+
rootsStatus = 'error';
|
|
220
|
+
report(err);
|
|
221
|
+
}
|
|
222
|
+
);
|
|
223
|
+
return () => controller.abort();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// List the current folder.
|
|
227
|
+
$effect(() => {
|
|
228
|
+
const src = source;
|
|
229
|
+
const id = rootId;
|
|
230
|
+
const folder = currentPath;
|
|
231
|
+
const includeHidden = showHidden;
|
|
232
|
+
const limit = pageSize;
|
|
233
|
+
const ready = rootsStatus === 'ready' && roots.some((r) => r.id === id);
|
|
234
|
+
void attempt;
|
|
235
|
+
if (!ready || id === undefined) return;
|
|
236
|
+
|
|
237
|
+
listController?.abort();
|
|
238
|
+
const controller = new AbortController();
|
|
239
|
+
listController = controller;
|
|
240
|
+
untrack(() => {
|
|
241
|
+
status = 'loading';
|
|
242
|
+
listError = undefined;
|
|
243
|
+
moreError = undefined;
|
|
244
|
+
loadingMore = false;
|
|
245
|
+
entries = [];
|
|
246
|
+
total = undefined;
|
|
247
|
+
nextCursor = null;
|
|
248
|
+
announcement = `Loading ${folderName}…`;
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
src.list(id, folder, { limit, includeHidden, signal: controller.signal }).then(
|
|
252
|
+
async (page) => {
|
|
253
|
+
if (controller.signal.aborted) return;
|
|
254
|
+
entries = page.entries;
|
|
255
|
+
total = page.total;
|
|
256
|
+
nextCursor = page.nextCursor ?? null;
|
|
257
|
+
status = 'ready';
|
|
258
|
+
announce();
|
|
259
|
+
if (focusAfterLoad) {
|
|
260
|
+
focusAfterLoad = false;
|
|
261
|
+
await tick();
|
|
262
|
+
list?.focus();
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
(err) => {
|
|
266
|
+
if (controller.signal.aborted || isAbortError(err)) return;
|
|
267
|
+
listError = err;
|
|
268
|
+
status = 'error';
|
|
269
|
+
announcement = describeFilesError(err).title;
|
|
270
|
+
report(err);
|
|
271
|
+
}
|
|
272
|
+
);
|
|
273
|
+
return () => controller.abort();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
function announce() {
|
|
277
|
+
const count = total ?? entries.length;
|
|
278
|
+
announcement =
|
|
279
|
+
count === 0
|
|
280
|
+
? `${folderName} is empty`
|
|
281
|
+
: `${folderName}: ${nf.format(count)} item${count === 1 ? '' : 's'}${
|
|
282
|
+
nextCursor ? `, showing ${nf.format(entries.length)}` : ''
|
|
283
|
+
}`;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async function loadMore() {
|
|
287
|
+
if (!nextCursor || loadingMore || rootId === undefined) return;
|
|
288
|
+
const controller = listController;
|
|
289
|
+
const id = rootId;
|
|
290
|
+
loadingMore = true;
|
|
291
|
+
moreError = undefined;
|
|
292
|
+
try {
|
|
293
|
+
const page = await source.list(id, currentPath, {
|
|
294
|
+
cursor: nextCursor,
|
|
295
|
+
limit: pageSize,
|
|
296
|
+
includeHidden: showHidden,
|
|
297
|
+
signal: controller?.signal,
|
|
298
|
+
});
|
|
299
|
+
if (controller?.signal.aborted) return;
|
|
300
|
+
const seen = new Set(entries.map((e) => e.path));
|
|
301
|
+
entries = [...entries, ...page.entries.filter((e) => !seen.has(e.path))];
|
|
302
|
+
total = page.total ?? total;
|
|
303
|
+
nextCursor = page.nextCursor ?? null;
|
|
304
|
+
announce();
|
|
305
|
+
} catch (err) {
|
|
306
|
+
if (controller?.signal.aborted || isAbortError(err)) return;
|
|
307
|
+
moreError = err;
|
|
308
|
+
report(err);
|
|
309
|
+
} finally {
|
|
310
|
+
if (!controller?.signal.aborted) loadingMore = false;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Server search seam: debounced, only when the source implements search().
|
|
315
|
+
$effect(() => {
|
|
316
|
+
const query = filter.trim();
|
|
317
|
+
const src = source;
|
|
318
|
+
const id = rootId;
|
|
319
|
+
const folder = currentPath;
|
|
320
|
+
const includeHidden = showHidden;
|
|
321
|
+
if (!src.search || !query || id === undefined) {
|
|
322
|
+
searchResults = null;
|
|
323
|
+
searchStatus = 'idle';
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const controller = new AbortController();
|
|
327
|
+
const timer = setTimeout(() => {
|
|
328
|
+
searchStatus = 'loading';
|
|
329
|
+
src.search!(id, query, {
|
|
330
|
+
path: folder,
|
|
331
|
+
includeHidden,
|
|
332
|
+
limit: pageSize,
|
|
333
|
+
signal: controller.signal,
|
|
334
|
+
}).then(
|
|
335
|
+
(page) => {
|
|
336
|
+
if (controller.signal.aborted) return;
|
|
337
|
+
searchResults = page.entries;
|
|
338
|
+
searchStatus = 'ready';
|
|
339
|
+
announcement = `${nf.format(page.total ?? page.entries.length)} results for “${query}”`;
|
|
340
|
+
},
|
|
341
|
+
(err) => {
|
|
342
|
+
if (controller.signal.aborted || isAbortError(err)) return;
|
|
343
|
+
searchStatus = 'error';
|
|
344
|
+
searchResults = [];
|
|
345
|
+
report(err);
|
|
346
|
+
}
|
|
347
|
+
);
|
|
348
|
+
}, 300);
|
|
349
|
+
return () => {
|
|
350
|
+
clearTimeout(timer);
|
|
351
|
+
controller.abort();
|
|
352
|
+
};
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
function navigate(next: string, focusChild?: string) {
|
|
356
|
+
let target: string;
|
|
357
|
+
try {
|
|
358
|
+
target = normalizePath(next);
|
|
359
|
+
} catch {
|
|
360
|
+
target = '';
|
|
361
|
+
}
|
|
362
|
+
filter = '';
|
|
363
|
+
sort = null;
|
|
364
|
+
selected = focusChild;
|
|
365
|
+
if (target === currentPath) {
|
|
366
|
+
attempt++;
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
path = target;
|
|
370
|
+
onpathchange?.(target);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function goUp() {
|
|
374
|
+
if (!currentPath) return;
|
|
375
|
+
focusAfterLoad = true;
|
|
376
|
+
navigate(parentPath(currentPath), currentPath);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function changeRoot(id: string, nextPath = '') {
|
|
380
|
+
if (id === rootId) return;
|
|
381
|
+
rootId = id;
|
|
382
|
+
filter = '';
|
|
383
|
+
sort = null;
|
|
384
|
+
selected = undefined;
|
|
385
|
+
onrootchange?.(id);
|
|
386
|
+
if (currentPath !== nextPath) {
|
|
387
|
+
path = nextPath;
|
|
388
|
+
onpathchange?.(nextPath);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** From the folder tree: a folder in this drive, or another drive (and folder). */
|
|
393
|
+
function treeNavigate(id: string, next: string) {
|
|
394
|
+
foldersOpen = false;
|
|
395
|
+
if (id !== rootId) changeRoot(id, next);
|
|
396
|
+
else navigate(next);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function refresh() {
|
|
400
|
+
attempt++;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function open(entry: FileEntry) {
|
|
404
|
+
if (entry.type === 'dir') {
|
|
405
|
+
focusAfterLoad = true;
|
|
406
|
+
navigate(entry.path);
|
|
407
|
+
} else if (entry.type === 'file') {
|
|
408
|
+
previewEntry = entry;
|
|
409
|
+
previewOpen = true;
|
|
410
|
+
onpreview?.(entry);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function onkeydown(event: KeyboardEvent) {
|
|
415
|
+
if (event.defaultPrevented) return;
|
|
416
|
+
if (previewOpen) return;
|
|
417
|
+
const typing = isTextInput(event.target);
|
|
418
|
+
if (event.key === '/' && !typing && !event.ctrlKey && !event.metaKey && !event.altKey) {
|
|
419
|
+
event.preventDefault();
|
|
420
|
+
document.getElementById(filterId)?.focus();
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if ((event.key === 'ArrowUp' && event.altKey) || (event.key === 'Backspace' && !typing)) {
|
|
424
|
+
if (!currentPath) return;
|
|
425
|
+
event.preventDefault();
|
|
426
|
+
goUp();
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (
|
|
430
|
+
event.key === 'Escape' &&
|
|
431
|
+
typing &&
|
|
432
|
+
event.target instanceof HTMLInputElement &&
|
|
433
|
+
event.target.id === filterId
|
|
434
|
+
) {
|
|
435
|
+
if (filter) {
|
|
436
|
+
event.preventDefault();
|
|
437
|
+
filter = '';
|
|
438
|
+
}
|
|
439
|
+
list?.focus();
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Offline banner; retry a failed listing when the connection comes back.
|
|
444
|
+
$effect(() => {
|
|
445
|
+
if (typeof window === 'undefined') return;
|
|
446
|
+
const up = () => {
|
|
447
|
+
online = true;
|
|
448
|
+
if (status === 'error' && isFilesError(listError) && listError.code === 'network') attempt++;
|
|
449
|
+
};
|
|
450
|
+
const down = () => (online = false);
|
|
451
|
+
window.addEventListener('online', up);
|
|
452
|
+
window.addEventListener('offline', down);
|
|
453
|
+
return () => {
|
|
454
|
+
window.removeEventListener('online', up);
|
|
455
|
+
window.removeEventListener('offline', down);
|
|
456
|
+
};
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
const showPane = $derived(navigationPane && wide.current && roots.length > 0);
|
|
460
|
+
const rootOptions = $derived(roots.map((r) => ({ value: r.id, label: r.label })));
|
|
461
|
+
const listLabel = $derived(`${folderName} contents`);
|
|
462
|
+
|
|
463
|
+
const countText = $derived.by(() => {
|
|
464
|
+
if (status !== 'ready') return '';
|
|
465
|
+
const loaded = entries.length;
|
|
466
|
+
const all = total ?? loaded;
|
|
467
|
+
if (searching) {
|
|
468
|
+
return searchStatus === 'loading'
|
|
469
|
+
? 'Searching…'
|
|
470
|
+
: `${nf.format(shown.length)} result${shown.length === 1 ? '' : 's'} in ${folderName}`;
|
|
471
|
+
}
|
|
472
|
+
if (filter.trim()) {
|
|
473
|
+
return `${nf.format(shown.length)} of ${nf.format(loaded)} loaded item${loaded === 1 ? '' : 's'} match${
|
|
474
|
+
hasMore ? ' (load more to filter the rest)' : ''
|
|
475
|
+
}`;
|
|
476
|
+
}
|
|
477
|
+
return loaded < all
|
|
478
|
+
? `Showing ${nf.format(loaded)} of ${nf.format(all)}`
|
|
479
|
+
: `${nf.format(all)} item${all === 1 ? '' : 's'}`;
|
|
480
|
+
});
|
|
481
|
+
</script>
|
|
482
|
+
|
|
483
|
+
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
|
|
484
|
+
<section
|
|
485
|
+
aria-label={label}
|
|
486
|
+
class={cn('flex min-h-0 flex-col rounded-lg border border-border bg-background', className)}
|
|
487
|
+
{onkeydown}
|
|
488
|
+
data-file-browser
|
|
489
|
+
>
|
|
490
|
+
{#if !online}
|
|
491
|
+
<Alert variant="warning" class="m-3 mb-0" data-file-browser-offline>
|
|
492
|
+
You're offline. The network drive needs a connection; this list may be out of date.
|
|
493
|
+
</Alert>
|
|
494
|
+
{/if}
|
|
495
|
+
|
|
496
|
+
{#if rootsStatus === 'error'}
|
|
497
|
+
<FileErrorState error={rootsError} onretry={() => rootsAttempt++} class="py-12" />
|
|
498
|
+
{:else if rootsStatus === 'ready' && roots.length === 0}
|
|
499
|
+
<EmptyState
|
|
500
|
+
title="No network drives"
|
|
501
|
+
description="You don't have access to any shared drives yet. Ask IT if you need one."
|
|
502
|
+
icon="folder-minus"
|
|
503
|
+
class="py-12"
|
|
504
|
+
/>
|
|
505
|
+
{:else}
|
|
506
|
+
<div class="flex flex-col gap-3 border-b border-border p-3">
|
|
507
|
+
<div class="flex flex-col gap-2 sm:flex-row sm:items-center">
|
|
508
|
+
{#if roots.length > 1 && !showPane}
|
|
509
|
+
<div class="sm:w-64">
|
|
510
|
+
<Select
|
|
511
|
+
label="Drive"
|
|
512
|
+
value={rootId}
|
|
513
|
+
options={rootOptions}
|
|
514
|
+
onValueChange={changeRoot}
|
|
515
|
+
hint={root?.description ?? undefined}
|
|
516
|
+
/>
|
|
517
|
+
</div>
|
|
518
|
+
{/if}
|
|
519
|
+
{#if root}
|
|
520
|
+
<FileBreadcrumbs
|
|
521
|
+
rootLabel={root.label}
|
|
522
|
+
path={currentPath}
|
|
523
|
+
onnavigate={(p) => navigate(p)}
|
|
524
|
+
class="min-w-0 flex-1"
|
|
525
|
+
/>
|
|
526
|
+
{/if}
|
|
527
|
+
</div>
|
|
528
|
+
{#if root?.description && (roots.length === 1 || showPane)}
|
|
529
|
+
<p class="-mt-1 text-xs text-muted-foreground">{root.description}</p>
|
|
530
|
+
{/if}
|
|
531
|
+
|
|
532
|
+
<div class="flex flex-wrap items-center gap-x-4 gap-y-2">
|
|
533
|
+
<div class="relative min-w-[12rem] flex-1">
|
|
534
|
+
<label for={filterId} class="sr-only"
|
|
535
|
+
>{source.search ? 'Search files' : 'Filter files in this folder'}</label
|
|
536
|
+
>
|
|
537
|
+
<Icon
|
|
538
|
+
name={source.search ? 'search' : 'filter'}
|
|
539
|
+
size={16}
|
|
540
|
+
class="pointer-events-none absolute left-2.5 top-1/2 -translate-y-1/2 text-muted-foreground"
|
|
541
|
+
/>
|
|
542
|
+
<input
|
|
543
|
+
id={filterId}
|
|
544
|
+
type="search"
|
|
545
|
+
bind:value={filter}
|
|
546
|
+
placeholder={source.search ? 'Search files' : 'Filter this folder'}
|
|
547
|
+
autocomplete="off"
|
|
548
|
+
aria-keyshortcuts="/"
|
|
549
|
+
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"
|
|
550
|
+
/>
|
|
551
|
+
<kbd
|
|
552
|
+
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"
|
|
553
|
+
aria-hidden="true">/</kbd
|
|
554
|
+
>
|
|
555
|
+
</div>
|
|
556
|
+
<div class="flex items-center gap-2">
|
|
557
|
+
<Switch id={hiddenId} checked={showHidden} onchange={(v) => (showHidden = v)} />
|
|
558
|
+
<label for={hiddenId} class="text-sm">Show hidden files</label>
|
|
559
|
+
</div>
|
|
560
|
+
{#if toolbar && caps.any}
|
|
561
|
+
<div class="flex items-center gap-2" data-file-browser-toolbar>
|
|
562
|
+
{@render toolbar(context)}
|
|
563
|
+
</div>
|
|
564
|
+
{/if}
|
|
565
|
+
{#if navigationPane && !wide.current && roots.length > 0}
|
|
566
|
+
<Button
|
|
567
|
+
variant="outline"
|
|
568
|
+
size="sm"
|
|
569
|
+
onclick={() => (foldersOpen = true)}
|
|
570
|
+
aria-haspopup="dialog"
|
|
571
|
+
>
|
|
572
|
+
<Icon name="sidebar" size={16} />
|
|
573
|
+
Folders
|
|
574
|
+
</Button>
|
|
575
|
+
{/if}
|
|
576
|
+
<Button variant="ghost" size="icon" onclick={refresh} aria-label="Refresh">
|
|
577
|
+
<Icon name="refresh" size={16} />
|
|
578
|
+
</Button>
|
|
579
|
+
</div>
|
|
580
|
+
</div>
|
|
581
|
+
|
|
582
|
+
{#if showPane}
|
|
583
|
+
<ResizablePanes
|
|
584
|
+
bind:size={paneSize}
|
|
585
|
+
min={12}
|
|
586
|
+
max={50}
|
|
587
|
+
persistKey={paneKey}
|
|
588
|
+
handleLabel="Resize folder pane"
|
|
589
|
+
class="min-h-0 flex-1"
|
|
590
|
+
startClass="bg-muted/20"
|
|
591
|
+
>
|
|
592
|
+
{#snippet start()}
|
|
593
|
+
<div class="h-full overflow-auto px-1" data-file-tree-scroll>
|
|
594
|
+
{@render tree()}
|
|
595
|
+
</div>
|
|
596
|
+
{/snippet}
|
|
597
|
+
{#snippet end()}
|
|
598
|
+
<div class="flex h-full min-h-0 flex-col">{@render listArea()}</div>
|
|
599
|
+
{/snippet}
|
|
600
|
+
</ResizablePanes>
|
|
601
|
+
{:else}
|
|
602
|
+
{@render listArea()}
|
|
603
|
+
{/if}
|
|
604
|
+
{/if}
|
|
605
|
+
|
|
606
|
+
<LiveRegion message={announcement} />
|
|
607
|
+
</section>
|
|
608
|
+
|
|
609
|
+
{#snippet listArea()}
|
|
610
|
+
<div class="flex items-center justify-between gap-2 px-3 pt-2 text-xs text-muted-foreground">
|
|
611
|
+
<span data-file-browser-count>{countText}</span>
|
|
612
|
+
{#if currentPath}
|
|
613
|
+
<button
|
|
614
|
+
type="button"
|
|
615
|
+
class="inline-flex items-center gap-1 rounded px-1 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
616
|
+
onclick={goUp}
|
|
617
|
+
aria-keyshortcuts="Alt+ArrowUp Backspace"
|
|
618
|
+
>
|
|
619
|
+
<Icon name="corner-up-left" size={14} />
|
|
620
|
+
Up a folder
|
|
621
|
+
</button>
|
|
622
|
+
{/if}
|
|
623
|
+
</div>
|
|
624
|
+
|
|
625
|
+
<div class="min-h-0 flex-1 overflow-auto">
|
|
626
|
+
{#if status === 'error'}
|
|
627
|
+
<FileErrorState
|
|
628
|
+
error={listError}
|
|
629
|
+
onretry={refresh}
|
|
630
|
+
onroot={() => navigate('')}
|
|
631
|
+
rootLabel={currentPath ? rootLabel : undefined}
|
|
632
|
+
class="py-12"
|
|
633
|
+
/>
|
|
634
|
+
{:else}
|
|
635
|
+
<FileList
|
|
636
|
+
bind:this={list}
|
|
637
|
+
bind:selected
|
|
638
|
+
bind:sort
|
|
639
|
+
entries={shown}
|
|
640
|
+
loading={rootsStatus === 'loading' ||
|
|
641
|
+
status === 'loading' ||
|
|
642
|
+
status === 'idle' ||
|
|
643
|
+
(searching && searchStatus === 'loading' && !searchResults)}
|
|
644
|
+
{loadingMore}
|
|
645
|
+
{hasMore}
|
|
646
|
+
{autoLoad}
|
|
647
|
+
onloadmore={loadMore}
|
|
648
|
+
onopen={open}
|
|
649
|
+
{total}
|
|
650
|
+
showCount={false}
|
|
651
|
+
label={listLabel}
|
|
652
|
+
{locale}
|
|
653
|
+
emptyTitle={filter.trim() ? `Nothing matches “${filter.trim()}”` : 'This folder is empty'}
|
|
654
|
+
rowActions={rowActions && caps.any ? actions : undefined}
|
|
655
|
+
/>
|
|
656
|
+
{#if moreError}
|
|
657
|
+
<div class="px-3 pb-3">
|
|
658
|
+
<Alert variant="destructive">
|
|
659
|
+
{describeFilesError(moreError).title}.
|
|
660
|
+
<Button variant="link" size="sm" onclick={loadMore}>Try again</Button>
|
|
661
|
+
</Alert>
|
|
662
|
+
</div>
|
|
663
|
+
{/if}
|
|
664
|
+
{/if}
|
|
665
|
+
</div>
|
|
666
|
+
{/snippet}
|
|
667
|
+
|
|
668
|
+
{#snippet tree()}
|
|
669
|
+
<FileTree {source} {roots} {rootId} path={currentPath} {showHidden} onnavigate={treeNavigate} />
|
|
670
|
+
{/snippet}
|
|
671
|
+
|
|
672
|
+
{#snippet actions(entry: FileEntry)}
|
|
673
|
+
{@render rowActions?.(entry, context)}
|
|
674
|
+
{/snippet}
|
|
675
|
+
|
|
676
|
+
{#if navigationPane && !wide.current}
|
|
677
|
+
<Sheet bind:open={foldersOpen} side="left" size="sm" title="Folders">
|
|
678
|
+
<div data-file-tree-scroll class="-mx-2">
|
|
679
|
+
{@render tree()}
|
|
680
|
+
</div>
|
|
681
|
+
</Sheet>
|
|
682
|
+
{/if}
|
|
683
|
+
|
|
684
|
+
{#if previewEntry && rootId !== undefined}
|
|
685
|
+
<Dialog
|
|
686
|
+
bind:open={previewOpen}
|
|
687
|
+
title={previewEntry.name}
|
|
688
|
+
size="full"
|
|
689
|
+
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"
|
|
690
|
+
>
|
|
691
|
+
<FilePreview
|
|
692
|
+
{source}
|
|
693
|
+
{rootId}
|
|
694
|
+
{rootLabel}
|
|
695
|
+
entry={previewEntry}
|
|
696
|
+
{viewers}
|
|
697
|
+
maxBytes={maxPreviewBytes}
|
|
698
|
+
showTitle={false}
|
|
699
|
+
{locale}
|
|
700
|
+
ondownload={(_, err) => {
|
|
701
|
+
if (err) report(err);
|
|
702
|
+
}}
|
|
703
|
+
class="min-h-0 flex-1"
|
|
704
|
+
/>
|
|
705
|
+
</Dialog>
|
|
706
|
+
{/if}
|