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.

Synchronize canvas store with designer (#762)

Bogdan Chadkin6387029b3cd2
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index 2ae3fdd6f8..511a675fb7 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -12,12 +12,12 @@ import {
type Tree,
} from "@webstudio-is/react-sdk";
import { publish, useSubscribe } from "~/shared/pubsub";
+import { registerContainers, useCanvasStore } from "~/shared/sync";
import { useShortcuts } from "./shared/use-shortcuts";
import {
useDeleteInstance,
useInsertInstance,
usePopulateRootInstance,
- usePublishRootInstance,
usePublishSelectedInstanceData,
usePublishTextEditingInstanceId,
useReparentInstance,
@@ -41,16 +41,12 @@ import {
useSetStyles,
useSubscribeScrollState,
} from "~/shared/nano-states";
-import { registerContainers } from "./shared/immerhin";
import { usePublishScrollState } from "./shared/use-publish-scroll-state";
import { useDragAndDrop } from "./shared/use-drag-drop";
import { utils } from "@webstudio-is/project";
import { useSubscribeDesignerReady } from "./shared/use-designer-ready";
import type { Asset } from "@webstudio-is/asset-uploader";
-import {
- useInitializeDesignTokens,
- usePublishDesignTokens,
-} from "./shared/design-tokens";
+import { useInitializeDesignTokens } from "./shared/design-tokens";
import { useInstanceCopyPaste } from "~/shared/copy-paste";
import { useSelectedInstance } from "./shared/nano-states";
import { customComponents } from "./custom-components";
@@ -140,14 +136,12 @@ type DesignModeProps = {
const DesignMode = ({ treeId, buildId }: DesignModeProps) => {
useManageBreakpoints();
- usePublishDesignTokens();
useManageDesignModeStyles();
useManageProps({ treeId });
usePublishSelectedInstanceData();
useInsertInstance({ treeId });
useReparentInstance();
useDeleteInstance();
- usePublishRootInstance();
useTrackSelectedElement();
useSync({ buildId, treeId });
useUpdateSelectedInstance();
@@ -180,6 +174,7 @@ export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {
useSetStyles(data.tree.styles);
usePopulateRootInstance(data.tree);
setParams(data.params ?? null);
+ useCanvasStore();
registerComponents(customComponents);
diff --git a/apps/designer/app/canvas/shared/breakpoints.ts b/apps/designer/app/canvas/shared/breakpoints.ts
index 7c8614121f..190f463df0 100644
--- a/apps/designer/app/canvas/shared/breakpoints.ts
+++ b/apps/designer/app/canvas/shared/breakpoints.ts
@@ -1,4 +1,3 @@
-import { useEffect } from "react";
import store from "immerhin";
import type { Breakpoint } from "@webstudio-is/css-data";
import { useSubscribe } from "~/shared/pubsub";
@@ -9,7 +8,6 @@ import {
stylesContainer,
useBreakpoints,
} from "~/shared/nano-states";
-import { publish } from "~/shared/pubsub";
import { addMediaRules } from "./styles";
import { useSyncInitializeOnce } from "~/shared/hook-utils";
import { filterMutable } from "~/shared/array-utils";
@@ -22,16 +20,6 @@ export const useInitializeBreakpoints = (breakpoints: Array<Breakpoint>) => {
});
};
-const usePublishBreakpoints = () => {
- const [breakpoints] = useBreakpoints();
- useEffect(() => {
- publish({
- type: "loadBreakpoints",
- payload: breakpoints,
- });
- }, [breakpoints]);
-};
-
const useBreakpointChange = () => {
useSubscribe("breakpointChange", (breakpoint) => {
store.createTransaction([breakpointsContainer], (breakpoints) => {
@@ -75,7 +63,6 @@ const useBreakpointDelete = () => {
};
export const useManageBreakpoints = () => {
- usePublishBreakpoints();
useBreakpointChange();
useBreakpointDelete();
};
diff --git a/apps/designer/app/canvas/shared/design-tokens.ts b/apps/designer/app/canvas/shared/design-tokens.ts
index d70eaf7f3b..fcd2ad67b4 100644
--- a/apps/designer/app/canvas/shared/design-tokens.ts
+++ b/apps/designer/app/canvas/shared/design-tokens.ts
@@ -1,8 +1,6 @@
import type { DesignToken } from "@webstudio-is/design-tokens";
-import { useEffect } from "react";
import { useSyncInitializeOnce } from "~/shared/hook-utils";
import { useDesignTokens } from "~/shared/nano-states";
-import { publish } from "~/shared/pubsub";
export const useInitializeDesignTokens = (designTokens: Array<DesignToken>) => {
const [, setDesignTokens] = useDesignTokens();
@@ -10,10 +8,3 @@ export const useInitializeDesignTokens = (designTokens: Array<DesignToken>) => {
setDesignTokens(designTokens);
});
};
-
-export const usePublishDesignTokens = () => {
- const [designTokens] = useDesignTokens();
- useEffect(() => {
- publish({ type: "loadDesignTokens", payload: designTokens });
- }, [designTokens]);
-};
diff --git a/apps/designer/app/canvas/shared/immerhin.ts b/apps/designer/app/canvas/shared/immerhin.ts
deleted file mode 100644
index e2e7cb9a91..0000000000
--- a/apps/designer/app/canvas/shared/immerhin.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { allUserPropsContainer } from "@webstudio-is/react-sdk";
-import store from "immerhin";
-import {
- rootInstanceContainer,
- breakpointsContainer,
- designTokensContainer,
- stylesContainer,
-} from "~/shared/nano-states";
-
-export const registerContainers = () => {
- store.register("breakpoints", breakpointsContainer);
- store.register("root", rootInstanceContainer);
- store.register("styles", stylesContainer);
- store.register("props", allUserPropsContainer);
- store.register("designTokens", designTokensContainer);
-};
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index dfa1da8da7..7adeddc8f1 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -27,7 +27,6 @@ declare module "~/shared/pubsub" {
export interface PubsubMap {
hoveredInstanceRect: DOMRect;
hoverInstance?: HoveredInstanceData;
- loadRootInstance?: Instance;
selectInstance?: SelectedInstanceData;
textEditingInstanceId?: Instance["id"];
insertInstance: {
@@ -182,16 +181,6 @@ export const usePublishSelectedInstanceData = () => {
}, [instance]);
};
-export const usePublishRootInstance = () => {
- const [rootInstance] = useRootInstance();
- useEffect(() => {
- publish({
- type: "loadRootInstance",
- payload: rootInstance,
- });
- }, [rootInstance]);
-};
-
/**
* We need to set the selected instance after a any root instance update,
* because anything that we change on the selected instance is actually done on the root, so
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index 9437f9eb55..b6387956e8 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -6,6 +6,7 @@ import {
utils as projectUtils,
} from "@webstudio-is/project";
import { Box, type CSS, Flex, Grid } from "@webstudio-is/design-system";
+import { useDesignerStore } from "~/shared/sync";
import interStyles from "~/shared/font-faces/inter.css";
import { SidebarLeft } from "./features/sidebar-left";
import { Inspector } from "./features/inspector";
@@ -39,7 +40,6 @@ import {
import { useClientSettings } from "./shared/client-settings";
import { Navigator } from "./features/sidebar-left";
import { getBuildUrl } from "~/shared/router-utils";
-import { useSubscribeDesignTokens } from "./shared/design-tokens-manager";
import { useInstanceCopyPaste } from "~/shared/copy-paste";
import { AssetsProvider, usePublishAssets } from "./shared/assets";
@@ -50,11 +50,6 @@ export const links = () => {
];
};
-const useSubscribeRootInstance = () => {
- const [, setValue] = useRootInstance();
- useSubscribe("loadRootInstance", setValue);
-};
-
const useSubscribeSelectedInstanceData = () => {
const [, setValue] = useSelectedInstanceData();
useSubscribe("selectInstance", setValue);
@@ -294,11 +289,10 @@ export const Designer = ({
buildOrigin,
}: DesignerProps) => {
useSubscribeSyncStatus();
- useSubscribeRootInstance();
+ useDesignerStore();
useSubscribeSelectedInstanceData();
useSubscribeHoveredInstanceData();
useSubscribeBreakpoints();
- useSubscribeDesignTokens();
useSetProject(project);
useSetPages(pages);
useSetCurrentPageId(pageId);
diff --git a/apps/designer/app/designer/features/breakpoints/use-subscribe-breakpoints.ts b/apps/designer/app/designer/features/breakpoints/use-subscribe-breakpoints.ts
index 3dc12d7a1f..d8cc2e292c 100644
--- a/apps/designer/app/designer/features/breakpoints/use-subscribe-breakpoints.ts
+++ b/apps/designer/app/designer/features/breakpoints/use-subscribe-breakpoints.ts
@@ -1,15 +1,12 @@
import { useEffect } from "react";
-import { useSubscribe } from "~/shared/pubsub";
import { useSelectedBreakpoint } from "~/designer/shared/nano-states";
import { useBreakpoints } from "~/shared/nano-states";
import { utils } from "@webstudio-is/project";
export const useSubscribeBreakpoints = () => {
- const [breakpoints, setBreakpoints] = useBreakpoints();
+ const [breakpoints] = useBreakpoints();
const [selectedBreakpoint, setSelectedBreakpoint] = useSelectedBreakpoint();
- useSubscribe("loadBreakpoints", setBreakpoints);
-
useEffect(() => {
// Set the initial selected breakpoint
if (selectedBreakpoint === undefined && breakpoints.length !== 0) {
diff --git a/apps/designer/app/designer/shared/design-tokens-manager/hooks.ts b/apps/designer/app/designer/shared/design-tokens-manager/hooks.ts
deleted file mode 100644
index 344590f09e..0000000000
--- a/apps/designer/app/designer/shared/design-tokens-manager/hooks.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { useSubscribe } from "~/shared/pubsub";
-import { useDesignTokens } from "~/shared/nano-states";
-import type { DesignToken } from "@webstudio-is/design-tokens";
-
-declare module "~/shared/pubsub" {
- export interface PubsubMap {
- loadDesignTokens: Array<DesignToken>;
- }
-}
-
-export const useSubscribeDesignTokens = () => {
- const [, setDesignTokens] = useDesignTokens();
- useSubscribe("loadDesignTokens", setDesignTokens);
-};
diff --git a/apps/designer/app/designer/shared/design-tokens-manager/index.ts b/apps/designer/app/designer/shared/design-tokens-manager/index.ts
index 5800cf9b40..5f7eeae37e 100644
--- a/apps/designer/app/designer/shared/design-tokens-manager/index.ts
+++ b/apps/designer/app/designer/shared/design-tokens-manager/index.ts
@@ -1,3 +1,2 @@
export * from "./design-tokens-manager";
export { tokensToStyle, updateTokenMutable, deleteTokenMutable } from "./utils";
-export * from "./hooks";
diff --git a/apps/designer/app/shared/sync/index.ts b/apps/designer/app/shared/sync/index.ts
index 583bfa713b..e2f02329d0 100644
--- a/apps/designer/app/shared/sync/index.ts
+++ b/apps/designer/app/shared/sync/index.ts
@@ -1 +1,2 @@
export * from "./sync-status";
+export * from "./sync-stores";
diff --git a/apps/designer/app/shared/sync/sync-stores.ts b/apps/designer/app/shared/sync/sync-stores.ts
new file mode 100644
index 0000000000..ebfaa05d7e
--- /dev/null
+++ b/apps/designer/app/shared/sync/sync-stores.ts
@@ -0,0 +1,104 @@
+import { applyPatches } from "immer";
+import store, { type Change } from "immerhin";
+import type { ValueContainer } from "react-nano-state";
+import { useEffect } from "react";
+import { allUserPropsContainer } from "@webstudio-is/react-sdk";
+import { publish, subscribe } from "~/shared/pubsub";
+import {
+ rootInstanceContainer,
+ breakpointsContainer,
+ designTokensContainer,
+ stylesContainer,
+} from "~/shared/nano-states";
+
+type StoreData = {
+ namespace: string;
+ value: unknown;
+};
+
+declare module "~/shared/pubsub" {
+ export interface PubsubMap {
+ sendStoreData: StoreData[];
+ sendStoreChanges: {
+ // distinct target to avoid infinite loop
+ // @todo implement two-way patches
+ target: "canvas" | "designer";
+ changes: Change[];
+ };
+ }
+}
+
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+const containers = new Map<string, ValueContainer<any>>([
+ ["breakpoints", breakpointsContainer],
+ ["root", rootInstanceContainer],
+ ["styles", stylesContainer],
+ ["props", allUserPropsContainer],
+ ["designTokens", designTokensContainer],
+]);
+
+export const registerContainers = () => {
+ for (const [namespace, container] of containers) {
+ store.register(namespace, container);
+ }
+};
+
+export const useCanvasStore = () => {
+ useEffect(() => {
+ // expect data to be populated by the time effect is called
+ const data = [];
+ for (const [namespace, container] of containers) {
+ data.push({
+ namespace,
+ value: container.value,
+ });
+ }
+ publish({
+ type: "sendStoreData",
+ payload: data,
+ });
+ const unsubscribeStore = store.subscribe((_transactionId, changes) => {
+ publish({
+ type: "sendStoreChanges",
+ payload: {
+ target: "designer",
+ changes,
+ },
+ });
+ });
+ return () => {
+ unsubscribeStore();
+ };
+ }, []);
+};
+
+export const useDesignerStore = () => {
+ useEffect(() => {
+ const unsubscribeSendStoreData = subscribe("sendStoreData", (data) => {
+ for (const { namespace, value } of data) {
+ const container = containers.get(namespace);
+ if (container) {
+ container.dispatch(value);
+ }
+ }
+ });
+ const unsubscribeSendStoreChanges = subscribe(
+ "sendStoreChanges",
+ ({ target, changes }) => {
+ if (target !== "designer") {
+ return;
+ }
+ for (const { namespace, patches } of changes) {
+ const container = containers.get(namespace);
+ if (container) {
+ container.dispatch(applyPatches(container.value, patches));
+ }
+ }
+ }
+ );
+ return () => {
+ unsubscribeSendStoreData();
+ unsubscribeSendStoreChanges();
+ };
+ }, []);
+};
diff --git a/apps/designer/package.json b/apps/designer/package.json
index 0ed2ea8df4..3d831731c4 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -44,7 +44,7 @@
"@webstudio-is/prisma-client": "workspace:^",
"@webstudio-is/project": "workspace:^",
"@webstudio-is/react-sdk": "workspace:^",
- "bson-objectid": "^2.0.2",
+ "bson-objectid": "^2.0.4",
"camelcase": "^6.3.0",
"colord": "^2.9.3",
"css-tree": "^2.3.0",
@@ -55,7 +55,7 @@
"fast-deep-equal": "3.1.3",
"hyphenate-style-name": "^1.0.4",
"immer": "^9.0.12",
- "immerhin": "^0.1.2",
+ "immerhin": "^0.2.1",
"lexical": "^0.6.0",
"lodash": "4.17.21",
"lodash.capitalize": "^4.2.1",
diff --git a/packages/asset-uploader/package.json b/packages/asset-uploader/package.json
index 70bbdb1018..d7f575cbe0 100644
--- a/packages/asset-uploader/package.json
+++ b/packages/asset-uploader/package.json
@@ -20,7 +20,7 @@
"@webstudio-is/fonts": "workspace:^",
"@webstudio-is/prisma-client": "workspace:^",
"app-root-path": "^3.0.0",
- "bson-objectid": "^2.0.2",
+ "bson-objectid": "^2.0.4",
"sharp": "^0.30.7"
},
"devDependencies": {
diff --git a/packages/project/package.json b/packages/project/package.json
index fbbec49983..dae48aa453 100644
--- a/packages/project/package.json
+++ b/packages/project/package.json
@@ -21,7 +21,7 @@
"@webstudio-is/fonts": "workspace:^",
"@webstudio-is/prisma-client": "workspace:^",
"@webstudio-is/react-sdk": "workspace:^",
- "bson-objectid": "^2.0.2",
+ "bson-objectid": "^2.0.4",
"dataloader": "^2.1.0",
"immer": "^9.0.12",
"nanoid": "^3.2.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bf9ed6f065..7820966139 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -96,7 +96,7 @@ importers:
'@webstudio-is/storybook-config': workspace:^
'@webstudio-is/tsconfig': workspace:^
babel-loader: ^8.2.5
- bson-objectid: ^2.0.2
+ bson-objectid: ^2.0.4
camelcase: ^6.3.0
colord: ^2.9.3
css-tree: ^2.3.0
@@ -109,7 +109,7 @@ importers:
fast-deep-equal: 3.1.3
hyphenate-style-name: ^1.0.4
immer: ^9.0.12
- immerhin: ^0.1.2
+ immerhin: ^0.2.1
jest: ^29.3.1
lexical: ^0.6.0
lodash: 4.17.21
@@ -173,7 +173,7 @@ importers:
'@webstudio-is/prisma-client': link:../../packages/prisma-client
'@webstudio-is/project': link:../../packages/project
'@webstudio-is/react-sdk': link:../../packages/react-sdk
- bson-objectid: 2.0.3
+ bson-objectid: 2.0.4
camelcase: 6.3.0
colord: 2.9.3
css-tree: 2.3.0
@@ -184,7 +184,7 @@ importers:
fast-deep-equal: 3.1.3
hyphenate-style-name: 1.0.4
immer: 9.0.15
- immerhin: 0.1.2
+ immerhin: 0.2.1
lexical: 0.6.0
lodash: 4.17.21
lodash.capitalize: 4.2.1
@@ -269,7 +269,7 @@ importers:
'@webstudio-is/scripts': workspace:^
'@webstudio-is/tsconfig': workspace:^
app-root-path: ^3.0.0
- bson-objectid: ^2.0.2
+ bson-objectid: ^2.0.4
jest: ^29.3.1
sharp: ^0.30.7
typescript: 4.7.4
@@ -281,7 +281,7 @@ importers:
'@webstudio-is/fonts': link:../fonts
'@webstudio-is/prisma-client': link:../prisma-client
app-root-path: 3.0.0
- bson-objectid: 2.0.3
+ bson-objectid: 2.0.4
sharp: 0.30.7
devDependencies:
'@jest/globals': 29.3.1
@@ -726,7 +726,7 @@ importers:
'@webstudio-is/react-sdk': workspace:^
'@webstudio-is/scripts': workspace:^
'@webstudio-is/tsconfig': workspace:^
- bson-objectid: ^2.0.2
+ bson-objectid: ^2.0.4
dataloader: ^2.1.0
immer: ^9.0.12
jest: ^29.3.1
@@ -745,7 +745,7 @@ importers:
'@webstudio-is/fonts': link:../fonts
'@webstudio-is/prisma-client': link:../prisma-client
'@webstudio-is/react-sdk': link:../react-sdk
- bson-objectid: 2.0.3
+ bson-objectid: 2.0.4
dataloader: 2.1.0
immer: 9.0.15
nanoid: 3.3.4
@@ -11397,8 +11397,8 @@ packages:
dependencies:
node-int64: 0.4.0
- /bson-objectid/2.0.3:
- resolution: {integrity: sha512-WYwVtY9yqk179EPMNuF3vcxufdrGLEo2XwqdRVbfLVe9X6jLt7WKZQgP+ObOcprakBGbHxzl76tgTaieqsH29g==}
+ /bson-objectid/2.0.4:
+ resolution: {integrity: sha512-vgnKAUzcDoa+AeyYwXCoHyF2q6u/8H46dxu5JN+4/TZeq/Dlinn0K6GvxsCLb3LHUJl0m/TLiEK31kUwtgocMQ==}
dev: false
/buffer-from/1.1.2:
@@ -12397,8 +12397,8 @@ packages:
resolution: {integrity: sha512-qTcEYLen3r7ojZNgVUaRggOI+KM7jrKxXeSHhogh/TWxYMeONEMqY+hmkobiYQozsGIyg9OYVzO4ZIfoB4I0pQ==}
dev: false
- /deasync/0.1.26:
- resolution: {integrity: sha512-YKw0BmJSWxkjtQsbgn6Q9CHSWB7DKMen8vKrgyC006zy0UZ6nWyGidB0IzZgqkVRkOglAeUaFtiRTeLyel72bg==}
+ /deasync/0.1.28:
+ resolution: {integrity: sha512-QqLF6inIDwiATrfROIyQtwOQxjZuek13WRYZ7donU5wJPLoP67MnYxA6QtqdvdBy2mMqv5m3UefBVdJjvevOYg==}
engines: {node: '>=0.11.0'}
requiresBuild: true
dependencies:
@@ -14558,7 +14558,7 @@ packages:
source-map: 0.6.1
wordwrap: 1.0.0
optionalDependencies:
- uglify-js: 3.16.2
+ uglify-js: 3.17.4
/has-bigints/1.0.2:
resolution: {integrity: sha512-tSvCKtBr9lkF0Ex0aQiP9N+OpV4zi2r/Nee5VkRDbaqv35RLYMzbwQfFSZZH0kR+Rd6302UJZ2p/bJCEoR3VoQ==}
@@ -14942,10 +14942,10 @@ packages:
resolution: {integrity: sha512-2eB/sswms9AEUSkOm4SbV5Y7Vmt/bKRwByd52jfLkW4OLYeaTP3EEiJ9agqU0O/tq6Dk62Zfj+TJSqfm1rLVGQ==}
dev: false
- /immerhin/0.1.2:
- resolution: {integrity: sha512-4UfFIeq9sAgR5WR8xebUhrxBHn7yfNua2mflqxSYMkTwg/mjgr5HP22RdgNrWWZ2yZ8hGl2ipPKS8B+xQPA1Cw==}
+ /immerhin/0.2.1:
+ resolution: {integrity: sha512-f+pfXUlcguigFuLFWSeadkIzO3Uiyc5bJ0vfwP/0/XioTvHfT210RYaOS4D5EIRtEdq7acsENM8dcfLXE1lCkQ==}
dependencies:
- bson-objectid: 2.0.3
+ bson-objectid: 2.0.4
immer: 9.0.15
dev: false
@@ -21230,8 +21230,8 @@ packages:
engines: {node: '>=4.2.0'}
hasBin: true
- /uglify-js/3.16.2:
- resolution: {integrity: sha512-AaQNokTNgExWrkEYA24BTNMSjyqEXPSfhqoS0AxmHkCJ4U+Dyy5AvbGV/sqxuxficEfGGoX3zWw9R7QpLFfEsg==}
+ /uglify-js/3.17.4:
+ resolution: {integrity: sha512-T9q82TJI9e/C1TAxYvfb16xO120tMVFZrGA3f9/P4424DNu6ypK103y0GPFVa17yotwSyZW5iYXgjYHkGrJW/g==}
engines: {node: '>=0.8.0'}
hasBin: true
requiresBuild: true
@@ -22161,7 +22161,7 @@ packages:
unist-util-visit: 4.1.0
vfile: 5.3.4
optionalDependencies:
- deasync: 0.1.26
+ deasync: 0.1.28
transitivePeerDependencies:
- supports-color
dev: true