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.

Migrate designer to normalized props (#868)

Bogdan Chadkin3f4cc6997625
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index a2a167fd3d..9e1acd68b7 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -27,9 +27,11 @@ import {
rootInstanceContainer,
selectedInstanceStore,
useBreakpoints,
+ useInstanceProps,
useRootInstance,
useSetBreakpoints,
useSetDesignTokens,
+ useSetProps,
useSetRootInstance,
useSetStyles,
useSubscribeScrollState,
@@ -95,13 +97,17 @@ const useAssets = (initialAssets: Array<Asset>) => {
};
const useCopyPaste = () => {
- const instance = useStore(selectedInstanceStore);
- const allUserProps = useAllUserProps();
-
- const selectedInstanceData = useMemo(
- () => instance && { instance, props: allUserProps[instance.id] },
- [allUserProps, instance]
- );
+ const selectedInstance = useStore(selectedInstanceStore);
+ const instanceProps = useInstanceProps(selectedInstance?.id);
+
+ const selectedInstanceData = useMemo(() => {
+ if (selectedInstance) {
+ return {
+ instance: selectedInstance,
+ props: instanceProps,
+ };
+ }
+ }, [selectedInstance, instanceProps]);
// We need to initialize this in both canvas and designer,
// because the events will fire in either one, depending on where the focus is
@@ -150,6 +156,7 @@ export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {
useSetBreakpoints(data.breakpoints);
useSetDesignTokens(data.designTokens);
useAllUserProps(data.props);
+ useSetProps(data.tree.props);
useSetStyles(data.tree.styles);
useSetRootInstance(data.tree.root);
setParams(data.params ?? null);
diff --git a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
index d6567fa4fa..b520943dea 100644
--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -8,12 +8,12 @@ import {
renderWrapperComponentChildren,
getComponent,
idAttribute,
- useAllUserProps,
} from "@webstudio-is/react-sdk";
import { shallowComputed } from "~/shared/store-utils";
import {
selectedInstanceIdStore,
stylesIndexStore,
+ useInstanceProps,
useTextEditingInstanceId,
} from "~/shared/nano-states";
import { useCssRules } from "~/canvas/shared/styles";
@@ -70,8 +70,7 @@ export const WrapperComponentDev = ({
useTextEditingInstanceId();
const selectedInstanceId = useStore(selectedInstanceIdStore);
- const allUserProps = useAllUserProps();
- const instanceProps = allUserProps[instance.id];
+ const instanceProps = useInstanceProps(instance.id);
const userProps = useMemo(() => {
const result: UserProps = {};
if (instanceProps === undefined) {
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index a8b2d8e393..a85dc09f73 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -9,6 +9,7 @@ import { useSubscribe } from "~/shared/pubsub";
import { utils, type InstanceInsertionSpec } from "@webstudio-is/project";
import store from "immerhin";
import {
+ propsStore,
rootInstanceContainer,
selectedInstanceIdStore,
useRootInstance,
@@ -58,28 +59,32 @@ export const findInsertLocation = (
};
export const useInsertInstance = () => {
- useSubscribe("insertInstance", ({ instance, dropTarget, props }) => {
- const selectedInstanceId = selectedInstanceIdStore.get();
- store.createTransaction(
- [rootInstanceContainer, allUserPropsContainer],
- (rootInstance, allUserProps) => {
- if (rootInstance === undefined) {
- return;
- }
- const hasInserted = utils.tree.insertInstanceMutable(
- rootInstance,
- instance,
- dropTarget ?? findInsertLocation(rootInstance, selectedInstanceId)
- );
- if (hasInserted) {
- selectedInstanceIdStore.set(instance.id);
+ useSubscribe(
+ "insertInstance",
+ ({ instance, dropTarget, props: insertedProps }) => {
+ const selectedInstanceId = selectedInstanceIdStore.get();
+ store.createTransaction(
+ [rootInstanceContainer, allUserPropsContainer, propsStore],
+ (rootInstance, allUserProps, props) => {
+ if (rootInstance === undefined) {
+ return;
+ }
+ const hasInserted = utils.tree.insertInstanceMutable(
+ rootInstance,
+ instance,
+ dropTarget ?? findInsertLocation(rootInstance, selectedInstanceId)
+ );
+ if (hasInserted) {
+ selectedInstanceIdStore.set(instance.id);
+ }
+ if (insertedProps !== undefined) {
+ allUserProps[instance.id] = insertedProps;
+ props.push(...insertedProps);
+ }
}
- if (props !== undefined) {
- allUserProps[instance.id] = props;
- }
- }
- );
- });
+ );
+ }
+ );
};
export const useReparentInstance = () => {
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index 5725df4acb..c4d55caa39 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -6,7 +6,7 @@ import {
type Project,
utils as projectUtils,
} from "@webstudio-is/project";
-import { Box, type CSS, Flex, Grid } from "@webstudio-is/design-system";
+import { theme, Box, type CSS, Flex, Grid } from "@webstudio-is/design-system";
import { registerContainers, useDesignerStore } from "~/shared/sync";
import { useSyncServer } from "./shared/sync-server";
// eslint-disable-next-line import/no-internal-modules
@@ -31,6 +31,7 @@ import { usePublishShortcuts } from "./shared/shortcuts";
import {
selectedInstanceStore,
useDragAndDropState,
+ useInstanceProps,
useIsPreviewMode,
} from "~/shared/nano-states";
import { useClientSettings } from "./shared/client-settings";
@@ -38,8 +39,6 @@ import { Navigator } from "./features/sidebar-left";
import { getBuildUrl } from "~/shared/router-utils";
import { useInstanceCopyPaste } from "~/shared/copy-paste";
import { AssetsProvider, usePublishAssets } from "./shared/assets";
-import { useAllUserProps } from "@webstudio-is/react-sdk";
-import { theme } from "@webstudio-is/design-system";
registerContainers();
export const links = () => {
@@ -85,16 +84,16 @@ const useSubscribeCanvasReady = (publish: Publish) => {
const useCopyPaste = (publish: Publish) => {
const selectedInstance = useStore(selectedInstanceStore);
- const allUserProps = useAllUserProps();
+ const instanceProps = useInstanceProps(selectedInstance?.id);
const selectedInstanceData = useMemo(() => {
if (selectedInstance) {
return {
instance: selectedInstance,
- props: allUserProps[selectedInstance.id] ?? [],
+ props: instanceProps,
};
}
- }, [selectedInstance, allUserProps]);
+ }, [selectedInstance, instanceProps]);
// We need to initialize this in both canvas and designer,
// because the events will fire in either one, depending on where the focus is
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 adefe184be..bdf2a11c61 100644
--- a/apps/designer/app/designer/features/props-panel/props-panel.tsx
+++ b/apps/designer/app/designer/features/props-panel/props-panel.tsx
@@ -4,12 +4,13 @@ import type { Instance, PropsItem } from "@webstudio-is/project-build";
import {
allUserPropsContainer,
getComponentMetaProps,
- useAllUserProps,
} from "@webstudio-is/react-sdk";
import { type Publish } from "~/shared/pubsub";
+import { propsStore, useInstanceProps } from "~/shared/nano-states";
import { Control } from "./control";
import { CollapsibleSection, ComponentInfo } from "~/designer/shared/inspector";
import {
+ theme,
Box,
Button,
Grid,
@@ -38,7 +39,6 @@ import {
useStyleData,
type SetProperty,
} from "../style-panel/shared/use-style-data";
-import { theme } from "@webstudio-is/design-system";
type ComboboxProps = {
isReadonly: boolean;
@@ -222,8 +222,7 @@ type PropsPanelProps = {
export const PropsPanel = ({ selectedInstance, publish }: PropsPanelProps) => {
const instanceId = selectedInstance.id;
- const allUserProps = useAllUserProps();
- const props = allUserProps[instanceId] ?? [];
+ const instanceProps = useInstanceProps(instanceId);
const {
userProps,
@@ -233,31 +232,43 @@ export const PropsPanel = ({ selectedInstance, publish }: PropsPanelProps) => {
handleDeleteProp,
isRequired,
} = usePropsLogic({
- props,
+ props: instanceProps,
selectedInstance,
updateProps: (update) => {
- store.createTransaction([allUserPropsContainer], (allUserProps) => {
- let props = allUserProps[instanceId];
- if (props === undefined) {
- props = [];
- allUserProps[instanceId] = props;
+ store.createTransaction(
+ [allUserPropsContainer, propsStore],
+ (allUserProps, props) => {
+ let instanceProps = allUserProps[instanceId];
+ if (instanceProps === undefined) {
+ instanceProps = [];
+ allUserProps[instanceId] = instanceProps;
+ }
+ replaceByOrAppendMutable(
+ instanceProps,
+ update,
+ (item) => item.id === update.id
+ );
+ replaceByOrAppendMutable(
+ props,
+ update,
+ (item) => item.id === update.id
+ );
}
- replaceByOrAppendMutable(
- props,
- update,
- (item) => item.id === update.id
- );
- });
+ );
},
deleteProp: (id) => {
- store.createTransaction([allUserPropsContainer], (allUserProps) => {
- const props = allUserProps[instanceId];
- if (props) {
+ store.createTransaction(
+ [allUserPropsContainer, propsStore],
+ (allUserProps, props) => {
+ const instanceProps = allUserProps[instanceId];
+ if (instanceProps) {
+ removeByMutable(instanceProps, (prop) => prop.id === id);
+ }
removeByMutable(props, (prop) => prop.id === id);
}
- });
+ );
},
});
diff --git a/apps/designer/app/shared/nano-states/nano-states.ts b/apps/designer/app/shared/nano-states/nano-states.ts
index a9a3a36989..b00d23dd15 100644
--- a/apps/designer/app/shared/nano-states/nano-states.ts
+++ b/apps/designer/app/shared/nano-states/nano-states.ts
@@ -1,6 +1,7 @@
+import { useMemo } from "react";
import { atom, computed, type WritableAtom } from "nanostores";
import { useStore } from "@nanostores/react";
-import type { Instance, Styles } from "@webstudio-is/project-build";
+import type { Instance, Props, Styles } from "@webstudio-is/project-build";
import type {
DropTargetChangePayload,
DragStartPayload,
@@ -8,6 +9,7 @@ import type {
import type { Breakpoint, Style } from "@webstudio-is/css-data";
import type { DesignToken } from "@webstudio-is/design-tokens";
import { useSyncInitializeOnce } from "../hook-utils";
+import { shallowComputed } from "../store-utils";
const useValue = <T>(atom: WritableAtom<T>) => {
const value = useStore(atom);
@@ -42,6 +44,40 @@ export const instancesIndexStore = computed(
}
);
+export const propsStore = atom<Props>([]);
+export const propsIndexStore = computed(propsStore, (props) => {
+ const propsByInstanceId = new Map<Instance["id"], Props>();
+ for (const prop of props) {
+ const { instanceId } = prop;
+ let instanceProps = propsByInstanceId.get(instanceId);
+ if (instanceProps === undefined) {
+ instanceProps = [];
+ propsByInstanceId.set(instanceId, instanceProps);
+ }
+ instanceProps.push(prop);
+ }
+ return {
+ propsByInstanceId,
+ };
+});
+export const useSetProps = (props: Props) => {
+ useSyncInitializeOnce(() => {
+ propsStore.set(props);
+ });
+};
+export const useInstanceProps = (instanceId: undefined | Instance["id"]) => {
+ const instancePropsStore = useMemo(() => {
+ return shallowComputed([propsIndexStore], (propsIndex) => {
+ if (instanceId === undefined) {
+ return [];
+ }
+ return propsIndex.propsByInstanceId.get(instanceId) ?? [];
+ });
+ }, [instanceId]);
+ const instanceProps = useStore(instancePropsStore);
+ return instanceProps;
+};
+
export const stylesContainer = atom<Styles>([]);
/**
* Indexed styles data is recomputed on every styles update
diff --git a/apps/designer/app/shared/sync/sync-stores.ts b/apps/designer/app/shared/sync/sync-stores.ts
index 01a144a7ad..9d8cfef589 100644
--- a/apps/designer/app/shared/sync/sync-stores.ts
+++ b/apps/designer/app/shared/sync/sync-stores.ts
@@ -5,6 +5,7 @@ import { allUserPropsContainer } from "@webstudio-is/react-sdk";
import { type Publish, subscribe } from "~/shared/pubsub";
import {
rootInstanceContainer,
+ propsStore,
breakpointsContainer,
designTokensContainer,
stylesContainer,
@@ -44,6 +45,7 @@ export const registerContainers = () => {
store.register("root", rootInstanceContainer);
store.register("styles", stylesContainer);
store.register("props", allUserPropsContainer);
+ store.register("props-modern", propsStore);
store.register("designTokens", designTokensContainer);
// synchronize whole states
clientStores.set("selectedInstanceId", selectedInstanceIdStore);