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

Co-authored-by: Lipis <lipiridis@gmail.com>
This commit is contained in:
David Luzar 2020-12-29 21:03:34 +01:00 committed by GitHub
parent 0cf58adb4c
commit aef3644c93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 16 deletions

View File

@ -861,7 +861,16 @@ class App extends React.Component<ExcalidrawProps, AppState> {
history.record(this.state, this.scene.getElementsIncludingDeleted()); 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 // Copy/paste

View File

@ -93,7 +93,6 @@ type Scene = ImportedDataState & { commitToHistory: boolean };
const initializeScene = async (opts: { const initializeScene = async (opts: {
resetScene: ExcalidrawImperativeAPI["resetScene"]; resetScene: ExcalidrawImperativeAPI["resetScene"];
initializeSocketClient: CollabAPI["initializeSocketClient"]; initializeSocketClient: CollabAPI["initializeSocketClient"];
onLateInitialization?: (scene: Scene) => void;
}): Promise<Scene | null> => { }): Promise<Scene | null> => {
const searchParams = new URLSearchParams(window.location.search); const searchParams = new URLSearchParams(window.location.search);
const id = searchParams.get("id"); const id = searchParams.get("id");
@ -124,17 +123,15 @@ const initializeScene = async (opts: {
} else { } else {
// https://github.com/excalidraw/excalidraw/issues/1919 // https://github.com/excalidraw/excalidraw/issues/1919
if (document.hidden) { if (document.hidden) {
window.addEventListener( return new Promise((resolve, reject) => {
"focus", window.addEventListener(
() => "focus",
initializeScene(opts).then((_scene) => { () => initializeScene(opts).then(resolve).catch(reject),
opts?.onLateInitialization?.(_scene || scene); {
}), once: true,
{ },
once: true, );
}, });
);
return null;
} }
isCollabScene = false; isCollabScene = false;
@ -222,9 +219,6 @@ function ExcalidrawWrapper(props: { collab: CollabAPI }) {
initializeScene({ initializeScene({
resetScene: excalidrawApi.resetScene, resetScene: excalidrawApi.resetScene,
initializeSocketClient: collab.initializeSocketClient, initializeSocketClient: collab.initializeSocketClient,
onLateInitialization: (scene) => {
initialStatePromiseRef.current.promise.resolve(scene);
},
}).then((scene) => { }).then((scene) => {
initialStatePromiseRef.current.promise.resolve(scene); initialStatePromiseRef.current.promise.resolve(scene);
}); });

View File

@ -27,6 +27,7 @@ Please add the latest change on the top under the correct section.
### Fixes ### 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) - 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) - 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) - Fix scroll-to-center on init for non-zero canvas offsets [#2445](https://github.com/excalidraw/excalidraw/pull/2445)