Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix: scene not initialized properly when tab not focused (#2677)

David Luzaraef3644c93ee
diff --git a/src/components/App.tsx b/src/components/App.tsx
index e60c27bf19..ecce0fd2f2 100644
--- a/src/components/App.tsx
+++ b/src/components/App.tsx
@@ -861,7 +861,16 @@ class App extends React.Component<ExcalidrawProps, AppState> {
history.record(this.state, this.scene.getElementsIncludingDeleted());
- this.props.onChange?.(this.scene.getElementsIncludingDeleted(), this.state);
+ // Do not notify consumers if we're still loading the scene. Among other
+ // potential issues, this fixes a case where the tab isn't focused during
+ // init, which would trigger onChange with empty elements, which would then
+ // override whatever is in localStorage currently.
+ if (!this.state.isLoading) {
+ this.props.onChange?.(
+ this.scene.getElementsIncludingDeleted(),
+ this.state,
+ );
+ }
}
// Copy/paste
diff --git a/src/excalidraw-app/index.tsx b/src/excalidraw-app/index.tsx
index c8b151f471..f8db9fc6a6 100644
--- a/src/excalidraw-app/index.tsx
+++ b/src/excalidraw-app/index.tsx
@@ -93,7 +93,6 @@ type Scene = ImportedDataState & { commitToHistory: boolean };
const initializeScene = async (opts: {
resetScene: ExcalidrawImperativeAPI["resetScene"];
initializeSocketClient: CollabAPI["initializeSocketClient"];
- onLateInitialization?: (scene: Scene) => void;
}): Promise<Scene | null> => {
const searchParams = new URLSearchParams(window.location.search);
const id = searchParams.get("id");
@@ -124,17 +123,15 @@ const initializeScene = async (opts: {
} else {
// https://github.com/excalidraw/excalidraw/issues/1919
if (document.hidden) {
- window.addEventListener(
- "focus",
- () =>
- initializeScene(opts).then((_scene) => {
- opts?.onLateInitialization?.(_scene || scene);
- }),
- {
- once: true,
- },
- );
- return null;
+ return new Promise((resolve, reject) => {
+ window.addEventListener(
+ "focus",
+ () => initializeScene(opts).then(resolve).catch(reject),
+ {
+ once: true,
+ },
+ );
+ });
}
isCollabScene = false;
@@ -222,9 +219,6 @@ function ExcalidrawWrapper(props: { collab: CollabAPI }) {
initializeScene({
resetScene: excalidrawApi.resetScene,
initializeSocketClient: collab.initializeSocketClient,
- onLateInitialization: (scene) => {
- initialStatePromiseRef.current.promise.resolve(scene);
- },
}).then((scene) => {
initialStatePromiseRef.current.promise.resolve(scene);
});
diff --git a/src/packages/excalidraw/CHANGELOG.md b/src/packages/excalidraw/CHANGELOG.md
index ecc9cc139b..ed69a57ec6 100644
--- a/src/packages/excalidraw/CHANGELOG.md
+++ b/src/packages/excalidraw/CHANGELOG.md
@@ -27,6 +27,7 @@ Please add the latest change on the top under the correct section.
### Fixes
+- Fix initialization when browser tab not focused [#2677](https://github.com/excalidraw/excalidraw/pull/2677)
- Consistent case for export locale strings [#2622](https://github.com/excalidraw/excalidraw/pull/2622)
- Remove unnecessary console.error as it was polluting Sentry [#2637](https://github.com/excalidraw/excalidraw/pull/2637)
- Fix scroll-to-center on init for non-zero canvas offsets [#2445](https://github.com/excalidraw/excalidraw/pull/2445)