@georapbox/files-dropzone-element 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -7
- package/dist/files-dropzone-defined.d.ts +3 -0
- package/dist/files-dropzone-defined.d.ts.map +1 -0
- package/dist/files-dropzone-defined.js +83 -1
- package/dist/files-dropzone-defined.js.map +1 -1
- package/dist/files-dropzone.d.ts +177 -0
- package/dist/files-dropzone.d.ts.map +1 -0
- package/dist/files-dropzone.js +83 -1
- package/dist/files-dropzone.js.map +1 -1
- package/dist/utils/files-selector.d.ts +2 -0
- package/dist/utils/files-selector.d.ts.map +1 -0
- package/dist/utils/is-valid-file.d.ts +9 -0
- package/dist/utils/is-valid-file.d.ts.map +1 -0
- package/package.json +13 -10
- package/src/files-dropzone.js +329 -137
- package/src/utils/files-selector.js +26 -18
- package/src/utils/is-valid-file.js +5 -3
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A map of common file extensions and their associated MIME types.
|
|
5
|
+
*/
|
|
1
6
|
const COMMON_MIME_TYPES = new Map([
|
|
2
7
|
// https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
|
3
8
|
['aac', 'audio/aac'],
|
|
@@ -29,6 +34,7 @@ const COMMON_MIME_TYPES = new Map([
|
|
|
29
34
|
['jar', 'application/java-archive'],
|
|
30
35
|
['jpeg', 'image/jpeg'],
|
|
31
36
|
['jpg', 'image/jpeg'],
|
|
37
|
+
['jxl', 'image/jxl'],
|
|
32
38
|
['js', 'text/javascript'],
|
|
33
39
|
['json', 'application/json'],
|
|
34
40
|
['jsonld', 'application/ld+json'],
|
|
@@ -95,15 +101,15 @@ const FILES_TO_IGNORE = [
|
|
|
95
101
|
* This is needed because Firefox doesn't add a type property to files dragged from the desktop.
|
|
96
102
|
* @bug https://bugzilla.mozilla.org/show_bug.cgi?id=1424689
|
|
97
103
|
*
|
|
98
|
-
* @param {File} file The file object to add the type property to.
|
|
99
|
-
* @returns {File} The file object with the type property added.
|
|
104
|
+
* @param {File} file - The file object to add the type property to.
|
|
105
|
+
* @returns {File} - The file object with the type property added.
|
|
100
106
|
*/
|
|
101
107
|
const toFileWithMimeType = file => {
|
|
102
108
|
const { name } = file;
|
|
103
109
|
const hasExtension = name && name.lastIndexOf('.') !== -1;
|
|
104
110
|
|
|
105
111
|
if (hasExtension && !file.type) {
|
|
106
|
-
const extension = name.split('.').pop().toLowerCase();
|
|
112
|
+
const extension = (name.split('.').pop() || '').toLowerCase();
|
|
107
113
|
const type = COMMON_MIME_TYPES.get(extension);
|
|
108
114
|
|
|
109
115
|
if (type) {
|
|
@@ -124,13 +130,14 @@ const toFileWithMimeType = file => {
|
|
|
124
130
|
* If `path` is not provided, the `webkitRelativePath` property of the file will be used
|
|
125
131
|
* or the file's name if `webkitRelativePath` is not available.
|
|
126
132
|
*
|
|
127
|
-
* @param {File} file The file object to add the path property to.
|
|
128
|
-
* @param {
|
|
129
|
-
* @returns {File} The file object with the path property added.
|
|
133
|
+
* @param {File} file - The file object to add the path property to.
|
|
134
|
+
* @param {string} [path] - The path to set on the file object.
|
|
135
|
+
* @returns {File} - The file object with the path property added.
|
|
130
136
|
*/
|
|
131
137
|
const toFileWithPath = (file, path) => {
|
|
132
138
|
const fileWithMimeType = toFileWithMimeType(file);
|
|
133
139
|
|
|
140
|
+
// @ts-ignore
|
|
134
141
|
if (typeof fileWithMimeType.path !== 'string') {
|
|
135
142
|
const { webkitRelativePath } = file;
|
|
136
143
|
|
|
@@ -149,8 +156,8 @@ const toFileWithPath = (file, path) => {
|
|
|
149
156
|
* Wrap `FileSystemDirectoryReader.readEntries` in a promise to make working with read entries easier.
|
|
150
157
|
* https://developer.mozilla.org/docs/Web/API/FileSystemDirectoryReader/readEntries
|
|
151
158
|
*
|
|
152
|
-
* @param {FileSystemDirectoryReader} directoryReader The directory reader to read entries from.
|
|
153
|
-
* @returns {Promise<
|
|
159
|
+
* @param {FileSystemDirectoryReader} directoryReader - The directory reader to read entries from.
|
|
160
|
+
* @returns {Promise<FileSystemEntry[]>} - A promise that resolves with an array of `FileSystemEntry` objects.
|
|
154
161
|
*/
|
|
155
162
|
const readEntriesPromise = async directoryReader => {
|
|
156
163
|
return await new Promise((resolve, reject) => {
|
|
@@ -162,8 +169,8 @@ const readEntriesPromise = async directoryReader => {
|
|
|
162
169
|
* Read all entries in a directory or sub-directory
|
|
163
170
|
* by calling `readEntries` until it returns an empty array.
|
|
164
171
|
*
|
|
165
|
-
* @param {FileSystemDirectoryReader} directoryReader The directory reader to read entries from.
|
|
166
|
-
* @returns {Promise<
|
|
172
|
+
* @param {FileSystemDirectoryReader} directoryReader - The directory reader to read entries from.
|
|
173
|
+
* @returns {Promise<FileSystemEntry[]>} - A promise that resolves with an array of `FileSystemEntry` objects.
|
|
167
174
|
*/
|
|
168
175
|
const readAllDirectoryEntries = async directoryReader => {
|
|
169
176
|
const entries = [];
|
|
@@ -180,8 +187,8 @@ const readAllDirectoryEntries = async directoryReader => {
|
|
|
180
187
|
/**
|
|
181
188
|
* Get a `File` object from a `FileSystemFileEntry` object.
|
|
182
189
|
*
|
|
183
|
-
* @param {FileSystemFileEntry} fileEntry The file entry to get a `File` object from.
|
|
184
|
-
* @returns {Promise<File>} A promise that resolves with a `File` object.
|
|
190
|
+
* @param {FileSystemFileEntry} fileEntry - The file entry to get a `File` object from.
|
|
191
|
+
* @returns {Promise<File>} - A promise that resolves with a `File` object.
|
|
185
192
|
*/
|
|
186
193
|
const getFileFromFileEntry = fileEntry => {
|
|
187
194
|
return new Promise((resolve, reject) => {
|
|
@@ -192,8 +199,8 @@ const getFileFromFileEntry = fileEntry => {
|
|
|
192
199
|
/**
|
|
193
200
|
* Get an array of `File` objects from a `DataTransferItemList` object.
|
|
194
201
|
*
|
|
195
|
-
* @param {DataTransferItemList} dataTransferItemList The item list to get an array of `File` objects from.
|
|
196
|
-
* @returns {Promise<File[]>} A promise that resolves with an array of `File` objects.
|
|
202
|
+
* @param {DataTransferItemList} dataTransferItemList - The item list to get an array of `File` objects from.
|
|
203
|
+
* @returns {Promise<File[]>} - A promise that resolves with an array of `File` objects.
|
|
197
204
|
*/
|
|
198
205
|
const getFilesFromDataTransferItemList = async dataTransferItemList => {
|
|
199
206
|
const files = [];
|
|
@@ -212,6 +219,7 @@ const getFilesFromDataTransferItemList = async dataTransferItemList => {
|
|
|
212
219
|
// https://developer.mozilla.org/docs/Web/API/DataTransferItem/webkitGetAsEntry
|
|
213
220
|
// This function is implemented as `webkitGetAsEntry()` in non-WebKit browsers
|
|
214
221
|
// including Firefox at this time but it may be renamed to `getAsEntry()` in the future.
|
|
222
|
+
// @ts-ignore
|
|
215
223
|
const entry = item.getAsEntry ? item.getAsEntry() : item.webkitGetAsEntry();
|
|
216
224
|
|
|
217
225
|
queue.push(entry);
|
|
@@ -239,8 +247,8 @@ const getFilesFromDataTransferItemList = async dataTransferItemList => {
|
|
|
239
247
|
/**
|
|
240
248
|
* Get an array of `File` objects from a `FileList` object.
|
|
241
249
|
*
|
|
242
|
-
* @param {FileList} fileList The file list to get an array of `File` objects from.
|
|
243
|
-
* @returns {Promise<File[]>} A promise that resolves with an array of `File` objects.
|
|
250
|
+
* @param {FileList} fileList - The file list to get an array of `File` objects from.
|
|
251
|
+
* @returns {Promise<File[]>} - A promise that resolves with an array of `File` objects.
|
|
244
252
|
*/
|
|
245
253
|
const getFilesFromFileList = async fileList => {
|
|
246
254
|
const files = [];
|
|
@@ -258,8 +266,8 @@ const getFilesFromFileList = async fileList => {
|
|
|
258
266
|
* Get an array of `File` objects from an event.
|
|
259
267
|
* This function supports both `drop` and `change` events.
|
|
260
268
|
*
|
|
261
|
-
* @param {
|
|
262
|
-
* @returns {Promise<File[]>} A promise that resolves with an array of `File` objects.
|
|
269
|
+
* @param {*} evt - The event to get an array of `File` objects from.
|
|
270
|
+
* @returns {Promise<File[]>} - A promise that resolves with an array of `File` objects.
|
|
263
271
|
*/
|
|
264
272
|
export const getFilesFromEvent = async evt => {
|
|
265
273
|
if (evt.dataTransfer) {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Checks if a file is valid based on the accepted file type specifiers.
|
|
3
5
|
*
|
|
4
|
-
* @param {File} file The File object to validate.
|
|
5
|
-
* @param {
|
|
6
|
-
* @returns {
|
|
6
|
+
* @param {File} file - The File object to validate.
|
|
7
|
+
* @param {string} [acceptedTypeSpecifiers=''] - The accepted file type specifiers.
|
|
8
|
+
* @returns {boolean} - True if the file is valid, false otherwise.
|
|
7
9
|
*/
|
|
8
10
|
export function isValidFile(file, acceptedTypeSpecifiers = '') {
|
|
9
11
|
if (!acceptedTypeSpecifiers) {
|