From 8670b2d58777d717bd04e996215a2efc05aac611 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Fri, 26 Mar 2021 22:12:02 +0100 Subject: [PATCH] fix: support d&d of files without extension (#3168) --- src/components/App.tsx | 31 ++++++++++++------------------- src/data/blob.ts | 10 ++-------- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 8340e193..f179aa98 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -3626,9 +3626,18 @@ class App extends React.Component { const file = event.dataTransfer?.files[0]; if ( - file?.type === "application/json" || - file?.name.endsWith(".excalidraw") + file?.type === MIME_TYPES.excalidrawlib || + file?.name?.endsWith(".excalidrawlib") ) { + Library.importLibrary(file) + .then(() => { + this.setState({ isLibraryOpen: false }); + }) + .catch((error) => + this.setState({ isLoading: false, errorMessage: error.message }), + ); + // default: assume an Excalidraw file regardless of extension/MimeType + } else { this.setState({ isLoading: true }); if (supported) { try { @@ -3640,23 +3649,7 @@ class App extends React.Component { console.warn(error.name, error.message); } } - this.loadFileToCanvas(file); - } else if ( - file?.type === MIME_TYPES.excalidrawlib || - file?.name.endsWith(".excalidrawlib") - ) { - Library.importLibrary(file) - .then(() => { - this.setState({ isLibraryOpen: false }); - }) - .catch((error) => - this.setState({ isLoading: false, errorMessage: error.message }), - ); - } else { - this.setState({ - isLoading: false, - errorMessage: t("alerts.couldNotLoadInvalidFile"), - }); + await this.loadFileToCanvas(file); } }; diff --git a/src/data/blob.ts b/src/data/blob.ts index 21bb9f20..0702436f 100644 --- a/src/data/blob.ts +++ b/src/data/blob.ts @@ -1,5 +1,5 @@ import { cleanAppStateForExport } from "../appState"; -import { EXPORT_DATA_TYPES, MIME_TYPES } from "../constants"; +import { EXPORT_DATA_TYPES } from "../constants"; import { clearElementsForExport } from "../element"; import { CanvasError } from "../errors"; import { t } from "../i18n"; @@ -95,13 +95,7 @@ export const loadFromBlob = async ( elements: clearElementsForExport(data.elements || []), appState: { theme: localAppState?.theme, - fileHandle: - blob.handle && - ["application/json", MIME_TYPES.excalidraw].includes( - getMimeType(blob), - ) - ? blob.handle - : null, + fileHandle: blob.handle ?? null, ...cleanAppStateForExport(data.appState || {}), ...(localAppState ? calculateScrollCenter(data.elements || [], localAppState, null)