Fixing file drop for Safari & Edge (#73)

This commit is contained in:
Jake Archibald
2018-07-02 18:55:46 +01:00
committed by GitHub
parent 68729979e3
commit 3e26a0a3cc

View File

@@ -28,10 +28,18 @@ interface FileDropEventInit extends EventInit {
file: File; file: File;
} }
// Safari and Edge don't quite support extending Event, this works around it.
function fixExtendedEvent(instance: Event, type: Function) {
if (!(instance instanceof type)) {
Object.setPrototypeOf(instance, type.prototype);
}
}
export class FileDropEvent extends Event { export class FileDropEvent extends Event {
private _file: File; private _file: File;
constructor(typeArg: string, eventInitDict: FileDropEventInit) { constructor(typeArg: string, eventInitDict: FileDropEventInit) {
super(typeArg, eventInitDict); super(typeArg, eventInitDict);
fixExtendedEvent(this, FileDropEvent);
this._file = eventInitDict.file; this._file = eventInitDict.file;
} }
@@ -78,8 +86,12 @@ export class FileDrop extends HTMLElement {
if (this._dragEnterCount > 1) return; if (this._dragEnterCount > 1) return;
// We don't have data, attempt to get it and if it matches, set the correct state. // We don't have data, attempt to get it and if it matches, set the correct state.
const dragDataItem = firstMatchingItem(event.dataTransfer.items, this.accept); const validDrop: boolean = event.dataTransfer.items.length ?
if (dragDataItem) { !!firstMatchingItem(event.dataTransfer.items, this.accept) :
// Safari doesn't give file information on drag enter, so the best we can do is return valid.
true;
if (validDrop) {
this.classList.add('drop-valid'); this.classList.add('drop-valid');
} else { } else {
this.classList.add('drop-invalid'); this.classList.add('drop-invalid');