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.

Move props updates to designer (#784)

Bogdan Chadkin35354522d999
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index 5336d0346d..ddb11be0e9 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -27,7 +27,6 @@ import { useManageDesignModeStyles, GlobalStyles } from "./shared/styles";
import { useTrackSelectedElement } from "./shared/use-track-selected-element";
import { WrapperComponentDev } from "./features/wrapper-component";
import { useSync } from "./shared/sync";
-import { useManageProps } from "./shared/props";
import {
rootInstanceContainer,
useBreakpoints,
@@ -133,7 +132,6 @@ type DesignModeProps = {
const DesignMode = ({ treeId, buildId }: DesignModeProps) => {
useManageDesignModeStyles();
- useManageProps();
usePublishSelectedInstanceData();
useInsertInstance();
useReparentInstance();
diff --git a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
index 5aaeef8a11..45b5c79e42 100644
--- a/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
+++ b/apps/designer/app/canvas/features/wrapper-component/selected-instance-connector.ts
@@ -90,8 +90,6 @@ export const SelectedInstanceConnector = ({
// recompute inline elements on tree changes
unsubscribeTreeChange = subscribeAll((type) => {
if (
- type === "updateProps" ||
- type === "deleteProp" ||
type === "insertInstance" ||
type === "deleteInstance" ||
type === "reparentInstance"
diff --git a/apps/designer/app/canvas/shared/props.ts b/apps/designer/app/canvas/shared/props.ts
deleted file mode 100644
index d011a1928d..0000000000
--- a/apps/designer/app/canvas/shared/props.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-import { allUserPropsContainer } from "@webstudio-is/react-sdk";
-import { useSubscribe } from "~/shared/pubsub";
-import store from "immerhin";
-import {
- updateAllUserPropsMutable,
- deletePropMutable,
-} from "~/shared/props-utils";
-
-export const useManageProps = () => {
- useSubscribe("updateProps", (userPropsUpdates) => {
- store.createTransaction([allUserPropsContainer], (allUserProps) => {
- updateAllUserPropsMutable(allUserProps, userPropsUpdates);
- });
- });
-
- useSubscribe("deleteProp", (deleteProp) => {
- store.createTransaction([allUserPropsContainer], (allUserProps) => {
- deletePropMutable(allUserProps, deleteProp);
- });
- });
-};
diff --git a/apps/designer/app/designer/features/props-panel/props-panel.tsx b/apps/designer/app/designer/features/props-panel/props-panel.tsx
index 5144de5a3c..f7c57b9014 100644
--- a/apps/designer/app/designer/features/props-panel/props-panel.tsx
+++ b/apps/designer/app/designer/features/props-panel/props-panel.tsx
@@ -1,5 +1,7 @@
import { useState } from "react";
+import store from "immerhin";
import {
+ allUserPropsContainer,
getComponentMetaProps,
type Instance,
type UserProp,
@@ -28,6 +30,10 @@ import {
ExclamationTriangleIcon,
ChevronDownIcon,
} from "@webstudio-is/icons";
+import {
+ removeByMutable,
+ replaceByOrAppendMutable,
+} from "~/shared/array-utils";
import { usePropsLogic, type UserPropValue } from "./use-props-logic";
import {
useStyleData,
@@ -231,7 +237,36 @@ export const PropsPanel = ({
handleChangePropValue,
handleDeleteProp,
isRequired,
- } = usePropsLogic({ selectedInstanceData, publish });
+ } = usePropsLogic({
+ selectedInstanceData,
+
+ updateProps: (updates) => {
+ store.createTransaction([allUserPropsContainer], (allUserProps) => {
+ const instanceId = selectedInstanceData.id;
+ let props = allUserProps[instanceId];
+ if (props === undefined) {
+ props = [];
+ allUserProps[instanceId] = props;
+ }
+ for (const update of updates) {
+ replaceByOrAppendMutable(
+ props,
+ update,
+ (item) => item.id === update.id
+ );
+ }
+ });
+ },
+
+ deleteProp: (id) => {
+ store.createTransaction([allUserPropsContainer], (allUserProps) => {
+ const props = allUserProps[selectedInstanceData.id];
+ if (props) {
+ removeByMutable(props, (prop) => prop.id === id);
+ }
+ });
+ },
+ });
const { setProperty: setCssProperty } = useStyleData({
selectedInstanceData,
diff --git a/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts b/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
index 3f0be5efab..4a762e2bc3 100644
--- a/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
+++ b/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
@@ -26,7 +26,8 @@ describe("usePropsLogic", () => {
const { result } = renderHook(() =>
usePropsLogic({
selectedInstanceData: getSelectedInstanceData("Link", []),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
expect(result.current.userProps.length).toEqual(1);
@@ -40,13 +41,15 @@ describe("usePropsLogic", () => {
const { result: res1 } = renderHook(() =>
usePropsLogic({
selectedInstanceData: getSelectedInstanceData("Heading", []),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
const { result: res2 } = renderHook(() =>
usePropsLogic({
selectedInstanceData: getSelectedInstanceData("Button", []),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
expect(res1.current.userProps[0]).toMatchObject({
@@ -63,7 +66,8 @@ describe("usePropsLogic", () => {
const { result } = renderHook(() =>
usePropsLogic({
selectedInstanceData: getSelectedInstanceData("Button", []),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
expect(result.current.userProps.length).toEqual(1);
@@ -84,7 +88,8 @@ describe("usePropsLogic", () => {
value: "submit",
},
]),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
expect(result.current.userProps.length).toEqual(1);
@@ -98,7 +103,8 @@ describe("usePropsLogic", () => {
const { result } = renderHook(() =>
usePropsLogic({
selectedInstanceData: getSelectedInstanceData("Box", []),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
@@ -130,7 +136,8 @@ describe("usePropsLogic", () => {
value: true,
},
]),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
@@ -170,7 +177,8 @@ describe("usePropsLogic", () => {
value: true,
},
]),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
@@ -223,7 +231,8 @@ describe("usePropsLogic", () => {
required: true,
},
]),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
@@ -273,7 +282,8 @@ describe("usePropsLogic", () => {
required: true,
},
]),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
@@ -320,7 +330,8 @@ describe("usePropsLogic", () => {
required: true,
},
]),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
@@ -363,7 +374,8 @@ describe("usePropsLogic", () => {
required: true,
},
]),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
@@ -492,7 +504,8 @@ describe("usePropsLogic", () => {
required: true,
},
]),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
@@ -517,7 +530,8 @@ describe("usePropsLogic", () => {
const { result } = renderHook(() =>
usePropsLogic({
selectedInstanceData: getSelectedInstanceData("Image", []),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
@@ -536,7 +550,8 @@ describe("usePropsLogic", () => {
const { result } = renderHook(() =>
usePropsLogic({
selectedInstanceData: getSelectedInstanceData("Image", []),
- publish: jest.fn(),
+ updateProps: jest.fn(),
+ deleteProp: jest.fn(),
})
);
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 3946e495ab..6d02deb559 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
@@ -1,10 +1,4 @@
-import type {
- DeleteProp,
- UserProp,
- UserPropsUpdates,
- MetaProps,
-} from "@webstudio-is/react-sdk";
-import { type Publish } from "~/shared/pubsub";
+import { UserProp, MetaProps } from "@webstudio-is/react-sdk";
import {
getComponentMeta,
getComponentMetaProps,
@@ -17,13 +11,6 @@ import { useState } from "react";
import type { SelectedInstanceData } from "@webstudio-is/project";
import warnOnce from "warn-once";
-declare module "~/shared/pubsub" {
- export interface PubsubMap {
- deleteProp: DeleteProp;
- updateProps: UserPropsUpdates;
- }
-}
-
export type UserPropValue = UserProp extends infer T
? T extends { value: unknown; type: unknown }
? { value: T["value"]; type: T["type"] }
@@ -156,8 +143,9 @@ const getInitialProps = (
};
type UsePropsLogic = {
- publish: Publish;
selectedInstanceData: SelectedInstanceData;
+ updateProps: (updates: Array<UserProp>) => void;
+ deleteProp: (id: UserProp["id"]) => void;
};
/**
@@ -165,7 +153,8 @@ type UsePropsLogic = {
*/
export const usePropsLogic = ({
selectedInstanceData,
- publish,
+ updateProps,
+ deleteProp,
}: UsePropsLogic) => {
const props = selectedInstanceData.props ?? [];
@@ -184,26 +173,6 @@ export const usePropsLogic = ({
uniqBy([...requiredProps, ...props], "prop")
);
- const updateProps = (updates: UserPropsUpdates["updates"]) => {
- publish({
- type: "updateProps",
- payload: {
- instanceId: selectedInstanceData.id,
- updates,
- },
- });
- };
-
- const deleteProp = (id: UserProp["id"]) => {
- publish({
- type: "deleteProp",
- payload: {
- instanceId: selectedInstanceData.id,
- propId: id,
- },
- });
- };
-
const handleChangePropName: HandleChangePropName = (id, name) => {
const nextUserProps = produce((draft: Array<UserProp>) => {
const index = draft.findIndex((item) => item.id === id);
diff --git a/apps/designer/app/shared/props-utils/__snapshots__/update-props.test.ts.snap b/apps/designer/app/shared/props-utils/__snapshots__/update-props.test.ts.snap
deleted file mode 100644
index 0ceac3683b..0000000000
--- a/apps/designer/app/shared/props-utils/__snapshots__/update-props.test.ts.snap
+++ /dev/null
@@ -1,48 +0,0 @@
-// Jest Snapshot v1, https://goo.gl/fbAQLP
-
-exports[`Update props with added prop 1`] = `
-{
- "instanceId": [
- {
- "id": "propId",
- "prop": "a",
- "type": "string",
- "value": "1",
- },
- ],
-}
-`;
-
-exports[`Update props with empty updates 1`] = `
-{
- "instanceId": [],
-}
-`;
-
-exports[`Update props with renamed prop name 1`] = `
-{
- "instanceId": [
- {
- "id": "propId",
- "prop": "b",
- "required": undefined,
- "type": "string",
- "value": "1",
- },
- ],
-}
-`;
-
-exports[`Update props with updated prop value 1`] = `
-{
- "instanceId": [
- {
- "id": "propId",
- "prop": "a",
- "required": undefined,
- "type": "string",
- "value": "2",
- },
- ],
-}
-`;
diff --git a/apps/designer/app/shared/props-utils/delete-prop.test.ts b/apps/designer/app/shared/props-utils/delete-prop.test.ts
deleted file mode 100644
index ebe1756f2c..0000000000
--- a/apps/designer/app/shared/props-utils/delete-prop.test.ts
+++ /dev/null
@@ -1,65 +0,0 @@
-import { describe, test, expect } from "@jest/globals";
-import { type AllUserProps } from "@webstudio-is/react-sdk";
-import { deletePropMutable } from "./delete-prop";
-
-describe("Delete prop", () => {
- test("found and deleted", () => {
- const propsMap: AllUserProps = {
- instanceId: [
- { id: "propId", prop: "b", value: "1", type: "string" },
- { id: "propId1", prop: "a", value: "1", type: "string" },
- ],
- };
-
- const isDeleted = deletePropMutable(propsMap, {
- instanceId: "instanceId",
- propId: "propId",
- });
- expect(propsMap).toMatchInlineSnapshot(`
- {
- "instanceId": [
- {
- "id": "propId1",
- "prop": "a",
- "type": "string",
- "value": "1",
- },
- ],
- }
- `);
- expect(isDeleted).toBeTruthy();
- });
-
- test("not found and not deleted", () => {
- const propsMap: AllUserProps = {
- instanceId: [
- { id: "propId", prop: "b", value: "1", type: "string" },
- { id: "propId1", prop: "a", value: "1", type: "string" },
- ],
- };
-
- const isDeleted = deletePropMutable(propsMap, {
- instanceId: "instanceId",
- propId: "notFound",
- });
- expect(propsMap).toMatchInlineSnapshot(`
- {
- "instanceId": [
- {
- "id": "propId",
- "prop": "b",
- "type": "string",
- "value": "1",
- },
- {
- "id": "propId1",
- "prop": "a",
- "type": "string",
- "value": "1",
- },
- ],
- }
- `);
- expect(isDeleted).toBeFalsy();
- });
-});
diff --git a/apps/designer/app/shared/props-utils/delete-prop.ts b/apps/designer/app/shared/props-utils/delete-prop.ts
deleted file mode 100644
index 7f2861057a..0000000000
--- a/apps/designer/app/shared/props-utils/delete-prop.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { type AllUserProps, type DeleteProp } from "@webstudio-is/react-sdk";
-
-export const deletePropMutable = (
- allProps: AllUserProps,
- deleteProp: DeleteProp
-) => {
- const props = allProps[deleteProp.instanceId];
- if (props === undefined) {
- return false;
- }
- const index = props.findIndex(({ id }) => id === deleteProp.propId);
- if (index === -1) {
- return false;
- }
- props.splice(index, 1);
- return true;
-};
diff --git a/apps/designer/app/shared/props-utils/index.ts b/apps/designer/app/shared/props-utils/index.ts
deleted file mode 100644
index e76c58df03..0000000000
--- a/apps/designer/app/shared/props-utils/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from "./update-props";
-export * from "./delete-prop";
diff --git a/apps/designer/app/shared/props-utils/update-props.test.ts b/apps/designer/app/shared/props-utils/update-props.test.ts
deleted file mode 100644
index 8e7b95aa53..0000000000
--- a/apps/designer/app/shared/props-utils/update-props.test.ts
+++ /dev/null
@@ -1,90 +0,0 @@
-import { describe, test, expect } from "@jest/globals";
-import { Asset } from "@webstudio-is/asset-uploader";
-import {
- type AllUserProps,
- type UserPropsUpdates,
-} from "@webstudio-is/react-sdk";
-import { updateAllUserPropsMutable } from "./update-props";
-
-describe("Update props", () => {
- test("with empty updates", () => {
- const propsMap: AllUserProps = {
- instanceId: [],
- };
- const update: UserPropsUpdates = {
- instanceId: "instanceId",
- updates: [],
- };
- updateAllUserPropsMutable(propsMap, update);
- expect(propsMap).toMatchSnapshot();
- });
-
- test("with added prop", () => {
- const propsMap: AllUserProps = {
- instanceId: [],
- };
- const update: UserPropsUpdates = {
- instanceId: "instanceId",
- updates: [{ id: "propId", prop: "a", value: "1", type: "string" }],
- };
- updateAllUserPropsMutable(propsMap, update);
- expect(propsMap).toMatchSnapshot();
- });
-
- test("with updated prop value", () => {
- const propsMap: AllUserProps = {
- instanceId: [{ id: "propId", prop: "a", value: "1", type: "string" }],
- };
- const update: UserPropsUpdates = {
- instanceId: "instanceId",
- updates: [{ id: "propId", prop: "a", value: "2", type: "string" }],
- };
- updateAllUserPropsMutable(propsMap, update);
- expect(propsMap).toMatchSnapshot();
- });
-
- test("with renamed prop name", () => {
- const propsMap: AllUserProps = {
- instanceId: [{ id: "propId", prop: "a", value: "1", type: "string" }],
- };
- const update: UserPropsUpdates = {
- instanceId: "instanceId",
- updates: [{ id: "propId", prop: "b", value: "1", type: "string" }],
- };
- updateAllUserPropsMutable(propsMap, update);
- expect(propsMap).toMatchSnapshot();
- });
-
- test("with changed value to asset type", () => {
- const propsMap: AllUserProps = {
- instanceId: [{ id: "propId", prop: "a", value: "1", type: "string" }],
- };
- const update: UserPropsUpdates = {
- instanceId: "instanceId",
- updates: [
- {
- id: "propId",
- prop: "a",
- type: "asset",
- value: { id: "assetId" } as Asset,
- },
- ],
- };
- updateAllUserPropsMutable(propsMap, update);
- expect(propsMap).toMatchInlineSnapshot(`
- {
- "instanceId": [
- {
- "id": "propId",
- "prop": "a",
- "required": undefined,
- "type": "asset",
- "value": {
- "id": "assetId",
- },
- },
- ],
- }
- `);
- });
-});
diff --git a/apps/designer/app/shared/props-utils/update-props.ts b/apps/designer/app/shared/props-utils/update-props.ts
deleted file mode 100644
index b0cbc969b8..0000000000
--- a/apps/designer/app/shared/props-utils/update-props.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import {
- type AllUserProps,
- type UserPropsUpdates,
-} from "@webstudio-is/react-sdk";
-
-export const updateAllUserPropsMutable = (
- allUserProps: AllUserProps,
- { instanceId, updates }: UserPropsUpdates
-) => {
- if (allUserProps[instanceId] === undefined) {
- allUserProps[instanceId] = [];
- }
- const instanceProps = allUserProps[instanceId];
- for (const update of updates) {
- const propIndex = instanceProps.findIndex(({ id }) => id === update.id);
-
- if (propIndex === -1) {
- instanceProps.push(update);
- } else {
- const prop = instanceProps[propIndex];
-
- prop.prop = update.prop;
- prop.type = update.type;
- prop.value = update.value;
- prop.required = update.required;
- }
- }
-};
diff --git a/packages/react-sdk/src/user-props/index.ts b/packages/react-sdk/src/user-props/index.ts
index d8a42eebec..0c44558bc4 100644
--- a/packages/react-sdk/src/user-props/index.ts
+++ b/packages/react-sdk/src/user-props/index.ts
@@ -1,4 +1,3 @@
export * from "./all-user-props";
export * from "./use-user-props";
-export * from "./types";
export * from "./schema";
diff --git a/packages/react-sdk/src/user-props/types.ts b/packages/react-sdk/src/user-props/types.ts
deleted file mode 100644
index eae803c420..0000000000
--- a/packages/react-sdk/src/user-props/types.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import type { Instance } from "../db";
-import { UserProp } from "./schema";
-
-export type UserPropsUpdates = {
- instanceId: Instance["id"];
- updates: Array<UserProp>;
-};
-
-export type DeleteProp = {
- instanceId: Instance["id"];
- propId: UserProp["id"];
-};