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.

Switch canvas and published rendering to normalized instances (#1422)

Bogdan Chadkind4753f0a2689
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsx
index d12ded0dd3..89acff28ff 100644
--- a/apps/builder/app/canvas/canvas.tsx
+++ b/apps/builder/app/canvas/canvas.tsx
@@ -2,7 +2,7 @@ import { useMemo, Fragment, useEffect } from "react";
import { useStore } from "@nanostores/react";
import { computed } from "nanostores";
import type { Params } from "@webstudio-is/react-sdk";
-import type { Instance, Page } from "@webstudio-is/project-build";
+import type { Instances, Page } from "@webstudio-is/project-build";
import {
createElementsTree,
registerComponents,
@@ -24,7 +24,6 @@ import {
propsIndexStore,
assetsStore,
pagesStore,
- useRootInstance,
instancesStore,
useIsPreviewMode,
selectedPageStore,
@@ -44,21 +43,28 @@ const propsByInstanceIdStore = computed(
(propsIndex) => propsIndex.propsByInstanceId
);
-const temporaryRootInstance: Instance = {
- type: "instance",
- id: "temporaryRootInstance",
- component: "Body",
- children: [],
-};
+const temporaryRootInstanceId = "temporaryRootInstance";
+const temporaryInstances: Instances = new Map([
+ [
+ temporaryRootInstanceId,
+ {
+ type: "instance",
+ id: temporaryRootInstanceId,
+ component: "Body",
+ children: [],
+ },
+ ],
+]);
const useElementsTree = (getComponent: GetComponent) => {
- const [rootInstance] = useRootInstance();
+ const instances = useStore(instancesStore);
+ const page = useStore(selectedPageStore);
+ const rootInstanceId = page?.rootInstanceId;
if (typeof window === "undefined") {
// @todo remove after https://github.com/webstudio-is/webstudio-builder/issues/1313 now its needed to be sure that no leaks exists
// eslint-disable-next-line no-console
console.log({
- rootInstance,
assetsStore: assetsStore.get().size,
pagesStore: pagesStore.get()?.pages.length ?? 0,
instancesStore: instancesStore.get().size,
@@ -81,16 +87,17 @@ const useElementsTree = (getComponent: GetComponent) => {
return useMemo(() => {
return createElementsTree({
sandbox: true,
+ instances: instances.size === 0 ? temporaryInstances : instances,
// fallback to temporary root instance to render scripts
// and receive real data from builder
- instance: rootInstance ?? temporaryRootInstance,
+ rootInstanceId: rootInstanceId ?? temporaryRootInstanceId,
propsByInstanceIdStore,
assetsStore,
pagesStore: pagesMapStore,
Component: WebstudioComponentDev,
getComponent,
});
- }, [rootInstance, getComponent, pagesMapStore]);
+ }, [instances, rootInstanceId, getComponent, pagesMapStore]);
};
const DesignMode = () => {
diff --git a/apps/builder/app/canvas/features/webstudio-component/selected-instance-connector.ts b/apps/builder/app/canvas/features/webstudio-component/selected-instance-connector.ts
index 8237cef768..db16e8adf7 100644
--- a/apps/builder/app/canvas/features/webstudio-component/selected-instance-connector.ts
+++ b/apps/builder/app/canvas/features/webstudio-component/selected-instance-connector.ts
@@ -1,6 +1,11 @@
import { useEffect } from "react";
import { useStore } from "@nanostores/react";
-import type { Instance, Prop, StyleDecl } from "@webstudio-is/project-build";
+import type {
+ Instance,
+ InstancesItem,
+ Prop,
+ StyleDecl,
+} from "@webstudio-is/project-build";
import { getBrowserStyle, idAttribute } from "@webstudio-is/react-sdk";
import { subscribe } from "~/shared/pubsub";
import { subscribeWindowResize } from "~/shared/dom-hooks";
@@ -72,7 +77,7 @@ export const SelectedInstanceConnector = ({
instanceProps,
}: {
instanceElementRef: { current: undefined | HTMLElement };
- instance: Instance;
+ instance: InstancesItem;
instanceStyles: StyleDecl[];
instanceProps: undefined | Prop[];
}) => {
diff --git a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
index 1b95fa60c5..82b7d3990b 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -8,6 +8,7 @@ import {
type Prop,
findTreeInstanceIds,
Instances,
+ type InstancesItem,
} from "@webstudio-is/project-build";
import {
renderWebstudioComponentChildren,
@@ -87,7 +88,7 @@ const getInstanceSelector = (
};
type WebstudioComponentDevProps = {
- instance: Instance;
+ instance: InstancesItem;
instanceSelector: InstanceSelector;
children: Array<JSX.Element | string>;
getComponent: GetComponent;
diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.ts
index d455b89885..60a085ddc7 100644
--- a/apps/builder/app/shared/nano-states/nano-states.ts
+++ b/apps/builder/app/shared/nano-states/nano-states.ts
@@ -25,11 +25,7 @@ import { shallowComputed } from "../store-utils";
import { type InstanceSelector } from "../tree-utils";
import type { htmlTags as HtmlTags } from "html-tags";
import { breakpointsStore } from "./breakpoints";
-import {
- instancesStore,
- rootInstanceContainer,
- selectedInstanceSelectorStore,
-} from "./instances";
+import { instancesStore, selectedInstanceSelectorStore } from "./instances";
import { selectedPageStore } from "./pages";
import type { UnitSizes } from "~/builder/features/style-panel/shared/css-value-input/convert-units";
@@ -48,11 +44,6 @@ export const rootInstanceStore = computed(
}
);
-export const useRootInstance = () => {
- const value = useStore(rootInstanceContainer);
- return [value] as const;
-};
-
export const propsStore = atom<Props>(new Map());
export const propsIndexStore = computed(propsStore, (props) => {
const propsByInstanceId = new Map<Instance["id"], Prop[]>();
diff --git a/packages/react-sdk/src/tree/create-elements-tree.tsx b/packages/react-sdk/src/tree/create-elements-tree.tsx
index 88162e2e9e..c2b146f30f 100644
--- a/packages/react-sdk/src/tree/create-elements-tree.tsx
+++ b/packages/react-sdk/src/tree/create-elements-tree.tsx
@@ -2,7 +2,11 @@ import { type ComponentProps, Fragment } from "react";
import type { ReadableAtom } from "nanostores";
import { Scripts, ScrollRestoration } from "@remix-run/react";
import type { Assets } from "@webstudio-is/asset-uploader";
-import type { Instance } from "@webstudio-is/project-build";
+import type {
+ Instance,
+ Instances,
+ InstancesItem,
+} from "@webstudio-is/project-build";
import type { GetComponent } from "../components/components-utils";
import { ReactSdkContext } from "../context";
import type { Pages, PropsByInstanceId } from "../props";
@@ -12,32 +16,40 @@ import { SessionStoragePolyfill } from "./session-storage-polyfill";
type InstanceSelector = Instance["id"][];
export const createElementsTree = ({
+ instances,
+ rootInstanceId,
sandbox,
- instance,
propsByInstanceIdStore,
assetsStore,
pagesStore,
Component,
getComponent,
}: {
+ instances: Instances;
+ rootInstanceId: Instance["id"];
sandbox?: boolean;
- instance: Instance;
propsByInstanceIdStore: ReadableAtom<PropsByInstanceId>;
assetsStore: ReadableAtom<Assets>;
pagesStore: ReadableAtom<Pages>;
Component: (props: ComponentProps<typeof WebstudioComponent>) => JSX.Element;
getComponent: GetComponent;
}) => {
- const rootInstanceSelector = [instance.id];
+ const rootInstance = instances.get(rootInstanceId);
+ if (rootInstance === undefined) {
+ return null;
+ }
+
+ const rootInstanceSelector = [rootInstanceId];
const children = createInstanceChildrenElements({
+ instances,
instanceSelector: rootInstanceSelector,
Component,
- children: instance.children,
+ children: rootInstance.children,
getComponent,
});
const root = createInstanceElement({
Component,
- instance,
+ instance: rootInstance,
instanceSelector: rootInstanceSelector,
children: [
<Fragment key="children">
@@ -59,13 +71,15 @@ export const createElementsTree = ({
};
const createInstanceChildrenElements = ({
+ instances,
instanceSelector,
children,
Component,
getComponent,
}: {
+ instances: Instances;
instanceSelector: InstanceSelector;
- children: Instance["children"];
+ children: InstancesItem["children"];
Component: (props: ComponentProps<typeof WebstudioComponent>) => JSX.Element;
getComponent: GetComponent;
}) => {
@@ -75,15 +89,20 @@ const createInstanceChildrenElements = ({
elements.push(child.value);
continue;
}
- const childInstanceSelector = [child.id, ...instanceSelector];
+ const childInstance = instances.get(child.value);
+ if (childInstance === undefined) {
+ continue;
+ }
+ const childInstanceSelector = [child.value, ...instanceSelector];
const children = createInstanceChildrenElements({
+ instances,
instanceSelector: childInstanceSelector,
- children: child.children,
+ children: childInstance.children,
Component,
getComponent,
});
const element = createInstanceElement({
- instance: child,
+ instance: childInstance,
instanceSelector: childInstanceSelector,
Component,
children,
@@ -101,7 +120,7 @@ const createInstanceElement = ({
children = [],
getComponent,
}: {
- instance: Instance;
+ instance: InstancesItem;
instanceSelector: InstanceSelector;
Component: (props: ComponentProps<typeof WebstudioComponent>) => JSX.Element;
children?: Array<JSX.Element | string>;
diff --git a/packages/react-sdk/src/tree/root.ts b/packages/react-sdk/src/tree/root.ts
index fd151a1d17..f13e8dd6a7 100644
--- a/packages/react-sdk/src/tree/root.ts
+++ b/packages/react-sdk/src/tree/root.ts
@@ -1,12 +1,6 @@
import type { ComponentProps } from "react";
import { atom } from "nanostores";
-import type {
- Build,
- Instance,
- Instances,
- InstancesItem,
- Page,
-} from "@webstudio-is/project-build";
+import type { Build, Page } from "@webstudio-is/project-build";
import type { Asset } from "@webstudio-is/asset-uploader";
import { createElementsTree } from "./create-elements-tree";
import { WebstudioComponent } from "./webstudio-component";
@@ -35,36 +29,6 @@ type RootProps = {
getComponent: GetComponent;
};
-const denormalizeTree = (
- instances: Instances,
- rootInstanceId: Instance["id"]
-) => {
- const convertTree = (instance: InstancesItem) => {
- const legacyInstance: Instance = {
- type: "instance",
- id: instance.id,
- component: instance.component,
- children: [],
- };
- for (const child of instance.children) {
- if (child.type === "id") {
- const childInstance = instances.get(child.value);
- if (childInstance) {
- legacyInstance.children.push(convertTree(childInstance));
- }
- } else {
- legacyInstance.children.push(child);
- }
- }
- return legacyInstance;
- };
- const rootInstance = instances.get(rootInstanceId);
- if (rootInstance === undefined) {
- return undefined;
- }
- return convertTree(rootInstance);
-};
-
export const InstanceRoot = ({
data,
Component,
@@ -72,17 +36,10 @@ export const InstanceRoot = ({
getComponent,
}: RootProps): JSX.Element | null => {
setParams(data.params ?? null);
-
registerComponents(customComponents);
- const instance = denormalizeTree(
- new Map(data.build.instances),
- data.page.rootInstanceId
- );
- if (instance === undefined) {
- return null;
- }
return createElementsTree({
- instance,
+ instances: new Map(data.build.instances),
+ rootInstanceId: data.page.rootInstanceId,
propsByInstanceIdStore: atom(
getPropsByInstanceId(new Map(data.build.props))
),
diff --git a/packages/react-sdk/src/tree/webstudio-component.tsx b/packages/react-sdk/src/tree/webstudio-component.tsx
index e07247b240..bdf1410357 100644
--- a/packages/react-sdk/src/tree/webstudio-component.tsx
+++ b/packages/react-sdk/src/tree/webstudio-component.tsx
@@ -1,5 +1,5 @@
import { Fragment } from "react";
-import type { Instance } from "@webstudio-is/project-build";
+import type { Instance, InstancesItem } from "@webstudio-is/project-build";
import type { GetComponent } from "../components/components-utils";
import { useInstanceProps } from "../props";
@@ -27,7 +27,7 @@ export const renderWebstudioComponentChildren = (
};
type WebstudioComponentProps = {
- instance: Instance;
+ instance: InstancesItem;
instanceSelector: Instance["id"][];
children: Array<JSX.Element | string>;
getComponent: GetComponent;