fix restoring appState (#2182)

This commit is contained in:
David Luzar 2020-09-22 21:51:49 +02:00 committed by GitHub
parent b2822f3538
commit adb1ac5788
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 74 additions and 49 deletions

View file

@ -1,8 +1,8 @@
import { getDefaultAppState, cleanAppStateForExport } from "../appState";
import { cleanAppStateForExport } from "../appState";
import { restore } from "./restore";
import { t } from "../i18n";
import { AppState } from "../types";
import { LibraryData } from "./types";
import { LibraryData, ImportedDataState } from "./types";
import { calculateScrollCenter } from "../scene";
const loadFileContents = async (blob: any) => {
@ -34,26 +34,24 @@ export const loadFromBlob = async (blob: any, appState?: AppState) => {
}
const contents = await loadFileContents(blob);
const defaultAppState = getDefaultAppState();
let elements = [];
let _appState = appState || defaultAppState;
try {
const data = JSON.parse(contents);
const data: ImportedDataState = JSON.parse(contents);
if (data.type !== "excalidraw") {
throw new Error(t("alerts.couldNotLoadInvalidFile"));
}
elements = data.elements || [];
_appState = {
...defaultAppState,
appearance: _appState.appearance,
...cleanAppStateForExport(data.appState as Partial<AppState>),
...(appState ? calculateScrollCenter(elements, appState, null) : {}),
};
return restore({
elements: data.elements,
appState: {
appearance: appState?.appearance,
...cleanAppStateForExport(data.appState || {}),
...(appState
? calculateScrollCenter(data.elements || [], appState, null)
: {}),
},
});
} catch {
throw new Error(t("alerts.couldNotLoadInvalidFile"));
}
return restore(elements, _appState);
};
export const loadLibraryFromBlob = async (blob: any) => {