| } | } | ||||
| /** | |||||
| * Use dataTransfer.items when available instead of dataTransfer.files (when files are dropped) | |||||
| * | |||||
| * Set to false to use dataTransfer.files only. | |||||
| * This is useful for environments where files cannot be read from FileSystemEntry like in figma plugins/widgets. | |||||
| */ | |||||
| static USE_DATA_TRANSFER_ITEMS = true | |||||
| /** | /** | ||||
| * References (and horror): | * References (and horror): | ||||
| * - https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/items | * - https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/items | ||||
| } | } | ||||
| // Prefer .items, which allow folder traversal if necessary. | // Prefer .items, which allow folder traversal if necessary. | ||||
| if (items.length > 0) { | |||||
| if (Dropzone.USE_DATA_TRANSFER_ITEMS && items.length > 0) { | |||||
| const entries = items.map((item) => item.webkitGetAsEntry()) | const entries = items.map((item) => item.webkitGetAsEntry()) | ||||
| // if (entries[0].name.match(/\.zip$/)) { | // if (entries[0].name.match(/\.zip$/)) { | ||||
| * @param {Array<FileSystemEntry>} entries | * @param {Array<FileSystemEntry>} entries | ||||
| * @param e | * @param e | ||||
| */ | */ | ||||
| private _loadNextEntry(fileMap: Map<string, DropFile>, entries: any[], e: DragEvent) { | |||||
| private _loadNextEntry(fileMap: Map<string, DropFile>, entries: (FileSystemEntry|null)[], e: DragEvent) { | |||||
| const entry = entries.pop() | const entry = entries.pop() | ||||
| if (!entry) { | if (!entry) { | ||||
| } | } | ||||
| if (entry.isFile) { | if (entry.isFile) { | ||||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-call | |||||
| entry.file((file: DropFile) => { | |||||
| (entry as FileSystemFileEntry).file((file: DropFile) => { | |||||
| file.filePath = entry.fullPath | file.filePath = entry.fullPath | ||||
| fileMap.set(entry.fullPath, file) | fileMap.set(entry.fullPath, file) | ||||
| this._loadNextEntry(fileMap, entries, e) | this._loadNextEntry(fileMap, entries, e) | ||||
| }, () => console.error('Could not load file: %s', entry.fullPath)) | |||||
| }, (err) => console.error('Could not load file: %s', entry.fullPath, err, 'Try setting Dropzone.USE_DATA_TRANSFER_ITEMS to false.')) | |||||
| } else if (entry.isDirectory) { | } else if (entry.isDirectory) { | ||||
| // readEntries() must be called repeatedly until it stops returning results. | // readEntries() must be called repeatedly until it stops returning results. | ||||
| // https://www.w3.org/TR/2012/WD-file-system-api-20120417/#the-directoryreader-interface | // https://www.w3.org/TR/2012/WD-file-system-api-20120417/#the-directoryreader-interface | ||||
| // https://bugs.chromium.org/p/chromium/issues/detail?id=378883 | // https://bugs.chromium.org/p/chromium/issues/detail?id=378883 | ||||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-call | |||||
| const reader = entry.createReader() | |||||
| const reader = (entry as FileSystemDirectoryEntry).createReader() | |||||
| const readerCallback = (newEntries: any[]) => { | const readerCallback = (newEntries: any[]) => { | ||||
| if (newEntries.length) { | if (newEntries.length) { | ||||
| entries = entries.concat(newEntries) | entries = entries.concat(newEntries) |