Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: drop support for Internet Explorer #109

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 2 additions & 36 deletions src/file-selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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[],
Expand Down
57 changes: 17 additions & 40 deletions src/file-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ function isObject<T>(v: any): v is T {
return typeof v === "object" && v !== null;
}

function getInputFiles(evt: Event) {
return fromList<FileWithPath>((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
Expand All @@ -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<DataTransferItem>(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<FileWithPath>(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<FileWithPath>(dt.files).map((file) => toFileWithPath(file)),
);
const files = await Promise.all(items.map(toFilePromises));
return noIgnoredFiles(flatten<FileWithPath>(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<T>(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") {
Expand Down