|
1 | 1 | <script lang="ts">
|
2 |
| - import { FileNode, FolderNode, type FileTree, type FileTreeNode } from "$lib/tree.svelte.js"; |
| 2 | + import { FileNode, FolderNode, type FileTreeNode } from "$lib/tree.svelte.js"; |
3 | 3 | import { composeEventHandlers, formatSize } from "$lib/utils.js";
|
4 | 4 | import { FolderIcon } from "@lucide/svelte";
|
5 | 5 | import { ContextMenu } from "bits-ui";
|
|
17 | 17 | import NameFormDialog from "./NameFormDialog.svelte";
|
18 | 18 | import TreeContextMenu from "./TreeContextMenu.svelte";
|
19 | 19 | import TreeItem from "./TreeItem.svelte";
|
20 |
| - import { NameConflictDialogState, NameFormDialogState } from "./state.svelte.js"; |
21 |
| - import type { FileDropState, TreeContextMenuState, TreeItemState, TreeProps } from "./types.js"; |
| 20 | + import { |
| 21 | + createContextMenuState, |
| 22 | + createFileInputState, |
| 23 | + createNameConflictDialogState, |
| 24 | + createNameFormDialogState, |
| 25 | + } from "./state.svelte.js"; |
| 26 | + import type { FileDropState, TreeItemState, TreeProps, UploadFilesArgs } from "./types.js"; |
22 | 27 |
|
23 | 28 | let {
|
24 | 29 | tree,
|
25 | 30 | onRenameItem = ({ target, name }) => {
|
26 | 31 | target.node.name = name;
|
27 | 32 | return true;
|
28 | 33 | },
|
| 34 | + onCreateFolder = ({ target, name }) => { |
| 35 | + const folder = new FolderNode({ |
| 36 | + id: crypto.randomUUID(), |
| 37 | + name, |
| 38 | + children: [], |
| 39 | + }); |
| 40 | + target.children.push(folder); |
| 41 | + return true; |
| 42 | + }, |
| 43 | + onUploadFiles = ({ target, files }) => { |
| 44 | + for (const file of files) { |
| 45 | + const node = new FileNode({ |
| 46 | + id: crypto.randomUUID(), |
| 47 | + name: file.name, |
| 48 | + size: file.size, |
| 49 | + }); |
| 50 | + target.children.push(node); |
| 51 | + } |
| 52 | + return true; |
| 53 | + }, |
29 | 54 | defaultSelectedIds,
|
30 | 55 | selectedIds = new SvelteSet(defaultSelectedIds),
|
31 | 56 | defaultExpandedIds,
|
|
39 | 64 | }: TreeProps = $props();
|
40 | 65 |
|
41 | 66 | let treeComponent: Tree<FileTreeNode> | null = $state.raw(null);
|
42 |
| - let menuState: TreeContextMenuState | undefined = $state.raw(); |
| 67 | + let fileInput: HTMLInputElement | null = $state.raw(null); |
| 68 | +
|
43 | 69 | let focusedItemId: string | undefined = $state.raw();
|
44 | 70 | let fileDropState: FileDropState | undefined = $state.raw();
|
45 | 71 |
|
46 |
| - const nameConflictDialogState = new NameConflictDialogState(); |
47 |
| - const nameFormDialogState = new NameFormDialogState(); |
| 72 | + const nameConflictDialogState = createNameConflictDialogState(); |
| 73 | + const nameFormDialogState = createNameFormDialogState(); |
| 74 | + const fileInputState = createFileInputState({ |
| 75 | + ref: () => fileInput, |
| 76 | + }); |
48 | 77 |
|
49 | 78 | const pasteDirection: string | undefined = $derived.by(() => {
|
50 | 79 | if (pasteOperation === undefined || focusedItemId === undefined) {
|
|
58 | 87 | return "After";
|
59 | 88 | });
|
60 | 89 |
|
61 |
| - function handleResolveNameConflict({ |
62 |
| - operation, |
63 |
| - name, |
64 |
| - }: ResolveNameConflictArgs): Promise<NameConflictResolution> { |
65 |
| - return new Promise((resolve) => { |
66 |
| - let title: string; |
67 |
| - switch (operation) { |
68 |
| - case "move": { |
69 |
| - title = "Failed to move items"; |
70 |
| - break; |
71 |
| - } |
72 |
| - case "copy-paste": { |
73 |
| - title = "Failed to paste items"; |
74 |
| - break; |
75 |
| - } |
76 |
| - } |
77 |
| -
|
78 |
| - nameConflictDialogState.show({ |
79 |
| - title, |
80 |
| - description: `An item with the name "${name}" already exists`, |
81 |
| - onClose: resolve, |
82 |
| - }); |
83 |
| - }); |
84 |
| - } |
85 |
| -
|
86 |
| - function handleCircularReferenceError({ |
87 |
| - target, |
88 |
| - position, |
89 |
| - }: CircularReferenceErrorArgs<FileTreeNode>): void { |
90 |
| - toast.error(`Cannot move "${target.name}" ${position} itself`); |
91 |
| - } |
92 |
| -
|
93 |
| - const handleTriggerContextMenu: EventHandler<Event, HTMLDivElement> = (event) => { |
94 |
| - if (event.defaultPrevented) { |
95 |
| - // A tree item handled the event. |
96 |
| - return; |
97 |
| - } |
98 |
| -
|
99 |
| - if (event.target instanceof Element && event.target.closest("[role='treeitem']") === null) { |
100 |
| - menuState = { |
101 |
| - type: "tree", |
102 |
| - }; |
103 |
| - } |
104 |
| - }; |
105 |
| -
|
106 | 90 | function showAlreadyExistsToast(name: string): void {
|
107 | 91 | toast.error(`An item with the name "${name}" already exists`);
|
108 | 92 | }
|
109 | 93 |
|
110 | 94 | function handleRename(target: TreeItemState): void {
|
111 |
| - nameFormDialogState.name = target.node.name; |
112 | 95 | nameFormDialogState.show({
|
113 | 96 | title: "Rename",
|
| 97 | + name: target.node.name, |
114 | 98 | onSubmit: async (name) => {
|
115 | 99 | if (name === target.node.name) {
|
116 | 100 | nameFormDialogState.close();
|
|
133 | 117 | });
|
134 | 118 | }
|
135 | 119 |
|
136 |
| - function handleUploadFiles(target: FolderNode | FileTree, files: FileList): void { |
137 |
| - for (const file of files) { |
138 |
| - const node = new FileNode({ |
139 |
| - id: crypto.randomUUID(), |
140 |
| - name: file.name, |
141 |
| - size: file.size, |
142 |
| - }); |
143 |
| - target.children.push(node); |
| 120 | + async function handleUploadFiles({ target, files }: UploadFilesArgs): Promise<void> { |
| 121 | + for (const child of target.children) { |
| 122 | + for (const file of files) { |
| 123 | + if (child.name === file.name) { |
| 124 | + showAlreadyExistsToast(file.name); |
| 125 | + return; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +
|
| 130 | + const didUpload = await onUploadFiles({ target, files }); |
| 131 | + if (didUpload) { |
| 132 | + // TODO: show toast after upload is done |
144 | 133 | }
|
145 | 134 | }
|
146 | 135 |
|
147 |
| - function handleCreateFolder(target: FolderNode | FileTree): void { |
148 |
| - nameFormDialogState.show({ |
149 |
| - title: "New Folder", |
150 |
| - onSubmit: (name) => { |
151 |
| - for (const child of target.children) { |
152 |
| - if (child.name === name) { |
153 |
| - showAlreadyExistsToast(name); |
154 |
| - return; |
| 136 | + const contextMenuState = createContextMenuState({ |
| 137 | + onRename: handleRename, |
| 138 | + onCopy: (target) => treeComponent!.copy(target, "copy"), |
| 139 | + onCut: (target) => treeComponent!.copy(target, "cut"), |
| 140 | + onPaste: (target) => treeComponent!.paste(target), |
| 141 | + onRemove: (target) => treeComponent!.remove(target), |
| 142 | + onCreateFolder: (target) => { |
| 143 | + nameFormDialogState.show({ |
| 144 | + title: "New Folder", |
| 145 | + onSubmit: async (name) => { |
| 146 | + for (const child of target.children) { |
| 147 | + if (child.name === name) { |
| 148 | + showAlreadyExistsToast(name); |
| 149 | + return; |
| 150 | + } |
155 | 151 | }
|
| 152 | +
|
| 153 | + const didCreate = await onCreateFolder({ target, name }); |
| 154 | + if (didCreate) { |
| 155 | + nameFormDialogState.close(); |
| 156 | + } |
| 157 | + }, |
| 158 | + }); |
| 159 | + }, |
| 160 | + onUploadFiles: (target) => { |
| 161 | + fileInputState.showPicker({ |
| 162 | + onPick: (files) => handleUploadFiles({ target, files }), |
| 163 | + }); |
| 164 | + }, |
| 165 | + }); |
| 166 | +
|
| 167 | + const handleTriggerContextMenu: EventHandler<Event, HTMLDivElement> = (event) => { |
| 168 | + if (event.defaultPrevented) { |
| 169 | + // A tree item handled the event. |
| 170 | + return; |
| 171 | + } |
| 172 | +
|
| 173 | + if (event.target instanceof Element && event.target.closest("[role='treeitem']") === null) { |
| 174 | + contextMenuState.setTarget({ |
| 175 | + type: "tree", |
| 176 | + tree: () => tree, |
| 177 | + }); |
| 178 | + } |
| 179 | + }; |
| 180 | +
|
| 181 | + function handleResolveNameConflict({ |
| 182 | + operation, |
| 183 | + name, |
| 184 | + }: ResolveNameConflictArgs): Promise<NameConflictResolution> { |
| 185 | + return new Promise((resolve) => { |
| 186 | + let title: string; |
| 187 | + switch (operation) { |
| 188 | + case "move": { |
| 189 | + title = "Failed to move items"; |
| 190 | + break; |
| 191 | + } |
| 192 | + case "copy-paste": { |
| 193 | + title = "Failed to paste items"; |
| 194 | + break; |
156 | 195 | }
|
| 196 | + } |
157 | 197 |
|
158 |
| - const node = new FolderNode({ |
159 |
| - id: crypto.randomUUID(), |
160 |
| - name, |
161 |
| - children: [], |
162 |
| - }); |
163 |
| - target.children.push(node); |
164 |
| - nameFormDialogState.close(); |
165 |
| - }, |
| 198 | + nameConflictDialogState.show({ |
| 199 | + title, |
| 200 | + description: `An item with the name "${name}" already exists`, |
| 201 | + onClose: resolve, |
| 202 | + }); |
166 | 203 | });
|
167 | 204 | }
|
168 | 205 |
|
| 206 | + function handleCircularReferenceError({ |
| 207 | + target, |
| 208 | + position, |
| 209 | + }: CircularReferenceErrorArgs<FileTreeNode>): void { |
| 210 | + toast.error(`Cannot move "${target.name}" ${position} itself`); |
| 211 | + } |
| 212 | +
|
169 | 213 | function handleExpand(target: TreeItemState): void {
|
170 | 214 | expandedIds.add(target.node.id);
|
171 | 215 | }
|
|
218 | 262 | return;
|
219 | 263 | }
|
220 | 264 |
|
221 |
| - handleUploadFiles(tree, files); |
| 265 | + handleUploadFiles({ |
| 266 | + target: tree, |
| 267 | + files, |
| 268 | + }); |
222 | 269 | event.preventDefault();
|
223 | 270 | };
|
224 | 271 |
|
225 | 272 | function handleCleanup(target: TreeItemState): void {
|
226 |
| - if (menuState?.type === "item" && menuState.item() === target) { |
227 |
| - menuState = undefined; |
228 |
| - } |
229 |
| -
|
230 | 273 | if (focusedItemId === target.node.id) {
|
231 | 274 | focusedItemId = undefined;
|
232 | 275 | }
|
233 | 276 |
|
234 | 277 | if (fileDropState?.type === "item" && fileDropState.item() === target) {
|
235 | 278 | fileDropState = undefined;
|
236 | 279 | }
|
| 280 | +
|
| 281 | + const menuTarget = contextMenuState.target(); |
| 282 | + if (menuTarget?.type === "item" && menuTarget.item() === target) { |
| 283 | + contextMenuState.close(); |
| 284 | + } |
237 | 285 | }
|
238 | 286 | </script>
|
239 | 287 |
|
|
247 | 295 | </div>
|
248 | 296 |
|
249 | 297 | <TreeContextMenu
|
250 |
| - {tree} |
251 |
| - bind:state={menuState} |
252 |
| - onRename={handleRename} |
253 |
| - onCopy={(target, operation) => treeComponent!.copy(target, operation)} |
254 |
| - onPaste={(target) => treeComponent!.paste(target)} |
255 |
| - onRemove={(target) => treeComponent!.remove(target)} |
256 |
| - onUploadFiles={handleUploadFiles} |
257 |
| - onCreateFolder={handleCreateFolder} |
| 298 | + target={contextMenuState.target()} |
| 299 | + onRename={contextMenuState.rename} |
| 300 | + onCopy={contextMenuState.copy} |
| 301 | + onCut={contextMenuState.cut} |
| 302 | + onPaste={contextMenuState.paste} |
| 303 | + onRemove={contextMenuState.remove} |
| 304 | + onCreateFolder={contextMenuState.createFolder} |
| 305 | + onUploadFiles={contextMenuState.uploadFiles} |
| 306 | + onClose={contextMenuState.close} |
258 | 307 | >
|
259 | 308 | <ContextMenu.Trigger
|
260 | 309 | class="relative grow overflow-y-auto"
|
|
281 | 330 | {#snippet item({ item })}
|
282 | 331 | <TreeItem
|
283 | 332 | {item}
|
284 |
| - bind:menuState |
285 | 333 | bind:fileDropState
|
286 | 334 | onExpand={handleExpand}
|
287 | 335 | onCollapse={handleCollapse}
|
288 | 336 | onRename={handleRename}
|
| 337 | + onContextMenuTargetChange={contextMenuState.setTarget} |
289 | 338 | onUploadFiles={handleUploadFiles}
|
290 | 339 | onCleanup={handleCleanup}
|
291 | 340 | onfocusin={() => handleItemFocusIn(item)}
|
|
343 | 392 | </div>
|
344 | 393 |
|
345 | 394 | <NameConflictDialog
|
346 |
| - open={nameConflictDialogState.open} |
347 |
| - title={nameConflictDialogState.title} |
348 |
| - description={nameConflictDialogState.description} |
349 |
| - onClose={(result) => nameConflictDialogState.close(result)} |
| 395 | + open={nameConflictDialogState.open()} |
| 396 | + title={nameConflictDialogState.title()} |
| 397 | + description={nameConflictDialogState.description()} |
| 398 | + onClose={nameConflictDialogState.close} |
350 | 399 | />
|
351 | 400 |
|
352 | 401 | <NameFormDialog
|
353 |
| - bind:name={nameFormDialogState.name} |
354 |
| - open={nameFormDialogState.open} |
355 |
| - title={nameFormDialogState.title} |
356 |
| - onSubmit={() => nameFormDialogState.submit()} |
357 |
| - onClose={() => nameFormDialogState.close()} |
| 402 | + bind:name={nameFormDialogState.name, nameFormDialogState.setName} |
| 403 | + open={nameFormDialogState.open()} |
| 404 | + title={nameFormDialogState.title()} |
| 405 | + onSubmit={nameFormDialogState.submit} |
| 406 | + onClose={nameFormDialogState.close} |
| 407 | +/> |
| 408 | + |
| 409 | +<input |
| 410 | + bind:this={fileInput} |
| 411 | + type="file" |
| 412 | + multiple |
| 413 | + class="hidden" |
| 414 | + onchange={fileInputState.onChange} |
| 415 | + oncancel={fileInputState.onCancel} |
358 | 416 | />
|
359 | 417 |
|
360 | 418 | <style>
|
|
0 commit comments