Load only valid excalidraw json when drag&drop (#678)

* Load only valid excalidraw json when drag&drop

* fix lint error
This commit is contained in:
Faustino Kialungila 2020-02-03 01:27:03 +01:00 committed by GitHub
parent 92a0f100b8
commit abd04cb870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,6 +24,9 @@ const BACKEND_GET = "https://json.excalidraw.com/api/v1/";
(window as any).handle = null; (window as any).handle = null;
interface DataState { interface DataState {
type?: string;
version?: string;
source?: string;
elements: readonly ExcalidrawElement[]; elements: readonly ExcalidrawElement[];
appState: AppState | null; appState: AppState | null;
selectedId?: number; selectedId?: number;
@ -92,6 +95,9 @@ export async function loadFromBlob(blob: any) {
let appState = defaultAppState; let appState = defaultAppState;
try { try {
const data = JSON.parse(contents); const data = JSON.parse(contents);
if (data.type !== "excalidraw") {
throw new Error("Cannot load invalid json");
}
elements = data.elements || []; elements = data.elements || [];
appState = { ...defaultAppState, ...data.appState }; appState = { ...defaultAppState, ...data.appState };
} catch (e) { } catch (e) {
@ -120,6 +126,9 @@ export async function loadFromBlob(blob: any) {
})(); })();
} }
const { elements, appState } = updateAppState(contents); const { elements, appState } = updateAppState(contents);
if (!elements.length) {
return Promise.reject("Cannot load invalid json");
}
return new Promise<DataState>(resolve => { return new Promise<DataState>(resolve => {
resolve(restore(elements, appState, { scrollToContent: true })); resolve(restore(elements, appState, { scrollToContent: true }));
}); });