@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,51 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import { type FileEntry, type FileSort } from '@classic-homes/files-core';
|
|
3
|
+
interface Props {
|
|
4
|
+
/** Entries to show, in display order (the server's order unless `sort` is set). */
|
|
5
|
+
entries: FileEntry[];
|
|
6
|
+
/** Path of the selected (focused) row. Bindable. */
|
|
7
|
+
selected?: string;
|
|
8
|
+
/** A folder or file was opened (click, tap or Enter). Never called for links. */
|
|
9
|
+
onopen?: (entry: FileEntry) => void;
|
|
10
|
+
/** The selected row changed. */
|
|
11
|
+
onselect?: (entry: FileEntry | undefined) => void;
|
|
12
|
+
/** Backspace or Alt+Up inside the list. */
|
|
13
|
+
onup?: () => void;
|
|
14
|
+
/** Client-side column sort over the loaded entries; `null` keeps the given order. Bindable. */
|
|
15
|
+
sort?: FileSort | null;
|
|
16
|
+
/** Offer sortable column headers. Default true. */
|
|
17
|
+
sortable?: boolean;
|
|
18
|
+
/** First page loading: shows skeleton rows. */
|
|
19
|
+
loading?: boolean;
|
|
20
|
+
/** A further page is loading. */
|
|
21
|
+
loadingMore?: boolean;
|
|
22
|
+
/** More pages exist on the server. */
|
|
23
|
+
hasMore?: boolean;
|
|
24
|
+
/** Load the next page ("Load more", or automatically with `autoLoad`). */
|
|
25
|
+
onloadmore?: () => void;
|
|
26
|
+
/** Load the next page when the end of the list scrolls into view. Default false. */
|
|
27
|
+
autoLoad?: boolean;
|
|
28
|
+
/** Entries in the whole folder, for "Showing N of total". */
|
|
29
|
+
total?: number;
|
|
30
|
+
/** Show "Showing N of total" under the list. Default true. */
|
|
31
|
+
showCount?: boolean;
|
|
32
|
+
/** Accessible name of the grid. Default "Files". */
|
|
33
|
+
label?: string;
|
|
34
|
+
locale?: string | string[];
|
|
35
|
+
/** Reference time for relative dates (tests). */
|
|
36
|
+
now?: number;
|
|
37
|
+
/** Title of the empty state. Default "This folder is empty". */
|
|
38
|
+
emptyTitle?: string;
|
|
39
|
+
/** Replaces the empty state. */
|
|
40
|
+
empty?: Snippet;
|
|
41
|
+
/** Per-row actions (write actions later). Rendered at the end of each row. */
|
|
42
|
+
rowActions?: Snippet<[FileEntry]>;
|
|
43
|
+
/** Explanation for links. */
|
|
44
|
+
linkMessage?: string;
|
|
45
|
+
class?: string;
|
|
46
|
+
}
|
|
47
|
+
declare const FileList: import("svelte").Component<Props, {
|
|
48
|
+
focus: () => void;
|
|
49
|
+
}, "sort" | "selected">;
|
|
50
|
+
type FileList = ReturnType<typeof FileList>;
|
|
51
|
+
export default FileList;
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { untrack } from 'svelte';
|
|
3
|
+
import { Alert, Button, EmptyState, Icon, Spinner } from '@classic-homes/theme-svelte';
|
|
4
|
+
import { DocumentViewer, type ViewerRegistry } from '@classic-homes/viewer-svelte';
|
|
5
|
+
import {
|
|
6
|
+
formatBytes,
|
|
7
|
+
isAbortError,
|
|
8
|
+
previewRoute,
|
|
9
|
+
type FileEntry,
|
|
10
|
+
type FilesSource,
|
|
11
|
+
} from '@classic-homes/files-core';
|
|
12
|
+
import FileDetails from './FileDetails.svelte';
|
|
13
|
+
import FileErrorState from './FileErrorState.svelte';
|
|
14
|
+
import FileIcon from './FileIcon.svelte';
|
|
15
|
+
import { cn, downloadEntry } from './utils.js';
|
|
16
|
+
|
|
17
|
+
/** The document viewers' default cap: 100 MB. */
|
|
18
|
+
const DEFAULT_MAX_BYTES = 100 * 1024 * 1024;
|
|
19
|
+
|
|
20
|
+
interface Props {
|
|
21
|
+
source: FilesSource;
|
|
22
|
+
rootId: string;
|
|
23
|
+
/** Label of the root, for details and "Copy path". */
|
|
24
|
+
rootLabel: string;
|
|
25
|
+
/** The file to preview. */
|
|
26
|
+
entry: FileEntry;
|
|
27
|
+
/**
|
|
28
|
+
* Document viewers, as lazy imports keyed by format (see DocumentViewer).
|
|
29
|
+
* Only formats listed here preview; add `dwg` only if the app opts into viewer-dwg.
|
|
30
|
+
*/
|
|
31
|
+
viewers?: ViewerRegistry;
|
|
32
|
+
/** Larger files are not fetched for preview. Default 100 MB (the viewers' cap). */
|
|
33
|
+
maxBytes?: number;
|
|
34
|
+
/** Show the file name in the toolbar. Default true; turn off inside a titled dialog. */
|
|
35
|
+
showTitle?: boolean;
|
|
36
|
+
locale?: string | string[];
|
|
37
|
+
/** Called after a download starts, or with the error when it fails. */
|
|
38
|
+
ondownload?: (entry: FileEntry, error?: unknown) => void;
|
|
39
|
+
class?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let {
|
|
43
|
+
source,
|
|
44
|
+
rootId,
|
|
45
|
+
rootLabel,
|
|
46
|
+
entry,
|
|
47
|
+
viewers = {},
|
|
48
|
+
maxBytes = DEFAULT_MAX_BYTES,
|
|
49
|
+
showTitle = true,
|
|
50
|
+
locale,
|
|
51
|
+
ondownload,
|
|
52
|
+
class: className,
|
|
53
|
+
}: Props = $props();
|
|
54
|
+
|
|
55
|
+
const route = $derived(previewRoute(entry, Object.keys(viewers)));
|
|
56
|
+
const tooLarge = $derived(route.kind !== 'none' && entry.size > maxBytes);
|
|
57
|
+
const willPreview = $derived(route.kind !== 'none' && !tooLarge);
|
|
58
|
+
|
|
59
|
+
let status = $state<'idle' | 'loading' | 'ready' | 'error'>('idle');
|
|
60
|
+
let blob = $state.raw<Blob>();
|
|
61
|
+
let error = $state.raw<unknown>();
|
|
62
|
+
let imageUrl = $state<string>();
|
|
63
|
+
let attempt = $state(0);
|
|
64
|
+
let showDetails = $state(false);
|
|
65
|
+
let downloading = $state(false);
|
|
66
|
+
let downloadError = $state.raw<unknown>();
|
|
67
|
+
|
|
68
|
+
// Load the bytes for a previewable file; abort when the file changes.
|
|
69
|
+
$effect(() => {
|
|
70
|
+
const path = entry.path;
|
|
71
|
+
const root = rootId;
|
|
72
|
+
const src = source;
|
|
73
|
+
void attempt;
|
|
74
|
+
if (!willPreview) {
|
|
75
|
+
status = 'idle';
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const controller = new AbortController();
|
|
79
|
+
untrack(() => {
|
|
80
|
+
status = 'loading';
|
|
81
|
+
error = undefined;
|
|
82
|
+
blob = undefined;
|
|
83
|
+
downloadError = undefined;
|
|
84
|
+
});
|
|
85
|
+
src.read(root, path, { signal: controller.signal }).then(
|
|
86
|
+
(b) => {
|
|
87
|
+
if (controller.signal.aborted) return;
|
|
88
|
+
blob = b;
|
|
89
|
+
status = 'ready';
|
|
90
|
+
},
|
|
91
|
+
(err) => {
|
|
92
|
+
if (controller.signal.aborted || isAbortError(err)) return;
|
|
93
|
+
error = err;
|
|
94
|
+
status = 'error';
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
return () => controller.abort();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Images go through an object URL, revoked when the file changes or the preview closes.
|
|
101
|
+
$effect(() => {
|
|
102
|
+
if (route.kind !== 'image' || !blob) {
|
|
103
|
+
imageUrl = undefined;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const url = URL.createObjectURL(blob);
|
|
107
|
+
imageUrl = url;
|
|
108
|
+
return () => URL.revokeObjectURL(url);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
$effect(() => {
|
|
112
|
+
void entry.path;
|
|
113
|
+
showDetails = false;
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
async function download() {
|
|
117
|
+
downloading = true;
|
|
118
|
+
downloadError = undefined;
|
|
119
|
+
try {
|
|
120
|
+
await downloadEntry(source, rootId, entry, { blob: status === 'ready' ? blob : undefined });
|
|
121
|
+
ondownload?.(entry);
|
|
122
|
+
} catch (err) {
|
|
123
|
+
downloadError = err;
|
|
124
|
+
ondownload?.(entry, err);
|
|
125
|
+
} finally {
|
|
126
|
+
downloading = false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
</script>
|
|
130
|
+
|
|
131
|
+
<div
|
|
132
|
+
class={cn('flex h-full min-h-0 flex-col', className)}
|
|
133
|
+
data-file-preview
|
|
134
|
+
data-route={route.kind}
|
|
135
|
+
>
|
|
136
|
+
<div class="flex flex-wrap items-center gap-2 border-b border-border px-3 py-2">
|
|
137
|
+
{#if showTitle}
|
|
138
|
+
<FileIcon name={entry.name} type={entry.type} />
|
|
139
|
+
<h3 class="min-w-0 flex-1 truncate text-sm font-medium" title={entry.name}>{entry.name}</h3>
|
|
140
|
+
{:else}
|
|
141
|
+
<span class="flex-1"></span>
|
|
142
|
+
{/if}
|
|
143
|
+
{#if willPreview}
|
|
144
|
+
<Button
|
|
145
|
+
variant="ghost"
|
|
146
|
+
size="sm"
|
|
147
|
+
onclick={() => (showDetails = !showDetails)}
|
|
148
|
+
aria-expanded={showDetails}
|
|
149
|
+
>
|
|
150
|
+
<Icon name="info" size={16} />
|
|
151
|
+
Details
|
|
152
|
+
</Button>
|
|
153
|
+
{/if}
|
|
154
|
+
<Button variant="outline" size="sm" loading={downloading} onclick={download}>
|
|
155
|
+
<Icon name="download" size={16} />
|
|
156
|
+
Download
|
|
157
|
+
</Button>
|
|
158
|
+
</div>
|
|
159
|
+
|
|
160
|
+
{#if downloadError}
|
|
161
|
+
<Alert variant="destructive" class="m-3">
|
|
162
|
+
Couldn't download {entry.name}. Try again in a moment.
|
|
163
|
+
</Alert>
|
|
164
|
+
{/if}
|
|
165
|
+
|
|
166
|
+
<div class="relative min-h-0 flex-1 overflow-auto">
|
|
167
|
+
{#if !willPreview}
|
|
168
|
+
<div class="mx-auto max-w-lg space-y-6 p-6">
|
|
169
|
+
{#if tooLarge}
|
|
170
|
+
<EmptyState
|
|
171
|
+
title="Too large to preview"
|
|
172
|
+
description={`This file is ${formatBytes(entry.size, { locale })}. Previews stop at ${formatBytes(maxBytes, { locale })}: download it instead.`}
|
|
173
|
+
icon="download"
|
|
174
|
+
size="sm"
|
|
175
|
+
headingLevel="h4"
|
|
176
|
+
/>
|
|
177
|
+
{:else}
|
|
178
|
+
<EmptyState
|
|
179
|
+
title="No preview for this file type"
|
|
180
|
+
description="Download it to open it in its own app."
|
|
181
|
+
icon="eye-off"
|
|
182
|
+
size="sm"
|
|
183
|
+
headingLevel="h4"
|
|
184
|
+
/>
|
|
185
|
+
{/if}
|
|
186
|
+
<FileDetails {entry} {rootLabel} {locale} />
|
|
187
|
+
</div>
|
|
188
|
+
{:else if status === 'loading'}
|
|
189
|
+
<div class="flex h-full min-h-48 items-center justify-center" role="status">
|
|
190
|
+
<Spinner />
|
|
191
|
+
<span class="sr-only">Loading {entry.name}…</span>
|
|
192
|
+
</div>
|
|
193
|
+
{:else if status === 'error'}
|
|
194
|
+
<FileErrorState {error} onretry={() => attempt++} class="py-10" />
|
|
195
|
+
{:else if status === 'ready' && blob}
|
|
196
|
+
{#if showDetails}
|
|
197
|
+
<div class="border-b border-border p-4">
|
|
198
|
+
<FileDetails {entry} {rootLabel} {locale} />
|
|
199
|
+
</div>
|
|
200
|
+
{/if}
|
|
201
|
+
{#if route.kind === 'image'}
|
|
202
|
+
{#if imageUrl}
|
|
203
|
+
<div class="flex min-h-full items-center justify-center bg-muted/30 p-4">
|
|
204
|
+
<img src={imageUrl} alt={entry.name} class="max-h-full max-w-full object-contain" />
|
|
205
|
+
</div>
|
|
206
|
+
{/if}
|
|
207
|
+
{:else if route.kind === 'document'}
|
|
208
|
+
<DocumentViewer
|
|
209
|
+
source={blob}
|
|
210
|
+
{viewers}
|
|
211
|
+
name={entry.name}
|
|
212
|
+
mime={blob.type || undefined}
|
|
213
|
+
{maxBytes}
|
|
214
|
+
class="h-full min-h-[24rem]"
|
|
215
|
+
/>
|
|
216
|
+
{/if}
|
|
217
|
+
{/if}
|
|
218
|
+
</div>
|
|
219
|
+
</div>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type ViewerRegistry } from '@classic-homes/viewer-svelte';
|
|
2
|
+
import { type FileEntry, type FilesSource } from '@classic-homes/files-core';
|
|
3
|
+
interface Props {
|
|
4
|
+
source: FilesSource;
|
|
5
|
+
rootId: string;
|
|
6
|
+
/** Label of the root, for details and "Copy path". */
|
|
7
|
+
rootLabel: string;
|
|
8
|
+
/** The file to preview. */
|
|
9
|
+
entry: FileEntry;
|
|
10
|
+
/**
|
|
11
|
+
* Document viewers, as lazy imports keyed by format (see DocumentViewer).
|
|
12
|
+
* Only formats listed here preview; add `dwg` only if the app opts into viewer-dwg.
|
|
13
|
+
*/
|
|
14
|
+
viewers?: ViewerRegistry;
|
|
15
|
+
/** Larger files are not fetched for preview. Default 100 MB (the viewers' cap). */
|
|
16
|
+
maxBytes?: number;
|
|
17
|
+
/** Show the file name in the toolbar. Default true; turn off inside a titled dialog. */
|
|
18
|
+
showTitle?: boolean;
|
|
19
|
+
locale?: string | string[];
|
|
20
|
+
/** Called after a download starts, or with the error when it fails. */
|
|
21
|
+
ondownload?: (entry: FileEntry, error?: unknown) => void;
|
|
22
|
+
class?: string;
|
|
23
|
+
}
|
|
24
|
+
declare const FilePreview: import("svelte").Component<Props, {}, "">;
|
|
25
|
+
type FilePreview = ReturnType<typeof FilePreview>;
|
|
26
|
+
export default FilePreview;
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Explorer-style navigation pane: drives at the top level, their folders
|
|
4
|
+
below, loaded lazily as they expand. The open folder is highlighted and
|
|
5
|
+
its ancestors are expanded, so its parent and siblings are always in view.
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import { tick, untrack } from 'svelte';
|
|
9
|
+
import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
|
10
|
+
import { Icon } from '@classic-homes/theme-svelte';
|
|
11
|
+
import {
|
|
12
|
+
isAbortError,
|
|
13
|
+
normalizePath,
|
|
14
|
+
pathSegments,
|
|
15
|
+
type FileEntry,
|
|
16
|
+
type FileRoot,
|
|
17
|
+
type FilesSource,
|
|
18
|
+
} from '@classic-homes/files-core';
|
|
19
|
+
import FileIcon from './FileIcon.svelte';
|
|
20
|
+
import { cn } from './utils.js';
|
|
21
|
+
|
|
22
|
+
interface Props {
|
|
23
|
+
source: FilesSource;
|
|
24
|
+
/** Drives shown at the top level. */
|
|
25
|
+
roots: FileRoot[];
|
|
26
|
+
/** Drive of the open folder. */
|
|
27
|
+
rootId?: string;
|
|
28
|
+
/** Open folder, root-relative (`''` = the drive itself). */
|
|
29
|
+
path?: string;
|
|
30
|
+
/** Include hidden folders. */
|
|
31
|
+
showHidden?: boolean;
|
|
32
|
+
/** A folder (or a drive, with `path` `''`) was chosen. */
|
|
33
|
+
onnavigate?: (rootId: string, path: string) => void;
|
|
34
|
+
/** Accessible name of the tree. Default "Folders". */
|
|
35
|
+
label?: string;
|
|
36
|
+
class?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let {
|
|
40
|
+
source,
|
|
41
|
+
roots,
|
|
42
|
+
rootId,
|
|
43
|
+
path = '',
|
|
44
|
+
showHidden = false,
|
|
45
|
+
onnavigate,
|
|
46
|
+
label = 'Folders',
|
|
47
|
+
class: className,
|
|
48
|
+
}: Props = $props();
|
|
49
|
+
|
|
50
|
+
type Status = 'loading' | 'ready' | 'error';
|
|
51
|
+
interface Children {
|
|
52
|
+
status: Status;
|
|
53
|
+
folders: FileEntry[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** `${rootId}|${path}` → its subfolders (`|` can't occur in SMB names). */
|
|
57
|
+
const children = new SvelteMap<string, Children>();
|
|
58
|
+
const expanded = new SvelteSet<string>();
|
|
59
|
+
const controllers = new Map<string, AbortController>();
|
|
60
|
+
|
|
61
|
+
const keyOf = (r: string, p: string) => `${r}|${p}`;
|
|
62
|
+
const currentPath = $derived.by(() => {
|
|
63
|
+
try {
|
|
64
|
+
return normalizePath(path);
|
|
65
|
+
} catch {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
const currentKey = $derived(rootId === undefined ? '' : keyOf(rootId, currentPath));
|
|
70
|
+
|
|
71
|
+
let focusedKey = $state<string>();
|
|
72
|
+
let tree = $state<HTMLElement>();
|
|
73
|
+
|
|
74
|
+
/** Load every subfolder (directories come first, so stop at the first file). */
|
|
75
|
+
async function load(r: string, p: string) {
|
|
76
|
+
const key = keyOf(r, p);
|
|
77
|
+
const state = children.get(key);
|
|
78
|
+
if (state && state.status !== 'error') return;
|
|
79
|
+
const controller = new AbortController();
|
|
80
|
+
controllers.set(key, controller);
|
|
81
|
+
children.set(key, { status: 'loading', folders: state?.folders ?? [] });
|
|
82
|
+
const folders: FileEntry[] = [];
|
|
83
|
+
try {
|
|
84
|
+
let cursor: string | undefined;
|
|
85
|
+
for (;;) {
|
|
86
|
+
const page = await source.list(r, p, {
|
|
87
|
+
limit: 1000,
|
|
88
|
+
cursor,
|
|
89
|
+
includeHidden: showHidden,
|
|
90
|
+
signal: controller.signal,
|
|
91
|
+
});
|
|
92
|
+
const dirs = page.entries.filter((e) => e.type === 'dir');
|
|
93
|
+
folders.push(...dirs);
|
|
94
|
+
if (!page.nextCursor || dirs.length < page.entries.length) break;
|
|
95
|
+
cursor = page.nextCursor;
|
|
96
|
+
}
|
|
97
|
+
if (!controller.signal.aborted) children.set(key, { status: 'ready', folders });
|
|
98
|
+
} catch (err) {
|
|
99
|
+
if (controller.signal.aborted || isAbortError(err)) return;
|
|
100
|
+
children.set(key, { status: 'error', folders: [] });
|
|
101
|
+
} finally {
|
|
102
|
+
if (controllers.get(key) === controller) controllers.delete(key);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Start over when the source or the hidden-files setting changes.
|
|
107
|
+
$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
|
+
};
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Reveal the open folder: expand its drive and every ancestor, and load them.
|
|
118
|
+
$effect(() => {
|
|
119
|
+
const r = rootId;
|
|
120
|
+
const p = currentPath;
|
|
121
|
+
void source;
|
|
122
|
+
void showHidden;
|
|
123
|
+
if (r === undefined || !roots.some((root) => root.id === r)) return;
|
|
124
|
+
untrack(() => {
|
|
125
|
+
const ancestors = ['', ...pathSegments(p).map((s) => s.path)];
|
|
126
|
+
// The open folder itself is listed by the browser; expand it here too so
|
|
127
|
+
// its subfolders show, as in Explorer.
|
|
128
|
+
for (const a of ancestors) {
|
|
129
|
+
expanded.add(keyOf(r, a));
|
|
130
|
+
void load(r, a);
|
|
131
|
+
}
|
|
132
|
+
focusedKey = keyOf(r, p);
|
|
133
|
+
});
|
|
134
|
+
void tick().then(revealCurrent);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
interface Item {
|
|
138
|
+
key: string;
|
|
139
|
+
rootId: string;
|
|
140
|
+
path: string;
|
|
141
|
+
name: string;
|
|
142
|
+
level: number;
|
|
143
|
+
isRoot: boolean;
|
|
144
|
+
hidden: boolean;
|
|
145
|
+
setsize: number;
|
|
146
|
+
posinset: number;
|
|
147
|
+
}
|
|
148
|
+
type Row =
|
|
149
|
+
| { kind: 'item'; item: Item }
|
|
150
|
+
| { kind: 'note'; key: string; level: number; text: string; retry?: () => void };
|
|
151
|
+
|
|
152
|
+
const rows = $derived.by<Row[]>(() => {
|
|
153
|
+
const out: Row[] = [];
|
|
154
|
+
const walk = (r: string, p: string, level: number) => {
|
|
155
|
+
const state = children.get(keyOf(r, p));
|
|
156
|
+
if (!state || (state.status === 'loading' && state.folders.length === 0)) {
|
|
157
|
+
out.push({ kind: 'note', key: `${keyOf(r, p)}:loading`, level, text: 'Loading…' });
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (state.status === 'error') {
|
|
161
|
+
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),
|
|
167
|
+
});
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
state.folders.forEach((f, i) => {
|
|
171
|
+
const key = keyOf(r, f.path);
|
|
172
|
+
out.push({
|
|
173
|
+
kind: 'item',
|
|
174
|
+
item: {
|
|
175
|
+
key,
|
|
176
|
+
rootId: r,
|
|
177
|
+
path: f.path,
|
|
178
|
+
name: f.name,
|
|
179
|
+
level,
|
|
180
|
+
isRoot: false,
|
|
181
|
+
hidden: f.hidden,
|
|
182
|
+
setsize: state.folders.length,
|
|
183
|
+
posinset: i + 1,
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
if (expanded.has(key)) walk(r, f.path, level + 1);
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
roots.forEach((root, i) => {
|
|
190
|
+
const key = keyOf(root.id, '');
|
|
191
|
+
out.push({
|
|
192
|
+
kind: 'item',
|
|
193
|
+
item: {
|
|
194
|
+
key,
|
|
195
|
+
rootId: root.id,
|
|
196
|
+
path: '',
|
|
197
|
+
name: root.label,
|
|
198
|
+
level: 1,
|
|
199
|
+
isRoot: true,
|
|
200
|
+
hidden: false,
|
|
201
|
+
setsize: roots.length,
|
|
202
|
+
posinset: i + 1,
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
if (expanded.has(key)) walk(root.id, '', 2);
|
|
206
|
+
});
|
|
207
|
+
return out;
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const items = $derived(rows.flatMap((r) => (r.kind === 'item' ? [r.item] : [])));
|
|
211
|
+
const tabKey = $derived(
|
|
212
|
+
items.some((i) => i.key === focusedKey)
|
|
213
|
+
? focusedKey
|
|
214
|
+
: (items.find((i) => i.key === currentKey) ?? items[0])?.key
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
/** Known to have no subfolders (loaded and empty). */
|
|
218
|
+
function isLeaf(item: Item): boolean {
|
|
219
|
+
const state = children.get(item.key);
|
|
220
|
+
return state?.status === 'ready' && state.folders.length === 0;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function toggle(item: Item, open?: boolean) {
|
|
224
|
+
const willOpen = open ?? !expanded.has(item.key);
|
|
225
|
+
if (willOpen) {
|
|
226
|
+
expanded.add(item.key);
|
|
227
|
+
void load(item.rootId, item.path);
|
|
228
|
+
} else {
|
|
229
|
+
expanded.delete(item.key);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function choose(item: Item) {
|
|
234
|
+
focusedKey = item.key;
|
|
235
|
+
onnavigate?.(item.rootId, item.path);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function el(key: string | undefined): HTMLElement | null {
|
|
239
|
+
if (!tree || key === undefined) return null;
|
|
240
|
+
for (const node of tree.querySelectorAll<HTMLElement>('[role="treeitem"]')) {
|
|
241
|
+
if (node.dataset.key === key) return node;
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function focusItem(item: Item | undefined) {
|
|
247
|
+
if (!item) return;
|
|
248
|
+
focusedKey = item.key;
|
|
249
|
+
await tick();
|
|
250
|
+
const node = el(item.key);
|
|
251
|
+
node?.focus({ preventScroll: true });
|
|
252
|
+
if (node) scrollIntoPane(node);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Scroll inside the pane only, never the page. */
|
|
256
|
+
function scrollIntoPane(node: HTMLElement) {
|
|
257
|
+
const pane = tree?.closest<HTMLElement>('[data-file-tree-scroll]') ?? tree?.parentElement;
|
|
258
|
+
if (!pane) return;
|
|
259
|
+
const p = pane.getBoundingClientRect();
|
|
260
|
+
const r = node.getBoundingClientRect();
|
|
261
|
+
if (r.top < p.top) pane.scrollTop -= p.top - r.top + 8;
|
|
262
|
+
else if (r.bottom > p.bottom) pane.scrollTop += r.bottom - p.bottom + 8;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function revealCurrent() {
|
|
266
|
+
const node = el(currentKey);
|
|
267
|
+
if (node) scrollIntoPane(node);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function onkeydown(event: KeyboardEvent, item: Item) {
|
|
271
|
+
const index = items.findIndex((i) => i.key === item.key);
|
|
272
|
+
const open = expanded.has(item.key);
|
|
273
|
+
switch (event.key) {
|
|
274
|
+
case 'ArrowDown':
|
|
275
|
+
event.preventDefault();
|
|
276
|
+
void focusItem(items[index + 1]);
|
|
277
|
+
break;
|
|
278
|
+
case 'ArrowUp':
|
|
279
|
+
if (event.altKey) return; // "up a folder" belongs to the browser
|
|
280
|
+
event.preventDefault();
|
|
281
|
+
void focusItem(items[index - 1]);
|
|
282
|
+
break;
|
|
283
|
+
case 'Home':
|
|
284
|
+
event.preventDefault();
|
|
285
|
+
void focusItem(items[0]);
|
|
286
|
+
break;
|
|
287
|
+
case 'End':
|
|
288
|
+
event.preventDefault();
|
|
289
|
+
void focusItem(items[items.length - 1]);
|
|
290
|
+
break;
|
|
291
|
+
case 'ArrowRight':
|
|
292
|
+
event.preventDefault();
|
|
293
|
+
if (isLeaf(item)) break;
|
|
294
|
+
if (!open) toggle(item, true);
|
|
295
|
+
else if (items[index + 1]?.level === item.level + 1) void focusItem(items[index + 1]);
|
|
296
|
+
break;
|
|
297
|
+
case 'ArrowLeft': {
|
|
298
|
+
event.preventDefault();
|
|
299
|
+
if (open && !isLeaf(item)) {
|
|
300
|
+
toggle(item, false);
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
304
|
+
if (items[i].level === item.level - 1) {
|
|
305
|
+
void focusItem(items[i]);
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
case 'Enter':
|
|
312
|
+
case ' ':
|
|
313
|
+
event.preventDefault();
|
|
314
|
+
choose(item);
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
</script>
|
|
319
|
+
|
|
320
|
+
<ul
|
|
321
|
+
bind:this={tree}
|
|
322
|
+
role="tree"
|
|
323
|
+
aria-label={label}
|
|
324
|
+
class={cn('select-none py-1 text-sm', className)}
|
|
325
|
+
data-file-tree
|
|
326
|
+
>
|
|
327
|
+
{#each rows as row (row.kind === 'item' ? row.item.key : row.key)}
|
|
328
|
+
{#if row.kind === 'note'}
|
|
329
|
+
<li
|
|
330
|
+
role="none"
|
|
331
|
+
style:padding-left="{row.level * 0.875 + 0.5}rem"
|
|
332
|
+
class="py-1 text-xs text-muted-foreground"
|
|
333
|
+
>
|
|
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}
|
|
344
|
+
</li>
|
|
345
|
+
{:else}
|
|
346
|
+
{@const item = row.item}
|
|
347
|
+
{@const open = expanded.has(item.key)}
|
|
348
|
+
{@const leaf = isLeaf(item)}
|
|
349
|
+
{@const current = item.key === currentKey}
|
|
350
|
+
<li
|
|
351
|
+
role="treeitem"
|
|
352
|
+
data-key={item.key}
|
|
353
|
+
aria-level={item.level}
|
|
354
|
+
aria-setsize={item.setsize}
|
|
355
|
+
aria-posinset={item.posinset}
|
|
356
|
+
aria-expanded={leaf ? undefined : open}
|
|
357
|
+
aria-selected={current}
|
|
358
|
+
aria-current={current ? 'location' : undefined}
|
|
359
|
+
tabindex={item.key === tabKey ? 0 : -1}
|
|
360
|
+
class={cn(
|
|
361
|
+
'flex cursor-pointer items-center gap-1 rounded-sm py-1 pr-2 outline-none',
|
|
362
|
+
'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring',
|
|
363
|
+
current ? 'bg-chart-1/15 font-medium text-foreground' : 'hover:bg-muted/60',
|
|
364
|
+
item.hidden && 'opacity-70'
|
|
365
|
+
)}
|
|
366
|
+
style:padding-left="{(item.level - 1) * 0.875 + 0.25}rem"
|
|
367
|
+
title={item.name}
|
|
368
|
+
onclick={() => choose(item)}
|
|
369
|
+
onkeydown={(e) => onkeydown(e, item)}
|
|
370
|
+
onfocus={() => (focusedKey = item.key)}
|
|
371
|
+
>
|
|
372
|
+
<!-- Chevron: expand/collapse without opening (as in Explorer). Keyboard: Right/Left. -->
|
|
373
|
+
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
|
|
374
|
+
<span
|
|
375
|
+
class={cn(
|
|
376
|
+
'flex h-5 w-5 shrink-0 items-center justify-center rounded text-muted-foreground hover:bg-muted',
|
|
377
|
+
leaf && 'invisible'
|
|
378
|
+
)}
|
|
379
|
+
aria-hidden="true"
|
|
380
|
+
onclick={(e) => {
|
|
381
|
+
e.stopPropagation();
|
|
382
|
+
toggle(item);
|
|
383
|
+
}}
|
|
384
|
+
>
|
|
385
|
+
<Icon name={open ? 'chevron-down' : 'chevron-right'} size={14} />
|
|
386
|
+
</span>
|
|
387
|
+
{#if item.isRoot}
|
|
388
|
+
<Icon name="home" size={16} class="shrink-0 text-muted-foreground" />
|
|
389
|
+
{:else}
|
|
390
|
+
<FileIcon type="dir" size={16} />
|
|
391
|
+
{/if}
|
|
392
|
+
<span class="truncate">{item.name}</span>
|
|
393
|
+
</li>
|
|
394
|
+
{/if}
|
|
395
|
+
{/each}
|
|
396
|
+
</ul>
|