mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
make clearing state for storage more type-safe (#1884)
This commit is contained in:
parent
6428b59ccb
commit
0ee2c15929
4 changed files with 107 additions and 36 deletions
|
@ -1,6 +1,7 @@
|
|||
import { getDefaultAppState } from "../appState";
|
||||
import { getDefaultAppState, cleanAppStateForExport } from "../appState";
|
||||
import { restore } from "./restore";
|
||||
import { t } from "../i18n";
|
||||
import { AppState } from "../types";
|
||||
|
||||
export const loadFromBlob = async (blob: any) => {
|
||||
const updateAppState = (contents: string) => {
|
||||
|
@ -13,7 +14,10 @@ export const loadFromBlob = async (blob: any) => {
|
|||
throw new Error(t("alerts.couldNotLoadInvalidFile"));
|
||||
}
|
||||
elements = data.elements || [];
|
||||
appState = { ...defaultAppState, ...data.appState };
|
||||
appState = {
|
||||
...defaultAppState,
|
||||
...cleanAppStateForExport(data.appState as Partial<AppState>),
|
||||
};
|
||||
} catch {
|
||||
throw new Error(t("alerts.couldNotLoadInvalidFile"));
|
||||
}
|
||||
|
|
|
@ -374,7 +374,7 @@ export const loadScene = async (id: string | null, privateKey?: string) => {
|
|||
|
||||
return {
|
||||
elements: data.elements,
|
||||
appState: data.appState && { ...data.appState },
|
||||
appState: data.appState,
|
||||
commitToHistory: false,
|
||||
};
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { ExcalidrawElement } from "../element/types";
|
||||
import { AppState, LibraryItems } from "../types";
|
||||
import { clearAppStateForLocalStorage } from "../appState";
|
||||
import { clearAppStateForLocalStorage, getDefaultAppState } from "../appState";
|
||||
import { restore } from "./restore";
|
||||
|
||||
const LOCAL_STORAGE_KEY = "excalidraw";
|
||||
|
@ -111,7 +111,8 @@ export const restoreFromLocalStorage = () => {
|
|||
if (savedElements) {
|
||||
try {
|
||||
elements = JSON.parse(savedElements);
|
||||
} catch {
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
// Do nothing because elements array is already empty
|
||||
}
|
||||
}
|
||||
|
@ -119,13 +120,14 @@ export const restoreFromLocalStorage = () => {
|
|||
let appState = null;
|
||||
if (savedState) {
|
||||
try {
|
||||
appState = JSON.parse(savedState) as AppState;
|
||||
// If we're retrieving from local storage, we should not be collaborating
|
||||
appState.isCollaborating = false;
|
||||
appState.collaborators = new Map();
|
||||
delete appState.width;
|
||||
delete appState.height;
|
||||
} catch {
|
||||
appState = {
|
||||
...getDefaultAppState(),
|
||||
...clearAppStateForLocalStorage(
|
||||
JSON.parse(savedState) as Partial<AppState>,
|
||||
),
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
// Do nothing because appState is already null
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue