From 5542e4528ad2d946bc94f606e3c980829aa23b00 Mon Sep 17 00:00:00 2001 From: "Daniel J. Geiger" <1852529+DanielJGeiger@users.noreply.github.com> Date: Sat, 31 Dec 2022 16:19:49 -0600 Subject: [PATCH] fix: Prevent local AppState reset during collaboration --- src/excalidraw-app/data/LocalData.ts | 25 ++++++++++++++++++++----- src/excalidraw-app/index.tsx | 4 +++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/excalidraw-app/data/LocalData.ts b/src/excalidraw-app/data/LocalData.ts index 08f91d8d6..f3b12fddc 100644 --- a/src/excalidraw-app/data/LocalData.ts +++ b/src/excalidraw-app/data/LocalData.ts @@ -46,12 +46,15 @@ class LocalFileManager extends FileManager { const saveDataStateToLocalStorage = ( elements: readonly ExcalidrawElement[], appState: AppState, + appStateOnly = false, ) => { try { - localStorage.setItem( - STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS, - JSON.stringify(clearElementsForLocalStorage(elements)), - ); + if (!appStateOnly) { + localStorage.setItem( + STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS, + JSON.stringify(clearElementsForLocalStorage(elements)), + ); + } localStorage.setItem( STORAGE_KEYS.LOCAL_STORAGE_APP_STATE, JSON.stringify(clearAppStateForLocalStorage(appState)), @@ -72,8 +75,12 @@ export class LocalData { appState: AppState, files: BinaryFiles, onFilesSaved: () => void, + appStateOnly = false, ) => { - saveDataStateToLocalStorage(elements, appState); + saveDataStateToLocalStorage(elements, appState, appStateOnly); + if (appStateOnly) { + return; + } await this.fileStorage.saveFiles({ elements, @@ -97,6 +104,14 @@ export class LocalData { } }; + /** Saves the AppState, only if saving is paused. */ + static saveAppState = (appState: AppState) => { + // we need to make the `isSavePaused` check synchronously (undebounced) + if (this.isSavePaused()) { + this._save([], appState, {}, () => {}, true); + } + }; + static flushSave = () => { this._save.flush(); }; diff --git a/src/excalidraw-app/index.tsx b/src/excalidraw-app/index.tsx index b12a41e31..e7186851a 100644 --- a/src/excalidraw-app/index.tsx +++ b/src/excalidraw-app/index.tsx @@ -189,7 +189,7 @@ const initializeScene = async (opts: { ...restoreAppState( { ...scene?.appState, - theme: localDataState?.appState?.theme || scene?.appState?.theme, + ...localDataState?.appState, }, excalidrawAPI.getAppState(), ), @@ -538,6 +538,8 @@ const ExcalidrawWrapper = () => { } } }); + } else { + LocalData.saveAppState(appState); } };