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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Support data source insert from template (#1826)
Bogdan Chadkin730d89703924
diff --git a/apps/builder/app/builder/features/props-panel/controls/combined.tsx b/apps/builder/app/builder/features/props-panel/controls/combined.tsxindex f2d66d1898..c150128ff9 100644--- a/apps/builder/app/builder/features/props-panel/controls/combined.tsx+++ b/apps/builder/app/builder/features/props-panel/controls/combined.tsx@@ -15,6 +15,10 @@ export const renderControl = ({prop,...rest}: ControlProps<string, string> & { key?: string }) => {+ if (prop?.type === "dataSource") {+ throw Error("Data source is not resolved");+ }+if (meta.control === "text" &&(prop === undefined || prop.type === "string")diff --git a/apps/builder/app/builder/features/props-panel/use-props-logic.ts b/apps/builder/app/builder/features/props-panel/use-props-logic.tsindex 68d14d69ae..a95639e5c8 100644--- a/apps/builder/app/builder/features/props-panel/use-props-logic.ts+++ b/apps/builder/app/builder/features/props-panel/use-props-logic.ts@@ -77,6 +77,10 @@ const getDefaultMetaForType = (type: Prop["type"]): PropMeta => {throw new Error("A prop with type string[] must have a meta, we can't provide a default one because we need a list of options");+ case "dataSource":+ throw new Error(+ "A prop with type dataSource must have a meta, we can't provide a default one because we need a list of options"+ );default:throw new Error(`Usupported data type: ${type satisfies never}`);}diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.tsindex 6973ba5923..21542c161a 100644--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts@@ -22,6 +22,7 @@ import {assetsStore,projectStore,registeredComponentMetasStore,+ dataSourcesStore,} from "../nano-states";import {type InstanceSelector,@@ -132,7 +133,8 @@ const getAssetsUsedInProps = (props: Prop[], foundAssetsIds = new Set()) => {prop.type === "string" ||prop.type === "boolean" ||prop.type === "page" ||- prop.type === "string[]"+ prop.type === "string[]" ||+ prop.type === "dataSource") {continue;}@@ -159,9 +161,25 @@ const getTreeData = (targetInstanceSelector: InstanceSelector) => {// first item is guaranteed root of copied treeconst treeInstances = getMapValuesByKeysSet(instances, treeInstanceIds);+ const dataSources = dataSourcesStore.get();const treeProps = getMapValuesBy(propsStore.get(), (prop) =>treeInstanceIds.has(prop.instanceId)- );+ ).map((prop) => {+ // unbind data source from prop+ if (prop.type === "dataSource") {+ const dataSource = dataSources.get(prop.value);+ if (dataSource) {+ const { id, name, ...rest } = dataSource;+ return {+ id: prop.id,+ instanceId: prop.instanceId,+ name: prop.name,+ ...rest,+ } satisfies Prop;+ }+ }+ return prop;+ });const treeStyleSourceSelections = getMapValuesByKeysSet(styleSourceSelections,diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex 7d93e664e7..055254c289 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -19,6 +19,7 @@ import {textEditingInstanceSelectorStore,breakpointsStore,registeredComponentMetasStore,+ dataSourcesStore,} from "./nano-states";import {type DroppableTarget,@@ -175,6 +176,7 @@ export const insertTemplate = (children,instances: insertedInstances,props: insertedProps,+ dataSources: insertedDataSources,styleSourceSelections: insertedStyleSourceSelections,styleSources: insertedStyleSources,styles: insertedStyles,@@ -184,11 +186,19 @@ export const insertTemplate = ([instancesStore,propsStore,+ dataSourcesStore,styleSourceSelectionsStore,styleSourcesStore,stylesStore,],- (instances, props, styleSourceSelections, styleSources, styles) => {+ (+ instances,+ props,+ dataSources,+ styleSourceSelections,+ styleSources,+ styles+ ) => {insertInstancesMutable(instances,props,@@ -198,6 +208,9 @@ export const insertTemplate = (dropTarget);insertPropsCopyMutable(props, insertedProps, new Map());+ for (const dataSource of insertedDataSources) {+ dataSources.set(dataSource.id, dataSource);+ }insertStyleSourcesCopyMutable(styleSources,insertedStyleSources,diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.tsindex 7eed83b6d4..01a8338da7 100644--- a/apps/builder/app/shared/nano-states/nano-states.ts+++ b/apps/builder/app/shared/nano-states/nano-states.ts@@ -7,6 +7,7 @@ import type { Asset, Assets } from "@webstudio-is/asset-uploader";import type { ItemDropTarget, Placement } from "@webstudio-is/design-system";import type {Breakpoint,+ DataSources,Instance,Prop,Props,@@ -59,6 +60,8 @@ export const rootInstanceStore = computed(});+export const dataSourcesStore = atom<DataSources>(new Map());+export const propsStore = atom<Props>(new Map());export const propsIndexStore = computed(propsStore, (props) => {const propsByInstanceId = new Map<Instance["id"], Prop[]>();diff --git a/apps/builder/app/shared/sync/sync-stores.ts b/apps/builder/app/shared/sync/sync-stores.tsindex a478c2fb95..89a644d283 100644--- a/apps/builder/app/shared/sync/sync-stores.ts+++ b/apps/builder/app/shared/sync/sync-stores.ts@@ -8,6 +8,7 @@ import {pagesStore,instancesStore,propsStore,+ dataSourcesStore,breakpointsStore,stylesStore,styleSourcesStore,@@ -66,6 +67,7 @@ export const registerContainers = () => {store.register("styleSources", styleSourcesStore);store.register("styleSourceSelections", styleSourceSelectionsStore);store.register("props", propsStore);+ store.register("dataSources", dataSourcesStore);store.register("assets", assetsStore);// synchronize whole statesclientStores.set("project", projectStore);diff --git a/packages/project-build/src/schema/data-sources.ts b/packages/project-build/src/schema/data-sources.tsindex 7bb293f1cd..18bd67e051 100644--- a/packages/project-build/src/schema/data-sources.ts+++ b/packages/project-build/src/schema/data-sources.ts@@ -7,13 +7,14 @@ export const DataSource = z.union([id: DataSourceId,name: z.string(),type: z.literal("string"),- defaultValue: z.string(),+ // initial value of data source store+ value: z.string(),}),z.object({id: DataSourceId,name: z.string(),type: z.literal("boolean"),- defaultValue: z.boolean(),+ value: z.boolean(),}),]);diff --git a/packages/project-build/src/schema/props.ts b/packages/project-build/src/schema/props.tsindex 880b254870..6a559ac5d0 100644--- a/packages/project-build/src/schema/props.ts+++ b/packages/project-build/src/schema/props.ts@@ -46,6 +46,12 @@ export const Prop = z.union([type: z.literal("string[]"),value: z.array(z.string()),}),+ z.object({+ ...baseProp,+ type: z.literal("dataSource"),+ // data source id+ value: z.string(),+ }),]);export type Prop = z.infer<typeof Prop>;diff --git a/packages/react-sdk/src/embed-template.test.ts b/packages/react-sdk/src/embed-template.test.tsindex 9b4296dc78..5d66ee928c 100644--- a/packages/react-sdk/src/embed-template.test.ts+++ b/packages/react-sdk/src/embed-template.test.ts@@ -1,5 +1,6 @@import { expect, test } from "@jest/globals";import { generateDataFromEmbedTemplate } from "./embed-template";+import { showAttribute } from "./tree";const expectString = expect.any(String) as unknown as string;@@ -44,6 +45,7 @@ test("generate data for embedding from instances and text", () => {},],props: [],+ dataSources: [],styleSourceSelections: [],styleSources: [],styles: [],@@ -112,6 +114,7 @@ test("generate data for embedding from props", () => {value: "value3",},],+ dataSources: [],styleSourceSelections: [],styleSources: [],styles: [],@@ -163,6 +166,7 @@ test("generate data for embedding from styles", () => {},],props: [],+ dataSources: [],styleSourceSelections: [{instanceId: expectString,@@ -208,3 +212,75 @@ test("generate data for embedding from styles", () => {],});});++test("generate data for embedding from props bound to data sources", () => {+ expect(+ generateDataFromEmbedTemplate(+ [+ {+ type: "instance",+ component: "Box1",+ props: [+ {+ type: "boolean",+ name: "showOtherBox",+ dataSourceRef: "showOtherBoxDataSource",+ value: false,+ },+ ],+ children: [],+ },+ {+ type: "instance",+ component: "Box2",+ props: [+ {+ type: "boolean",+ name: showAttribute,+ dataSourceRef: "showOtherBoxDataSource",+ value: false,+ },+ ],+ children: [],+ },+ ],+ defaultBreakpointId+ )+ ).toEqual({+ children: [+ { type: "id", value: expectString },+ { type: "id", value: expectString },+ ],+ instances: [+ { type: "instance", id: expectString, component: "Box1", children: [] },+ { type: "instance", id: expectString, component: "Box2", children: [] },+ ],+ props: [+ {+ id: expectString,+ instanceId: expectString,+ type: "dataSource",+ name: "showOtherBox",+ value: expectString,+ },+ {+ id: expectString,+ instanceId: expectString,+ type: "dataSource",+ name: showAttribute,+ value: expectString,+ },+ ],+ dataSources: [+ {+ id: expectString,+ name: "showOtherBoxDataSource",+ type: "boolean",+ value: false,+ },+ ],+ styleSourceSelections: [],+ styleSources: [],+ styles: [],+ });+});diff --git a/packages/react-sdk/src/embed-template.ts b/packages/react-sdk/src/embed-template.tsindex 4757905031..dc4b6d8537 100644--- a/packages/react-sdk/src/embed-template.ts+++ b/packages/react-sdk/src/embed-template.ts@@ -8,6 +8,7 @@ import {StyleSourcesList,StylesList,Breakpoint,+ DataSource,} from "@webstudio-is/project-build";import { StyleValue, type StyleProperty } from "@webstudio-is/css-data";import type { Simplify } from "type-fest";@@ -23,21 +24,25 @@ const EmbedTemplateProp = z.union([z.object({type: z.literal("number"),name: z.string(),+ dataSourceRef: z.optional(z.string()),value: z.number(),}),z.object({type: z.literal("string"),name: z.string(),+ dataSourceRef: z.optional(z.string()),value: z.string(),}),z.object({type: z.literal("boolean"),name: z.string(),+ dataSourceRef: z.optional(z.string()),value: z.boolean(),}),z.object({type: z.literal("string[]"),name: z.string(),+ dataSourceRef: z.optional(z.string()),value: z.array(z.string()),}),]);@@ -91,6 +96,7 @@ const createInstancesFromTemplate = (treeTemplate: WsEmbedTemplate,instances: InstancesList,props: PropsList,+ dataSourceByRef: Map<string, DataSource>,styleSourceSelections: StyleSourceSelectionsList,styleSources: StyleSourcesList,styles: StylesList,@@ -104,10 +110,30 @@ const createInstancesFromTemplate = (// populate propsif (item.props) {for (const prop of item.props) {+ const propId = nanoid();+ if (prop.dataSourceRef === undefined) {+ props.push({ id: propId, instanceId, ...prop });+ continue;+ }+ let dataSource = dataSourceByRef.get(prop.dataSourceRef);+ if (dataSource === undefined) {+ const id = nanoid();+ const { name: propName, dataSourceRef: name, ...rest } = prop;+ if (rest.type === "boolean" || rest.type === "string") {+ dataSource = { id, name, ...rest };+ } else {+ // ensure only number and string[] are not mapped to data sources+ rest.type satisfies "number" | "string[]";+ continue;+ }+ dataSourceByRef.set(name, dataSource);+ }props.push({- id: nanoid(),+ id: propId,instanceId,- ...prop,+ type: "dataSource",+ name: prop.name,+ value: dataSource.id,});}}@@ -148,6 +174,7 @@ const createInstancesFromTemplate = (item.children,instances,props,+ dataSourceByRef,styleSourceSelections,styleSources,styles,@@ -175,6 +202,7 @@ export const generateDataFromEmbedTemplate = () => {const instances: InstancesList = [];const props: PropsList = [];+ const dataSourceByRef = new Map<string, DataSource>();const styleSourceSelections: StyleSourceSelectionsList = [];const styleSources: StyleSourcesList = [];const styles: StylesList = [];@@ -183,6 +211,7 @@ export const generateDataFromEmbedTemplate = (treeTemplate,instances,props,+ dataSourceByRef,styleSourceSelections,styleSources,styles,@@ -192,6 +221,7 @@ export const generateDataFromEmbedTemplate = (children,instances,props,+ dataSources: Array.from(dataSourceByRef.values()),styleSourceSelections,styleSources,styles,