From dc52c9285b353bdc31012e22cb55a39812495937 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Sat, 9 Nov 2024 13:13:32 +0100 Subject: [PATCH] chore!: drop support for Internet Explorer Signed-off-by: Jon Koops BREAKING CHANGE: drops support for Internet Explorer --- src/file-selector.spec.ts | 38 ++------------------------ src/file-selector.ts | 57 ++++++++++++--------------------------- 2 files changed, 19 insertions(+), 76 deletions(-) diff --git a/src/file-selector.spec.ts b/src/file-selector.spec.ts index 82c67d9..f67e87f 100644 --- a/src/file-selector.spec.ts +++ b/src/file-selector.spec.ts @@ -75,30 +75,6 @@ it("should return an empty array if the passed event is not a DragEvent", async expect(files).toHaveLength(0); }); -it("should return {files} from DataTransfer if {items} is not defined (e.g. IE11)", async () => { - const name = "test.json"; - const mockFile = createFile( - name, - { ping: true }, - { - type: "application/json", - }, - ); - const evt = dragEvt([mockFile]); - - const files = await fromEvent(evt); - expect(files).toHaveLength(1); - expect(files.every((file) => file instanceof File)).toBe(true); - - const [file] = files as FileWithPath[]; - - expect(file.name).toBe(mockFile.name); - expect(file.type).toBe(mockFile.type); - expect(file.size).toBe(mockFile.size); - expect(file.lastModified).toBe(mockFile.lastModified); - expect(file.path).toBe(`./${name}`); -}); - it("should return files from DataTransfer {items} if the passed event is a DragEvent", async () => { const name = "test.json"; const mockFile = createFile( @@ -269,7 +245,8 @@ it("filters thumbnail cache files", async () => { type: "text/plain", }, ); - const evt = dragEvt([mockFile]); + const item = dataTransferItemFromFile(mockFile); + const evt = dragEvtFromFilesAndItems([], [item]); const items = await fromEvent(evt); expect(items).toHaveLength(0); }); @@ -363,17 +340,6 @@ function dragEvtFromItems( } as any; } -function dragEvt( - files?: File[], - items?: DataTransferItem[], - type: string = "drop", -): DragEvent { - return { - type, - dataTransfer: { items, files }, - } as any; -} - function dragEvtFromFilesAndItems( files: File[], items: DataTransferItem[], diff --git a/src/file-selector.ts b/src/file-selector.ts index ba0228f..58b66c5 100644 --- a/src/file-selector.ts +++ b/src/file-selector.ts @@ -44,10 +44,12 @@ function isObject(v: any): v is T { return typeof v === "object" && v !== null; } -function getInputFiles(evt: Event) { - return fromList((evt.target as HTMLInputElement).files).map( - (file) => toFileWithPath(file), - ); +function getInputFiles(event: Event): FileWithPath[] { + if (!(event.target instanceof HTMLInputElement) || !event.target.files) { + return []; + } + + return Array.from(event.target.files).map((file) => toFileWithPath(file)); } // Ee expect each handle to be https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle @@ -56,50 +58,25 @@ async function getFsHandleFiles(handles: any[]) { return files.map((file) => toFileWithPath(file)); } -async function getDataTransferFiles(dt: DataTransfer, type: string) { - // IE11 does not support dataTransfer.items - // See https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/items#Browser_compatibility - if (dt.items) { - const items = fromList(dt.items).filter( - (item) => item.kind === "file", - ); - // According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents, - // only 'dragstart' and 'drop' has access to the data (source node) - if (type !== "drop") { - return items; - } - const files = await Promise.all(items.map(toFilePromises)); - return noIgnoredFiles(flatten(files)); +async function getDataTransferFiles(dataTransfer: DataTransfer, type: string) { + const items = Array.from(dataTransfer.items).filter( + (item) => item.kind === "file", + ); + + // According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents, + // only 'dragstart' and 'drop' has access to the data (source node) + if (type !== "drop") { + return items; } - return noIgnoredFiles( - fromList(dt.files).map((file) => toFileWithPath(file)), - ); + const files = await Promise.all(items.map(toFilePromises)); + return noIgnoredFiles(flatten(files)); } function noIgnoredFiles(files: FileWithPath[]) { return files.filter((file) => FILES_TO_IGNORE.indexOf(file.name) === -1); } -// IE11 does not support Array.from() -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Browser_compatibility -// https://developer.mozilla.org/en-US/docs/Web/API/FileList -// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItemList -function fromList(items: DataTransferItemList | FileList | null): T[] { - if (items === null) { - return []; - } - - const files = []; - - for (let i = 0; i < items.length; i++) { - const file = items[i]; - files.push(file); - } - - return files as any; -} - // https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem function toFilePromises(item: DataTransferItem) { if (typeof item.webkitGetAsEntry !== "function") {