Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

reverse the ready event to account for canvas reloading

Roman Pominov26cc4985e861
diff --git a/apps/designer/app/canvas/shared/use-designer-ready.ts b/apps/designer/app/canvas/shared/use-designer-ready.ts
index 1a13769ca1..266143e8a0 100644
--- a/apps/designer/app/canvas/shared/use-designer-ready.ts
+++ b/apps/designer/app/canvas/shared/use-designer-ready.ts
@@ -1,18 +1,30 @@
import { useState } from "react";
+import { useInterval } from "react-use";
import { publish, useSubscribe } from "~/shared/pubsub";
declare module "~/shared/pubsub" {
export interface PubsubMap {
- designerReady: undefined;
- designerReadyAck: undefined;
+ canvasReady: undefined;
+ canvasReadyAck: undefined;
}
}
export const useSubscribeDesignerReady = () => {
- const [isReady, setIsReady] = useState<boolean>(false);
- useSubscribe("designerReady", () => {
- setIsReady(true);
- publish({ type: "designerReadyAck" });
+ const [isAcknowledged, setIsAcknowledged] = useState(false);
+
+ useInterval(
+ () => {
+ // We publish this event to make sure designer is 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: "canvasReady" });
+ },
+ isAcknowledged ? null : 100
+ );
+
+ useSubscribe("canvasReadyAck", () => {
+ setIsAcknowledged(true);
});
- return isReady;
+
+ return isAcknowledged;
};
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index 72cf5459a3..84a94da24e 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -42,7 +42,6 @@ import { useClientSettings } from "./shared/client-settings";
import { Navigator } from "./features/sidebar-left";
import { PANEL_WIDTH } from "./shared/constants";
import { Asset } from "@webstudio-is/asset-uploader";
-import { useInterval } from "react-use";
export const links = () => {
return [
@@ -108,20 +107,9 @@ const useNavigatorLayout = () => {
return isLoaded ? clientSettings.navigatorLayout : "docked";
};
-const usePublishDesignerReady = (publish: Publish) => {
- const [isAcknowledged, setIsAcknowledged] = useState(false);
-
- useInterval(
- () => {
- // 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" });
- },
- isAcknowledged ? null : 100
- );
-
- useSubscribe("designerReadyAck", () => {
- setIsAcknowledged(true);
+export const useSubscribeCanvasReady = (publish: Publish) => {
+ useSubscribe("canvasReady", () => {
+ publish({ type: "canvasReadyAck" });
});
};
@@ -289,7 +277,7 @@ export const Designer = ({ config, project, pages, pageId }: DesignerProps) => {
const onRefReadCanvasWidth = useUpdateCanvasWidth();
const { onRef: onRefReadCanvas, onTransitionEnd } = useReadCanvasRect();
const [dragAndDropState] = useDragAndDropState();
- usePublishDesignerReady(publish);
+ useSubscribeCanvasReady(publish);
const iframeRefCallback = useCallback(
(ref) => {