@georapbox/files-dropzone-element 1.1.1 → 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 +67 -52
- 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 +67 -52
- 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 +25 -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'],
|
|
@@ -96,15 +101,15 @@ const FILES_TO_IGNORE = [
|
|
|
96
101
|
* This is needed because Firefox doesn't add a type property to files dragged from the desktop.
|
|
97
102
|
* @bug https://bugzilla.mozilla.org/show_bug.cgi?id=1424689
|
|
98
103
|
*
|
|
99
|
-
* @param {File} file The file object to add the type property to.
|
|
100
|
-
* @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.
|
|
101
106
|
*/
|
|
102
107
|
const toFileWithMimeType = file => {
|
|
103
108
|
const { name } = file;
|
|
104
109
|
const hasExtension = name && name.lastIndexOf('.') !== -1;
|
|
105
110
|
|
|
106
111
|
if (hasExtension && !file.type) {
|
|
107
|
-
const extension = name.split('.').pop().toLowerCase();
|
|
112
|
+
const extension = (name.split('.').pop() || '').toLowerCase();
|
|
108
113
|
const type = COMMON_MIME_TYPES.get(extension);
|
|
109
114
|
|
|
110
115
|
if (type) {
|
|
@@ -125,13 +130,14 @@ const toFileWithMimeType = file => {
|
|
|
125
130
|
* If `path` is not provided, the `webkitRelativePath` property of the file will be used
|
|
126
131
|
* or the file's name if `webkitRelativePath` is not available.
|
|
127
132
|
*
|
|
128
|
-
* @param {File} file The file object to add the path property to.
|
|
129
|
-
* @param {
|
|
130
|
-
* @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.
|
|
131
136
|
*/
|
|
132
137
|
const toFileWithPath = (file, path) => {
|
|
133
138
|
const fileWithMimeType = toFileWithMimeType(file);
|
|
134
139
|
|
|
140
|
+
// @ts-ignore
|
|
135
141
|
if (typeof fileWithMimeType.path !== 'string') {
|
|
136
142
|
const { webkitRelativePath } = file;
|
|
137
143
|
|
|
@@ -150,8 +156,8 @@ const toFileWithPath = (file, path) => {
|
|
|
150
156
|
* Wrap `FileSystemDirectoryReader.readEntries` in a promise to make working with read entries easier.
|
|
151
157
|
* https://developer.mozilla.org/docs/Web/API/FileSystemDirectoryReader/readEntries
|
|
152
158
|
*
|
|
153
|
-
* @param {FileSystemDirectoryReader} directoryReader The directory reader to read entries from.
|
|
154
|
-
* @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.
|
|
155
161
|
*/
|
|
156
162
|
const readEntriesPromise = async directoryReader => {
|
|
157
163
|
return await new Promise((resolve, reject) => {
|
|
@@ -163,8 +169,8 @@ const readEntriesPromise = async directoryReader => {
|
|
|
163
169
|
* Read all entries in a directory or sub-directory
|
|
164
170
|
* by calling `readEntries` until it returns an empty array.
|
|
165
171
|
*
|
|
166
|
-
* @param {FileSystemDirectoryReader} directoryReader The directory reader to read entries from.
|
|
167
|
-
* @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.
|
|
168
174
|
*/
|
|
169
175
|
const readAllDirectoryEntries = async directoryReader => {
|
|
170
176
|
const entries = [];
|
|
@@ -181,8 +187,8 @@ const readAllDirectoryEntries = async directoryReader => {
|
|
|
181
187
|
/**
|
|
182
188
|
* Get a `File` object from a `FileSystemFileEntry` object.
|
|
183
189
|
*
|
|
184
|
-
* @param {FileSystemFileEntry} fileEntry The file entry to get a `File` object from.
|
|
185
|
-
* @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.
|
|
186
192
|
*/
|
|
187
193
|
const getFileFromFileEntry = fileEntry => {
|
|
188
194
|
return new Promise((resolve, reject) => {
|
|
@@ -193,8 +199,8 @@ const getFileFromFileEntry = fileEntry => {
|
|
|
193
199
|
/**
|
|
194
200
|
* Get an array of `File` objects from a `DataTransferItemList` object.
|
|
195
201
|
*
|
|
196
|
-
* @param {DataTransferItemList} dataTransferItemList The item list to get an array of `File` objects from.
|
|
197
|
-
* @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.
|
|
198
204
|
*/
|
|
199
205
|
const getFilesFromDataTransferItemList = async dataTransferItemList => {
|
|
200
206
|
const files = [];
|
|
@@ -213,6 +219,7 @@ const getFilesFromDataTransferItemList = async dataTransferItemList => {
|
|
|
213
219
|
// https://developer.mozilla.org/docs/Web/API/DataTransferItem/webkitGetAsEntry
|
|
214
220
|
// This function is implemented as `webkitGetAsEntry()` in non-WebKit browsers
|
|
215
221
|
// including Firefox at this time but it may be renamed to `getAsEntry()` in the future.
|
|
222
|
+
// @ts-ignore
|
|
216
223
|
const entry = item.getAsEntry ? item.getAsEntry() : item.webkitGetAsEntry();
|
|
217
224
|
|
|
218
225
|
queue.push(entry);
|
|
@@ -240,8 +247,8 @@ const getFilesFromDataTransferItemList = async dataTransferItemList => {
|
|
|
240
247
|
/**
|
|
241
248
|
* Get an array of `File` objects from a `FileList` object.
|
|
242
249
|
*
|
|
243
|
-
* @param {FileList} fileList The file list to get an array of `File` objects from.
|
|
244
|
-
* @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.
|
|
245
252
|
*/
|
|
246
253
|
const getFilesFromFileList = async fileList => {
|
|
247
254
|
const files = [];
|
|
@@ -259,8 +266,8 @@ const getFilesFromFileList = async fileList => {
|
|
|
259
266
|
* Get an array of `File` objects from an event.
|
|
260
267
|
* This function supports both `drop` and `change` events.
|
|
261
268
|
*
|
|
262
|
-
* @param {
|
|
263
|
-
* @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.
|
|
264
271
|
*/
|
|
265
272
|
export const getFilesFromEvent = async evt => {
|
|
266
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) {
|