@widgetic/file-browser 0.5.36 → 0.5.38
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/dist/components/core/FileBrowser.svelte +88 -98
- package/dist/components/core/FileUploader.svelte +307 -133
- package/dist/components/core/Header.svelte +1 -1
- package/dist/components/core/RecentFiles.svelte +21 -8
- package/dist/components/core/SearchDropdown.svelte +312 -0
- package/dist/components/core/SearchDropdown.svelte.d.ts +30 -0
- package/dist/components/core/UploadPanel.svelte +27 -15
- package/dist/components/core/UploadPanel.svelte.d.ts +3 -0
- package/dist/components/core/UploadRow.svelte +22 -1
- package/dist/components/core/UploadRow.svelte.d.ts +3 -0
- package/dist/components/core/index.d.ts +1 -0
- package/dist/components/core/index.js +1 -0
- package/dist/components/items/FileItem.svelte +28 -3
- package/dist/components/items/FolderItem.svelte +21 -2
- package/dist/components/modals/MoveDialog.svelte +16 -11
- package/dist/components/modals/MoveDialog.svelte.d.ts +1 -0
- package/dist/components/modals/SearchModal.svelte +4 -44
- package/dist/components/ui/DropdownMenu.svelte +7 -3
- package/dist/components/ui/DropdownMenu.svelte.d.ts +1 -0
- package/dist/i18n/messages.d.ts +7 -1
- package/dist/i18n/messages.js +7 -1
- package/dist/utils/fileNames.d.ts +9 -0
- package/dist/utils/fileNames.js +19 -0
- package/dist/utils/libraryTree.d.ts +22 -0
- package/dist/utils/libraryTree.js +177 -0
- package/dist/utils/searchItems.d.ts +27 -0
- package/dist/utils/searchItems.js +77 -0
- package/dist/utils/uploadProgressMath.d.ts +29 -0
- package/dist/utils/uploadProgressMath.js +66 -0
- package/dist/utils/uploadResponseMapping.d.ts +11 -0
- package/dist/utils/uploadResponseMapping.js +51 -0
- package/dist/utils/uploadUiHelpers.d.ts +28 -0
- package/dist/utils/uploadUiHelpers.js +49 -0
- package/dist/utils/userUploadsStorage.d.ts +2 -2
- package/dist/utils/userUploadsStorage.js +25 -17
- package/dist/utils/widgeticTusUpload.d.ts +2 -0
- package/dist/utils/widgeticTusUpload.js +4 -20
- package/dist/utils/widgeticUpload.js +3 -66
- package/dist/utils/xhrMultipartUpload.d.ts +16 -0
- package/dist/utils/xhrMultipartUpload.js +125 -0
- package/package.json +3 -3
|
@@ -26,11 +26,26 @@
|
|
|
26
26
|
|
|
27
27
|
$: isEmpty = item.matchingFileCount === 0;
|
|
28
28
|
|
|
29
|
+
function isFromMenuOrThumbControl(event: Event): boolean {
|
|
30
|
+
const target = event.target as HTMLElement | null;
|
|
31
|
+
if (!target) return false;
|
|
32
|
+
if (target.closest('[data-fb-menu-root]')) return true;
|
|
33
|
+
if (target.closest('button[data-fb-action]')) return true;
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
29
37
|
function handleIconClick(event: Event) {
|
|
30
38
|
event.stopPropagation();
|
|
31
39
|
dispatch('click');
|
|
32
40
|
}
|
|
33
41
|
|
|
42
|
+
/** Whole tile opens the folder like the folder thumb; menu clicks stay separate. */
|
|
43
|
+
function handleTileClick(event: MouseEvent) {
|
|
44
|
+
if (isFromMenuOrThumbControl(event)) return;
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
dispatch('click');
|
|
47
|
+
}
|
|
48
|
+
|
|
34
49
|
function handleDropdownAction(event: CustomEvent) {
|
|
35
50
|
const { action } = event.detail;
|
|
36
51
|
dispatch('action', { action, item });
|
|
@@ -48,8 +63,11 @@
|
|
|
48
63
|
</script>
|
|
49
64
|
|
|
50
65
|
<div class="fb-folder-item-ct group relative w-full">
|
|
66
|
+
<!-- Tile activation mirrors the folder thumb; keyboard users use the existing open-folder control. -->
|
|
51
67
|
<div
|
|
52
|
-
class="flex h-16 w-full items-center gap-3 rounded-xl border border-border bg-background p-2 transition-colors duration-fast hover:bg-accent has-[[data-state=open]]:bg-accent"
|
|
68
|
+
class="flex h-16 w-full cursor-pointer items-center gap-3 rounded-xl border border-border bg-background p-2 transition-colors duration-fast hover:bg-accent has-[[data-state=open]]:bg-accent"
|
|
69
|
+
onclick={handleTileClick}
|
|
70
|
+
role="presentation"
|
|
53
71
|
>
|
|
54
72
|
<button
|
|
55
73
|
type="button"
|
|
@@ -77,10 +95,11 @@
|
|
|
77
95
|
<div
|
|
78
96
|
class="fb-folder-menu-ct shrink-0 transition-opacity duration-fast {menuVisibleClass}"
|
|
79
97
|
role="presentation"
|
|
98
|
+
data-fb-menu-root
|
|
80
99
|
onclick={stopMenuBubble}
|
|
81
100
|
onkeydown={stopMenuBubble}
|
|
82
101
|
>
|
|
83
|
-
<DropdownMenu isFolder={true} on:action={handleDropdownAction} />
|
|
102
|
+
<DropdownMenu isFolder={true} itemId={item.id} on:action={handleDropdownAction} />
|
|
84
103
|
</div>
|
|
85
104
|
</div>
|
|
86
105
|
</div>
|
|
@@ -41,6 +41,8 @@
|
|
|
41
41
|
export let sourceItems: FileSystemItem[] = [];
|
|
42
42
|
export let currentPath: string[] = [];
|
|
43
43
|
export let target: string = 'move-dialog'; // target for Portal
|
|
44
|
+
/** When true (the Widgetic library has no demo move endpoint), skip the server round-trip and only update the local tree. */
|
|
45
|
+
export let localOnly: boolean = false;
|
|
44
46
|
|
|
45
47
|
const treeScrollHeightPx = 348;
|
|
46
48
|
|
|
@@ -169,16 +171,18 @@
|
|
|
169
171
|
|
|
170
172
|
const results = await Promise.allSettled(
|
|
171
173
|
sourceItems.map((sourceItem) =>
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
174
|
+
localOnly
|
|
175
|
+
? Promise.resolve({ success: true })
|
|
176
|
+
: fetch(ENDPOINTS.FILE_MOVE, {
|
|
177
|
+
method: 'POST',
|
|
178
|
+
headers: {
|
|
179
|
+
'Content-Type': 'application/json'
|
|
180
|
+
},
|
|
181
|
+
body: JSON.stringify({
|
|
182
|
+
itemId: sourceItem.id,
|
|
183
|
+
targetFolderId: actualTargetId
|
|
184
|
+
})
|
|
185
|
+
}).then((response) => response.json())
|
|
182
186
|
)
|
|
183
187
|
);
|
|
184
188
|
|
|
@@ -191,7 +195,8 @@
|
|
|
191
195
|
const movedItem = findItemById(sourceItem.id);
|
|
192
196
|
if (movedItem) {
|
|
193
197
|
if (movedItem.type === 'folder') {
|
|
194
|
-
|
|
198
|
+
// The folder's new parent is the whole path to the target folder (a nested target is not a root child)
|
|
199
|
+
const newParentPath = targetPath ?? [];
|
|
195
200
|
navigationStore.updateMovedFolderPaths(movedItem.id, newParentPath);
|
|
196
201
|
}
|
|
197
202
|
|
|
@@ -21,6 +21,7 @@ declare const MoveDialog: $$__sveltets_2_IsomorphicComponent<{
|
|
|
21
21
|
sourceItems?: FileSystemItem[];
|
|
22
22
|
currentPath?: string[];
|
|
23
23
|
target?: string;
|
|
24
|
+
/** When true (the Widgetic library has no demo move endpoint), skip the server round-trip and only update the local tree. */ localOnly?: boolean;
|
|
24
25
|
}, {
|
|
25
26
|
move: CustomEvent<any>;
|
|
26
27
|
navigate: CustomEvent<any>;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
/**
|
|
3
3
|
* SearchModal Component
|
|
4
|
+
*
|
|
5
|
+
* Kept for hosts that mount this modal themselves. The File Browser uses inline SearchDropdown instead.
|
|
4
6
|
* Provides real-time search functionality for files and folders with keyboard navigation
|
|
5
7
|
* Features:
|
|
6
8
|
* - Instant search results as user types
|
|
@@ -30,9 +32,9 @@
|
|
|
30
32
|
import type { FileSystemItem } from '../../types';
|
|
31
33
|
|
|
32
34
|
// Local utils imports
|
|
33
|
-
import { getFileTypeOf } from '../../utils/fileKinds';
|
|
34
35
|
import { type FileFilterType } from '../../utils/fileTypes';
|
|
35
36
|
import { highlightMatchesAsHtml } from '../../utils/safeText';
|
|
37
|
+
import { filterSearchResults, findItemPath, searchFileSystem } from '../../utils/searchItems';
|
|
36
38
|
import { useI18n } from '../../i18n';
|
|
37
39
|
import { dirForUserText, useTextDirection } from '@widgetic/design-system/components/textDirection';
|
|
38
40
|
|
|
@@ -87,9 +89,7 @@
|
|
|
87
89
|
|
|
88
90
|
if (term) {
|
|
89
91
|
searchDebounceTimer = setTimeout(() => {
|
|
90
|
-
searchResults = searchFileSystem(root, term)
|
|
91
|
-
(item) => item.name !== 'Root' && (item.type === 'folder' || matchesFilter(item))
|
|
92
|
-
);
|
|
92
|
+
searchResults = filterSearchResults(searchFileSystem(root, term, filter), filter);
|
|
93
93
|
activeIndex = 0;
|
|
94
94
|
}, 150);
|
|
95
95
|
} else {
|
|
@@ -156,46 +156,6 @@
|
|
|
156
156
|
dispatch('close');
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
function searchFileSystem(item: FileSystemItem, term: string): FileSystemItem[] {
|
|
160
|
-
let results: FileSystemItem[] = [];
|
|
161
|
-
|
|
162
|
-
if (item.name.toLowerCase().includes(term) && item.name !== 'Root') {
|
|
163
|
-
if (item.type === 'folder' || matchesFilter(item)) {
|
|
164
|
-
results.push(item);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (item.type === 'folder' && item.children) {
|
|
169
|
-
results = results.concat(item.children.flatMap((child) => searchFileSystem(child, term)));
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return results;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
function matchesFilter(item: FileSystemItem): boolean {
|
|
176
|
-
if (currentFilter === 'all') return true;
|
|
177
|
-
if (item.type === 'folder') return true;
|
|
178
|
-
const fileType = getFileTypeOf(item);
|
|
179
|
-
return fileType === currentFilter;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function findItemPath(root: FileSystemItem, target: FileSystemItem): string[] | null {
|
|
183
|
-
if (root.id === target.id) {
|
|
184
|
-
return [root.id];
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (root.type === 'folder' && root.children) {
|
|
188
|
-
for (const child of root.children) {
|
|
189
|
-
const path = findItemPath(child, target);
|
|
190
|
-
if (path) {
|
|
191
|
-
return [root.id, ...path];
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
159
|
function handleSearchInput(event: Event) {
|
|
200
160
|
const input = event.target as HTMLInputElement;
|
|
201
161
|
input.value = input.value.replace(/^\s+/, '');
|
|
@@ -21,13 +21,17 @@
|
|
|
21
21
|
export let isFolder: boolean;
|
|
22
22
|
export let fileUrl: string | undefined = undefined;
|
|
23
23
|
export let isMissing: boolean = false;
|
|
24
|
+
/** Built-in "My uploads" folder must stay at the root for library hydration — hide destructive / relocate actions. */
|
|
25
|
+
export let itemId: string | undefined = undefined;
|
|
24
26
|
|
|
25
27
|
const { capabilities } = useMediaLibraryContext();
|
|
26
28
|
|
|
27
|
-
$:
|
|
28
|
-
|
|
29
|
+
$: isBuiltInUploadsFolder = isFolder && itemId === 'folder_my_uploads';
|
|
30
|
+
|
|
31
|
+
$: canRename = capabilities.rename && !isMissing && !isBuiltInUploadsFolder;
|
|
32
|
+
$: canMove = capabilities.folders && !isMissing && !isBuiltInUploadsFolder;
|
|
29
33
|
$: canDownload = capabilities.download && !isMissing && !isFolder && !!fileUrl;
|
|
30
|
-
$: canDelete = capabilities.delete;
|
|
34
|
+
$: canDelete = capabilities.delete && !isBuiltInUploadsFolder;
|
|
31
35
|
$: hasAnyAction = canRename || canMove || canDownload || canDelete;
|
|
32
36
|
|
|
33
37
|
const triggerClass =
|
|
@@ -16,6 +16,7 @@ declare const DropdownMenu: $$__sveltets_2_IsomorphicComponent<{
|
|
|
16
16
|
isFolder: boolean;
|
|
17
17
|
fileUrl?: string | undefined;
|
|
18
18
|
isMissing?: boolean;
|
|
19
|
+
/** Built-in "My uploads" folder must stay at the root for library hydration — hide destructive / relocate actions. */ itemId?: string | undefined;
|
|
19
20
|
}, {
|
|
20
21
|
action: CustomEvent<any>;
|
|
21
22
|
} & {
|
package/dist/i18n/messages.d.ts
CHANGED
|
@@ -137,12 +137,18 @@ export declare const FILE_BROWSER_MESSAGES: {
|
|
|
137
137
|
readonly 'upload.unknownError': "Unknown error";
|
|
138
138
|
readonly 'upload.skippedOne': "Skipped duplicate file: {name}";
|
|
139
139
|
readonly 'upload.skippedMany': "{count, plural, one {Skipped # duplicate file} other {Skipped # duplicate files}}";
|
|
140
|
-
readonly 'upload.time.calculating': "calculating…";
|
|
141
140
|
readonly 'upload.time.lessThanSecond': "less than a second";
|
|
142
141
|
readonly 'upload.time.seconds': "{count, plural, one {# second} other {# seconds}}";
|
|
143
142
|
readonly 'upload.time.minutes': "{count, plural, one {# minute} other {# minutes}}";
|
|
144
143
|
/** {time} is a time from "upload.time.*" ("5 seconds"). */
|
|
145
144
|
readonly 'upload.timeLeft': "{time} left";
|
|
145
|
+
readonly 'upload.rowStarting': "Starting…";
|
|
146
|
+
/** Finishing state still shows total size. */
|
|
147
|
+
readonly 'upload.rowFinishingSized': "Finishing… · {total}";
|
|
148
|
+
/** While uploading: localized percent, human-readable sent/total sizes ({sent}, {total}). Optional {time} is already "N seconds" or "less than a second". */
|
|
149
|
+
readonly 'upload.rowActive': "{percent} · {sent} of {total}";
|
|
150
|
+
readonly 'upload.rowActiveWithTime': "{percent} · {sent} of {total} · {time}";
|
|
151
|
+
readonly 'upload.batchSummaryActive': "{total, plural, one {Uploading # file · {percent}} other {Uploading # files · {percent}}}";
|
|
146
152
|
readonly 'upload.complete': "Upload complete.";
|
|
147
153
|
readonly 'upload.removeFile': "Remove file";
|
|
148
154
|
readonly 'upload.uploadingProgress': "{total, plural, other {Uploading {done}/# files}}";
|
package/dist/i18n/messages.js
CHANGED
|
@@ -144,12 +144,18 @@ export const FILE_BROWSER_MESSAGES = {
|
|
|
144
144
|
'upload.unknownError': 'Unknown error',
|
|
145
145
|
'upload.skippedOne': 'Skipped duplicate file: {name}',
|
|
146
146
|
'upload.skippedMany': '{count, plural, one {Skipped # duplicate file} other {Skipped # duplicate files}}',
|
|
147
|
-
'upload.time.calculating': 'calculating…',
|
|
148
147
|
'upload.time.lessThanSecond': 'less than a second',
|
|
149
148
|
'upload.time.seconds': '{count, plural, one {# second} other {# seconds}}',
|
|
150
149
|
'upload.time.minutes': '{count, plural, one {# minute} other {# minutes}}',
|
|
151
150
|
/** {time} is a time from "upload.time.*" ("5 seconds"). */
|
|
152
151
|
'upload.timeLeft': '{time} left',
|
|
152
|
+
'upload.rowStarting': 'Starting…',
|
|
153
|
+
/** Finishing state still shows total size. */
|
|
154
|
+
'upload.rowFinishingSized': 'Finishing… · {total}',
|
|
155
|
+
/** While uploading: localized percent, human-readable sent/total sizes ({sent}, {total}). Optional {time} is already "N seconds" or "less than a second". */
|
|
156
|
+
'upload.rowActive': '{percent} · {sent} of {total}',
|
|
157
|
+
'upload.rowActiveWithTime': '{percent} · {sent} of {total} · {time}',
|
|
158
|
+
'upload.batchSummaryActive': '{total, plural, one {Uploading # file · {percent}} other {Uploading # files · {percent}}}',
|
|
153
159
|
'upload.complete': 'Upload complete.',
|
|
154
160
|
'upload.removeFile': 'Remove file',
|
|
155
161
|
'upload.uploadingProgress': '{total, plural, other {Uploading {done}/# files}}',
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
export type SplitFileName = {
|
|
2
|
+
baseName: string;
|
|
3
|
+
extension: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Base name vs extension using the same rule as `getRenamedFileName`: the last `.` segment is the extension when
|
|
7
|
+
* there is more than one segment; a leading dot (`.env`) means no extension; `a.b.c` → base `a.b`, extension `c`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function splitFileName(name: string): SplitFileName;
|
|
1
10
|
/**
|
|
2
11
|
* The name a file gets when the visitor types `typedName` for it. A file keeps its extension so it stays the same
|
|
3
12
|
* kind of file: typing "holiday" for "beach.jpg" gives "holiday.jpg", while typing "holiday.jpg" stays as typed.
|
package/dist/utils/fileNames.js
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base name vs extension using the same rule as `getRenamedFileName`: the last `.` segment is the extension when
|
|
3
|
+
* there is more than one segment; a leading dot (`.env`) means no extension; `a.b.c` → base `a.b`, extension `c`.
|
|
4
|
+
*/
|
|
5
|
+
export function splitFileName(name) {
|
|
6
|
+
// One segment after a leading dot only (`/.env`, `.gitignore`): not a "base.extension" pair for the rename UI.
|
|
7
|
+
if (/^\.[^.]+$/.test(name)) {
|
|
8
|
+
return { baseName: name, extension: '' };
|
|
9
|
+
}
|
|
10
|
+
const parts = name.split('.');
|
|
11
|
+
if (parts.length <= 1) {
|
|
12
|
+
return { baseName: name, extension: '' };
|
|
13
|
+
}
|
|
14
|
+
const originalExtension = parts[parts.length - 1];
|
|
15
|
+
return {
|
|
16
|
+
baseName: parts.slice(0, -1).join('.'),
|
|
17
|
+
extension: originalExtension ?? ''
|
|
18
|
+
};
|
|
19
|
+
}
|
|
1
20
|
/**
|
|
2
21
|
* The name a file gets when the visitor types `typedName` for it. A file keeps its extension so it stays the same
|
|
3
22
|
* kind of file: typing "holiday" for "beach.jpg" gives "holiday.jpg", while typing "holiday.jpg" stays as typed.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers for the Widgetic library-mode tree: persistence covers the whole root and API hydration merges by id.
|
|
3
|
+
*/
|
|
4
|
+
import type { FileSystemItem } from '../types';
|
|
5
|
+
/** Deep clone with Date revival for `modified` fields (JSON persistence). */
|
|
6
|
+
export declare function reviveFileSystemDates(items: FileSystemItem[]): FileSystemItem[];
|
|
7
|
+
export declare function findItemByIdInTree(root: FileSystemItem, id: string): FileSystemItem | null;
|
|
8
|
+
/**
|
|
9
|
+
* Removes duplicate ids from a tree (keeps the first occurrence per depth-first order).
|
|
10
|
+
* Why: legacy data could list the same file id twice.
|
|
11
|
+
*/
|
|
12
|
+
export declare function dedupeTreeIds(items: FileSystemItem[]): FileSystemItem[];
|
|
13
|
+
/**
|
|
14
|
+
* Merges flat API file list into a saved tree: known ids refresh in place (their saved, possibly renamed, name is kept);
|
|
15
|
+
* unknown files go under My Uploads (top level).
|
|
16
|
+
*/
|
|
17
|
+
export declare function mergeApiFilesIntoSavedTree(rootChildren: FileSystemItem[] | null, apiFiles: FileSystemItem[], myUploadsName: string): FileSystemItem[];
|
|
18
|
+
/**
|
|
19
|
+
* One-step legacy migration: old persist stored only My Uploads' children under key `current`.
|
|
20
|
+
* Wraps them inside `folder_my_uploads` at the root.
|
|
21
|
+
*/
|
|
22
|
+
export declare function migrateLegacyUploadsOnlyChildren(legacyChildren: FileSystemItem[], myUploadsName: string): FileSystemItem[];
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers for the Widgetic library-mode tree: persistence covers the whole root and API hydration merges by id.
|
|
3
|
+
*/
|
|
4
|
+
const BUILTIN_UPLOADS_ID = 'folder_my_uploads';
|
|
5
|
+
function sortFolderContentsLikeGrid(items, sort) {
|
|
6
|
+
items.sort((a, b) => {
|
|
7
|
+
if (a.type === 'folder' && b.type !== 'folder')
|
|
8
|
+
return -1;
|
|
9
|
+
if (a.type !== 'folder' && b.type === 'folder')
|
|
10
|
+
return 1;
|
|
11
|
+
let comparison = 0;
|
|
12
|
+
switch (sort.criteria) {
|
|
13
|
+
case 'name':
|
|
14
|
+
comparison = a.name.localeCompare(b.name);
|
|
15
|
+
break;
|
|
16
|
+
case 'date':
|
|
17
|
+
comparison = new Date(a.modified || 0).getTime() - new Date(b.modified || 0).getTime();
|
|
18
|
+
break;
|
|
19
|
+
case 'size':
|
|
20
|
+
comparison = (a.size || 0) - (b.size || 0);
|
|
21
|
+
break;
|
|
22
|
+
default:
|
|
23
|
+
comparison = 0;
|
|
24
|
+
}
|
|
25
|
+
return sort.direction === 'asc' ? comparison : -comparison;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** Sort children like the File Browser grid: folders first, then by name. */
|
|
29
|
+
function sortRootLevelItems(a, b) {
|
|
30
|
+
if (a.type === 'folder' && b.type !== 'folder')
|
|
31
|
+
return -1;
|
|
32
|
+
if (a.type !== 'folder' && b.type === 'folder')
|
|
33
|
+
return 1;
|
|
34
|
+
return a.name.localeCompare(b.name);
|
|
35
|
+
}
|
|
36
|
+
function ensureDate(value) {
|
|
37
|
+
if (value === undefined)
|
|
38
|
+
return undefined;
|
|
39
|
+
if (value instanceof Date)
|
|
40
|
+
return value.toISOString();
|
|
41
|
+
return typeof value === 'string' ? value : undefined;
|
|
42
|
+
}
|
|
43
|
+
/** Deep clone with Date revival for `modified` fields (JSON persistence). */
|
|
44
|
+
export function reviveFileSystemDates(items) {
|
|
45
|
+
return items.map((item) => {
|
|
46
|
+
const revived = {
|
|
47
|
+
...item,
|
|
48
|
+
modified: item.modified !== undefined ? new Date(item.modified) : undefined
|
|
49
|
+
};
|
|
50
|
+
if (item.children?.length) {
|
|
51
|
+
revived.children = reviveFileSystemDates(item.children);
|
|
52
|
+
}
|
|
53
|
+
return revived;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export function findItemByIdInTree(root, id) {
|
|
57
|
+
if (root.id === id)
|
|
58
|
+
return root;
|
|
59
|
+
if (!root.children)
|
|
60
|
+
return null;
|
|
61
|
+
for (const child of root.children) {
|
|
62
|
+
const found = findItemByIdInTree(child, id);
|
|
63
|
+
if (found)
|
|
64
|
+
return found;
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Removes duplicate ids from a tree (keeps the first occurrence per depth-first order).
|
|
70
|
+
* Why: legacy data could list the same file id twice.
|
|
71
|
+
*/
|
|
72
|
+
export function dedupeTreeIds(items) {
|
|
73
|
+
const seen = new Set();
|
|
74
|
+
function walk(list) {
|
|
75
|
+
const out = [];
|
|
76
|
+
for (const item of list) {
|
|
77
|
+
if (seen.has(item.id))
|
|
78
|
+
continue;
|
|
79
|
+
seen.add(item.id);
|
|
80
|
+
const next = { ...item };
|
|
81
|
+
if (item.children?.length) {
|
|
82
|
+
next.children = walk(item.children);
|
|
83
|
+
}
|
|
84
|
+
out.push(next);
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
return walk(items);
|
|
89
|
+
}
|
|
90
|
+
function getOrCreateMyUploadsFolder(rootChildren, myUploadsName) {
|
|
91
|
+
let myUploads = rootChildren.find((c) => c.id === BUILTIN_UPLOADS_ID && c.type === 'folder');
|
|
92
|
+
if (!myUploads) {
|
|
93
|
+
myUploads = {
|
|
94
|
+
id: BUILTIN_UPLOADS_ID,
|
|
95
|
+
name: myUploadsName,
|
|
96
|
+
type: 'folder',
|
|
97
|
+
children: [],
|
|
98
|
+
modified: new Date().toISOString()
|
|
99
|
+
};
|
|
100
|
+
return { rootChildren: [...rootChildren, myUploads].sort(sortRootLevelItems), myUploads };
|
|
101
|
+
}
|
|
102
|
+
if (myUploads.name !== myUploadsName) {
|
|
103
|
+
const updated = { ...myUploads, name: myUploadsName };
|
|
104
|
+
return {
|
|
105
|
+
rootChildren: rootChildren.map((c) => (c === myUploads ? updated : c)),
|
|
106
|
+
myUploads: updated
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return { rootChildren, myUploads };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Merges flat API file list into a saved tree: known ids refresh in place (their saved, possibly renamed, name is kept);
|
|
113
|
+
* unknown files go under My Uploads (top level).
|
|
114
|
+
*/
|
|
115
|
+
export function mergeApiFilesIntoSavedTree(rootChildren, apiFiles, myUploadsName) {
|
|
116
|
+
let children = dedupeTreeIds(reviveFileSystemDates(rootChildren ?? []));
|
|
117
|
+
const ensured = getOrCreateMyUploadsFolder(children, myUploadsName);
|
|
118
|
+
children = ensured.rootChildren;
|
|
119
|
+
const myUploads = ensured.myUploads;
|
|
120
|
+
function replaceFileById(items, id, file) {
|
|
121
|
+
for (let i = 0; i < items.length; i++) {
|
|
122
|
+
const it = items[i];
|
|
123
|
+
if (it.type === 'file' && it.id === id) {
|
|
124
|
+
// The server owns the file (url, size, thumbnail), the visitor owns its name: a rename stays after a reload
|
|
125
|
+
items[i] = { ...file, name: it.name, modified: ensureDate(file.modified) };
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
if (it.type === 'folder' && it.children?.length) {
|
|
129
|
+
if (replaceFileById(it.children, id, file))
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
const rootFolder = {
|
|
136
|
+
id: 'root',
|
|
137
|
+
name: 'Root',
|
|
138
|
+
type: 'folder',
|
|
139
|
+
children
|
|
140
|
+
};
|
|
141
|
+
for (const file of apiFiles) {
|
|
142
|
+
if (file.type !== 'file')
|
|
143
|
+
continue;
|
|
144
|
+
const normalized = {
|
|
145
|
+
...file,
|
|
146
|
+
modified: ensureDate(file.modified)
|
|
147
|
+
};
|
|
148
|
+
if (findItemByIdInTree(rootFolder, file.id)) {
|
|
149
|
+
replaceFileById(rootFolder.children, file.id, normalized);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
myUploads.children = myUploads.children ?? [];
|
|
153
|
+
if (!myUploads.children.some((c) => c.type === 'file' && c.id === file.id)) {
|
|
154
|
+
myUploads.children.push(normalized);
|
|
155
|
+
sortFolderContentsLikeGrid(myUploads.children, {
|
|
156
|
+
criteria: 'name',
|
|
157
|
+
direction: 'asc'
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return dedupeTreeIds(rootFolder.children);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* One-step legacy migration: old persist stored only My Uploads' children under key `current`.
|
|
166
|
+
* Wraps them inside `folder_my_uploads` at the root.
|
|
167
|
+
*/
|
|
168
|
+
export function migrateLegacyUploadsOnlyChildren(legacyChildren, myUploadsName) {
|
|
169
|
+
const uploadsFolder = {
|
|
170
|
+
id: BUILTIN_UPLOADS_ID,
|
|
171
|
+
name: myUploadsName,
|
|
172
|
+
type: 'folder',
|
|
173
|
+
children: dedupeTreeIds(reviveFileSystemDates(legacyChildren)),
|
|
174
|
+
modified: new Date().toISOString()
|
|
175
|
+
};
|
|
176
|
+
return [uploadsFolder];
|
|
177
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global file-tree search helpers shared by SearchModal and SearchDropdown.
|
|
3
|
+
*/
|
|
4
|
+
import type { FileSystemItem } from '../types';
|
|
5
|
+
import type { FileFilterType } from './fileTypes';
|
|
6
|
+
/** Whether a file matches the current type filter (folders are handled separately). */
|
|
7
|
+
export declare function matchesFileFilter(item: FileSystemItem, currentFilter: FileFilterType): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Depth-first collect of items whose name includes `term` (case-insensitive via caller passing lowercased term),
|
|
10
|
+
* skipping the synthetic item named `Root`. Folders always participate; files only if they match the type filter.
|
|
11
|
+
*/
|
|
12
|
+
export declare function searchFileSystem(item: FileSystemItem, termLower: string, currentFilter: FileFilterType): FileSystemItem[];
|
|
13
|
+
/**
|
|
14
|
+
* Returns the path of folder ids from `root` down to `target`, or null. Path includes root id and target id.
|
|
15
|
+
*/
|
|
16
|
+
export declare function findItemPath(root: FileSystemItem, target: FileSystemItem): string[] | null;
|
|
17
|
+
/** Joins the names of a location in a result line ("Files / Uploads"): a path separator, not a word, so it is not translated. */
|
|
18
|
+
export declare const SEARCH_LOCATION_SEPARATOR = " / ";
|
|
19
|
+
/**
|
|
20
|
+
* Breadcrumb-style location for a search result: translated root label plus parent folder names (not the result name).
|
|
21
|
+
* `idPath` is from {@link findItemPath} for the result item.
|
|
22
|
+
*/
|
|
23
|
+
export declare function parentFolderLocationLabel(root: FileSystemItem, idPath: string[], rootDisplayLabel: string, listSeparator: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Applies the same post-search filter as the legacy modal: drop `Root`, keep folders always, files must match filter.
|
|
26
|
+
*/
|
|
27
|
+
export declare function filterSearchResults(items: FileSystemItem[], currentFilter: FileFilterType): FileSystemItem[];
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { getFileTypeOf } from './fileKinds';
|
|
2
|
+
/** Whether a file matches the current type filter (folders are handled separately). */
|
|
3
|
+
export function matchesFileFilter(item, currentFilter) {
|
|
4
|
+
if (currentFilter === 'all')
|
|
5
|
+
return true;
|
|
6
|
+
if (item.type === 'folder')
|
|
7
|
+
return true;
|
|
8
|
+
const fileType = getFileTypeOf(item);
|
|
9
|
+
return fileType === currentFilter;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Depth-first collect of items whose name includes `term` (case-insensitive via caller passing lowercased term),
|
|
13
|
+
* skipping the synthetic item named `Root`. Folders always participate; files only if they match the type filter.
|
|
14
|
+
*/
|
|
15
|
+
export function searchFileSystem(item, termLower, currentFilter) {
|
|
16
|
+
let results = [];
|
|
17
|
+
if (item.name.toLowerCase().includes(termLower) && item.name !== 'Root') {
|
|
18
|
+
if (item.type === 'folder' || matchesFileFilter(item, currentFilter)) {
|
|
19
|
+
results.push(item);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (item.type === 'folder' && item.children) {
|
|
23
|
+
results = results.concat(item.children.flatMap((child) => searchFileSystem(child, termLower, currentFilter)));
|
|
24
|
+
}
|
|
25
|
+
return results;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns the path of folder ids from `root` down to `target`, or null. Path includes root id and target id.
|
|
29
|
+
*/
|
|
30
|
+
export function findItemPath(root, target) {
|
|
31
|
+
if (root.id === target.id) {
|
|
32
|
+
return [root.id];
|
|
33
|
+
}
|
|
34
|
+
if (root.type === 'folder' && root.children) {
|
|
35
|
+
for (const child of root.children) {
|
|
36
|
+
const path = findItemPath(child, target);
|
|
37
|
+
if (path) {
|
|
38
|
+
return [root.id, ...path];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
/** Joins the names of a location in a result line ("Files / Uploads"): a path separator, not a word, so it is not translated. */
|
|
45
|
+
export const SEARCH_LOCATION_SEPARATOR = ' / ';
|
|
46
|
+
/**
|
|
47
|
+
* Breadcrumb-style location for a search result: translated root label plus parent folder names (not the result name).
|
|
48
|
+
* `idPath` is from {@link findItemPath} for the result item.
|
|
49
|
+
*/
|
|
50
|
+
export function parentFolderLocationLabel(root, idPath, rootDisplayLabel, listSeparator) {
|
|
51
|
+
if (!idPath.length)
|
|
52
|
+
return '';
|
|
53
|
+
/** The tree root row in search is still "in" the library; show the same word as the header. */
|
|
54
|
+
if (idPath.length === 1) {
|
|
55
|
+
return rootDisplayLabel;
|
|
56
|
+
}
|
|
57
|
+
const parts = [];
|
|
58
|
+
let node = root;
|
|
59
|
+
parts.push(rootDisplayLabel);
|
|
60
|
+
for (let i = 1; i < idPath.length - 1; i++) {
|
|
61
|
+
const segmentId = idPath[i];
|
|
62
|
+
const child = node.children?.find((c) => c.id === segmentId);
|
|
63
|
+
if (!child)
|
|
64
|
+
break;
|
|
65
|
+
parts.push(child.name);
|
|
66
|
+
if (child.type === 'folder') {
|
|
67
|
+
node = child;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return parts.join(listSeparator);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Applies the same post-search filter as the legacy modal: drop `Root`, keep folders always, files must match filter.
|
|
74
|
+
*/
|
|
75
|
+
export function filterSearchResults(items, currentFilter) {
|
|
76
|
+
return items.filter((item) => item.name !== 'Root' && (item.type === 'folder' || matchesFileFilter(item, currentFilter)));
|
|
77
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Minimum sampling window before we show a time-left estimate (ms). */
|
|
2
|
+
export declare const UPLOAD_TIME_ESTIMATE_MIN_SAMPLES_MS = 1000;
|
|
3
|
+
/** Visual throttle for progress UI (ms). */
|
|
4
|
+
export declare const UPLOAD_UI_PROGRESS_THROTTLE_MS = 100;
|
|
5
|
+
export type ThroughputSample = {
|
|
6
|
+
t: number;
|
|
7
|
+
uploadedBytes: number;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Exponential moving average of throughput (bytes per second) from (time, cumulative uploaded) samples.
|
|
11
|
+
* Returns a non-negative finite rate or undefined when there is not enough data.
|
|
12
|
+
*/
|
|
13
|
+
export declare function movingAverageThroughputBps(previousBps: number | undefined, prevSample: ThroughputSample | undefined, next: ThroughputSample, smoothing?: number): number | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Seconds left from remaining bytes and average throughput; never negative or NaN.
|
|
16
|
+
*/
|
|
17
|
+
export declare function secondsRemainingFromThroughput(remainingBytes: number, throughputBps: number | undefined): number | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Overall fractional progress from per-file byte counts (0–1). Completed/failed items contribute full size when `includeComplete` is true.
|
|
20
|
+
*/
|
|
21
|
+
export declare function overallBytesProgressRatio(params: {
|
|
22
|
+
items: Array<{
|
|
23
|
+
status: 'uploading' | 'complete' | 'failed';
|
|
24
|
+
bytesUploaded: number;
|
|
25
|
+
bytesTotal: number;
|
|
26
|
+
}>;
|
|
27
|
+
includeComplete: boolean;
|
|
28
|
+
}): number;
|
|
29
|
+
export declare function ratioToPercent(ratio: number): number;
|