Pārlūkot izejas kodu

Support custom domElement and nativeEvent in DropzonePlugin

master
Palash Bansal pirms 3 gadiem
vecāks
revīzija
f7bac26a05
Revīzijas autora e-pasta adrese nav piesaistīta nevienam kontam
2 mainītis faili ar 20 papildinājumiem un 14 dzēšanām
  1. 4
    3
      src/plugins/interaction/DropzonePlugin.ts
  2. 16
    11
      src/utils/Dropzone.ts

+ 4
- 3
src/plugins/interaction/DropzonePlugin.ts Parādīt failu

super.onAdded(viewer) super.onAdded(viewer)
this._inputEl = document.createElement('input')! this._inputEl = document.createElement('input')!
this._inputEl.type = 'file' this._inputEl.type = 'file'
this._dropzone = new Dropzone(this._domElement || viewer.canvas, this._inputEl, {
if (!this._domElement) this._domElement = viewer.canvas
this._dropzone = new Dropzone(this._domElement, this._inputEl, {
drop: this._onFileDrop.bind(this), drop: this._onFileDrop.bind(this),
}) })
this.allowedExtensions = this._allowedExtensions this.allowedExtensions = this._allowedExtensions
this._inputEl = undefined this._inputEl = undefined
} }


private async _onFileDrop({files}: {files: Map<string, File>}&any) {
private async _onFileDrop({files, nativeEvent}: {files: Map<string, File>, nativeEvent: DragEvent}) {
if (!files) return if (!files) return
if (!this.enabled) return if (!this.enabled) return
const viewer = this._viewer const viewer = this._viewer
assets = await manager.loadImported(toAdd, {...this.addOptions}) assets = await manager.loadImported(toAdd, {...this.addOptions})
} }
} }
this.dispatchEvent({type: 'drop', files, imported, assets})
this.dispatchEvent({type: 'drop', files, imported, assets, nativeEvent})
} }


} }

+ 16
- 11
src/utils/Dropzone.ts Parādīt failu

// if (entries[0].name.match(/\.zip$/)) { // if (entries[0].name.match(/\.zip$/)) {
// this._loadZip(items[0].getAsFile()) // this._loadZip(items[0].getAsFile())
// } else { // } else {
this._loadNextEntry(new Map(), entries)
this._loadNextEntry(new Map(), entries, e)
// } // }


return return
// if (files.length === 1 && files[0].name.match(/\.zip$/)) { // if (files.length === 1 && files[0].name.match(/\.zip$/)) {
// this._loadZip(files[0]) // this._loadZip(files[0])
// } // }
this._emit('drop', {files: new Map(files.map((file) => {
file.filePath = file.name
return [file.filePath, file]
}))})
this._emit('drop', {
nativeEvent: e,
files: new Map(files.map((file) => {
file.filePath = file.name
return [file.filePath, file]
})),
})
} }


/** /**
file.filePath = (file as any).webkitRelativePath || file.name file.filePath = (file as any).webkitRelativePath || file.name
fileMap.set(file.filePath, file) fileMap.set(file.filePath, file)
}) })
this._emit('drop', {files: fileMap})
this._emit('drop', {files: fileMap, nativeEvent: e})
} }


/** /**
* Iterates through a list of FileSystemEntry objects, creates the fileMap * Iterates through a list of FileSystemEntry objects, creates the fileMap
* tree, and emits the result. * tree, and emits the result.
* @param fileMap
* @param {Array<FileSystemEntry>} entries * @param {Array<FileSystemEntry>} entries
* @param e
*/ */
private _loadNextEntry(fileMap: Map<string, DropFile>, entries: any[]) {
private _loadNextEntry(fileMap: Map<string, DropFile>, entries: any[], e: DragEvent) {
const entry = entries.pop() const entry = entries.pop()


if (!entry) { if (!entry) {
this._emit('drop', {files: fileMap})
this._emit('drop', {files: fileMap, nativeEvent: e})
return return
} }


entry.file((file: DropFile) => { entry.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)
this._loadNextEntry(fileMap, entries, e)
}, () => console.error('Could not load file: %s', entry.fullPath)) }, () => console.error('Could not load file: %s', entry.fullPath))
} 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.
// eslint-disable-next-line @typescript-eslint/no-unsafe-call // eslint-disable-next-line @typescript-eslint/no-unsafe-call
reader.readEntries(readerCallback) reader.readEntries(readerCallback)
} else { } else {
this._loadNextEntry(fileMap, entries)
this._loadNextEntry(fileMap, entries, e)
} }
} }
// eslint-disable-next-line @typescript-eslint/no-unsafe-call // eslint-disable-next-line @typescript-eslint/no-unsafe-call
reader.readEntries(readerCallback) reader.readEntries(readerCallback)
} else { } else {
console.warn('Unknown asset type: ' + entry.fullPath) console.warn('Unknown asset type: ' + entry.fullPath)
this._loadNextEntry(fileMap, entries)
this._loadNextEntry(fileMap, entries, e)
} }
} }



Notiek ielāde…
Atcelt
Saglabāt