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.

Insert styles from embed template (#1538)

Bogdan Chadkin5595742f75fc
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index a33e009b5d..b3b9155c29 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -9,6 +9,7 @@ import {
instancesStore,
selectedStyleSourceSelectorStore,
textEditingInstanceSelectorStore,
+ breakpointsStore,
} from "./nano-states";
import {
type DroppableTarget,
@@ -19,25 +20,61 @@ import {
reparentInstanceMutable,
getAncestorInstanceSelector,
insertPropsCopyMutable,
+ insertStyleSourcesCopyMutable,
+ insertStyleSourceSelectionsCopyMutable,
+ insertStylesCopyMutable,
} from "./tree-utils";
import { removeByMutable } from "./array-utils";
+import { isBaseBreakpoint } from "./breakpoints";
export const insertNewComponentInstance = (
component: string,
dropTarget: DroppableTarget
) => {
- const { instances: insertedInstances, props: insertedProps } =
- createComponentInstance(component);
+ const breakpoints = breakpointsStore.get();
+ const breakpointValues = Array.from(breakpoints.values());
+ const baseBreakpoint = breakpointValues.find(isBaseBreakpoint);
+ if (baseBreakpoint === undefined) {
+ return;
+ }
+ const {
+ instances: insertedInstances,
+ props: insertedProps,
+ styleSourceSelections: insertedStyleSourceSelections,
+ styleSources: insertedStyleSources,
+ styles: insertedStyles,
+ } = createComponentInstance(component, baseBreakpoint.id);
const rootInstanceId = insertedInstances[0].id;
- store.createTransaction([instancesStore, propsStore], (instances, props) => {
- insertInstancesMutable(
- instances,
- insertedInstances,
- [rootInstanceId],
- dropTarget
- );
- insertPropsCopyMutable(props, insertedProps, new Map());
- });
+ store.createTransaction(
+ [
+ instancesStore,
+ propsStore,
+ styleSourceSelectionsStore,
+ styleSourcesStore,
+ stylesStore,
+ ],
+ (instances, props, styleSourceSelections, styleSources, styles) => {
+ insertInstancesMutable(
+ instances,
+ insertedInstances,
+ [rootInstanceId],
+ dropTarget
+ );
+ insertPropsCopyMutable(props, insertedProps, new Map());
+ insertStyleSourcesCopyMutable(
+ styleSources,
+ insertedStyleSources,
+ new Set()
+ );
+ insertStyleSourceSelectionsCopyMutable(
+ styleSourceSelections,
+ insertedStyleSourceSelections,
+ new Map(),
+ new Map()
+ );
+ insertStylesCopyMutable(styles, insertedStyles, new Map(), new Map());
+ }
+ );
selectedInstanceSelectorStore.set([
rootInstanceId,
diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.ts
index a3fd1a8d31..c754af1791 100644
--- a/apps/builder/app/shared/tree-utils.ts
+++ b/apps/builder/app/shared/tree-utils.ts
@@ -51,10 +51,21 @@ export const areInstanceSelectorsEqual = (
return left.join(",") === right.join(",");
};
-export const createComponentInstance = (component: Instance["component"]) => {
+export const createComponentInstance = (
+ component: Instance["component"],
+ defaultBreakpointId: Breakpoint["id"]
+) => {
const componentMeta = getComponentMeta(component);
- const { children, instances, props } = generateDataFromEmbedTemplate(
- componentMeta?.children ?? []
+ const {
+ children,
+ instances,
+ props,
+ styleSourceSelections,
+ styleSources,
+ styles,
+ } = generateDataFromEmbedTemplate(
+ componentMeta?.children ?? [],
+ defaultBreakpointId
);
// put first to be interpreted as root
instances.unshift({
@@ -63,7 +74,7 @@ export const createComponentInstance = (component: Instance["component"]) => {
component,
children,
});
- return { instances, props };
+ return { instances, props, styleSourceSelections, styleSources, styles };
};
const isInstanceDroppable = (instance: Instance) => {
diff --git a/packages/react-sdk/src/embed-template.test.ts b/packages/react-sdk/src/embed-template.test.ts
index d1cd339e53..9b4296dc78 100644
--- a/packages/react-sdk/src/embed-template.test.ts
+++ b/packages/react-sdk/src/embed-template.test.ts
@@ -3,19 +3,24 @@ import { generateDataFromEmbedTemplate } from "./embed-template";
const expectString = expect.any(String) as unknown as string;
+const defaultBreakpointId = "base";
+
test("generate data for embedding from instances and text", () => {
expect(
- generateDataFromEmbedTemplate([
- { type: "text", value: "hello" },
- {
- type: "instance",
- component: "Box1",
- children: [
- { type: "instance", component: "Box2", children: [] },
- { type: "text", value: "world" },
- ],
- },
- ])
+ generateDataFromEmbedTemplate(
+ [
+ { type: "text", value: "hello" },
+ {
+ type: "instance",
+ component: "Box1",
+ children: [
+ { type: "instance", component: "Box2", children: [] },
+ { type: "text", value: "world" },
+ ],
+ },
+ ],
+ defaultBreakpointId
+ )
).toEqual({
children: [
{ type: "text", value: "hello" },
@@ -39,29 +44,35 @@ test("generate data for embedding from instances and text", () => {
},
],
props: [],
+ styleSourceSelections: [],
+ styleSources: [],
+ styles: [],
});
});
test("generate data for embedding from props", () => {
expect(
- generateDataFromEmbedTemplate([
- {
- type: "instance",
- component: "Box1",
- props: [
- { type: "string", name: "data-prop1", value: "value1" },
- { type: "string", name: "data-prop2", value: "value2" },
- ],
- children: [
- {
- type: "instance",
- component: "Box2",
- props: [{ type: "string", name: "data-prop3", value: "value3" }],
- children: [],
- },
- ],
- },
- ])
+ generateDataFromEmbedTemplate(
+ [
+ {
+ type: "instance",
+ component: "Box1",
+ props: [
+ { type: "string", name: "data-prop1", value: "value1" },
+ { type: "string", name: "data-prop2", value: "value2" },
+ ],
+ children: [
+ {
+ type: "instance",
+ component: "Box2",
+ props: [{ type: "string", name: "data-prop3", value: "value3" }],
+ children: [],
+ },
+ ],
+ },
+ ],
+ defaultBreakpointId
+ )
).toEqual({
children: [{ type: "id", value: expectString }],
instances: [
@@ -101,5 +112,99 @@ test("generate data for embedding from props", () => {
value: "value3",
},
],
+ styleSourceSelections: [],
+ styleSources: [],
+ styles: [],
+ });
+});
+
+test("generate data for embedding from styles", () => {
+ expect(
+ generateDataFromEmbedTemplate(
+ [
+ {
+ type: "instance",
+ component: "Box1",
+ styles: [
+ { property: "width", value: { type: "keyword", value: "auto" } },
+ { property: "height", value: { type: "keyword", value: "auto" } },
+ ],
+ children: [
+ {
+ type: "instance",
+ component: "Box2",
+ styles: [
+ {
+ property: "color",
+ value: { type: "keyword", value: "black" },
+ },
+ ],
+ children: [],
+ },
+ ],
+ },
+ ],
+ defaultBreakpointId
+ )
+ ).toEqual({
+ children: [{ type: "id", value: expectString }],
+ instances: [
+ {
+ type: "instance",
+ id: expectString,
+ component: "Box1",
+ children: [{ type: "id", value: expectString }],
+ },
+ {
+ type: "instance",
+ id: expectString,
+ component: "Box2",
+ children: [],
+ },
+ ],
+ props: [],
+ styleSourceSelections: [
+ {
+ instanceId: expectString,
+ values: [expectString],
+ },
+ {
+ instanceId: expectString,
+ values: [expectString],
+ },
+ ],
+ styleSources: [
+ {
+ type: "local",
+ id: expectString,
+ },
+ {
+ type: "local",
+ id: expectString,
+ },
+ ],
+ styles: [
+ {
+ breakpointId: "base",
+ styleSourceId: expectString,
+ state: undefined,
+ property: "width",
+ value: { type: "keyword", value: "auto" },
+ },
+ {
+ breakpointId: "base",
+ styleSourceId: expectString,
+ state: undefined,
+ property: "height",
+ value: { type: "keyword", value: "auto" },
+ },
+ {
+ breakpointId: "base",
+ styleSourceId: expectString,
+ state: undefined,
+ property: "color",
+ value: { type: "keyword", value: "black" },
+ },
+ ],
});
});
diff --git a/packages/react-sdk/src/embed-template.ts b/packages/react-sdk/src/embed-template.ts
index 33a12a7910..5ead7e46f0 100644
--- a/packages/react-sdk/src/embed-template.ts
+++ b/packages/react-sdk/src/embed-template.ts
@@ -4,6 +4,10 @@ import {
type Instance,
type InstancesList,
PropsList,
+ StyleSourceSelectionsList,
+ StyleSourcesList,
+ StylesList,
+ Breakpoint,
} from "@webstudio-is/project-build";
import { StyleValue, type StyleProperty } from "@webstudio-is/css-data";
@@ -75,7 +79,11 @@ export type WsEmbedTemplate = z.infer<typeof WsEmbedTemplate>;
const createInstancesFromTemplate = (
treeTemplate: WsEmbedTemplate,
instances: InstancesList,
- props: PropsList
+ props: PropsList,
+ styleSourceSelections: StyleSourceSelectionsList,
+ styleSources: StyleSourcesList,
+ styles: StylesList,
+ defaultBreakpointId: Breakpoint["id"]
) => {
const parentChildren: Instance["children"] = [];
for (const item of treeTemplate) {
@@ -93,6 +101,28 @@ const createInstancesFromTemplate = (
}
}
+ // populate styles
+ if (item.styles) {
+ const styleSourceId = nanoid();
+ styleSources.push({
+ type: "local",
+ id: styleSourceId,
+ });
+ styleSourceSelections.push({
+ instanceId,
+ values: [styleSourceId],
+ });
+ for (const styleDecl of item.styles) {
+ styles.push({
+ breakpointId: defaultBreakpointId,
+ styleSourceId,
+ state: styleDecl.state,
+ property: styleDecl.property,
+ value: styleDecl.value,
+ });
+ }
+ }
+
// populate instances
const instance: Instance = {
type: "instance",
@@ -105,13 +135,18 @@ const createInstancesFromTemplate = (
instance.children = createInstancesFromTemplate(
item.children,
instances,
- props
+ props,
+ styleSourceSelections,
+ styleSources,
+ styles,
+ defaultBreakpointId
);
parentChildren.push({
type: "id",
value: instanceId,
});
}
+
if (item.type === "text") {
parentChildren.push({
type: "text",
@@ -123,10 +158,30 @@ const createInstancesFromTemplate = (
};
export const generateDataFromEmbedTemplate = (
- treeTemplate: WsEmbedTemplate
+ treeTemplate: WsEmbedTemplate,
+ defaultBreakpointId: Breakpoint["id"]
) => {
const instances: InstancesList = [];
const props: PropsList = [];
- const children = createInstancesFromTemplate(treeTemplate, instances, props);
- return { children, instances, props };
+ const styleSourceSelections: StyleSourceSelectionsList = [];
+ const styleSources: StyleSourcesList = [];
+ const styles: StylesList = [];
+
+ const children = createInstancesFromTemplate(
+ treeTemplate,
+ instances,
+ props,
+ styleSourceSelections,
+ styleSources,
+ styles,
+ defaultBreakpointId
+ );
+ return {
+ children,
+ instances,
+ props,
+ styleSourceSelections,
+ styleSources,
+ styles,
+ };
};