@jobber/components 9.16.7 → 9.16.8
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/Countdown/index.cjs +0 -2
- package/dist/Countdown/index.mjs +0 -2
- package/dist/Countdown-cjs.js +1204 -23
- package/dist/Countdown-es.js +1182 -1
- package/dist/InputAvatar/index.cjs +1 -2
- package/dist/InputAvatar/index.mjs +1 -2
- package/dist/InputFile/InputFile.messages.d.ts +16 -0
- package/dist/InputFile/index.cjs +3 -2
- package/dist/InputFile/index.mjs +3 -2
- package/dist/InputFile-cjs.js +1199 -1397
- package/dist/InputFile-es.js +1201 -1399
- package/dist/docs/InputFile/InputFile.md +17 -2
- package/dist/index.cjs +0 -1
- package/dist/index.mjs +0 -1
- package/dist/tslib.es6-cjs.js +0 -54
- package/dist/tslib.es6-es.js +1 -53
- package/package.json +3 -3
- package/dist/index-cjs.js +0 -1189
- package/dist/index-es.js +0 -1186
package/dist/InputFile-es.js
CHANGED
|
@@ -1,1441 +1,1222 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import React__default, { useMemo, useRef, useReducer,
|
|
1
|
+
import { _ as __rest, a as __awaiter } from './tslib.es6-es.js';
|
|
2
|
+
import React__default, { useMemo, useRef, useReducer, useCallback, useEffect, forwardRef, useImperativeHandle, createContext, useContext, useState } from 'react';
|
|
3
3
|
import classnames from 'classnames';
|
|
4
|
-
import {
|
|
4
|
+
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
5
5
|
import axios from 'axios';
|
|
6
6
|
import { T as Text } from './Text-es.js';
|
|
7
7
|
import { B as Button } from './Button-es.js';
|
|
8
8
|
import { I as InputValidation } from './InputValidation-es.js';
|
|
9
|
+
import { d as defineMessages, u as useAtlantisI18n } from './useAtlantisI18n-es.js';
|
|
10
|
+
import './AtlantisContext-es.js';
|
|
9
11
|
import { C as Content } from './Content-es.js';
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
['php', 'application/x-httpd-php'],
|
|
61
|
-
['ppt', 'application/vnd.ms-powerpoint'],
|
|
62
|
-
['pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
|
|
63
|
-
['rar', 'application/vnd.rar'],
|
|
64
|
-
['rtf', 'application/rtf'],
|
|
65
|
-
['sh', 'application/x-sh'],
|
|
66
|
-
['svg', 'image/svg+xml'],
|
|
67
|
-
['swf', 'application/x-shockwave-flash'],
|
|
68
|
-
['tar', 'application/x-tar'],
|
|
69
|
-
['tif', 'image/tiff'],
|
|
70
|
-
['tiff', 'image/tiff'],
|
|
71
|
-
['ts', 'video/mp2t'],
|
|
72
|
-
['ttf', 'font/ttf'],
|
|
73
|
-
['txt', 'text/plain'],
|
|
74
|
-
['vsd', 'application/vnd.visio'],
|
|
75
|
-
['wav', 'audio/wav'],
|
|
76
|
-
['weba', 'audio/webm'],
|
|
77
|
-
['webm', 'video/webm'],
|
|
78
|
-
['webp', 'image/webp'],
|
|
79
|
-
['woff', 'font/woff'],
|
|
80
|
-
['woff2', 'font/woff2'],
|
|
81
|
-
['xhtml', 'application/xhtml+xml'],
|
|
82
|
-
['xls', 'application/vnd.ms-excel'],
|
|
83
|
-
['xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
|
|
84
|
-
['xml', 'application/xml'],
|
|
85
|
-
['xul', 'application/vnd.mozilla.xul+xml'],
|
|
86
|
-
['zip', 'application/zip'],
|
|
87
|
-
['7z', 'application/x-7z-compressed'],
|
|
88
|
-
// Others
|
|
89
|
-
['mkv', 'video/x-matroska'],
|
|
90
|
-
['mov', 'video/quicktime'],
|
|
91
|
-
['msg', 'application/vnd.ms-outlook']
|
|
13
|
+
//#region src/mime-default.ts
|
|
14
|
+
/**
|
|
15
|
+
* A small set of the most common file extensions, bundled into the main entry so that
|
|
16
|
+
* {@link fromEvent} guesses a MIME type for typeless files out of the box.
|
|
17
|
+
*
|
|
18
|
+
* This is the single source of truth for these defaults: the full extension-to-MIME table
|
|
19
|
+
* (`COMMON_MIME_TYPES`, exposed via the `file-selector/mime` subpath) spreads this map in
|
|
20
|
+
* rather than redefining these entries, so no value is duplicated across modules.
|
|
21
|
+
*
|
|
22
|
+
* See https://github.com/react-dropzone/file-selector/issues/127
|
|
23
|
+
*/
|
|
24
|
+
const DEFAULT_MIME_TYPES = /* @__PURE__ */ new Map([
|
|
25
|
+
["avif", "image/avif"],
|
|
26
|
+
["bmp", "image/bmp"],
|
|
27
|
+
["css", "text/css"],
|
|
28
|
+
["csv", "text/csv"],
|
|
29
|
+
["doc", "application/msword"],
|
|
30
|
+
["docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
|
|
31
|
+
["gif", "image/gif"],
|
|
32
|
+
["gz", "application/gzip"],
|
|
33
|
+
["htm", "text/html"],
|
|
34
|
+
["html", "text/html"],
|
|
35
|
+
["ico", "image/x-icon"],
|
|
36
|
+
["jpeg", "image/jpeg"],
|
|
37
|
+
["jpg", "image/jpeg"],
|
|
38
|
+
["js", "application/javascript"],
|
|
39
|
+
["json", "application/json"],
|
|
40
|
+
["md", "text/markdown"],
|
|
41
|
+
["mjs", "application/javascript"],
|
|
42
|
+
["mp3", "audio/mpeg"],
|
|
43
|
+
["mp4", "video/mp4"],
|
|
44
|
+
["ogg", "audio/ogg"],
|
|
45
|
+
["pdf", "application/pdf"],
|
|
46
|
+
["png", "image/png"],
|
|
47
|
+
["ppt", "application/powerpoint"],
|
|
48
|
+
["pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"],
|
|
49
|
+
["svg", "image/svg+xml"],
|
|
50
|
+
["tif", "image/tiff"],
|
|
51
|
+
["tiff", "image/tiff"],
|
|
52
|
+
["txt", "text/plain"],
|
|
53
|
+
["wasm", "application/wasm"],
|
|
54
|
+
["wav", "audio/x-wav"],
|
|
55
|
+
["weba", "audio/webm"],
|
|
56
|
+
["webm", "video/webm"],
|
|
57
|
+
["webp", "image/webp"],
|
|
58
|
+
["xls", "application/vnd.ms-excel"],
|
|
59
|
+
["xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],
|
|
60
|
+
["xml", "application/xml"],
|
|
61
|
+
["zip", "application/zip"]
|
|
92
62
|
]);
|
|
93
|
-
function toFileWithPath(file, path) {
|
|
94
|
-
var f = withMimeType(file);
|
|
95
|
-
if (typeof f.path !== 'string') { // on electron, path is already set to the absolute path
|
|
96
|
-
var webkitRelativePath = file.webkitRelativePath;
|
|
97
|
-
Object.defineProperty(f, 'path', {
|
|
98
|
-
value: typeof path === 'string'
|
|
99
|
-
? path
|
|
100
|
-
// If <input webkitdirectory> is set,
|
|
101
|
-
// the File will have a {webkitRelativePath} property
|
|
102
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory
|
|
103
|
-
: typeof webkitRelativePath === 'string' && webkitRelativePath.length > 0
|
|
104
|
-
? webkitRelativePath
|
|
105
|
-
: file.name,
|
|
106
|
-
writable: false,
|
|
107
|
-
configurable: false,
|
|
108
|
-
enumerable: true
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
return f;
|
|
112
|
-
}
|
|
113
|
-
function withMimeType(file) {
|
|
114
|
-
var name = file.name;
|
|
115
|
-
var hasExtension = name && name.lastIndexOf('.') !== -1;
|
|
116
|
-
if (hasExtension && !file.type) {
|
|
117
|
-
var ext = name.split('.')
|
|
118
|
-
.pop().toLowerCase();
|
|
119
|
-
var type = COMMON_MIME_TYPES.get(ext);
|
|
120
|
-
if (type) {
|
|
121
|
-
Object.defineProperty(file, 'type', {
|
|
122
|
-
value: type,
|
|
123
|
-
writable: false,
|
|
124
|
-
configurable: false,
|
|
125
|
-
enumerable: true
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return file;
|
|
130
|
-
}
|
|
131
63
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
64
|
+
//#region src/error.ts
|
|
65
|
+
var UnexpectedObjectError = class extends Error {
|
|
66
|
+
constructor(item) {
|
|
67
|
+
super("DataTransferItem is not a file");
|
|
68
|
+
this.item = item;
|
|
69
|
+
this.name = "UnexpectedObjectError";
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/file.ts
|
|
74
|
+
function toFileWithPath(file, path, h) {
|
|
75
|
+
const f = file;
|
|
76
|
+
const { webkitRelativePath } = file;
|
|
77
|
+
const p = typeof path === "string" ? path : typeof webkitRelativePath === "string" && webkitRelativePath.length > 0 ? webkitRelativePath : `./${file.name}`;
|
|
78
|
+
if (typeof f.path !== "string") setObjProp(f, "path", p);
|
|
79
|
+
if (h !== void 0) Object.defineProperty(f, "handle", {
|
|
80
|
+
value: h,
|
|
81
|
+
writable: false,
|
|
82
|
+
configurable: false,
|
|
83
|
+
enumerable: true
|
|
84
|
+
});
|
|
85
|
+
setObjProp(f, "relativePath", p);
|
|
86
|
+
return f;
|
|
87
|
+
}
|
|
137
88
|
/**
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
89
|
+
* Sets a File's `type` from its extension using the provided `mimeTypes` lookup,
|
|
90
|
+
* but only when the browser didn't already set a type. Returns the same File.
|
|
91
|
+
*/
|
|
92
|
+
function withMimeType(file, mimeTypes = DEFAULT_MIME_TYPES) {
|
|
93
|
+
const { name } = file;
|
|
94
|
+
if (name && name.lastIndexOf(".") !== -1 && !file.type) {
|
|
95
|
+
const ext = name.split(".").pop().toLowerCase();
|
|
96
|
+
const type = mimeTypes.get(ext);
|
|
97
|
+
if (type) Object.defineProperty(file, "type", {
|
|
98
|
+
value: type,
|
|
99
|
+
writable: false,
|
|
100
|
+
configurable: false,
|
|
101
|
+
enumerable: true
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return file;
|
|
105
|
+
}
|
|
106
|
+
function setObjProp(f, key, value) {
|
|
107
|
+
Object.defineProperty(f, key, {
|
|
108
|
+
value,
|
|
109
|
+
writable: false,
|
|
110
|
+
configurable: false,
|
|
111
|
+
enumerable: true
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/file-selector.ts
|
|
116
|
+
const FILES_TO_IGNORE = [".DS_Store", "Thumbs.db"];
|
|
117
|
+
/**
|
|
118
|
+
* Convert a DragEvent's DataTrasfer object to a list of File objects
|
|
119
|
+
* NOTE: If some of the items are folders,
|
|
120
|
+
* everything will be flattened and placed in the same list but the paths will be kept as a {path} property.
|
|
121
|
+
*
|
|
122
|
+
* A https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent (e.g. a paste of a screenshot)
|
|
123
|
+
* is also supported: its files are read from {clipboardData}.
|
|
124
|
+
*
|
|
125
|
+
* EXPERIMENTAL: A list of https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle objects can also be passed as an arg
|
|
126
|
+
* and a list of File objects will be returned.
|
|
127
|
+
*
|
|
128
|
+
* @param evt
|
|
129
|
+
* @param options
|
|
130
|
+
*/
|
|
131
|
+
async function fromEvent(evt, { mimeTypes = DEFAULT_MIME_TYPES } = {}) {
|
|
132
|
+
return (await getFilesOrItems(evt)).map((item) => item instanceof File ? withMimeType(item, mimeTypes) : item);
|
|
133
|
+
}
|
|
134
|
+
async function getFilesOrItems(evt) {
|
|
135
|
+
if (isObject(evt) && isDataTransfer(evt.dataTransfer)) return getDataTransferFiles(evt.dataTransfer, evt.type);
|
|
136
|
+
else if (isClipboardEvt(evt)) return getClipboardFiles(evt.clipboardData);
|
|
137
|
+
else if (isChangeEvt(evt)) return getInputFiles(evt);
|
|
138
|
+
else if (Array.isArray(evt) && evt.every((item) => "getFile" in item && typeof item.getFile === "function")) return getFsHandleFiles(evt);
|
|
139
|
+
return [];
|
|
162
140
|
}
|
|
163
141
|
function isDataTransfer(value) {
|
|
164
|
-
|
|
142
|
+
return isObject(value);
|
|
143
|
+
}
|
|
144
|
+
function isClipboardEvt(value) {
|
|
145
|
+
return isObject(value) && isDataTransfer(value.clipboardData);
|
|
165
146
|
}
|
|
166
147
|
function isChangeEvt(value) {
|
|
167
|
-
|
|
148
|
+
return isObject(value) && isObject(value.target);
|
|
168
149
|
}
|
|
169
150
|
function isObject(v) {
|
|
170
|
-
|
|
151
|
+
return typeof v === "object" && v !== null;
|
|
171
152
|
}
|
|
172
153
|
function getInputFiles(evt) {
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
// Ee expect each handle to be https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle
|
|
176
|
-
function getFsHandleFiles(handles) {
|
|
177
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
178
|
-
var files;
|
|
179
|
-
return __generator(this, function (_a) {
|
|
180
|
-
switch (_a.label) {
|
|
181
|
-
case 0: return [4 /*yield*/, Promise.all(handles.map(function (h) { return h.getFile(); }))];
|
|
182
|
-
case 1:
|
|
183
|
-
files = _a.sent();
|
|
184
|
-
return [2 /*return*/, files.map(function (file) { return toFileWithPath(file); })];
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
});
|
|
154
|
+
return fromList(evt.target.files).map((file) => toFileWithPath(file));
|
|
188
155
|
}
|
|
189
|
-
function
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
.filter(function (item) { return item.kind === 'file'; });
|
|
201
|
-
// According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents,
|
|
202
|
-
// only 'dragstart' and 'drop' has access to the data (source node)
|
|
203
|
-
if (type !== 'drop') {
|
|
204
|
-
return [2 /*return*/, items];
|
|
205
|
-
}
|
|
206
|
-
return [4 /*yield*/, Promise.all(items.map(toFilePromises))];
|
|
207
|
-
case 1:
|
|
208
|
-
files = _a.sent();
|
|
209
|
-
return [2 /*return*/, noIgnoredFiles(flatten(files))];
|
|
210
|
-
case 2: return [2 /*return*/, noIgnoredFiles(fromList(dt.files)
|
|
211
|
-
.map(function (file) { return toFileWithPath(file); }))];
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
});
|
|
156
|
+
async function getFsHandleFiles(handles) {
|
|
157
|
+
return (await Promise.all(handles.map((h) => h.getFile()))).map((file) => toFileWithPath(file));
|
|
158
|
+
}
|
|
159
|
+
async function getDataTransferFiles(dt, type) {
|
|
160
|
+
const items = fromList(dt.items).filter((item) => item.kind === "file");
|
|
161
|
+
if (type !== "drop") return items;
|
|
162
|
+
return noIgnoredFiles(flatten(await Promise.all(items.map(toFilePromises))));
|
|
163
|
+
}
|
|
164
|
+
function getClipboardFiles(dt) {
|
|
165
|
+
const fromItems = fromList(dt.items).filter((item) => item.kind === "file").map((item) => item.getAsFile()).filter((file) => file !== null);
|
|
166
|
+
return noIgnoredFiles((fromItems.length > 0 ? fromItems : fromList(dt.files)).map((file) => toFileWithPath(file)));
|
|
215
167
|
}
|
|
216
168
|
function noIgnoredFiles(files) {
|
|
217
|
-
|
|
169
|
+
return files.filter((file) => FILES_TO_IGNORE.indexOf(file.name) === -1);
|
|
218
170
|
}
|
|
219
|
-
// IE11 does not support Array.from()
|
|
220
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Browser_compatibility
|
|
221
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/FileList
|
|
222
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItemList
|
|
223
171
|
function fromList(items) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
var files = [];
|
|
228
|
-
// tslint:disable: prefer-for-of
|
|
229
|
-
for (var i = 0; i < items.length; i++) {
|
|
230
|
-
var file = items[i];
|
|
231
|
-
files.push(file);
|
|
232
|
-
}
|
|
233
|
-
return files;
|
|
172
|
+
if (items === null) return [];
|
|
173
|
+
return Array.from(items);
|
|
234
174
|
}
|
|
235
|
-
|
|
236
|
-
function
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (entry && entry.isDirectory) {
|
|
245
|
-
return fromDirEntry(entry);
|
|
246
|
-
}
|
|
247
|
-
return fromDataTransferItem(item);
|
|
175
|
+
async function toFilePromises(item) {
|
|
176
|
+
if (typeof item.webkitGetAsEntry !== "function") return fromDataTransferItem(item);
|
|
177
|
+
const entry = item.webkitGetAsEntry();
|
|
178
|
+
if (entry?.isDirectory) {
|
|
179
|
+
const handle = await getFsHandle(item);
|
|
180
|
+
if (handle?.kind === "directory") return fromDirHandle(handle, `/${handle.name}`);
|
|
181
|
+
return fromDirEntry(entry);
|
|
182
|
+
}
|
|
183
|
+
return fromDataTransferItem(item, entry);
|
|
248
184
|
}
|
|
249
185
|
function flatten(items) {
|
|
250
|
-
|
|
186
|
+
const result = [];
|
|
187
|
+
for (const item of items) if (Array.isArray(item)) result.push(...flatten(item));
|
|
188
|
+
else result.push(item);
|
|
189
|
+
return result;
|
|
251
190
|
}
|
|
252
|
-
function fromDataTransferItem(item) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
191
|
+
async function fromDataTransferItem(item, entry) {
|
|
192
|
+
const syncFile = item.getAsFile();
|
|
193
|
+
const h = await getFsHandle(item);
|
|
194
|
+
if (h !== null && h !== void 0) {
|
|
195
|
+
const file = syncFile ?? await h.getFile();
|
|
196
|
+
file.handle = h;
|
|
197
|
+
return toFileWithPath(file);
|
|
198
|
+
}
|
|
199
|
+
if (!syncFile) throw new UnexpectedObjectError(item);
|
|
200
|
+
return toFileWithPath(syncFile, entry?.fullPath ?? void 0);
|
|
201
|
+
}
|
|
202
|
+
async function getFsHandle(item) {
|
|
203
|
+
if (globalThis.isSecureContext && typeof item.getAsFileSystemHandle === "function") return item.getAsFileSystemHandle();
|
|
204
|
+
}
|
|
205
|
+
async function fromDirHandle(handle, path) {
|
|
206
|
+
const files = [];
|
|
207
|
+
for await (const child of handle.values()) {
|
|
208
|
+
const childPath = `${path}/${child.name}`;
|
|
209
|
+
if (child.kind === "directory") files.push(...await fromDirHandle(child, childPath));
|
|
210
|
+
else {
|
|
211
|
+
const file = await child.getFile();
|
|
212
|
+
files.push(toFileWithPath(file, childPath, child));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return files;
|
|
216
|
+
}
|
|
217
|
+
async function fromEntry(entry) {
|
|
218
|
+
return entry.isDirectory ? fromDirEntry(entry) : fromFileEntry(entry);
|
|
267
219
|
}
|
|
268
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry
|
|
269
220
|
function fromDirEntry(entry) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
case 3:
|
|
292
|
-
err_1 = _a.sent();
|
|
293
|
-
reject(err_1);
|
|
294
|
-
return [3 /*break*/, 4];
|
|
295
|
-
case 4: return [3 /*break*/, 6];
|
|
296
|
-
case 5:
|
|
297
|
-
items = Promise.all(batch.map(fromEntry));
|
|
298
|
-
entries.push(items);
|
|
299
|
-
// Continue reading
|
|
300
|
-
readEntries();
|
|
301
|
-
_a.label = 6;
|
|
302
|
-
case 6: return [2 /*return*/];
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
}); }, function (err) {
|
|
306
|
-
reject(err);
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
readEntries();
|
|
310
|
-
});
|
|
221
|
+
const reader = entry.createReader();
|
|
222
|
+
return new Promise((resolve, reject) => {
|
|
223
|
+
const entries = [];
|
|
224
|
+
function readEntries() {
|
|
225
|
+
reader.readEntries(async (batch) => {
|
|
226
|
+
if (!batch.length) try {
|
|
227
|
+
resolve(await Promise.all(entries));
|
|
228
|
+
} catch (err) {
|
|
229
|
+
reject(err);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
const items = Promise.all(batch.map(fromEntry));
|
|
233
|
+
entries.push(items);
|
|
234
|
+
readEntries();
|
|
235
|
+
}
|
|
236
|
+
}, (err) => {
|
|
237
|
+
reject(err);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
readEntries();
|
|
241
|
+
});
|
|
311
242
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
}, function (err) {
|
|
321
|
-
reject(err);
|
|
322
|
-
});
|
|
323
|
-
})];
|
|
324
|
-
});
|
|
325
|
-
});
|
|
243
|
+
async function fromFileEntry(entry) {
|
|
244
|
+
return new Promise((resolve, reject) => {
|
|
245
|
+
entry.file((file) => {
|
|
246
|
+
resolve(toFileWithPath(file, entry.fullPath));
|
|
247
|
+
}, (err) => {
|
|
248
|
+
reject(err);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
326
251
|
}
|
|
327
252
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
function _nonIterableRest$1() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
360
|
-
|
|
361
|
-
function _unsupportedIterableToArray$1(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$1(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$1(o, minLen); }
|
|
362
|
-
|
|
363
|
-
function _arrayLikeToArray$1(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
364
|
-
|
|
365
|
-
function _iterableToArrayLimit$1(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
366
|
-
|
|
367
|
-
function _arrayWithHoles$1(arr) { if (Array.isArray(arr)) return arr; }
|
|
368
|
-
|
|
369
|
-
var FILE_INVALID_TYPE = 'file-invalid-type';
|
|
370
|
-
var FILE_TOO_LARGE = 'file-too-large';
|
|
371
|
-
var FILE_TOO_SMALL = 'file-too-small';
|
|
372
|
-
var TOO_MANY_FILES = 'too-many-files';
|
|
253
|
+
//#region src/index.ts
|
|
254
|
+
/**
|
|
255
|
+
* Check if the provided file should be accepted by an `<input type="file">`
|
|
256
|
+
* with the given `accept` attribute value.
|
|
257
|
+
*
|
|
258
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#attr-accept
|
|
259
|
+
*
|
|
260
|
+
* Inspired by https://github.com/enyo/dropzone.
|
|
261
|
+
*
|
|
262
|
+
* @param file The file to check. When omitted or `null`, the file is accepted.
|
|
263
|
+
* @param acceptedFiles One or more MIME types and/or file extensions, either as
|
|
264
|
+
* a comma-delimited string or an array. When omitted or empty, any file is accepted.
|
|
265
|
+
* @returns `true` if the file matches (or when there is nothing to match against).
|
|
266
|
+
*/
|
|
267
|
+
function accept(file, acceptedFiles) {
|
|
268
|
+
if (file && acceptedFiles) {
|
|
269
|
+
const acceptedFilesArray = Array.isArray(acceptedFiles) ? acceptedFiles : acceptedFiles.split(",");
|
|
270
|
+
if (acceptedFilesArray.length === 0) return true;
|
|
271
|
+
const fileName = file.name || "";
|
|
272
|
+
const mimeType = (file.type || "").toLowerCase();
|
|
273
|
+
const baseMimeType = mimeType.replace(/\/.*$/, "");
|
|
274
|
+
return acceptedFilesArray.some((type) => {
|
|
275
|
+
const validType = type.trim().toLowerCase();
|
|
276
|
+
if (validType.charAt(0) === ".") return fileName.toLowerCase().endsWith(validType);
|
|
277
|
+
else if (validType.endsWith("/*")) return baseMimeType === validType.replace(/\/.*$/, "");
|
|
278
|
+
return mimeType === validType;
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
373
283
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
284
|
+
//#region src/utils/index.ts
|
|
285
|
+
const accepts = typeof accept === "function" ? accept : accept.default;
|
|
286
|
+
const FILE_INVALID_TYPE = "file-invalid-type";
|
|
287
|
+
const FILE_TOO_LARGE = "file-too-large";
|
|
288
|
+
const FILE_TOO_SMALL = "file-too-small";
|
|
289
|
+
const TOO_MANY_FILES = "too-many-files";
|
|
290
|
+
function getInvalidTypeRejectionErr(accept = "") {
|
|
291
|
+
const acceptArr = accept.split(",");
|
|
292
|
+
const msg = acceptArr.length > 1 ? `one of ${acceptArr.join(", ")}` : acceptArr[0];
|
|
293
|
+
return {
|
|
294
|
+
code: FILE_INVALID_TYPE,
|
|
295
|
+
message: `File type must be ${msg}`
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
const FILE_SIZE_UNITS = [
|
|
299
|
+
"KB",
|
|
300
|
+
"MB",
|
|
301
|
+
"GB",
|
|
302
|
+
"TB",
|
|
303
|
+
"PB"
|
|
304
|
+
];
|
|
305
|
+
/**
|
|
306
|
+
* Format a byte count into a human-readable string, e.g. `1111` -> `1.08 KB`.
|
|
307
|
+
* Values below 1 KB are kept in bytes to preserve the singular/plural wording.
|
|
308
|
+
*/
|
|
309
|
+
function formatBytes(bytes) {
|
|
310
|
+
if (bytes < 1024) return `${bytes} ${bytes === 1 ? "byte" : "bytes"}`;
|
|
311
|
+
let size = bytes / 1024;
|
|
312
|
+
let unitIndex = 0;
|
|
313
|
+
while (size >= 1024 && unitIndex < FILE_SIZE_UNITS.length - 1) {
|
|
314
|
+
size /= 1024;
|
|
315
|
+
unitIndex++;
|
|
316
|
+
}
|
|
317
|
+
return `${Number(size.toFixed(2))} ${FILE_SIZE_UNITS[unitIndex]}`;
|
|
318
|
+
}
|
|
319
|
+
function getTooLargeRejectionErr(maxSize) {
|
|
320
|
+
return {
|
|
321
|
+
code: FILE_TOO_LARGE,
|
|
322
|
+
message: `File is larger than ${formatBytes(maxSize)}`
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
function getTooSmallRejectionErr(minSize) {
|
|
326
|
+
return {
|
|
327
|
+
code: FILE_TOO_SMALL,
|
|
328
|
+
message: `File is smaller than ${formatBytes(minSize)}`
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
const TOO_MANY_FILES_REJECTION = {
|
|
332
|
+
code: TOO_MANY_FILES,
|
|
333
|
+
message: "Too many files"
|
|
393
334
|
};
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
335
|
+
/**
|
|
336
|
+
* Check if the given file is a DataTransferItem with an empty type.
|
|
337
|
+
*
|
|
338
|
+
* During drag events, browsers may return DataTransferItem objects instead of File objects.
|
|
339
|
+
* Some browsers (e.g., Chrome) return an empty MIME type for certain file types (like .md files)
|
|
340
|
+
* on DataTransferItem during drag events, even though the type is correctly set during drop.
|
|
341
|
+
*/
|
|
342
|
+
function isDataTransferItemWithEmptyType(file) {
|
|
343
|
+
return file.type === "" && typeof file.getAsFile === "function";
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Check if file is accepted.
|
|
347
|
+
*
|
|
348
|
+
* Firefox versions prior to 53 return a bogus MIME type for every file drag,
|
|
349
|
+
* so dragovers with that MIME type will always be accepted.
|
|
350
|
+
*
|
|
351
|
+
* Chrome/other browsers may return an empty MIME type for files during drag events,
|
|
352
|
+
* so we accept those as well (we'll validate properly on drop).
|
|
353
|
+
*/
|
|
400
354
|
function fileAccepted(file, accept) {
|
|
401
|
-
|
|
402
|
-
|
|
355
|
+
const isAcceptable = file.type === "application/x-moz-file" || accepts(file, accept ?? "") || isDataTransferItemWithEmptyType(file);
|
|
356
|
+
return [isAcceptable, isAcceptable ? null : getInvalidTypeRejectionErr(accept)];
|
|
403
357
|
}
|
|
404
358
|
function fileMatchSize(file, minSize, maxSize) {
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
359
|
+
if (isDefined(file.size)) {
|
|
360
|
+
if (isDefined(minSize) && isDefined(maxSize)) {
|
|
361
|
+
if (file.size > maxSize) return [false, getTooLargeRejectionErr(maxSize)];
|
|
362
|
+
if (file.size < minSize) return [false, getTooSmallRejectionErr(minSize)];
|
|
363
|
+
} else if (isDefined(minSize) && file.size < minSize) return [false, getTooSmallRejectionErr(minSize)];
|
|
364
|
+
else if (isDefined(maxSize) && file.size > maxSize) return [false, getTooLargeRejectionErr(maxSize)];
|
|
365
|
+
}
|
|
366
|
+
return [true, null];
|
|
413
367
|
}
|
|
414
|
-
|
|
415
368
|
function isDefined(value) {
|
|
416
|
-
|
|
369
|
+
return value !== void 0 && value !== null;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Check if a value is thenable (Promise-like), used to tell a synchronous validator result from an
|
|
373
|
+
* asynchronous one.
|
|
374
|
+
*/
|
|
375
|
+
function isThenable(value) {
|
|
376
|
+
return value != null && typeof value.then === "function";
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Classify a set of dragged files for the `isDragAccept`/`isDragReject`/`isDragUnknown` states.
|
|
380
|
+
*
|
|
381
|
+
* During `dragenter`/`dragover` the browser only exposes `DataTransferItem`s, which carry a MIME
|
|
382
|
+
* `type` but no file name, extension or size (see https://html.spec.whatwg.org/multipage/dnd.html#dndevents).
|
|
383
|
+
* So the drag-time check is deliberately optimistic about anything it can't see:
|
|
384
|
+
*
|
|
385
|
+
* - The custom `validator` is **never** run here. It is typed `(file: File) => ...` and users
|
|
386
|
+
* routinely read `file.name`/`file.size`, which are `undefined` on a `DataTransferItem` - running
|
|
387
|
+
* it would throw and abort the whole drag handler (leaving `isDragActive` stuck at `false`).
|
|
388
|
+
* See https://github.com/react-dropzone/react-dropzone/issues/1408
|
|
389
|
+
* - When a `validator` is configured we therefore can't promise the files are acceptable, so the
|
|
390
|
+
* verdict is `unknown` rather than a misleading `reject` (or a premature `accept`).
|
|
391
|
+
* See https://github.com/react-dropzone/react-dropzone/issues/1244
|
|
392
|
+
*
|
|
393
|
+
* The full check (including the `validator`) still runs on drop in {@link fileAccepted}/`setFiles`.
|
|
394
|
+
*/
|
|
395
|
+
function evaluateDragFiles({ files, accept, minSize, maxSize, multiple, maxFiles = 0, validator, getErrorMessage }) {
|
|
396
|
+
const acceptedFiles = [];
|
|
397
|
+
const rejections = [];
|
|
398
|
+
const localizeError = (error, file) => getErrorMessage && typeof File !== "undefined" && file instanceof File ? {
|
|
399
|
+
...error,
|
|
400
|
+
message: getErrorMessage(error, file)
|
|
401
|
+
} : error;
|
|
402
|
+
files.forEach((file) => {
|
|
403
|
+
const [accepted, acceptError] = fileAccepted(file, accept);
|
|
404
|
+
const [sizeMatch, sizeError] = fileMatchSize(file, minSize, maxSize);
|
|
405
|
+
if (accepted && sizeMatch) acceptedFiles.push(file);
|
|
406
|
+
else rejections.push({
|
|
407
|
+
file,
|
|
408
|
+
errors: [acceptError, sizeError].filter((error) => error != null).map((error) => localizeError(error, file))
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
const acceptedFilesLimit = multiple ? maxFiles >= 1 ? maxFiles : Number.POSITIVE_INFINITY : 1;
|
|
412
|
+
if (acceptedFiles.length > acceptedFilesLimit) acceptedFiles.slice(acceptedFilesLimit).forEach((file) => {
|
|
413
|
+
rejections.push({
|
|
414
|
+
file,
|
|
415
|
+
errors: [localizeError(TOO_MANY_FILES_REJECTION, file)]
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
if (rejections.length > 0) return {
|
|
419
|
+
verdict: "reject",
|
|
420
|
+
rejections
|
|
421
|
+
};
|
|
422
|
+
return {
|
|
423
|
+
verdict: validator ? "unknown" : "accept",
|
|
424
|
+
rejections
|
|
425
|
+
};
|
|
417
426
|
}
|
|
418
|
-
|
|
419
|
-
function allFilesAccepted(_ref) {
|
|
420
|
-
var files = _ref.files,
|
|
421
|
-
accept = _ref.accept,
|
|
422
|
-
minSize = _ref.minSize,
|
|
423
|
-
maxSize = _ref.maxSize,
|
|
424
|
-
multiple = _ref.multiple,
|
|
425
|
-
maxFiles = _ref.maxFiles;
|
|
426
|
-
|
|
427
|
-
if (!multiple && files.length > 1 || multiple && maxFiles >= 1 && files.length > maxFiles) {
|
|
428
|
-
return false;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
return files.every(function (file) {
|
|
432
|
-
var _fileAccepted = fileAccepted(file, accept),
|
|
433
|
-
_fileAccepted2 = _slicedToArray$1(_fileAccepted, 1),
|
|
434
|
-
accepted = _fileAccepted2[0];
|
|
435
|
-
|
|
436
|
-
var _fileMatchSize = fileMatchSize(file, minSize, maxSize),
|
|
437
|
-
_fileMatchSize2 = _slicedToArray$1(_fileMatchSize, 1),
|
|
438
|
-
sizeMatch = _fileMatchSize2[0];
|
|
439
|
-
|
|
440
|
-
return accepted && sizeMatch;
|
|
441
|
-
});
|
|
442
|
-
} // React's synthetic events has event.isPropagationStopped,
|
|
443
|
-
// but to remain compatibility with other libs (Preact) fall back
|
|
444
|
-
// to check event.cancelBubble
|
|
445
|
-
|
|
446
427
|
function isPropagationStopped(event) {
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
return event.cancelBubble;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
return false;
|
|
428
|
+
if (typeof event.isPropagationStopped === "function") return event.isPropagationStopped();
|
|
429
|
+
else if (typeof event.cancelBubble !== "undefined") return event.cancelBubble;
|
|
430
|
+
return false;
|
|
454
431
|
}
|
|
455
432
|
function isEvtWithFiles(event) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
return Array.prototype.some.call(event.dataTransfer.types, function (type) {
|
|
463
|
-
return type === 'Files' || type === 'application/x-moz-file';
|
|
464
|
-
});
|
|
433
|
+
const dataTransfer = event.dataTransfer ?? event.clipboardData;
|
|
434
|
+
if (!dataTransfer) return !!event.target && !!event.target.files;
|
|
435
|
+
return Array.prototype.some.call(dataTransfer.types, (type) => type === "Files" || type === "application/x-moz-file") || Array.prototype.some.call(dataTransfer.items ?? [], isKindFile);
|
|
436
|
+
}
|
|
437
|
+
function isKindFile(item) {
|
|
438
|
+
return typeof item === "object" && item !== null && item.kind === "file";
|
|
465
439
|
}
|
|
466
|
-
|
|
467
440
|
function onDocumentDragOver(event) {
|
|
468
|
-
|
|
441
|
+
event.preventDefault();
|
|
469
442
|
}
|
|
470
|
-
|
|
471
443
|
function isIe(userAgent) {
|
|
472
|
-
|
|
444
|
+
return userAgent.indexOf("MSIE") !== -1 || userAgent.indexOf("Trident/") !== -1;
|
|
473
445
|
}
|
|
474
|
-
|
|
475
446
|
function isEdge(userAgent) {
|
|
476
|
-
|
|
447
|
+
return userAgent.indexOf("Edge/") !== -1;
|
|
477
448
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
var userAgent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.navigator.userAgent;
|
|
481
|
-
return isIe(userAgent) || isEdge(userAgent);
|
|
449
|
+
function isIeOrEdge(userAgent = window.navigator.userAgent) {
|
|
450
|
+
return isIe(userAgent) || isEdge(userAgent);
|
|
482
451
|
}
|
|
483
452
|
/**
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
496
|
-
fns[_key] = arguments[_key];
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
return function (event) {
|
|
500
|
-
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
501
|
-
args[_key2 - 1] = arguments[_key2];
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
return fns.some(function (fn) {
|
|
505
|
-
if (!isPropagationStopped(event) && fn) {
|
|
506
|
-
fn.apply(void 0, [event].concat(args));
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
return isPropagationStopped(event);
|
|
510
|
-
});
|
|
511
|
-
};
|
|
453
|
+
* This is intended to be used to compose event handlers.
|
|
454
|
+
* They are executed in order until one of them calls `event.isPropagationStopped()`.
|
|
455
|
+
* Note that the check is done on the first invoke too,
|
|
456
|
+
* meaning that if propagation was stopped before invoking the fns,
|
|
457
|
+
* no handlers will be executed.
|
|
458
|
+
*/
|
|
459
|
+
function composeEventHandlers(...fns) {
|
|
460
|
+
return (event, ...args) => fns.some((fn) => {
|
|
461
|
+
if (!isPropagationStopped(event) && fn) fn(event, ...args);
|
|
462
|
+
return isPropagationStopped(event);
|
|
463
|
+
});
|
|
512
464
|
}
|
|
513
465
|
/**
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
* @returns {boolean}
|
|
517
|
-
*/
|
|
518
|
-
|
|
466
|
+
* canUseFileSystemAccessAPI checks if the File System Access API is supported by the browser.
|
|
467
|
+
*/
|
|
519
468
|
function canUseFileSystemAccessAPI() {
|
|
520
|
-
|
|
469
|
+
return "showOpenFilePicker" in window;
|
|
521
470
|
}
|
|
522
471
|
/**
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
return [{
|
|
532
|
-
description: 'everything',
|
|
533
|
-
// TODO: Need to handle filtering more elegantly than this!
|
|
534
|
-
accept: Array.isArray(accept) // Accept just MIME types as per spec
|
|
535
|
-
// NOTE: accept can be https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#unique_file_type_specifiers
|
|
536
|
-
? accept.filter(function (item) {
|
|
537
|
-
return item === 'audio/*' || item === 'video/*' || item === 'image/*' || item === 'text/*' || /\w+\/[-+.\w]+/g.test(item);
|
|
538
|
-
}).reduce(function (a, b) {
|
|
539
|
-
return _objectSpread$1(_objectSpread$1({}, a), {}, _defineProperty$1({}, b, []));
|
|
540
|
-
}, {}) : {}
|
|
541
|
-
}];
|
|
472
|
+
* Coerce an `accept` extension value to an array. `window.showOpenFilePicker` allows a single
|
|
473
|
+
* extension string as well as an array, so we accept both and normalize to an array. Anything
|
|
474
|
+
* else (a malformed value) collapses to an empty list.
|
|
475
|
+
*/
|
|
476
|
+
function toExtensions(ext) {
|
|
477
|
+
if (Array.isArray(ext)) return ext;
|
|
478
|
+
if (typeof ext === "string") return [ext];
|
|
479
|
+
return [];
|
|
542
480
|
}
|
|
543
|
-
|
|
544
|
-
var _excluded = ["children"],
|
|
545
|
-
_excluded2 = ["open"],
|
|
546
|
-
_excluded3 = ["refKey", "role", "onKeyDown", "onFocus", "onBlur", "onClick", "onDragEnter", "onDragOver", "onDragLeave", "onDrop"],
|
|
547
|
-
_excluded4 = ["refKey", "onChange", "onClick"];
|
|
548
|
-
|
|
549
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
550
|
-
|
|
551
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
552
|
-
|
|
553
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
554
|
-
|
|
555
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
556
|
-
|
|
557
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
558
|
-
|
|
559
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
560
|
-
|
|
561
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
562
|
-
|
|
563
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
564
|
-
|
|
565
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
566
|
-
|
|
567
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
568
|
-
|
|
569
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
570
|
-
|
|
571
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
572
|
-
|
|
573
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
574
|
-
|
|
575
|
-
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
576
|
-
|
|
577
|
-
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
578
481
|
/**
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
var children = _ref.children,
|
|
595
|
-
params = _objectWithoutProperties(_ref, _excluded);
|
|
596
|
-
|
|
597
|
-
var _useDropzone = useDropzone(params),
|
|
598
|
-
open = _useDropzone.open,
|
|
599
|
-
props = _objectWithoutProperties(_useDropzone, _excluded2);
|
|
600
|
-
|
|
601
|
-
useImperativeHandle(ref, function () {
|
|
602
|
-
return {
|
|
603
|
-
open: open
|
|
604
|
-
};
|
|
605
|
-
}, [open]); // TODO: Figure out why react-styleguidist cannot create docs if we don't return a jsx element
|
|
606
|
-
|
|
607
|
-
return /*#__PURE__*/React__default.createElement(Fragment, null, children(_objectSpread(_objectSpread({}, props), {}, {
|
|
608
|
-
open: open
|
|
609
|
-
})));
|
|
610
|
-
});
|
|
611
|
-
Dropzone.displayName = 'Dropzone'; // Add default props for react-docgen
|
|
612
|
-
|
|
613
|
-
var defaultProps = {
|
|
614
|
-
disabled: false,
|
|
615
|
-
getFilesFromEvent: fromEvent,
|
|
616
|
-
maxSize: Infinity,
|
|
617
|
-
minSize: 0,
|
|
618
|
-
multiple: true,
|
|
619
|
-
maxFiles: 0,
|
|
620
|
-
preventDropOnDocument: true,
|
|
621
|
-
noClick: false,
|
|
622
|
-
noKeyboard: false,
|
|
623
|
-
noDrag: false,
|
|
624
|
-
noDragEventsBubbling: false,
|
|
625
|
-
validator: null,
|
|
626
|
-
useFsAccessApi: false
|
|
627
|
-
};
|
|
628
|
-
Dropzone.defaultProps = defaultProps;
|
|
629
|
-
Dropzone.propTypes = {
|
|
630
|
-
/**
|
|
631
|
-
* Render function that exposes the dropzone state and prop getter fns
|
|
632
|
-
*
|
|
633
|
-
* @param {object} params
|
|
634
|
-
* @param {Function} params.getRootProps Returns the props you should apply to the root drop container you render
|
|
635
|
-
* @param {Function} params.getInputProps Returns the props you should apply to hidden file input you render
|
|
636
|
-
* @param {Function} params.open Open the native file selection dialog
|
|
637
|
-
* @param {boolean} params.isFocused Dropzone area is in focus
|
|
638
|
-
* @param {boolean} params.isFileDialogActive File dialog is opened
|
|
639
|
-
* @param {boolean} params.isDragActive Active drag is in progress
|
|
640
|
-
* @param {boolean} params.isDragAccept Dragged files are accepted
|
|
641
|
-
* @param {boolean} params.isDragReject Some dragged files are rejected
|
|
642
|
-
* @param {File[]} params.draggedFiles Files in active drag
|
|
643
|
-
* @param {File[]} params.acceptedFiles Accepted files
|
|
644
|
-
* @param {FileRejection[]} params.fileRejections Rejected files and why they were rejected
|
|
645
|
-
*/
|
|
646
|
-
children: PropTypes.func,
|
|
647
|
-
|
|
648
|
-
/**
|
|
649
|
-
* Set accepted file types.
|
|
650
|
-
* See https://github.com/okonet/attr-accept for more information.
|
|
651
|
-
* Keep in mind that mime type determination is not reliable across platforms. CSV files,
|
|
652
|
-
* for example, are reported as text/plain under macOS but as application/vnd.ms-excel under
|
|
653
|
-
* Windows. In some cases there might not be a mime type set at all.
|
|
654
|
-
* See: https://github.com/react-dropzone/react-dropzone/issues/276
|
|
655
|
-
*/
|
|
656
|
-
accept: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
|
657
|
-
|
|
658
|
-
/**
|
|
659
|
-
* Allow drag 'n' drop (or selection from the file dialog) of multiple files
|
|
660
|
-
*/
|
|
661
|
-
multiple: PropTypes.bool,
|
|
662
|
-
|
|
663
|
-
/**
|
|
664
|
-
* If false, allow dropped items to take over the current browser window
|
|
665
|
-
*/
|
|
666
|
-
preventDropOnDocument: PropTypes.bool,
|
|
667
|
-
|
|
668
|
-
/**
|
|
669
|
-
* If true, disables click to open the native file selection dialog
|
|
670
|
-
*/
|
|
671
|
-
noClick: PropTypes.bool,
|
|
672
|
-
|
|
673
|
-
/**
|
|
674
|
-
* If true, disables SPACE/ENTER to open the native file selection dialog.
|
|
675
|
-
* Note that it also stops tracking the focus state.
|
|
676
|
-
*/
|
|
677
|
-
noKeyboard: PropTypes.bool,
|
|
678
|
-
|
|
679
|
-
/**
|
|
680
|
-
* If true, disables drag 'n' drop
|
|
681
|
-
*/
|
|
682
|
-
noDrag: PropTypes.bool,
|
|
683
|
-
|
|
684
|
-
/**
|
|
685
|
-
* If true, stops drag event propagation to parents
|
|
686
|
-
*/
|
|
687
|
-
noDragEventsBubbling: PropTypes.bool,
|
|
688
|
-
|
|
689
|
-
/**
|
|
690
|
-
* Minimum file size (in bytes)
|
|
691
|
-
*/
|
|
692
|
-
minSize: PropTypes.number,
|
|
693
|
-
|
|
694
|
-
/**
|
|
695
|
-
* Maximum file size (in bytes)
|
|
696
|
-
*/
|
|
697
|
-
maxSize: PropTypes.number,
|
|
698
|
-
|
|
699
|
-
/**
|
|
700
|
-
* Maximum accepted number of files
|
|
701
|
-
* The default value is 0 which means there is no limitation to how many files are accepted.
|
|
702
|
-
*/
|
|
703
|
-
maxFiles: PropTypes.number,
|
|
704
|
-
|
|
705
|
-
/**
|
|
706
|
-
* Enable/disable the dropzone
|
|
707
|
-
*/
|
|
708
|
-
disabled: PropTypes.bool,
|
|
709
|
-
|
|
710
|
-
/**
|
|
711
|
-
* Use this to provide a custom file aggregator
|
|
712
|
-
*
|
|
713
|
-
* @param {(DragEvent|Event)} event A drag event or input change event (if files were selected via the file dialog)
|
|
714
|
-
*/
|
|
715
|
-
getFilesFromEvent: PropTypes.func,
|
|
716
|
-
|
|
717
|
-
/**
|
|
718
|
-
* Cb for when closing the file dialog with no selection
|
|
719
|
-
*/
|
|
720
|
-
onFileDialogCancel: PropTypes.func,
|
|
721
|
-
|
|
722
|
-
/**
|
|
723
|
-
* Cb for when opening the file dialog
|
|
724
|
-
*/
|
|
725
|
-
onFileDialogOpen: PropTypes.func,
|
|
726
|
-
|
|
727
|
-
/**
|
|
728
|
-
* Set to true to use the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API
|
|
729
|
-
* to open the file picker instead of using an `<input type="file">` click event.
|
|
730
|
-
*/
|
|
731
|
-
useFsAccessApi: PropTypes.bool,
|
|
732
|
-
|
|
733
|
-
/**
|
|
734
|
-
* Cb for when the `dragenter` event occurs.
|
|
735
|
-
*
|
|
736
|
-
* @param {DragEvent} event
|
|
737
|
-
*/
|
|
738
|
-
onDragEnter: PropTypes.func,
|
|
739
|
-
|
|
740
|
-
/**
|
|
741
|
-
* Cb for when the `dragleave` event occurs
|
|
742
|
-
*
|
|
743
|
-
* @param {DragEvent} event
|
|
744
|
-
*/
|
|
745
|
-
onDragLeave: PropTypes.func,
|
|
746
|
-
|
|
747
|
-
/**
|
|
748
|
-
* Cb for when the `dragover` event occurs
|
|
749
|
-
*
|
|
750
|
-
* @param {DragEvent} event
|
|
751
|
-
*/
|
|
752
|
-
onDragOver: PropTypes.func,
|
|
753
|
-
|
|
754
|
-
/**
|
|
755
|
-
* Cb for when the `drop` event occurs.
|
|
756
|
-
* Note that this callback is invoked after the `getFilesFromEvent` callback is done.
|
|
757
|
-
*
|
|
758
|
-
* Files are accepted or rejected based on the `accept`, `multiple`, `minSize` and `maxSize` props.
|
|
759
|
-
* `accept` must be a valid [MIME type](http://www.iana.org/assignments/media-types/media-types.xhtml) according to [input element specification](https://www.w3.org/wiki/HTML/Elements/input/file) or a valid file extension.
|
|
760
|
-
* If `multiple` is set to false and additional files are dropped,
|
|
761
|
-
* all files besides the first will be rejected.
|
|
762
|
-
* Any file which does not have a size in the [`minSize`, `maxSize`] range, will be rejected as well.
|
|
763
|
-
*
|
|
764
|
-
* Note that the `onDrop` callback will always be invoked regardless if the dropped files were accepted or rejected.
|
|
765
|
-
* If you'd like to react to a specific scenario, use the `onDropAccepted`/`onDropRejected` props.
|
|
766
|
-
*
|
|
767
|
-
* `onDrop` will provide you with an array of [File](https://developer.mozilla.org/en-US/docs/Web/API/File) objects which you can then process and send to a server.
|
|
768
|
-
* For example, with [SuperAgent](https://github.com/visionmedia/superagent) as a http/ajax library:
|
|
769
|
-
*
|
|
770
|
-
* ```js
|
|
771
|
-
* function onDrop(acceptedFiles) {
|
|
772
|
-
* const req = request.post('/upload')
|
|
773
|
-
* acceptedFiles.forEach(file => {
|
|
774
|
-
* req.attach(file.name, file)
|
|
775
|
-
* })
|
|
776
|
-
* req.end(callback)
|
|
777
|
-
* }
|
|
778
|
-
* ```
|
|
779
|
-
*
|
|
780
|
-
* @param {File[]} acceptedFiles
|
|
781
|
-
* @param {FileRejection[]} fileRejections
|
|
782
|
-
* @param {(DragEvent|Event)} event A drag event or input change event (if files were selected via the file dialog)
|
|
783
|
-
*/
|
|
784
|
-
onDrop: PropTypes.func,
|
|
785
|
-
|
|
786
|
-
/**
|
|
787
|
-
* Cb for when the `drop` event occurs.
|
|
788
|
-
* Note that if no files are accepted, this callback is not invoked.
|
|
789
|
-
*
|
|
790
|
-
* @param {File[]} files
|
|
791
|
-
* @param {(DragEvent|Event)} event
|
|
792
|
-
*/
|
|
793
|
-
onDropAccepted: PropTypes.func,
|
|
794
|
-
|
|
795
|
-
/**
|
|
796
|
-
* Cb for when the `drop` event occurs.
|
|
797
|
-
* Note that if no files are rejected, this callback is not invoked.
|
|
798
|
-
*
|
|
799
|
-
* @param {FileRejection[]} fileRejections
|
|
800
|
-
* @param {(DragEvent|Event)} event
|
|
801
|
-
*/
|
|
802
|
-
onDropRejected: PropTypes.func,
|
|
803
|
-
|
|
804
|
-
/**
|
|
805
|
-
* Custom validation function
|
|
806
|
-
* @param {File} file
|
|
807
|
-
* @returns {FileError|FileError[]}
|
|
808
|
-
*/
|
|
809
|
-
validator: PropTypes.func
|
|
810
|
-
};
|
|
482
|
+
* Normalize either `accept` form to the internal array of `{description, accept}` groups.
|
|
483
|
+
*
|
|
484
|
+
* - The object form (`{mime: ext}`) becomes a single group; its `description` (which the object
|
|
485
|
+
* form can't express) is derived from the extensions like any other group.
|
|
486
|
+
* - The array form is passed through; entries missing an `accept` map are dropped.
|
|
487
|
+
*
|
|
488
|
+
* In both cases a missing `description` is filled in later from the group's extensions (see
|
|
489
|
+
* {@link describeAccept}), so the picker always gets the non-empty description it requires
|
|
490
|
+
* (https://crbug.com/1264708). Returns `undefined` when no `accept` is provided.
|
|
491
|
+
*/
|
|
492
|
+
function normalizeAcceptGroups(accept) {
|
|
493
|
+
if (!isDefined(accept)) return;
|
|
494
|
+
if (Array.isArray(accept)) return accept.filter((group) => isDefined(group) && isDefined(group.accept));
|
|
495
|
+
return [{ accept }];
|
|
496
|
+
}
|
|
811
497
|
/**
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
498
|
+
* Build a human-readable label for a picker group from its (validated) accept map, used when the
|
|
499
|
+
* caller doesn't supply a `description`. Prefer the file extensions, fall back to the MIME types,
|
|
500
|
+
* then to a generic label - `showOpenFilePicker` requires a non-empty description (crbug 1264708).
|
|
501
|
+
*/
|
|
502
|
+
function describeAccept(accept) {
|
|
503
|
+
const extensions = [];
|
|
504
|
+
const mimeTypes = Object.keys(accept);
|
|
505
|
+
for (const ext of Object.values(accept)) for (const e of toExtensions(ext)) if (!extensions.includes(e)) extensions.push(e);
|
|
506
|
+
if (extensions.length > 0) return extensions.join(", ");
|
|
507
|
+
if (mimeTypes.length > 0) return mimeTypes.join(", ");
|
|
508
|
+
return "Files";
|
|
509
|
+
}
|
|
820
510
|
/**
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
511
|
+
* Merge every `accept` group into a single MIME-type -> extensions map. Duplicate MIME keys union
|
|
512
|
+
* their extensions. This flattened map drives the native `<input accept>` attribute and the
|
|
513
|
+
* drag/drop validators, which have no concept of groups or descriptions.
|
|
514
|
+
*/
|
|
515
|
+
function flattenAccept(accept) {
|
|
516
|
+
const groups = normalizeAcceptGroups(accept);
|
|
517
|
+
if (!isDefined(groups)) return;
|
|
518
|
+
const merged = {};
|
|
519
|
+
for (const group of groups) for (const [mimeType, ext] of Object.entries(group.accept)) {
|
|
520
|
+
const existing = merged[mimeType] ?? (merged[mimeType] = []);
|
|
521
|
+
for (const e of toExtensions(ext)) if (!existing.includes(e)) existing.push(e);
|
|
522
|
+
}
|
|
523
|
+
return merged;
|
|
524
|
+
}
|
|
830
525
|
/**
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
526
|
+
* Convert the `{accept}` dropzone prop to the `{types}` option for showOpenFilePicker.
|
|
527
|
+
*
|
|
528
|
+
* Each group becomes one filter row in the picker. Invalid MIME types / extensions are dropped
|
|
529
|
+
* with a warning; a group left with no valid entries is omitted entirely (showOpenFilePicker
|
|
530
|
+
* rejects an empty `accept`). Returns `undefined` when nothing valid remains.
|
|
531
|
+
*/
|
|
532
|
+
function pickerOptionsFromAccept(accept) {
|
|
533
|
+
const groups = normalizeAcceptGroups(accept);
|
|
534
|
+
if (!isDefined(groups)) return;
|
|
535
|
+
const options = groups.map((group) => {
|
|
536
|
+
const acceptForPicker = Object.entries(group.accept).filter(([mimeType, ext]) => {
|
|
537
|
+
let ok = true;
|
|
538
|
+
if (!isMIMEType(mimeType)) {
|
|
539
|
+
console.warn(`Skipped "${mimeType}" because it is not a valid MIME type. Check https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types for a list of valid MIME types.`);
|
|
540
|
+
ok = false;
|
|
541
|
+
}
|
|
542
|
+
if (!(Array.isArray(ext) || typeof ext === "string") || !toExtensions(ext).every(isExt)) {
|
|
543
|
+
console.warn(`Skipped "${mimeType}" because an invalid file extension was provided.`);
|
|
544
|
+
ok = false;
|
|
545
|
+
}
|
|
546
|
+
return ok;
|
|
547
|
+
}).reduce((agg, [mimeType, ext]) => {
|
|
548
|
+
agg[mimeType] = toExtensions(ext);
|
|
549
|
+
return agg;
|
|
550
|
+
}, {});
|
|
551
|
+
return {
|
|
552
|
+
description: isDefined(group.description) && group.description !== "" ? group.description : describeAccept(acceptForPicker),
|
|
553
|
+
accept: acceptForPicker
|
|
554
|
+
};
|
|
555
|
+
}).filter((option) => Object.keys(option.accept).length > 0);
|
|
556
|
+
return options.length > 0 ? options : void 0;
|
|
557
|
+
}
|
|
840
558
|
/**
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
559
|
+
* Convert the `{accept}` dropzone prop to a comma-separated accept attribute string.
|
|
560
|
+
*
|
|
561
|
+
* When `omitWildcardMimeTypesWithExtensions` is set, a wildcard MIME type (e.g. `image/*`)
|
|
562
|
+
* that is paired with explicit extensions is dropped in favour of those extensions. The
|
|
563
|
+
* accept attribute is an OR list, so leaving `image/*` in would make both the native file
|
|
564
|
+
* picker and the drop-time validator accept ANY file of that type, ignoring the extension
|
|
565
|
+
* restriction. The drag-time `isDragAccept` check keeps the wildcard because file names
|
|
566
|
+
* (and therefore extensions) aren't readable during a drag.
|
|
567
|
+
*
|
|
568
|
+
* See https://github.com/react-dropzone/react-dropzone/issues/1220
|
|
569
|
+
*/
|
|
570
|
+
function acceptPropAsAcceptAttr(accept, { omitWildcardMimeTypesWithExtensions = false } = {}) {
|
|
571
|
+
if (isDefined(accept)) return Object.entries(accept).reduce((a, [mimeType, extVal]) => {
|
|
572
|
+
const ext = toExtensions(extVal);
|
|
573
|
+
if (omitWildcardMimeTypesWithExtensions && isMIMETypeWildcard(mimeType) && ext.some(isExt)) a.push(...ext);
|
|
574
|
+
else a.push(mimeType, ...ext);
|
|
575
|
+
return a;
|
|
576
|
+
}, []).filter((v) => isMIMEType(v) || isExt(v)).join(",");
|
|
577
|
+
}
|
|
849
578
|
/**
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
* @returns {(File[]|Promise<File[]>)}
|
|
856
|
-
*/
|
|
857
|
-
|
|
579
|
+
* Check if v is an exception caused by aborting a request (e.g window.showOpenFilePicker()).
|
|
580
|
+
*/
|
|
581
|
+
function isAbort(v) {
|
|
582
|
+
return v instanceof DOMException && (v.name === "AbortError" || v.code === v.ABORT_ERR);
|
|
583
|
+
}
|
|
858
584
|
/**
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
585
|
+
* Check if v is a security error.
|
|
586
|
+
*/
|
|
587
|
+
function isSecurityError(v) {
|
|
588
|
+
return v instanceof DOMException && (v.name === "SecurityError" || v.code === v.SECURITY_ERR);
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Check if v is a "not allowed" error.
|
|
592
|
+
*
|
|
593
|
+
* Some browsers/configurations block `window.showOpenFilePicker()` outright and reject with a
|
|
594
|
+
* `NotAllowedError` instead of showing the picker (e.g. Microsoft Edge for Business, or other
|
|
595
|
+
* restrictive enterprise/security policies). We treat this like a security error and fall back
|
|
596
|
+
* to the native `<input>`. See https://github.com/react-dropzone/react-dropzone/issues/1429
|
|
597
|
+
*/
|
|
598
|
+
function isNotAllowedError(v) {
|
|
599
|
+
return v instanceof DOMException && v.name === "NotAllowedError";
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Check if v is a MIME type string.
|
|
603
|
+
*/
|
|
604
|
+
function isMIMEType(v) {
|
|
605
|
+
return v === "audio/*" || v === "video/*" || v === "image/*" || v === "text/*" || v === "application/*" || /\w+\/[-+.\w]+/g.test(v);
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Check if v is a wildcard MIME type (e.g. `image/*`).
|
|
609
|
+
*/
|
|
610
|
+
function isMIMETypeWildcard(v) {
|
|
611
|
+
return v.endsWith("/*");
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Check if v is a file extension.
|
|
615
|
+
*/
|
|
616
|
+
function isExt(v) {
|
|
617
|
+
return /^.*\.[\w]+$/.test(v);
|
|
618
|
+
}
|
|
619
|
+
//#endregion
|
|
620
|
+
//#region src/index.tsx
|
|
621
|
+
/**
|
|
622
|
+
* Convenience wrapper component for the `useDropzone` hook
|
|
623
|
+
*
|
|
624
|
+
* ```jsx
|
|
625
|
+
* <Dropzone>
|
|
626
|
+
* {({getRootProps, getInputProps}) => (
|
|
627
|
+
* <div {...getRootProps()}>
|
|
628
|
+
* <input {...getInputProps()} />
|
|
629
|
+
* <p>Drag 'n' drop some files here, or click to select files</p>
|
|
630
|
+
* </div>
|
|
631
|
+
* )}
|
|
632
|
+
* </Dropzone>
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
const Dropzone = forwardRef(({ children, ...params }, ref) => {
|
|
636
|
+
const { open, ...props } = useDropzone(params);
|
|
637
|
+
useImperativeHandle(ref, () => ({ open }), [open]);
|
|
638
|
+
return /* @__PURE__ */ jsx(Fragment, { children: children?.({
|
|
639
|
+
...props,
|
|
640
|
+
open
|
|
641
|
+
}) });
|
|
642
|
+
});
|
|
643
|
+
Dropzone.displayName = "Dropzone";
|
|
644
|
+
const initialState = {
|
|
645
|
+
isFocused: false,
|
|
646
|
+
isFileDialogActive: false,
|
|
647
|
+
isDragActive: false,
|
|
648
|
+
isDragAccept: false,
|
|
649
|
+
isDragReject: false,
|
|
650
|
+
isDragUnknown: false,
|
|
651
|
+
isDragGlobal: false,
|
|
652
|
+
isProcessing: false,
|
|
653
|
+
acceptedFiles: [],
|
|
654
|
+
fileRejections: [],
|
|
655
|
+
dragFileRejections: []
|
|
884
656
|
};
|
|
885
657
|
/**
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
658
|
+
* A React hook that creates a drag 'n' drop area.
|
|
659
|
+
*
|
|
660
|
+
* ```jsx
|
|
661
|
+
* function MyDropzone(props) {
|
|
662
|
+
* const {getRootProps, getInputProps} = useDropzone({
|
|
663
|
+
* onDrop: acceptedFiles => {
|
|
664
|
+
* // do something with the File objects, e.g. upload to some server
|
|
665
|
+
* }
|
|
666
|
+
* });
|
|
667
|
+
* return (
|
|
668
|
+
* <div {...getRootProps()}>
|
|
669
|
+
* <input {...getInputProps()} />
|
|
670
|
+
* <p>Drag and drop some files here, or click to select files</p>
|
|
671
|
+
* </div>
|
|
672
|
+
* )
|
|
673
|
+
* }
|
|
674
|
+
* ```
|
|
675
|
+
*/
|
|
676
|
+
function useDropzone(props = {}) {
|
|
677
|
+
const { accept, disabled = false, getFilesFromEvent = fromEvent, maxSize = Number.POSITIVE_INFINITY, minSize = 0, multiple = true, maxFiles = 0, onDragEnter, onDragLeave, onDragOver, onDrop, onDropAccepted, onDropRejected, onFileDialogCancel, onFileDialogOpen, useFsAccessApi = false, autoFocus = false, preventDropOnDocument = true, noClick = false, noKeyboard = false, noDrag = false, noDragEventsBubbling = false, noPaste = false, onError, validator, getErrorMessage } = props;
|
|
678
|
+
const flatAccept = useMemo(() => flattenAccept(accept), [accept]);
|
|
679
|
+
const acceptAttr = useMemo(() => acceptPropAsAcceptAttr(flatAccept), [flatAccept]);
|
|
680
|
+
const inputAcceptAttr = useMemo(() => acceptPropAsAcceptAttr(flatAccept, { omitWildcardMimeTypesWithExtensions: true }), [flatAccept]);
|
|
681
|
+
const pickerTypes = useMemo(() => pickerOptionsFromAccept(accept), [accept]);
|
|
682
|
+
const onFileDialogOpenCb = useMemo(() => typeof onFileDialogOpen === "function" ? onFileDialogOpen : noop, [onFileDialogOpen]);
|
|
683
|
+
const onFileDialogCancelCb = useMemo(() => typeof onFileDialogCancel === "function" ? onFileDialogCancel : noop, [onFileDialogCancel]);
|
|
684
|
+
const rootRef = useRef(null);
|
|
685
|
+
const inputRef = useRef(null);
|
|
686
|
+
const [state, dispatch] = useReducer(reducer, initialState);
|
|
687
|
+
const { isFocused, isFileDialogActive } = state;
|
|
688
|
+
const isFileDialogActiveRef = useRef(isFileDialogActive);
|
|
689
|
+
isFileDialogActiveRef.current = isFileDialogActive;
|
|
690
|
+
const processingAbortRef = useRef(null);
|
|
691
|
+
const beginProcessing = useCallback(() => {
|
|
692
|
+
processingAbortRef.current?.abort();
|
|
693
|
+
const controller = new AbortController();
|
|
694
|
+
processingAbortRef.current = controller;
|
|
695
|
+
dispatch({
|
|
696
|
+
type: "setProcessing",
|
|
697
|
+
isProcessing: true
|
|
698
|
+
});
|
|
699
|
+
return controller.signal;
|
|
700
|
+
}, []);
|
|
701
|
+
const endProcessing = useCallback((signal) => {
|
|
702
|
+
if (!signal.aborted) dispatch({
|
|
703
|
+
type: "setProcessing",
|
|
704
|
+
isProcessing: false
|
|
705
|
+
});
|
|
706
|
+
}, []);
|
|
707
|
+
const fsAccessApiWorksRef = useRef(typeof window !== "undefined" && window.isSecureContext && useFsAccessApi && canUseFileSystemAccessAPI());
|
|
708
|
+
const onWindowFocus = () => {
|
|
709
|
+
if (!fsAccessApiWorksRef.current && isFileDialogActive) setTimeout(() => {
|
|
710
|
+
if (inputRef.current) {
|
|
711
|
+
const { files } = inputRef.current;
|
|
712
|
+
if (!files?.length) {
|
|
713
|
+
dispatch({ type: "closeDialog" });
|
|
714
|
+
onFileDialogCancelCb();
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}, 300);
|
|
718
|
+
};
|
|
719
|
+
useEffect(() => {
|
|
720
|
+
window.addEventListener("focus", onWindowFocus, false);
|
|
721
|
+
return () => {
|
|
722
|
+
window.removeEventListener("focus", onWindowFocus, false);
|
|
723
|
+
};
|
|
724
|
+
}, [
|
|
725
|
+
inputRef,
|
|
726
|
+
isFileDialogActive,
|
|
727
|
+
onFileDialogCancelCb,
|
|
728
|
+
fsAccessApiWorksRef
|
|
729
|
+
]);
|
|
730
|
+
const dragTargetsRef = useRef([]);
|
|
731
|
+
const globalDragTargetsRef = useRef([]);
|
|
732
|
+
const onDocumentDrop = (event) => {
|
|
733
|
+
if (rootRef.current && event.target && rootRef.current.contains(event.target) && event.defaultPrevented) return;
|
|
734
|
+
event.preventDefault();
|
|
735
|
+
dragTargetsRef.current = [];
|
|
736
|
+
};
|
|
737
|
+
useEffect(() => {
|
|
738
|
+
if (preventDropOnDocument) {
|
|
739
|
+
document.addEventListener("dragover", onDocumentDragOver, false);
|
|
740
|
+
document.addEventListener("drop", onDocumentDrop, false);
|
|
741
|
+
}
|
|
742
|
+
return () => {
|
|
743
|
+
if (preventDropOnDocument) {
|
|
744
|
+
document.removeEventListener("dragover", onDocumentDragOver);
|
|
745
|
+
document.removeEventListener("drop", onDocumentDrop);
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
}, [rootRef, preventDropOnDocument]);
|
|
749
|
+
useEffect(() => {
|
|
750
|
+
const onDocumentDragEnter = (event) => {
|
|
751
|
+
if (event.target) globalDragTargetsRef.current = [...globalDragTargetsRef.current, event.target];
|
|
752
|
+
if (isEvtWithFiles(event)) dispatch({
|
|
753
|
+
isDragGlobal: true,
|
|
754
|
+
type: "setDragGlobal"
|
|
755
|
+
});
|
|
756
|
+
};
|
|
757
|
+
const onDocumentDragLeave = (event) => {
|
|
758
|
+
globalDragTargetsRef.current = globalDragTargetsRef.current.filter((el) => el !== event.target && el !== null);
|
|
759
|
+
if (globalDragTargetsRef.current.length > 0) return;
|
|
760
|
+
dispatch({
|
|
761
|
+
isDragGlobal: false,
|
|
762
|
+
type: "setDragGlobal"
|
|
763
|
+
});
|
|
764
|
+
};
|
|
765
|
+
const onDocumentDragEnd = () => {
|
|
766
|
+
globalDragTargetsRef.current = [];
|
|
767
|
+
dispatch({
|
|
768
|
+
isDragGlobal: false,
|
|
769
|
+
type: "setDragGlobal"
|
|
770
|
+
});
|
|
771
|
+
};
|
|
772
|
+
const onDocumentDropGlobal = () => {
|
|
773
|
+
globalDragTargetsRef.current = [];
|
|
774
|
+
dispatch({
|
|
775
|
+
isDragGlobal: false,
|
|
776
|
+
type: "setDragGlobal"
|
|
777
|
+
});
|
|
778
|
+
};
|
|
779
|
+
document.addEventListener("dragenter", onDocumentDragEnter, false);
|
|
780
|
+
document.addEventListener("dragleave", onDocumentDragLeave, false);
|
|
781
|
+
document.addEventListener("dragend", onDocumentDragEnd, false);
|
|
782
|
+
document.addEventListener("drop", onDocumentDropGlobal, false);
|
|
783
|
+
return () => {
|
|
784
|
+
document.removeEventListener("dragenter", onDocumentDragEnter);
|
|
785
|
+
document.removeEventListener("dragleave", onDocumentDragLeave);
|
|
786
|
+
document.removeEventListener("dragend", onDocumentDragEnd);
|
|
787
|
+
document.removeEventListener("drop", onDocumentDropGlobal);
|
|
788
|
+
};
|
|
789
|
+
}, [rootRef]);
|
|
790
|
+
useEffect(() => {
|
|
791
|
+
if (!disabled && autoFocus && rootRef.current) rootRef.current.focus();
|
|
792
|
+
return () => {};
|
|
793
|
+
}, [
|
|
794
|
+
rootRef,
|
|
795
|
+
autoFocus,
|
|
796
|
+
disabled
|
|
797
|
+
]);
|
|
798
|
+
const onErrCb = useCallback((e) => {
|
|
799
|
+
if (onError) onError(e);
|
|
800
|
+
else console.error(e);
|
|
801
|
+
}, [onError]);
|
|
802
|
+
const onDragEnterCb = useCallback((event) => {
|
|
803
|
+
event.preventDefault();
|
|
804
|
+
event.persist?.();
|
|
805
|
+
stopPropagation(event);
|
|
806
|
+
if (isFileDialogActiveRef.current) return;
|
|
807
|
+
dragTargetsRef.current = [...dragTargetsRef.current, event.target];
|
|
808
|
+
if (isEvtWithFiles(event)) Promise.resolve(getFilesFromEvent(event)).then((files) => {
|
|
809
|
+
if (isPropagationStopped(event) && !noDragEventsBubbling) return;
|
|
810
|
+
const evaluation = files.length > 0 ? evaluateDragFiles({
|
|
811
|
+
files,
|
|
812
|
+
accept: acceptAttr,
|
|
813
|
+
minSize,
|
|
814
|
+
maxSize,
|
|
815
|
+
multiple,
|
|
816
|
+
maxFiles,
|
|
817
|
+
validator,
|
|
818
|
+
getErrorMessage
|
|
819
|
+
}) : null;
|
|
820
|
+
dispatch({
|
|
821
|
+
isDragAccept: evaluation?.verdict === "accept",
|
|
822
|
+
isDragReject: evaluation?.verdict === "reject",
|
|
823
|
+
isDragUnknown: evaluation?.verdict === "unknown",
|
|
824
|
+
isDragActive: true,
|
|
825
|
+
dragFileRejections: evaluation?.rejections ?? [],
|
|
826
|
+
type: "setDraggedFiles"
|
|
827
|
+
});
|
|
828
|
+
if (onDragEnter) onDragEnter(event);
|
|
829
|
+
}).catch((e) => onErrCb(e));
|
|
830
|
+
}, [
|
|
831
|
+
getFilesFromEvent,
|
|
832
|
+
onDragEnter,
|
|
833
|
+
onErrCb,
|
|
834
|
+
noDragEventsBubbling,
|
|
835
|
+
acceptAttr,
|
|
836
|
+
minSize,
|
|
837
|
+
maxSize,
|
|
838
|
+
multiple,
|
|
839
|
+
maxFiles,
|
|
840
|
+
validator,
|
|
841
|
+
getErrorMessage
|
|
842
|
+
]);
|
|
843
|
+
const onDragOverCb = useCallback((event) => {
|
|
844
|
+
event.preventDefault();
|
|
845
|
+
event.persist?.();
|
|
846
|
+
stopPropagation(event);
|
|
847
|
+
if (isFileDialogActiveRef.current) return false;
|
|
848
|
+
const hasFiles = isEvtWithFiles(event);
|
|
849
|
+
if (hasFiles && event.dataTransfer) try {
|
|
850
|
+
event.dataTransfer.dropEffect = "copy";
|
|
851
|
+
} catch {}
|
|
852
|
+
if (hasFiles && onDragOver) onDragOver(event);
|
|
853
|
+
return false;
|
|
854
|
+
}, [onDragOver, noDragEventsBubbling]);
|
|
855
|
+
const onDragLeaveCb = useCallback((event) => {
|
|
856
|
+
event.preventDefault();
|
|
857
|
+
event.persist?.();
|
|
858
|
+
stopPropagation(event);
|
|
859
|
+
const targets = dragTargetsRef.current.filter((target) => rootRef.current?.contains(target));
|
|
860
|
+
const targetIdx = targets.indexOf(event.target);
|
|
861
|
+
if (targetIdx !== -1) targets.splice(targetIdx, 1);
|
|
862
|
+
dragTargetsRef.current = targets;
|
|
863
|
+
if (targets.length > 0) return;
|
|
864
|
+
dispatch({
|
|
865
|
+
type: "setDraggedFiles",
|
|
866
|
+
isDragActive: false,
|
|
867
|
+
isDragAccept: false,
|
|
868
|
+
isDragReject: false,
|
|
869
|
+
isDragUnknown: false,
|
|
870
|
+
dragFileRejections: []
|
|
871
|
+
});
|
|
872
|
+
if (isEvtWithFiles(event) && onDragLeave) onDragLeave(event);
|
|
873
|
+
}, [
|
|
874
|
+
rootRef,
|
|
875
|
+
onDragLeave,
|
|
876
|
+
noDragEventsBubbling
|
|
877
|
+
]);
|
|
878
|
+
const setFiles = useCallback(async (files, event, signal) => {
|
|
879
|
+
const localizeError = (error, file) => getErrorMessage ? {
|
|
880
|
+
...error,
|
|
881
|
+
message: getErrorMessage(error, file)
|
|
882
|
+
} : error;
|
|
883
|
+
const commit = (results) => {
|
|
884
|
+
const acceptedFiles = [];
|
|
885
|
+
const fileRejections = [];
|
|
886
|
+
results.forEach(({ file, accepted, acceptError, sizeMatch, sizeError, customErrors }) => {
|
|
887
|
+
const valid = !customErrors || Array.isArray(customErrors) && customErrors.length === 0;
|
|
888
|
+
if (accepted && sizeMatch && valid) acceptedFiles.push(file);
|
|
889
|
+
else {
|
|
890
|
+
let errors = [acceptError, sizeError];
|
|
891
|
+
if (customErrors) errors = errors.concat(customErrors);
|
|
892
|
+
fileRejections.push({
|
|
893
|
+
file,
|
|
894
|
+
errors: errors.filter((e) => e != null).map((error) => localizeError(error, file))
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
});
|
|
898
|
+
const acceptedFilesLimit = multiple ? maxFiles >= 1 ? maxFiles : Number.POSITIVE_INFINITY : 1;
|
|
899
|
+
if (acceptedFiles.length > acceptedFilesLimit) acceptedFiles.splice(acceptedFilesLimit).forEach((file) => {
|
|
900
|
+
fileRejections.push({
|
|
901
|
+
file,
|
|
902
|
+
errors: [localizeError(TOO_MANY_FILES_REJECTION, file)]
|
|
903
|
+
});
|
|
904
|
+
});
|
|
905
|
+
dispatch({
|
|
906
|
+
acceptedFiles,
|
|
907
|
+
fileRejections,
|
|
908
|
+
type: "setFiles"
|
|
909
|
+
});
|
|
910
|
+
if (onDrop) onDrop(acceptedFiles, fileRejections, event);
|
|
911
|
+
if (fileRejections.length > 0 && onDropRejected) onDropRejected(fileRejections, event);
|
|
912
|
+
if (acceptedFiles.length > 0 && onDropAccepted) onDropAccepted(acceptedFiles, event);
|
|
913
|
+
};
|
|
914
|
+
const pending = files.map((file) => {
|
|
915
|
+
const [accepted, acceptError] = fileAccepted(file, inputAcceptAttr);
|
|
916
|
+
const [sizeMatch, sizeError] = fileMatchSize(file, minSize, maxSize);
|
|
917
|
+
return {
|
|
918
|
+
file,
|
|
919
|
+
accepted,
|
|
920
|
+
acceptError,
|
|
921
|
+
sizeMatch,
|
|
922
|
+
sizeError,
|
|
923
|
+
customErrors: validator ? validator(file) : null
|
|
924
|
+
};
|
|
925
|
+
});
|
|
926
|
+
if (!pending.some(({ customErrors }) => isThenable(customErrors))) {
|
|
927
|
+
commit(pending);
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
let results;
|
|
931
|
+
try {
|
|
932
|
+
results = await Promise.all(pending.map(async ({ customErrors, ...rest }) => ({
|
|
933
|
+
...rest,
|
|
934
|
+
customErrors: await customErrors
|
|
935
|
+
})));
|
|
936
|
+
} catch (e) {
|
|
937
|
+
if (!signal.aborted) {
|
|
938
|
+
endProcessing(signal);
|
|
939
|
+
onErrCb(e);
|
|
940
|
+
}
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
if (signal.aborted) return;
|
|
944
|
+
commit(results);
|
|
945
|
+
}, [
|
|
946
|
+
dispatch,
|
|
947
|
+
multiple,
|
|
948
|
+
inputAcceptAttr,
|
|
949
|
+
minSize,
|
|
950
|
+
maxSize,
|
|
951
|
+
maxFiles,
|
|
952
|
+
onDrop,
|
|
953
|
+
onDropAccepted,
|
|
954
|
+
onDropRejected,
|
|
955
|
+
validator,
|
|
956
|
+
getErrorMessage,
|
|
957
|
+
onErrCb,
|
|
958
|
+
endProcessing
|
|
959
|
+
]);
|
|
960
|
+
const onDropCb = useCallback((event) => {
|
|
961
|
+
event.preventDefault();
|
|
962
|
+
event.persist?.();
|
|
963
|
+
stopPropagation(event);
|
|
964
|
+
dragTargetsRef.current = [];
|
|
965
|
+
if (isFileDialogActiveRef.current && event.dataTransfer) return;
|
|
966
|
+
dispatch({ type: "reset" });
|
|
967
|
+
if (isEvtWithFiles(event)) {
|
|
968
|
+
const signal = beginProcessing();
|
|
969
|
+
Promise.resolve(getFilesFromEvent(event)).then((files) => {
|
|
970
|
+
if (signal.aborted) return;
|
|
971
|
+
if (isPropagationStopped(event) && !noDragEventsBubbling) {
|
|
972
|
+
endProcessing(signal);
|
|
973
|
+
return;
|
|
974
|
+
}
|
|
975
|
+
return setFiles(files, event, signal);
|
|
976
|
+
}).catch((e) => {
|
|
977
|
+
if (!signal.aborted) {
|
|
978
|
+
endProcessing(signal);
|
|
979
|
+
onErrCb(e);
|
|
980
|
+
}
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
}, [
|
|
984
|
+
getFilesFromEvent,
|
|
985
|
+
setFiles,
|
|
986
|
+
onErrCb,
|
|
987
|
+
noDragEventsBubbling,
|
|
988
|
+
beginProcessing,
|
|
989
|
+
endProcessing
|
|
990
|
+
]);
|
|
991
|
+
const onPasteCb = useCallback((event) => {
|
|
992
|
+
if (!isEvtWithFiles(event)) return;
|
|
993
|
+
event.preventDefault();
|
|
994
|
+
event.persist?.();
|
|
995
|
+
stopPropagation(event);
|
|
996
|
+
const signal = beginProcessing();
|
|
997
|
+
Promise.resolve(getFilesFromEvent(event)).then((files) => {
|
|
998
|
+
if (signal.aborted) return;
|
|
999
|
+
if (isPropagationStopped(event) && !noDragEventsBubbling) {
|
|
1000
|
+
endProcessing(signal);
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
return setFiles(files, event, signal);
|
|
1004
|
+
}).catch((e) => {
|
|
1005
|
+
if (!signal.aborted) {
|
|
1006
|
+
endProcessing(signal);
|
|
1007
|
+
onErrCb(e);
|
|
1008
|
+
}
|
|
1009
|
+
});
|
|
1010
|
+
}, [
|
|
1011
|
+
getFilesFromEvent,
|
|
1012
|
+
setFiles,
|
|
1013
|
+
onErrCb,
|
|
1014
|
+
noDragEventsBubbling,
|
|
1015
|
+
beginProcessing,
|
|
1016
|
+
endProcessing
|
|
1017
|
+
]);
|
|
1018
|
+
const openFileDialog = useCallback(() => {
|
|
1019
|
+
if (fsAccessApiWorksRef.current) {
|
|
1020
|
+
dispatch({ type: "openDialog" });
|
|
1021
|
+
onFileDialogOpenCb();
|
|
1022
|
+
const opts = {
|
|
1023
|
+
multiple,
|
|
1024
|
+
types: pickerTypes
|
|
1025
|
+
};
|
|
1026
|
+
let signal;
|
|
1027
|
+
window.showOpenFilePicker(opts).then((handles) => {
|
|
1028
|
+
signal = beginProcessing();
|
|
1029
|
+
return getFilesFromEvent(handles);
|
|
1030
|
+
}).then((files) => {
|
|
1031
|
+
dispatch({ type: "closeDialog" });
|
|
1032
|
+
if (signal.aborted) return;
|
|
1033
|
+
return setFiles(files, null, signal);
|
|
1034
|
+
}).catch((e) => {
|
|
1035
|
+
if (signal) endProcessing(signal);
|
|
1036
|
+
if (isAbort(e)) {
|
|
1037
|
+
onFileDialogCancelCb(e);
|
|
1038
|
+
dispatch({ type: "closeDialog" });
|
|
1039
|
+
} else if (isSecurityError(e) || isNotAllowedError(e)) {
|
|
1040
|
+
fsAccessApiWorksRef.current = false;
|
|
1041
|
+
if (inputRef.current) {
|
|
1042
|
+
inputRef.current.value = "";
|
|
1043
|
+
inputRef.current.click();
|
|
1044
|
+
} else onErrCb(/* @__PURE__ */ new Error("Cannot open the file picker because the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API is not supported and no <input> was provided."));
|
|
1045
|
+
} else onErrCb(e);
|
|
1046
|
+
});
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
1049
|
+
if (inputRef.current) {
|
|
1050
|
+
dispatch({ type: "openDialog" });
|
|
1051
|
+
onFileDialogOpenCb();
|
|
1052
|
+
inputRef.current.value = "";
|
|
1053
|
+
inputRef.current.click();
|
|
1054
|
+
}
|
|
1055
|
+
}, [
|
|
1056
|
+
dispatch,
|
|
1057
|
+
onFileDialogOpenCb,
|
|
1058
|
+
onFileDialogCancelCb,
|
|
1059
|
+
useFsAccessApi,
|
|
1060
|
+
setFiles,
|
|
1061
|
+
onErrCb,
|
|
1062
|
+
pickerTypes,
|
|
1063
|
+
multiple,
|
|
1064
|
+
beginProcessing,
|
|
1065
|
+
endProcessing
|
|
1066
|
+
]);
|
|
1067
|
+
const onKeyDownCb = useCallback((event) => {
|
|
1068
|
+
if (!rootRef.current?.isEqualNode(event.target)) return;
|
|
1069
|
+
if (event.key === " " || event.key === "Enter" || event.keyCode === 32 || event.keyCode === 13) {
|
|
1070
|
+
event.preventDefault();
|
|
1071
|
+
openFileDialog();
|
|
1072
|
+
}
|
|
1073
|
+
}, [rootRef, openFileDialog]);
|
|
1074
|
+
const onFocusCb = useCallback(() => {
|
|
1075
|
+
dispatch({ type: "focus" });
|
|
1076
|
+
}, []);
|
|
1077
|
+
const onBlurCb = useCallback(() => {
|
|
1078
|
+
dispatch({ type: "blur" });
|
|
1079
|
+
}, []);
|
|
1080
|
+
const onClickCb = useCallback(() => {
|
|
1081
|
+
if (noClick) return;
|
|
1082
|
+
if (isIeOrEdge()) setTimeout(openFileDialog, 0);
|
|
1083
|
+
else openFileDialog();
|
|
1084
|
+
}, [noClick, openFileDialog]);
|
|
1085
|
+
const composeHandler = (fn) => {
|
|
1086
|
+
return disabled ? null : fn;
|
|
1087
|
+
};
|
|
1088
|
+
const composeKeyboardHandler = (fn) => {
|
|
1089
|
+
return noKeyboard ? null : composeHandler(fn);
|
|
1090
|
+
};
|
|
1091
|
+
const composeDragHandler = (fn) => {
|
|
1092
|
+
return noDrag ? null : composeHandler(fn);
|
|
1093
|
+
};
|
|
1094
|
+
const composePasteHandler = (fn) => {
|
|
1095
|
+
return noPaste ? null : composeHandler(fn);
|
|
1096
|
+
};
|
|
1097
|
+
const stopPropagation = (event) => {
|
|
1098
|
+
if (noDragEventsBubbling) event.stopPropagation();
|
|
1099
|
+
};
|
|
1100
|
+
const getRootProps = useMemo(() => ({ refKey = "ref", role, onKeyDown, onFocus, onBlur, onClick, onDragEnter, onDragOver, onDragLeave, onDrop, onPaste, ...rest } = {}) => ({
|
|
1101
|
+
onKeyDown: composeKeyboardHandler(composeEventHandlers(onKeyDown, onKeyDownCb)),
|
|
1102
|
+
onFocus: composeKeyboardHandler(composeEventHandlers(onFocus, onFocusCb)),
|
|
1103
|
+
onBlur: composeKeyboardHandler(composeEventHandlers(onBlur, onBlurCb)),
|
|
1104
|
+
onClick: composeHandler(composeEventHandlers(onClick, onClickCb)),
|
|
1105
|
+
onDragEnter: composeDragHandler(composeEventHandlers(onDragEnter, onDragEnterCb)),
|
|
1106
|
+
onDragOver: composeDragHandler(composeEventHandlers(onDragOver, onDragOverCb)),
|
|
1107
|
+
onDragLeave: composeDragHandler(composeEventHandlers(onDragLeave, onDragLeaveCb)),
|
|
1108
|
+
onDrop: composeDragHandler(composeEventHandlers(onDrop, onDropCb)),
|
|
1109
|
+
onPaste: composePasteHandler(composeEventHandlers(onPaste, onPasteCb)),
|
|
1110
|
+
role: typeof role === "string" && role !== "" ? role : "presentation",
|
|
1111
|
+
[refKey]: rootRef,
|
|
1112
|
+
...!disabled && !noKeyboard ? { tabIndex: 0 } : {},
|
|
1113
|
+
...disabled ? { "aria-disabled": true } : {},
|
|
1114
|
+
...rest
|
|
1115
|
+
}), [
|
|
1116
|
+
rootRef,
|
|
1117
|
+
onKeyDownCb,
|
|
1118
|
+
onFocusCb,
|
|
1119
|
+
onBlurCb,
|
|
1120
|
+
onClickCb,
|
|
1121
|
+
onDragEnterCb,
|
|
1122
|
+
onDragOverCb,
|
|
1123
|
+
onDragLeaveCb,
|
|
1124
|
+
onDropCb,
|
|
1125
|
+
onPasteCb,
|
|
1126
|
+
noKeyboard,
|
|
1127
|
+
noDrag,
|
|
1128
|
+
noPaste,
|
|
1129
|
+
disabled
|
|
1130
|
+
]);
|
|
1131
|
+
const onInputElementClick = useCallback((event) => {
|
|
1132
|
+
event.stopPropagation();
|
|
1133
|
+
}, []);
|
|
1134
|
+
const getInputProps = useMemo(() => ({ refKey = "ref", onChange, onClick, ...rest } = {}) => {
|
|
1135
|
+
return {
|
|
1136
|
+
accept: inputAcceptAttr,
|
|
1137
|
+
multiple,
|
|
1138
|
+
type: "file",
|
|
1139
|
+
"aria-label": "file upload",
|
|
1140
|
+
style: {
|
|
1141
|
+
border: 0,
|
|
1142
|
+
display: "block",
|
|
1143
|
+
height: 0,
|
|
1144
|
+
margin: 0,
|
|
1145
|
+
opacity: 0,
|
|
1146
|
+
overflow: "hidden",
|
|
1147
|
+
padding: 0,
|
|
1148
|
+
width: 0
|
|
1149
|
+
},
|
|
1150
|
+
onChange: composeHandler(composeEventHandlers(onChange, onDropCb)),
|
|
1151
|
+
onClick: composeHandler(composeEventHandlers(onClick, onInputElementClick)),
|
|
1152
|
+
tabIndex: -1,
|
|
1153
|
+
[refKey]: inputRef,
|
|
1154
|
+
...rest
|
|
1155
|
+
};
|
|
1156
|
+
}, [
|
|
1157
|
+
inputRef,
|
|
1158
|
+
accept,
|
|
1159
|
+
multiple,
|
|
1160
|
+
onDropCb,
|
|
1161
|
+
disabled
|
|
1162
|
+
]);
|
|
1163
|
+
return {
|
|
1164
|
+
...state,
|
|
1165
|
+
isFocused: isFocused && !disabled,
|
|
1166
|
+
getRootProps,
|
|
1167
|
+
getInputProps,
|
|
1168
|
+
rootRef,
|
|
1169
|
+
inputRef,
|
|
1170
|
+
open: composeHandler(openFileDialog)
|
|
1171
|
+
};
|
|
1391
1172
|
}
|
|
1392
|
-
|
|
1393
1173
|
function reducer(state, action) {
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1174
|
+
switch (action.type) {
|
|
1175
|
+
case "focus": return {
|
|
1176
|
+
...state,
|
|
1177
|
+
isFocused: true
|
|
1178
|
+
};
|
|
1179
|
+
case "blur": return {
|
|
1180
|
+
...state,
|
|
1181
|
+
isFocused: false
|
|
1182
|
+
};
|
|
1183
|
+
case "openDialog": return {
|
|
1184
|
+
...initialState,
|
|
1185
|
+
isFileDialogActive: true
|
|
1186
|
+
};
|
|
1187
|
+
case "closeDialog": return {
|
|
1188
|
+
...state,
|
|
1189
|
+
isFileDialogActive: false
|
|
1190
|
+
};
|
|
1191
|
+
case "setDraggedFiles": return {
|
|
1192
|
+
...state,
|
|
1193
|
+
isDragActive: action.isDragActive,
|
|
1194
|
+
isDragAccept: action.isDragAccept,
|
|
1195
|
+
isDragReject: action.isDragReject,
|
|
1196
|
+
isDragUnknown: action.isDragUnknown,
|
|
1197
|
+
dragFileRejections: action.dragFileRejections
|
|
1198
|
+
};
|
|
1199
|
+
case "setProcessing": return {
|
|
1200
|
+
...state,
|
|
1201
|
+
isProcessing: action.isProcessing
|
|
1202
|
+
};
|
|
1203
|
+
case "setFiles": return {
|
|
1204
|
+
...state,
|
|
1205
|
+
acceptedFiles: action.acceptedFiles,
|
|
1206
|
+
fileRejections: action.fileRejections,
|
|
1207
|
+
dragFileRejections: [],
|
|
1208
|
+
isProcessing: false,
|
|
1209
|
+
isDragReject: false,
|
|
1210
|
+
isDragUnknown: false
|
|
1211
|
+
};
|
|
1212
|
+
case "setDragGlobal": return {
|
|
1213
|
+
...state,
|
|
1214
|
+
isDragGlobal: action.isDragGlobal
|
|
1215
|
+
};
|
|
1216
|
+
case "reset": return { ...initialState };
|
|
1217
|
+
default: return state;
|
|
1218
|
+
}
|
|
1437
1219
|
}
|
|
1438
|
-
|
|
1439
1220
|
function noop() {}
|
|
1440
1221
|
|
|
1441
1222
|
var styles = {"dropZoneBase":"fSDkYM-382I-","dropZone":"yT6m1v71VK8-","active":"fCb0O1wAjR0-","error":"Cm5wIem0XQI-","validationErrors":"wyjYhAlF1qA-","dropzoneContent":"sAxF04DcAi4-"};
|
|
@@ -1532,38 +1313,46 @@ function InputFileValidationErrors({ validationErrors, }) {
|
|
|
1532
1313
|
if (validationErrors.length === 0) {
|
|
1533
1314
|
return null;
|
|
1534
1315
|
}
|
|
1535
|
-
return (React__default.createElement("div", { className: styles.validationErrors }, validationErrors.map(error => (React__default.createElement(InputValidation, { message: error.message, key: error.code })))));
|
|
1316
|
+
return (React__default.createElement("div", { className: styles.validationErrors }, validationErrors.map((error, index) => (React__default.createElement(InputValidation, { message: error.message, key: `${error.code}-${index}` })))));
|
|
1536
1317
|
}
|
|
1537
1318
|
|
|
1319
|
+
const inputFileMessages = defineMessages({
|
|
1320
|
+
fileLimit: {
|
|
1321
|
+
en: {
|
|
1322
|
+
one: "Cannot exceed a maximum of {count} file.",
|
|
1323
|
+
other: "Cannot exceed a maximum of {count} files.",
|
|
1324
|
+
},
|
|
1325
|
+
es: {
|
|
1326
|
+
one: "No se puede superar el máximo de {count} archivo.",
|
|
1327
|
+
other: "No se puede superar el máximo de {count} archivos.",
|
|
1328
|
+
},
|
|
1329
|
+
},
|
|
1330
|
+
uploadFile: {
|
|
1331
|
+
en: "Upload file",
|
|
1332
|
+
es: "Subir archivo",
|
|
1333
|
+
},
|
|
1334
|
+
});
|
|
1335
|
+
|
|
1538
1336
|
/* eslint-disable max-statements */
|
|
1539
1337
|
function InputFile({ variation = "dropzone", size = "base", name, buttonLabel: providedButtonLabel, allowMultiple = false, allowedTypes = "all", description, hintText, maxFilesValidation, getUploadParams, onUploadStart, onUploadProgress, onUploadComplete, onUploadError, validator, children, }) {
|
|
1338
|
+
const { t, plural } = useAtlantisI18n(inputFileMessages);
|
|
1540
1339
|
const maxFiles = (maxFilesValidation === null || maxFilesValidation === void 0 ? void 0 : maxFilesValidation.maxFiles) || 0;
|
|
1541
1340
|
const numberOfCurrentFiles = (maxFilesValidation === null || maxFilesValidation === void 0 ? void 0 : maxFilesValidation.numberOfCurrentFiles) || 0;
|
|
1542
|
-
const
|
|
1543
|
-
if (maxFiles &&
|
|
1544
|
-
numberOfCurrentFiles &&
|
|
1545
|
-
maxFiles - numberOfCurrentFiles <= 0) {
|
|
1546
|
-
return {
|
|
1547
|
-
code: "too-many-files",
|
|
1548
|
-
message: `Cannot exceed a maximum of ${maxFiles} files.`,
|
|
1549
|
-
};
|
|
1550
|
-
}
|
|
1551
|
-
return validator ? validator(file) : null;
|
|
1552
|
-
}, [maxFiles, numberOfCurrentFiles, validator]);
|
|
1341
|
+
const [limitError, setLimitError] = useState();
|
|
1553
1342
|
const options = {
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
onDrop:
|
|
1557
|
-
validator
|
|
1343
|
+
// InputFile applies count limits after per-file validation, including single-file drops.
|
|
1344
|
+
multiple: true,
|
|
1345
|
+
onDrop: handleDrop,
|
|
1346
|
+
validator,
|
|
1558
1347
|
};
|
|
1559
1348
|
if (allowedTypes === "images") {
|
|
1560
|
-
options.accept = "image/*";
|
|
1349
|
+
options.accept = { "image/*": [] };
|
|
1561
1350
|
}
|
|
1562
1351
|
else if (allowedTypes === "basicImages") {
|
|
1563
|
-
options.accept = convertToMimeTypes(BASIC_IMAGE_TYPES)
|
|
1352
|
+
options.accept = mimeTypesToAccept(convertToMimeTypes(BASIC_IMAGE_TYPES));
|
|
1564
1353
|
}
|
|
1565
1354
|
else if (Array.isArray(allowedTypes)) {
|
|
1566
|
-
options.accept = convertToMimeTypes(allowedTypes)
|
|
1355
|
+
options.accept = mimeTypesToAccept(convertToMimeTypes(allowedTypes));
|
|
1567
1356
|
}
|
|
1568
1357
|
const { getRootProps, getInputProps, isDragActive, fileRejections } = useDropzone(options);
|
|
1569
1358
|
const allowedTypesString = Array.isArray(allowedTypes)
|
|
@@ -1571,15 +1360,7 @@ function InputFile({ variation = "dropzone", size = "base", name, buttonLabel: p
|
|
|
1571
1360
|
: allowedTypes;
|
|
1572
1361
|
const validationErrors = fileRejections === null || fileRejections === void 0 ? void 0 : fileRejections.reduce((acc, { file, errors }) => {
|
|
1573
1362
|
errors.forEach(error => {
|
|
1574
|
-
if (error.code === "
|
|
1575
|
-
if (!acc.some(e => e.code === "too-many-files")) {
|
|
1576
|
-
acc.push({
|
|
1577
|
-
code: "too-many-files",
|
|
1578
|
-
message: `Cannot exceed a maximum of ${maxFiles} files.`,
|
|
1579
|
-
});
|
|
1580
|
-
}
|
|
1581
|
-
}
|
|
1582
|
-
else if (error.code === "file-invalid-type") {
|
|
1363
|
+
if (error.code === "file-invalid-type") {
|
|
1583
1364
|
let formatMessage;
|
|
1584
1365
|
if (allowedTypes === "basicImages") {
|
|
1585
1366
|
formatMessage = formatMimeTypes(BASIC_IMAGE_TYPES.map(mimeTypeToReadable));
|
|
@@ -1607,10 +1388,12 @@ function InputFile({ variation = "dropzone", size = "base", name, buttonLabel: p
|
|
|
1607
1388
|
});
|
|
1608
1389
|
return acc;
|
|
1609
1390
|
}, []);
|
|
1391
|
+
if (limitError)
|
|
1392
|
+
validationErrors.push(limitError);
|
|
1610
1393
|
const dropZone = classnames(styles.dropZoneBase, {
|
|
1611
1394
|
[styles.dropZone]: variation === "dropzone",
|
|
1612
1395
|
[styles.active]: isDragActive,
|
|
1613
|
-
[styles.error]:
|
|
1396
|
+
[styles.error]: validationErrors.length > 0,
|
|
1614
1397
|
});
|
|
1615
1398
|
const fileType = allowedTypes === "images" || allowedTypes === "basicImages"
|
|
1616
1399
|
? "Image"
|
|
@@ -1632,11 +1415,27 @@ function InputFile({ variation = "dropzone", size = "base", name, buttonLabel: p
|
|
|
1632
1415
|
React__default.createElement(InputFile.Description, null)))))),
|
|
1633
1416
|
variation === "button" && React__default.createElement(InputFile.Button, { fullWidth: true })));
|
|
1634
1417
|
return (React__default.createElement(React__default.Fragment, null,
|
|
1635
|
-
React__default.createElement("div", Object.assign({}, getRootProps({ className: dropZone }), { tabIndex: variation === "button" ? -1 : 0 }),
|
|
1636
|
-
React__default.createElement("input", Object.assign({}, getInputProps(
|
|
1418
|
+
React__default.createElement("div", Object.assign({}, getRootProps({ className: dropZone, role: "button" }), { tabIndex: variation === "button" ? -1 : 0 }),
|
|
1419
|
+
React__default.createElement("input", Object.assign({}, getInputProps({
|
|
1420
|
+
multiple: allowMultiple,
|
|
1421
|
+
"aria-label": providedButtonLabel || t("uploadFile"),
|
|
1422
|
+
}), { "data-testid": "input-file-input", name: name })),
|
|
1637
1423
|
React__default.createElement(InputFileContentContext.Provider, { value: contentContext }, children || defaultContent)),
|
|
1638
1424
|
React__default.createElement(InputFileValidationErrors, { validationErrors: validationErrors })));
|
|
1639
1425
|
function handleDrop(acceptedFiles) {
|
|
1426
|
+
const remainingCapacity = maxFiles > 0 ? Math.max(0, maxFiles - numberOfCurrentFiles) : Infinity;
|
|
1427
|
+
const capacity = allowMultiple
|
|
1428
|
+
? remainingCapacity
|
|
1429
|
+
: Math.min(1, remainingCapacity);
|
|
1430
|
+
if (acceptedFiles.length > capacity) {
|
|
1431
|
+
const maximum = allowMultiple || remainingCapacity === 0 ? maxFiles : 1;
|
|
1432
|
+
setLimitError({
|
|
1433
|
+
code: "too-many-files",
|
|
1434
|
+
message: plural("fileLimit", maximum),
|
|
1435
|
+
});
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
setLimitError(undefined);
|
|
1640
1439
|
acceptedFiles.forEach(file => {
|
|
1641
1440
|
uploadFile(file);
|
|
1642
1441
|
});
|
|
@@ -1681,6 +1480,9 @@ function InputFile({ variation = "dropzone", size = "base", name, buttonLabel: p
|
|
|
1681
1480
|
});
|
|
1682
1481
|
}
|
|
1683
1482
|
}
|
|
1483
|
+
function mimeTypesToAccept(mimeTypes) {
|
|
1484
|
+
return Object.fromEntries(mimeTypes.map(mimeType => [mimeType, []]));
|
|
1485
|
+
}
|
|
1684
1486
|
function createAxiosConfig({ url, httpMethod = "POST", fields = {}, file, handleUploadProgress, }) {
|
|
1685
1487
|
let data;
|
|
1686
1488
|
let headers;
|