Merge pull request #284 from webstudio-is/fix-designer-ready

Wait for designerReady event on the canvas before publishing data
This commit is contained in:
Oleg Isonen authored and GitHub committed 2022-09-16 15:12:18 +02:00
commit 7dbcf75843
3 files changed
+23 -2

No files matched your search

+3 -1
View File
@@ -46,6 +46,7 @@ import { usePublishScrollState } from "./shared/use-publish-scroll-state";
import { useDragAndDrop } from "./shared/use-drag-drop";
import { setInstanceChildrenMutable } from "~/shared/tree-utils";
import { CanvasData } from "~/shared/db";
import { useDesignerReady } from "./shared/use-designer-ready";
registerContainers();
@@ -118,6 +119,7 @@ export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {
if (data.tree === null) {
throw new Error("Tree is null");
}
const isDesignerReady = useDesignerReady();
useInitializeBreakpoints(data.breakpoints);
globalStyles();
useAllUserProps(data.props);
@@ -129,7 +131,7 @@ export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {
if (elements === undefined) return null;
if (isPreviewMode) {
if (isPreviewMode || isDesignerReady === false) {
return elements;
}
@@ -0,0 +1,16 @@
import { useState } from "react";
import { useSubscribe } from "~/shared/pubsub";
declare module "~/shared/pubsub" {
export interface PubsubMap {
designerReady: undefined;
}
}
export const useDesignerReady = () => {
const [isReady, setIsReady] = useState<boolean>(false);
useSubscribe("designerReady", () => {
setIsReady(true);
});
return isReady;
};
+4 -1
View File
@@ -232,8 +232,11 @@ export const Designer = ({ config, project }: DesignerProps) => {
publishRef.current = ref;
onRefReadCanvasWidth(ref);
onRefReadCanvas(ref);
// We publish this even to let canvas know that we are now listening to the events, otherwise if canvas loads faster than designer, which is possible with SSR,
// we can miss the events and designer will just not connect to the canvas.
publish({ type: "designerReady" });
},
[publishRef, onRefReadCanvasWidth, onRefReadCanvas]
[publishRef, onRefReadCanvasWidth, onRefReadCanvas, publish]
);
return (