gradio-pr-bot commited on
Commit
67ad93e
·
verified ·
1 Parent(s): 1cbbbdb

Upload folder using huggingface_hub

Browse files
6.4.1/upload/package.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/upload",
3
+ "version": "0.17.4",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "main": "src/index.ts",
7
+ "author": "",
8
+ "license": "ISC",
9
+ "dependencies": {
10
+ "@gradio/atoms": "workspace:^",
11
+ "@gradio/icons": "workspace:^",
12
+ "@gradio/client": "workspace:^",
13
+ "@gradio/utils": "workspace:^"
14
+ },
15
+ "main_changeset": true,
16
+ "exports": {
17
+ ".": {
18
+ "gradio": "./src/index.ts",
19
+ "svelte": "./dist/src/index.js",
20
+ "types": "./dist/src/index.d.ts"
21
+ }
22
+ },
23
+ "peerDependencies": {
24
+ "svelte": "^5.48.0"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/gradio-app/gradio.git",
29
+ "directory": "js/upload"
30
+ }
31
+ }
6.4.1/upload/src/ModifyUpload.svelte ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { IconButton, IconButtonWrapper } from "@gradio/atoms";
3
+ import type { I18nFormatter } from "@gradio/utils";
4
+ import { Edit, Clear, Undo, Download } from "@gradio/icons";
5
+ import { DownloadLink } from "@gradio/atoms";
6
+
7
+ let {
8
+ editable = false,
9
+ undoable = false,
10
+ download = null,
11
+ i18n,
12
+ onedit,
13
+ onclear,
14
+ onundo,
15
+ children
16
+ }: {
17
+ editable?: boolean;
18
+ undoable?: boolean;
19
+ download?: string | null;
20
+ i18n: I18nFormatter;
21
+ onedit?: () => void;
22
+ onclear?: () => void;
23
+ onundo?: () => void;
24
+ children?: import("svelte").Snippet;
25
+ } = $props();
26
+ </script>
27
+
28
+ <IconButtonWrapper>
29
+ {#if editable}
30
+ <IconButton
31
+ Icon={Edit}
32
+ label={i18n("common.edit")}
33
+ onclick={() => onedit?.()}
34
+ />
35
+ {/if}
36
+
37
+ {#if undoable}
38
+ <IconButton
39
+ Icon={Undo}
40
+ label={i18n("common.undo")}
41
+ onclick={() => onundo?.()}
42
+ />
43
+ {/if}
44
+
45
+ {#if download}
46
+ <DownloadLink href={download} download>
47
+ <IconButton Icon={Download} label={i18n("common.download")} />
48
+ </DownloadLink>
49
+ {/if}
50
+
51
+ {#if children}{@render children()}{/if}
52
+
53
+ <IconButton
54
+ Icon={Clear}
55
+ label={i18n("common.clear")}
56
+ onclick={(event) => {
57
+ onclear?.();
58
+ event.stopPropagation();
59
+ }}
60
+ />
61
+ </IconButtonWrapper>
6.4.1/upload/src/Upload.svelte ADDED
@@ -0,0 +1,398 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { tick } from "svelte";
3
+ import type { FileData } from "@gradio/client";
4
+ import { prepare_files, type Client } from "@gradio/client";
5
+ import UploadProgress from "./UploadProgress.svelte";
6
+ import { create_drag } from "./utils";
7
+
8
+ const { drag, open_file_upload: _open_file_upload } = create_drag();
9
+
10
+ let {
11
+ filetype = null,
12
+ dragging = $bindable(false),
13
+ boundedheight = true,
14
+ center = true,
15
+ flex = true,
16
+ file_count = "single",
17
+ disable_click = false,
18
+ root,
19
+ hidden = false,
20
+ format = "file",
21
+ uploading = $bindable(false),
22
+ show_progress = true,
23
+ max_file_size = null,
24
+ upload,
25
+ stream_handler,
26
+ icon_upload = false,
27
+ height = undefined,
28
+ aria_label = undefined,
29
+ upload_promise = $bindable(),
30
+ onload,
31
+ onerror,
32
+ children
33
+ }: {
34
+ filetype?: string | string[] | null;
35
+ dragging?: boolean;
36
+ boundedheight?: boolean;
37
+ center?: boolean;
38
+ flex?: boolean;
39
+ file_count?: "single" | "multiple" | "directory";
40
+ disable_click?: boolean;
41
+ root: string;
42
+ hidden?: boolean;
43
+ format?: "blob" | "file";
44
+ uploading?: boolean;
45
+ show_progress?: boolean;
46
+ max_file_size?: number | null;
47
+ upload: Client["upload"];
48
+ stream_handler: Client["stream"];
49
+ icon_upload?: boolean;
50
+ height?: number | string | undefined;
51
+ aria_label?: string | undefined;
52
+ upload_promise?: Promise<(FileData | null)[]>;
53
+ onload?: (data: FileData | FileData[] | Blob | File) => void;
54
+ onerror?: (error: string) => void;
55
+ children?: import("svelte").Snippet;
56
+ } = $props();
57
+
58
+ export function open_upload(): void {
59
+ _open_file_upload();
60
+ }
61
+
62
+ let upload_id: string = "";
63
+ let file_data: FileData[];
64
+ let accept_file_types: string | null = $state(null);
65
+ let use_post_upload_validation: boolean | null = null;
66
+
67
+ const get_ios = (): boolean => {
68
+ if (typeof navigator !== "undefined") {
69
+ const userAgent = navigator.userAgent.toLowerCase();
70
+ return userAgent.indexOf("iphone") > -1 || userAgent.indexOf("ipad") > -1;
71
+ }
72
+ return false;
73
+ };
74
+
75
+ let ios = get_ios();
76
+
77
+ const validFileTypes = ["image", "video", "audio", "text", "file"];
78
+ const process_file_type = (type: string): string => {
79
+ if (ios && type.startsWith(".")) {
80
+ use_post_upload_validation = true;
81
+ return type;
82
+ }
83
+ if (ios && type.includes("file/*")) {
84
+ return "*";
85
+ }
86
+ if (type.startsWith(".") || type.endsWith("/*")) {
87
+ return type;
88
+ }
89
+ if (validFileTypes.includes(type)) {
90
+ return type + "/*";
91
+ }
92
+ return "." + type;
93
+ };
94
+
95
+ $effect(() => {
96
+ if (filetype == null) {
97
+ accept_file_types = null;
98
+ } else if (typeof filetype === "string") {
99
+ accept_file_types = process_file_type(filetype);
100
+ } else if (ios && filetype.includes("file/*")) {
101
+ accept_file_types = "*";
102
+ } else {
103
+ const processed = filetype.map(process_file_type);
104
+ accept_file_types = processed.join(", ");
105
+ }
106
+ });
107
+
108
+ export function paste_clipboard(): void {
109
+ navigator.clipboard.read().then(async (items) => {
110
+ for (let i = 0; i < items.length; i++) {
111
+ const type = items[i].types.find((t) => t.startsWith("image/"));
112
+ if (type) {
113
+ items[i].getType(type).then(async (blob) => {
114
+ const file = new File(
115
+ [blob],
116
+ `clipboard.${type.replace("image/", "")}`
117
+ );
118
+ await load_files([file]);
119
+ });
120
+ break;
121
+ }
122
+ }
123
+ });
124
+ }
125
+
126
+ export function open_file_upload(): void {
127
+ _open_file_upload();
128
+ }
129
+
130
+ async function handle_upload(
131
+ file_data: FileData[],
132
+ _upload_id?: string
133
+ ): Promise<(FileData | null)[]> {
134
+ if (!_upload_id) {
135
+ upload_id = Math.random().toString(36).substring(2, 15);
136
+ } else {
137
+ upload_id = _upload_id;
138
+ }
139
+
140
+ await tick();
141
+ uploading = true;
142
+ upload_promise = new Promise(async (resolve) => {
143
+ try {
144
+ const _file_data = await upload(
145
+ file_data,
146
+ root,
147
+ upload_id,
148
+ max_file_size ?? Infinity
149
+ );
150
+ onload?.(file_count === "single" ? _file_data?.[0] : _file_data);
151
+ resolve(_file_data || []);
152
+ uploading = false;
153
+ } catch (e) {
154
+ onerror?.((e as Error).message);
155
+ uploading = false;
156
+ resolve([]);
157
+ }
158
+ });
159
+
160
+ return upload_promise;
161
+ }
162
+
163
+ function is_valid_mimetype(
164
+ file_accept: string | string[] | null,
165
+ uploaded_file_extension: string,
166
+ uploaded_file_type: string
167
+ ): boolean {
168
+ if (
169
+ !file_accept ||
170
+ file_accept === "*" ||
171
+ file_accept === "file/*" ||
172
+ (Array.isArray(file_accept) &&
173
+ file_accept.some((accept) => accept === "*" || accept === "file/*"))
174
+ ) {
175
+ return true;
176
+ }
177
+ let acceptArray: string[];
178
+ if (typeof file_accept === "string") {
179
+ acceptArray = file_accept.split(",").map((s) => s.trim());
180
+ } else if (Array.isArray(file_accept)) {
181
+ acceptArray = file_accept;
182
+ } else {
183
+ return false;
184
+ }
185
+
186
+ return (
187
+ acceptArray.includes(uploaded_file_extension) ||
188
+ acceptArray.some((type) => {
189
+ const [category] = type.split("/").map((s) => s.trim());
190
+ return (
191
+ type.endsWith("/*") && uploaded_file_type.startsWith(category + "/")
192
+ );
193
+ })
194
+ );
195
+ }
196
+
197
+ export async function load_files(
198
+ files: File[] | Blob[],
199
+ upload_id?: string
200
+ ): Promise<(FileData | null)[] | void> {
201
+ if (!files.length) {
202
+ return;
203
+ }
204
+ let _files: File[] = files.map(
205
+ (f) =>
206
+ new File([f], f instanceof File ? f.name : "file", { type: f.type })
207
+ );
208
+
209
+ if (ios && use_post_upload_validation) {
210
+ _files = _files.filter((file) => {
211
+ if (is_valid_file(file)) {
212
+ return true;
213
+ }
214
+ onerror?.(`Invalid file type: ${file.name}. Only ${filetype} allowed.`);
215
+ return false;
216
+ });
217
+
218
+ if (_files.length === 0) {
219
+ return [];
220
+ }
221
+ }
222
+
223
+ file_data = await prepare_files(_files);
224
+ return await handle_upload(file_data, upload_id);
225
+ }
226
+
227
+ function is_valid_file(file: File): boolean {
228
+ if (!filetype) return true;
229
+
230
+ const allowed_types = Array.isArray(filetype) ? filetype : [filetype];
231
+
232
+ return allowed_types.some((type) => {
233
+ const processed_type = process_file_type(type);
234
+
235
+ if (processed_type.startsWith(".")) {
236
+ return file.name.toLowerCase().endsWith(processed_type.toLowerCase());
237
+ }
238
+
239
+ if (processed_type === "*") {
240
+ return true;
241
+ }
242
+
243
+ if (processed_type.endsWith("/*")) {
244
+ const [category] = processed_type.split("/");
245
+ return file.type.startsWith(category + "/");
246
+ }
247
+
248
+ return file.type === processed_type;
249
+ });
250
+ }
251
+
252
+ async function load_files_from_upload(files: File[]): Promise<void> {
253
+ const files_to_load = files.filter((file) => {
254
+ const file_extension = "." + file.name.toLowerCase().split(".").pop();
255
+ if (
256
+ file_extension &&
257
+ is_valid_mimetype(accept_file_types, file_extension, file.type)
258
+ ) {
259
+ return true;
260
+ }
261
+ if (
262
+ file_extension && Array.isArray(filetype)
263
+ ? filetype.includes(file_extension)
264
+ : file_extension === filetype
265
+ ) {
266
+ return true;
267
+ }
268
+ onerror?.(`Invalid file type only ${filetype} allowed.`);
269
+ return false;
270
+ });
271
+ if (format != "blob") {
272
+ await load_files(files_to_load);
273
+ } else {
274
+ if (file_count === "single") {
275
+ onload?.(files_to_load[0]);
276
+ return;
277
+ }
278
+ onload?.(files_to_load);
279
+ }
280
+ }
281
+
282
+ export async function load_files_from_drop(e: DragEvent): Promise<void> {
283
+ dragging = false;
284
+ if (!e.dataTransfer?.files) return;
285
+ const files_to_load = Array.from(e.dataTransfer.files).filter(
286
+ is_valid_file
287
+ );
288
+
289
+ if (format != "blob") {
290
+ await load_files(files_to_load);
291
+ } else {
292
+ if (file_count === "single") {
293
+ onload?.(files_to_load[0]);
294
+ return;
295
+ }
296
+ onload?.(files_to_load);
297
+ }
298
+ }
299
+ </script>
300
+
301
+ {#if filetype === "clipboard"}
302
+ <button
303
+ class:hidden
304
+ class:center
305
+ class:boundedheight
306
+ class:flex
307
+ class:icon-mode={icon_upload}
308
+ style:height={icon_upload
309
+ ? ""
310
+ : height
311
+ ? typeof height === "number"
312
+ ? height + "px"
313
+ : height
314
+ : "100%"}
315
+ tabindex={hidden ? -1 : 0}
316
+ onclick={paste_clipboard}
317
+ aria-label={aria_label || "Paste from clipboard"}
318
+ >
319
+ {#if children}{@render children()}{/if}
320
+ </button>
321
+ {:else if uploading && show_progress}
322
+ {#if !hidden}
323
+ <UploadProgress {root} {upload_id} files={file_data} {stream_handler} />
324
+ {/if}
325
+ {:else}
326
+ <button
327
+ class:hidden
328
+ class:center
329
+ class:boundedheight
330
+ class:flex
331
+ class:disable_click
332
+ class:icon-mode={icon_upload}
333
+ style:height={icon_upload
334
+ ? ""
335
+ : height
336
+ ? typeof height === "number"
337
+ ? height + "px"
338
+ : height
339
+ : "100%"}
340
+ tabindex={hidden ? -1 : 0}
341
+ use:drag={{
342
+ on_drag_change: (d) => (dragging = d),
343
+ on_files: (files) => load_files_from_upload(files),
344
+ accepted_types: accept_file_types,
345
+ mode: file_count,
346
+ disable_click
347
+ }}
348
+ aria-label={aria_label || "Click to upload or drop files"}
349
+ aria-dropeffect="copy"
350
+ >
351
+ {#if children}{@render children()}{/if}
352
+ </button>
353
+ {/if}
354
+
355
+ <style>
356
+ button {
357
+ cursor: pointer;
358
+ width: var(--size-full);
359
+ }
360
+
361
+ .center {
362
+ display: flex;
363
+ justify-content: center;
364
+ }
365
+ .flex {
366
+ display: flex;
367
+ flex-direction: column;
368
+ justify-content: center;
369
+ align-items: center;
370
+ }
371
+ .hidden {
372
+ display: none;
373
+ position: absolute;
374
+ flex-grow: 0;
375
+ }
376
+
377
+ .hidden :global(svg) {
378
+ display: none;
379
+ }
380
+
381
+ .disable_click {
382
+ cursor: default;
383
+ }
384
+
385
+ .icon-mode {
386
+ position: absolute !important;
387
+ width: var(--size-4);
388
+ height: var(--size-4);
389
+ padding: 0;
390
+ min-height: 0;
391
+ border-radius: var(--radius-circle);
392
+ }
393
+
394
+ .icon-mode :global(svg) {
395
+ width: var(--size-4);
396
+ height: var(--size-4);
397
+ }
398
+ </style>
6.4.1/upload/src/UploadProgress.svelte ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { FileData, type Client } from "@gradio/client";
3
+ import { onMount, onDestroy } from "svelte";
4
+
5
+ type FileDataWithProgress = FileData & { progress: number };
6
+
7
+ let {
8
+ upload_id,
9
+ root,
10
+ files,
11
+ stream_handler,
12
+ ondone
13
+ }: {
14
+ upload_id: string;
15
+ root: string;
16
+ files: FileData[];
17
+ stream_handler: Client["stream"];
18
+ ondone?: () => void;
19
+ } = $props();
20
+
21
+ let stream: Awaited<ReturnType<Client["stream"]>>;
22
+ let progress = $state(false);
23
+ let current_file_upload = $state<FileDataWithProgress>();
24
+ let file_to_display = $derived(current_file_upload || files_with_progress[0]);
25
+
26
+ let files_with_progress = $state<FileDataWithProgress[]>(
27
+ files.map((file) => {
28
+ return {
29
+ ...file,
30
+ progress: 0
31
+ };
32
+ })
33
+ );
34
+
35
+ function handleProgress(filename: string, chunk_size: number): void {
36
+ // Find the corresponding file in the array and update its progress
37
+ files_with_progress = files_with_progress.map((file) => {
38
+ if (file.orig_name === filename) {
39
+ file.progress += chunk_size;
40
+ }
41
+ return file;
42
+ });
43
+ }
44
+
45
+ function getProgress(file: FileDataWithProgress): number {
46
+ return (file.progress * 100) / (file.size || 0) || 0;
47
+ }
48
+
49
+ onMount(async () => {
50
+ stream = await stream_handler(
51
+ new URL(`${root}/gradio_api/upload_progress?upload_id=${upload_id}`)
52
+ );
53
+
54
+ if (stream == null) {
55
+ throw new Error("Event source is not defined");
56
+ }
57
+ // Event listener for progress updates
58
+ stream.onmessage = async function (event) {
59
+ const _data = JSON.parse(event.data);
60
+ if (!progress) progress = true;
61
+ if (_data.msg === "done") {
62
+ // the stream will close itself but is here for clarity; remove .close() in 5.0
63
+ stream?.close();
64
+ ondone?.();
65
+ } else {
66
+ current_file_upload = _data;
67
+ handleProgress(_data.orig_name, _data.chunk_size);
68
+ }
69
+ };
70
+ });
71
+
72
+ onDestroy(() => {
73
+ // the stream will close itself but is here for clarity; remove .close() in 5.0
74
+ if (stream != null || stream != undefined) stream.close();
75
+ });
76
+
77
+ function calculateTotalProgress(files: FileDataWithProgress[]): number {
78
+ let totalProgress = 0;
79
+ files.forEach((file) => {
80
+ totalProgress += getProgress(file);
81
+ });
82
+
83
+ document.documentElement.style.setProperty(
84
+ "--upload-progress-width",
85
+ (totalProgress / files.length).toFixed(2) + "%"
86
+ );
87
+
88
+ return totalProgress / files.length;
89
+ }
90
+
91
+ $effect(() => {
92
+ calculateTotalProgress(files_with_progress);
93
+ });
94
+ </script>
95
+
96
+ <div class="wrap" class:progress>
97
+ <span class="uploading"
98
+ >Uploading {files_with_progress.length}
99
+ {files_with_progress.length > 1 ? "files" : "file"}...</span
100
+ >
101
+
102
+ {#if file_to_display}
103
+ <div class="file">
104
+ <span>
105
+ <div class="progress-bar">
106
+ <progress
107
+ style="visibility:hidden;height:0;width:0;"
108
+ value={getProgress(file_to_display)}
109
+ max="100">{getProgress(file_to_display)}</progress
110
+ >
111
+ </div>
112
+ </span>
113
+ <span class="file-name">
114
+ {file_to_display.orig_name}
115
+ </span>
116
+ </div>
117
+ {/if}
118
+ </div>
119
+
120
+ <style>
121
+ .wrap {
122
+ overflow-y: auto;
123
+ transition: opacity 0.5s ease-in-out;
124
+ background: var(--block-background-fill);
125
+ position: relative;
126
+ display: flex;
127
+ flex-direction: column;
128
+ align-items: center;
129
+ justify-content: center;
130
+ min-height: var(--size-40);
131
+ width: var(--size-full);
132
+ }
133
+
134
+ .wrap::after {
135
+ content: "";
136
+ position: absolute;
137
+ top: 0;
138
+ left: 0;
139
+ width: var(--upload-progress-width);
140
+ height: 100%;
141
+ transition: all 0.5s ease-in-out;
142
+ z-index: 1;
143
+ }
144
+
145
+ .uploading {
146
+ font-size: var(--text-lg);
147
+ font-family: var(--font);
148
+ z-index: 2;
149
+ }
150
+
151
+ .file-name {
152
+ margin: var(--spacing-md);
153
+ font-size: var(--text-lg);
154
+ color: var(--body-text-color-subdued);
155
+ }
156
+
157
+ .file {
158
+ font-size: var(--text-md);
159
+ z-index: 2;
160
+ display: flex;
161
+ align-items: center;
162
+ }
163
+
164
+ .file progress {
165
+ display: inline;
166
+ height: var(--size-1);
167
+ width: 100%;
168
+ transition: all 0.5s ease-in-out;
169
+ color: var(--color-accent);
170
+ border: none;
171
+ }
172
+
173
+ .file progress[value]::-webkit-progress-value {
174
+ background-color: var(--color-accent);
175
+ border-radius: 20px;
176
+ }
177
+
178
+ .file progress[value]::-webkit-progress-bar {
179
+ background-color: var(--border-color-accent);
180
+ border-radius: 20px;
181
+ }
182
+
183
+ .progress-bar {
184
+ width: 14px;
185
+ height: 14px;
186
+ border-radius: 50%;
187
+ background:
188
+ radial-gradient(
189
+ closest-side,
190
+ var(--block-background-fill) 64%,
191
+ transparent 53% 100%
192
+ ),
193
+ conic-gradient(
194
+ var(--color-accent) var(--upload-progress-width),
195
+ var(--border-color-accent) 0
196
+ );
197
+ transition: all 0.5s ease-in-out;
198
+ }
199
+ </style>
6.4.1/upload/src/index.ts ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ export { default as Upload } from "./Upload.svelte";
2
+ export { default as ModifyUpload } from "./ModifyUpload.svelte";
3
+ export { default as UploadProgress } from "./UploadProgress.svelte";
4
+ export { create_drag } from "./utils";
6.4.1/upload/src/utils.ts ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ interface DragActionOptions {
2
+ disable_click?: boolean;
3
+ accepted_types?: string | string[] | null;
4
+ mode?: "single" | "multiple" | "directory";
5
+ on_drag_change?: (dragging: boolean) => void;
6
+ on_files?: (files: File[]) => void;
7
+ }
8
+
9
+ type ActionReturn = {
10
+ update: (new_options: DragActionOptions) => void;
11
+ destroy: () => void;
12
+ };
13
+
14
+ export function create_drag(): {
15
+ drag: (node: HTMLElement, options: DragActionOptions) => ActionReturn;
16
+ open_file_upload: () => void;
17
+ } {
18
+ let hidden_input: HTMLInputElement;
19
+ let _options: DragActionOptions;
20
+ return {
21
+ drag(
22
+ node: HTMLElement,
23
+ options: DragActionOptions = {}
24
+ ): {
25
+ update: (new_options: DragActionOptions) => void;
26
+ destroy: () => void;
27
+ } {
28
+ _options = options;
29
+
30
+ // Create and configure hidden file input
31
+ function setup_hidden_input(): void {
32
+ hidden_input = document.createElement("input");
33
+ hidden_input.type = "file";
34
+ hidden_input.style.display = "none";
35
+ hidden_input.setAttribute("aria-label", "File upload");
36
+ hidden_input.setAttribute("data-testid", "file-upload");
37
+ const accept_options = Array.isArray(_options.accepted_types)
38
+ ? _options.accepted_types.join(",")
39
+ : _options.accepted_types || undefined;
40
+
41
+ if (accept_options) {
42
+ hidden_input.accept = accept_options;
43
+ }
44
+
45
+ hidden_input.multiple = _options.mode === "multiple" || false;
46
+ if (_options.mode === "directory") {
47
+ hidden_input.webkitdirectory = true;
48
+ hidden_input.setAttribute("directory", "");
49
+ hidden_input.setAttribute("mozdirectory", "");
50
+ }
51
+ node.appendChild(hidden_input);
52
+ }
53
+
54
+ setup_hidden_input();
55
+
56
+ function handle_drag(e: DragEvent): void {
57
+ e.preventDefault();
58
+ e.stopPropagation();
59
+ }
60
+
61
+ function handle_drag_enter(e: DragEvent): void {
62
+ e.preventDefault();
63
+ e.stopPropagation();
64
+ _options.on_drag_change?.(true);
65
+ }
66
+
67
+ function handle_drag_leave(e: DragEvent): void {
68
+ e.preventDefault();
69
+ e.stopPropagation();
70
+ _options.on_drag_change?.(false);
71
+ }
72
+
73
+ function handle_drop(e: DragEvent): void {
74
+ e.preventDefault();
75
+ e.stopPropagation();
76
+ _options.on_drag_change?.(false);
77
+
78
+ if (!e.dataTransfer?.files) return;
79
+ const files = Array.from(e.dataTransfer.files);
80
+ if (files.length > 0) {
81
+ _options.on_files?.(files);
82
+ }
83
+ }
84
+
85
+ function handle_click(): void {
86
+ if (!_options.disable_click) {
87
+ hidden_input.value = "";
88
+ hidden_input.click();
89
+ }
90
+ }
91
+
92
+ function handle_file_input_change(): void {
93
+ if (hidden_input.files) {
94
+ const files = Array.from(hidden_input.files);
95
+ if (files.length > 0) {
96
+ _options.on_files?.(files);
97
+ }
98
+ }
99
+ }
100
+
101
+ // Add all event listeners
102
+ node.addEventListener("drag", handle_drag);
103
+ node.addEventListener("dragstart", handle_drag);
104
+ node.addEventListener("dragend", handle_drag);
105
+ node.addEventListener("dragover", handle_drag);
106
+ node.addEventListener("dragenter", handle_drag_enter);
107
+ node.addEventListener("dragleave", handle_drag_leave);
108
+ node.addEventListener("drop", handle_drop);
109
+ node.addEventListener("click", handle_click);
110
+ hidden_input!.addEventListener("change", handle_file_input_change);
111
+
112
+ return {
113
+ update(new_options: DragActionOptions) {
114
+ _options = new_options;
115
+ // Recreate hidden input with new options
116
+ hidden_input.remove();
117
+ setup_hidden_input();
118
+ hidden_input.addEventListener("change", handle_file_input_change);
119
+ },
120
+ destroy() {
121
+ node.removeEventListener("drag", handle_drag);
122
+ node.removeEventListener("dragstart", handle_drag);
123
+ node.removeEventListener("dragend", handle_drag);
124
+ node.removeEventListener("dragover", handle_drag);
125
+ node.removeEventListener("dragenter", handle_drag_enter);
126
+ node.removeEventListener("dragleave", handle_drag_leave);
127
+ node.removeEventListener("drop", handle_drop);
128
+ node.removeEventListener("click", handle_click);
129
+ hidden_input.removeEventListener("change", handle_file_input_change);
130
+ hidden_input.remove();
131
+ }
132
+ };
133
+ },
134
+ open_file_upload(): void {
135
+ if (hidden_input) {
136
+ hidden_input.value = "";
137
+ hidden_input.click();
138
+ }
139
+ }
140
+ };
141
+ }