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.

Remove treeId from props updates (#635)

Bogdan Chadkin1459fd7cbd42
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index f18a8e9d1a..6c622f52a0 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -139,8 +139,8 @@ const DesignMode = ({ treeId, buildId }: DesignModeProps) => {
useManageBreakpoints();
usePublishDesignTokens();
useManageDesignModeStyles();
- useManageProps();
- usePublishSelectedInstanceData(treeId);
+ useManageProps({ treeId });
+ usePublishSelectedInstanceData();
useInsertInstance({ treeId });
useReparentInstance();
useDeleteInstance();
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index f63a29e281..04f05c9840 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -188,7 +188,7 @@ export const useDeleteInstance = () => {
});
};
-export const usePublishSelectedInstanceData = (treeId: Tree["id"]) => {
+export const usePublishSelectedInstanceData = () => {
const [instance] = useSelectedInstance();
const [selectedElement] = useSelectedElement();
const [allUserProps] = useAllUserProps();
@@ -204,11 +204,9 @@ export const usePublishSelectedInstanceData = (treeId: Tree["id"]) => {
useEffect(() => {
// Unselects the instance by `undefined`
- let payload;
+ let payload: undefined | SelectedInstanceData;
if (instance !== undefined) {
- const props =
- allUserProps[instance.id] ??
- utils.props.createInstanceProps({ instanceId: instance.id, treeId });
+ const props = allUserProps[instance.id];
const browserStyle = getBrowserStyle(selectedElement);
payload = {
id: instance.id,
@@ -222,7 +220,7 @@ export const usePublishSelectedInstanceData = (treeId: Tree["id"]) => {
type: "selectInstance",
payload,
});
- }, [instance, allUserProps, treeId, selectedElement, styleKey]);
+ }, [instance, allUserProps, selectedElement, styleKey]);
};
export const usePublishHoveredInstanceData = () => {
diff --git a/apps/designer/app/canvas/shared/props.ts b/apps/designer/app/canvas/shared/props.ts
index d011a1928d..6efde3c311 100644
--- a/apps/designer/app/canvas/shared/props.ts
+++ b/apps/designer/app/canvas/shared/props.ts
@@ -6,10 +6,10 @@ import {
deletePropMutable,
} from "~/shared/props-utils";
-export const useManageProps = () => {
+export const useManageProps = ({ treeId }: { treeId: string }) => {
useSubscribe("updateProps", (userPropsUpdates) => {
store.createTransaction([allUserPropsContainer], (allUserProps) => {
- updateAllUserPropsMutable(allUserProps, userPropsUpdates);
+ updateAllUserPropsMutable(treeId, allUserProps, userPropsUpdates);
});
});
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index c6083f28ab..829e08b82f 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -114,7 +114,9 @@ const useCopyPaste = (publish: Publish) => {
rootInstance,
selectedInstance.id
);
- return instance && { instance, props: selectedInstance.props.props };
+ return (
+ instance && { instance, props: selectedInstance.props?.props ?? [] }
+ );
}
}, [rootInstance, selectedInstance]);
diff --git a/apps/designer/app/designer/features/props-panel/use-props-logic.ts b/apps/designer/app/designer/features/props-panel/use-props-logic.ts
index d0daf33095..e2868ba8ba 100644
--- a/apps/designer/app/designer/features/props-panel/use-props-logic.ts
+++ b/apps/designer/app/designer/features/props-panel/use-props-logic.ts
@@ -204,8 +204,7 @@ export const usePropsLogic = ({
publish({
type: "updateProps",
payload: {
- treeId: selectedInstanceData.props.treeId,
- propsId: selectedInstanceData.props.id,
+ propsId: selectedInstanceData.props?.id,
instanceId: selectedInstanceData.id,
updates,
},
diff --git a/apps/designer/app/shared/props-utils/update-props.test.ts b/apps/designer/app/shared/props-utils/update-props.test.ts
index 309d6ebc59..c51471f55a 100644
--- a/apps/designer/app/shared/props-utils/update-props.test.ts
+++ b/apps/designer/app/shared/props-utils/update-props.test.ts
@@ -18,10 +18,9 @@ describe("Update props", () => {
const update: UserPropsUpdates = {
propsId: "id",
instanceId: "instanceId",
- treeId: "treeId",
updates: [],
};
- updateAllUserPropsMutable(propsMap, update);
+ updateAllUserPropsMutable("treeId", propsMap, update);
expect(propsMap).toMatchSnapshot();
});
@@ -37,10 +36,9 @@ describe("Update props", () => {
const update: UserPropsUpdates = {
propsId: "id",
instanceId: "instanceId",
- treeId: "treeId",
updates: [{ id: "propId", prop: "a", value: "1", type: "string" }],
};
- updateAllUserPropsMutable(propsMap, update);
+ updateAllUserPropsMutable("treeId", propsMap, update);
expect(propsMap).toMatchSnapshot();
});
@@ -56,10 +54,9 @@ describe("Update props", () => {
const update: UserPropsUpdates = {
propsId: "id",
instanceId: "instanceId",
- treeId: "treeId",
updates: [{ id: "propId", prop: "a", value: "2", type: "string" }],
};
- updateAllUserPropsMutable(propsMap, update);
+ updateAllUserPropsMutable("treeId", propsMap, update);
expect(propsMap).toMatchSnapshot();
});
@@ -75,10 +72,9 @@ describe("Update props", () => {
const update: UserPropsUpdates = {
propsId: "id",
instanceId: "instanceId",
- treeId: "treeId",
updates: [{ id: "propId", prop: "b", value: "1", type: "string" }],
};
- updateAllUserPropsMutable(propsMap, update);
+ updateAllUserPropsMutable("treeId", propsMap, update);
expect(propsMap).toMatchSnapshot();
});
@@ -94,7 +90,6 @@ describe("Update props", () => {
const update: UserPropsUpdates = {
propsId: "id",
instanceId: "instanceId",
- treeId: "treeId",
updates: [
{
id: "propId",
@@ -104,7 +99,7 @@ describe("Update props", () => {
},
],
};
- updateAllUserPropsMutable(propsMap, update);
+ updateAllUserPropsMutable("treeId", propsMap, update);
expect(propsMap).toMatchInlineSnapshot(`
{
"instanceId": {
diff --git a/apps/designer/app/shared/props-utils/update-props.ts b/apps/designer/app/shared/props-utils/update-props.ts
index cdf458c7b4..2db47e51f9 100644
--- a/apps/designer/app/shared/props-utils/update-props.ts
+++ b/apps/designer/app/shared/props-utils/update-props.ts
@@ -1,15 +1,17 @@
+import { v4 as uuid } from "uuid";
import {
type AllUserProps,
type UserPropsUpdates,
} from "@webstudio-is/react-sdk";
export const updateAllUserPropsMutable = (
+ treeId: string,
allUserProps: AllUserProps,
- { instanceId, propsId, treeId, updates }: UserPropsUpdates
+ { instanceId, propsId, updates }: UserPropsUpdates
) => {
if (instanceId in allUserProps === false) {
allUserProps[instanceId] = {
- id: propsId,
+ id: propsId ?? uuid(),
instanceId,
treeId,
props: [],
diff --git a/packages/project/src/shared/canvas-components/instance-data.ts b/packages/project/src/shared/canvas-components/instance-data.ts
index 32dd75c60f..755c8f5bf5 100644
--- a/packages/project/src/shared/canvas-components/instance-data.ts
+++ b/packages/project/src/shared/canvas-components/instance-data.ts
@@ -12,7 +12,7 @@ export type SelectedInstanceData = {
component: Instance["component"];
cssRules: Array<CssRule>;
browserStyle: Style;
- props: InstanceProps;
+ props?: InstanceProps;
};
export type HoveredInstanceData = {
diff --git a/packages/react-sdk/src/user-props/types.ts b/packages/react-sdk/src/user-props/types.ts
index 79175a0e65..6a6cf0b8a3 100644
--- a/packages/react-sdk/src/user-props/types.ts
+++ b/packages/react-sdk/src/user-props/types.ts
@@ -2,9 +2,8 @@ import type { InstanceProps, Instance } from "../db";
import { UserProp } from "./schema";
export type UserPropsUpdates = {
- treeId: InstanceProps["treeId"];
- propsId: InstanceProps["id"];
instanceId: Instance["id"];
+ propsId?: InstanceProps["id"];
updates: Array<UserProp>;
};