From 4d5f00ff08cd5696402305c6ed6e5e38505c9aaf Mon Sep 17 00:00:00 2001 From: Tom Milligan Date: Wed, 9 Mar 2022 10:51:13 +0000 Subject: [PATCH] fix: don't crash on drop highlighted text onto canvas (#4890) --- src/components/App.tsx | 6 +++--- src/tests/helpers/api.ts | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index d873bfce..b235973b 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -4932,7 +4932,7 @@ class App extends React.Component { private handleAppOnDrop = async (event: React.DragEvent) => { try { - const file = event.dataTransfer.files[0]; + const file = event.dataTransfer.files.item(0); if (isSupportedImageFile(file)) { // first attempt to decode scene from the image if it's embedded @@ -5008,7 +5008,7 @@ class App extends React.Component { return; } - const file = event.dataTransfer?.files[0]; + const file = event.dataTransfer?.files.item(0); if ( file?.type === MIME_TYPES.excalidrawlib || file?.name?.endsWith(".excalidrawlib") @@ -5024,7 +5024,7 @@ class App extends React.Component { this.setState({ isLoading: false, errorMessage: error.message }), ); // default: assume an Excalidraw file regardless of extension/MimeType - } else { + } else if (file) { this.setState({ isLoading: true }); if (nativeFileSystemSupported) { try { diff --git a/src/tests/helpers/api.ts b/src/tests/helpers/api.ts index 9c119574..62f9e928 100644 --- a/src/tests/helpers/api.ts +++ b/src/tests/helpers/api.ts @@ -213,9 +213,12 @@ export class API { } }); + const files = [blob] as File[] & { item: (index: number) => File }; + files.item = (index: number) => files[index]; + Object.defineProperty(fileDropEvent, "dataTransfer", { value: { - files: [blob], + files, getData: (type: string) => { if (type === blob.type) { return text;