fix: shareable links being merged with current scene data (#5547)

This commit is contained in:
David Luzar 2022-08-08 17:51:19 +02:00 committed by GitHub
parent 93cccd596a
commit 2d800feeeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -96,6 +96,7 @@ languageDetector.init({
const initializeScene = async (opts: { const initializeScene = async (opts: {
collabAPI: CollabAPI; collabAPI: CollabAPI;
excalidrawAPI: ExcalidrawImperativeAPI;
}): Promise< }): Promise<
{ scene: ExcalidrawInitialDataState | null } & ( { scene: ExcalidrawInitialDataState | null } & (
| { isExternalScene: true; id: string; key: string } | { isExternalScene: true; id: string; key: string }
@ -180,8 +181,28 @@ const initializeScene = async (opts: {
} }
if (roomLinkData) { if (roomLinkData) {
const { excalidrawAPI } = opts;
const scene = await opts.collabAPI.startCollaboration(roomLinkData);
return { return {
scene: await opts.collabAPI.startCollaboration(roomLinkData), // when collaborating, the state may have already been updated at this
// point (we may have received updates from other clients), so reconcile
// elements and appState with existing state
scene: {
...scene,
appState: {
...restoreAppState(scene?.appState, excalidrawAPI.getAppState()),
// necessary if we're invoking from a hashchange handler which doesn't
// go through App.initializeScene() that resets this flag
isLoading: false,
},
elements: reconcileElements(
scene?.elements || [],
excalidrawAPI.getSceneElementsIncludingDeleted(),
excalidrawAPI.getAppState(),
),
},
isExternalScene: true, isExternalScene: true,
id: roomLinkData.roomId, id: roomLinkData.roomId,
key: roomLinkData.roomKey, key: roomLinkData.roomKey,
@ -335,23 +356,9 @@ const ExcalidrawWrapper = () => {
} }
}; };
initializeScene({ collabAPI }).then(async (data) => { initializeScene({ collabAPI, excalidrawAPI }).then(async (data) => {
loadImages(data, /* isInitialLoad */ true); loadImages(data, /* isInitialLoad */ true);
initialStatePromiseRef.current.promise.resolve(data.scene);
initialStatePromiseRef.current.promise.resolve({
...data.scene,
// at this point the state may have already been updated (e.g. when
// collaborating, we may have received updates from other clients)
appState: restoreAppState(
data.scene?.appState,
excalidrawAPI.getAppState(),
),
elements: reconcileElements(
data.scene?.elements || [],
excalidrawAPI.getSceneElementsIncludingDeleted(),
excalidrawAPI.getAppState(),
),
});
}); });
const onHashChange = async (event: HashChangeEvent) => { const onHashChange = async (event: HashChangeEvent) => {
@ -366,7 +373,7 @@ const ExcalidrawWrapper = () => {
} }
excalidrawAPI.updateScene({ appState: { isLoading: true } }); excalidrawAPI.updateScene({ appState: { isLoading: true } });
initializeScene({ collabAPI }).then((data) => { initializeScene({ collabAPI, excalidrawAPI }).then((data) => {
loadImages(data); loadImages(data);
if (data.scene) { if (data.scene) {
excalidrawAPI.updateScene({ excalidrawAPI.updateScene({