mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
fix restoring appState (#2182)
This commit is contained in:
parent
b2822f3538
commit
adb1ac5788
8 changed files with 74 additions and 49 deletions
|
@ -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) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue