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
feat: support copy pasting text content bindings (#2788)
Bogdan Chadkin0c9ee3823585
diff --git a/apps/builder/app/shared/instance-utils.test.ts b/apps/builder/app/shared/instance-utils.test.tsindex a57532e340..4ae2e320c8 100644--- a/apps/builder/app/shared/instance-utils.test.ts+++ b/apps/builder/app/shared/instance-utils.test.ts@@ -864,7 +864,7 @@ describe("get instances slice", () => {]);});- test("collect data sources used in expressions within instances", () => {+ test("collect data sources used in expression props within instances", () => {// body// box1// box2@@ -1060,6 +1060,65 @@ describe("get instances slice", () => {},]);});++ test("collect data sources used in expression children within instances", () => {+ // body+ // box+ $instances.set(+ toMap([+ createInstance("body", "Body", [{ type: "id", value: "box" }]),+ createInstance("box", "Box", [+ {+ type: "expression",+ value: "$ws$dataSource$body + $ws$dataSource$box",+ },+ ]),+ ])+ );+ $dataSources.set(+ toMap([+ {+ id: "body",+ scopeInstanceId: "body",+ type: "variable",+ name: "body",+ value: { type: "string", value: "body" },+ },+ {+ id: "bodyUnused",+ scopeInstanceId: "body",+ type: "variable",+ name: "bodyUnused",+ value: { type: "string", value: "bodyUnused" },+ },+ {+ id: "box",+ scopeInstanceId: "box",+ type: "variable",+ name: "box",+ value: { type: "string", value: "box" },+ },+ ])+ );+ const { dataSources } = getInstancesSlice("box");++ expect(dataSources).toEqual([+ {+ id: "body",+ scopeInstanceId: "body",+ type: "variable",+ name: "body",+ value: { type: "string", value: "body" },+ },+ {+ id: "box",+ scopeInstanceId: "box",+ type: "variable",+ name: "box",+ value: { type: "string", value: "box" },+ },+ ]);+ });});describe("insert instances slice copy", () => {@@ -1419,6 +1478,11 @@ describe("insert instances slice copy", () => {value: { type: "string", value: "" },},],+ instances: [+ createInstance("body", "Body", [+ { type: "expression", value: "$ws$dataSource$variableId" },+ ]),+ ],props: [{id: "expressionId",@@ -1452,27 +1516,33 @@ describe("insert instances slice copy", () => {availableDataSources: new Set(),});const [newVariableId] = data.dataSources.keys();+ const [newInstanceId] = data.instances.keys();expect(newVariableId).not.toEqual("variableId");expect(Array.from(data.dataSources.values())).toEqual([{id: newVariableId,- scopeInstanceId: expect.not.stringMatching("body"),+ scopeInstanceId: newInstanceId,type: "variable",name: "myVariable",value: { type: "string", value: "" },},]);+ expect(Array.from(data.instances.values())).toEqual([+ createInstance(newInstanceId, "Body", [+ { type: "expression", value: encodeDataSourceVariable(newVariableId) },+ ]),+ ]);expect(Array.from(data.props.values())).toEqual([{id: expect.not.stringMatching("expressionId"),- instanceId: expect.not.stringMatching("body"),+ instanceId: newInstanceId,name: "myProp1",type: "expression",value: encodeDataSourceVariable(newVariableId),},{id: expect.not.stringMatching("actionId"),- instanceId: expect.not.stringMatching("body"),+ instanceId: newInstanceId,name: "myProp2",type: "action",value: [@@ -1485,7 +1555,7 @@ describe("insert instances slice copy", () => {},{id: expect.not.stringMatching("parameterId"),- instanceId: expect.not.stringMatching("body"),+ instanceId: newInstanceId,name: "myProp3",type: "parameter",value: newVariableId,@@ -1515,6 +1585,15 @@ describe("insert instances slice copy", () => {value: { type: "string", value: "inside" },},],+ instances: [+ createInstance("body", "Body", [+ {+ type: "expression",+ value:+ "$ws$dataSource$outsideVariableId + $ws$dataSource$insideVariableId",+ },+ ]),+ ],props: [{id: "expressionId",@@ -1546,18 +1625,27 @@ describe("insert instances slice copy", () => {},availableDataSources: new Set(["insideVariableId"]),});+ const [newInstanceId] = data.instances.keys();expect(data.dataSources).toEqual(new Map());+ expect(Array.from(data.instances.values())).toEqual([+ createInstance(newInstanceId, "Body", [+ {+ type: "expression",+ value: `"outside" + $ws$dataSource$insideVariableId`,+ },+ ]),+ ]);expect(Array.from(data.props.values())).toEqual([{id: expect.not.stringMatching("expressionId"),- instanceId: expect.not.stringMatching("body"),+ instanceId: newInstanceId,name: "myProp1",type: "expression",value: `"outside" + $ws$dataSource$insideVariableId`,},{id: expect.not.stringMatching("actionId"),- instanceId: expect.not.stringMatching("body"),+ instanceId: newInstanceId,name: "myProp2",type: "action",value: [@@ -1590,7 +1678,9 @@ describe("insert instances slice copy", () => {type: "instance",id: "fragment",component: "Fragment",- children: [{ type: "id", value: "box" }],+ children: [+ { type: "expression", value: "$ws$dataSource$variableId" },+ ],},],dataSources: [@@ -1643,6 +1733,12 @@ describe("insert instances slice copy", () => {value: { type: "string", value: "" },},]);+ expect(data.instances.get("fragment")).toEqual({+ type: "instance",+ id: "fragment",+ component: "Fragment",+ children: [{ type: "expression", value: "$ws$dataSource$variableId" }],+ });expect(Array.from(data.props.values())).toEqual([{id: "expressionId",@@ -1683,18 +1779,16 @@ describe("insert instances slice copy", () => {// portal// fragmentinstances: [- {- type: "instance",- id: "portal",- component: portalComponent,- children: [{ type: "id", value: "fragment" }],- },- {- type: "instance",- id: "fragment",- component: "Fragment",- children: [{ type: "id", value: "box" }],- },+ createInstance("portal", portalComponent, [+ { type: "id", value: "fragment" },+ ]),+ createInstance("fragment", "Fragment", [+ {+ type: "expression",+ value:+ "$ws$dataSource$outsideVariableId + $ws$dataSource$insideVariableId",+ },+ ]),],dataSources: [{@@ -1744,6 +1838,14 @@ describe("insert instances slice copy", () => {availableDataSources: new Set(["insideVariableId"]),});expect(data.dataSources).toEqual(new Map());+ expect(data.instances.get("fragment")).toEqual(+ createInstance("fragment", "Fragment", [+ {+ type: "expression",+ value: `"outside" + $ws$dataSource$insideVariableId`,+ },+ ])+ );expect(Array.from(data.props.values())).toEqual([{id: "expressionId",diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex 285b2c7cfe..401dbf8c18 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -593,6 +593,26 @@ const traverseStyleValue = (value satisfies never;};+const collectUsedDataSources = (+ expression: string,+ usedDataSourceIds: Set<DataSource["id"]>+) => {+ try {+ validateExpression(expression, {+ effectful: true,+ transformIdentifier(identifier) {+ const id = decodeDataSourceVariable(identifier);+ if (id !== undefined) {+ usedDataSourceIds.add(id);+ }+ return identifier;+ },+ });+ } catch {+ // empty block+ }+};+export const getInstancesSlice = (rootInstanceId: string): WebstudioFragment => {@@ -610,10 +630,16 @@ export const getInstancesSlice = (const slicedInstances: Instance[] = [];const slicedStyleSourceSelections: StyleSourceSelection[] = [];const slicedStyleSources: StyleSources = new Map();+ const usedDataSourceIds = new Set<DataSource["id"]>();for (const instanceId of slicedInstanceIds) {const instance = instances.get(instanceId);if (instance) {slicedInstances.push(instance);+ for (const child of instance.children) {+ if (child.type === "expression") {+ collectUsedDataSources(child.value, usedDataSourceIds);+ }+ }}// collect all style sources bound to these instances@@ -670,7 +696,6 @@ export const getInstancesSlice = (// collect props bound to these instancesconst slicedProps: Props = new Map();- const usedDataSourceIds = new Set<DataSource["id"]>();for (const prop of props.values()) {if (slicedInstanceIds.has(prop.instanceId) === false) {continue;@@ -679,30 +704,13 @@ export const getInstancesSlice = (slicedProps.set(prop.id, prop);if (prop.type === "expression") {- validateExpression(prop.value, {- transformIdentifier(identifier) {- const id = decodeDataSourceVariable(identifier);- if (id !== undefined) {- usedDataSourceIds.add(id);- }- return identifier;- },- });+ collectUsedDataSources(prop.value, usedDataSourceIds);}if (prop.type === "action") {for (const value of prop.value) {if (value.type === "execute") {- validateExpression(value.code, {- effectful: true,- transformIdentifier(identifier) {- const id = decodeDataSourceVariable(identifier);- if (id !== undefined) {- usedDataSourceIds.add(id);- }- return identifier;- },- });+ collectUsedDataSources(value.code, usedDataSourceIds);}}}@@ -948,11 +956,6 @@ export const insertInstancesSliceCopy = ({sliceInstances,rootInstanceId);- for (const instance of slice.instances) {- if (instanceIds.has(instance.id)) {- instances.set(instance.id, instance);- }- }const availablePortalDataSources = new Set(availableDataSources);for (const dataSource of slice.dataSources) {@@ -966,6 +969,28 @@ export const insertInstancesSliceCopy = ({}}+ for (const instance of slice.instances) {+ if (instanceIds.has(instance.id)) {+ instances.set(instance.id, {+ ...instance,+ children: instance.children.map((child) => {+ if (child.type === "expression") {+ const { code } = inlineUnavailableDataSources({+ code: child.value,+ availableDataSources: availablePortalDataSources,+ dataSources: sliceDataSources,+ });+ return {+ type: "expression",+ value: code,+ };+ }+ return child;+ }),+ });+ }+ }+for (let prop of slice.props) {if (instanceIds.has(prop.instanceId) === false) {continue;@@ -1054,6 +1079,24 @@ export const insertInstancesSliceCopy = ({for (const instanceId of sliceInstanceIds) {newInstanceIds.set(instanceId, nanoid());}++ const availableFragmentDataSources = new Set(availableDataSources);+ const newDataSourceIds = new Map<DataSource["id"], DataSource["id"]>();+ for (const dataSource of slice.dataSources) {+ const { scopeInstanceId } = dataSource;+ // insert only data sources within portal content+ if (scopeInstanceId && sliceInstanceIds.has(scopeInstanceId)) {+ availableFragmentDataSources.add(dataSource.id);+ const newId = nanoid();+ newDataSourceIds.set(dataSource.id, newId);+ dataSources.set(newId, {+ ...dataSource,+ id: newId,+ scopeInstanceId: newInstanceIds.get(scopeInstanceId),+ });+ }+ }+for (const instance of slice.instances) {if (sliceInstanceIds.has(instance.id)) {const newId = newInstanceIds.get(instance.id) ?? instance.id;@@ -1067,29 +1110,23 @@ export const insertInstancesSliceCopy = ({value: newInstanceIds.get(child.value) ?? child.value,};}+ if (child.type === "expression") {+ const { code } = inlineUnavailableDataSources({+ code: child.value,+ availableDataSources: availableFragmentDataSources,+ dataSources: sliceDataSources,+ });+ return {+ type: "expression",+ value: replaceDataSources(code, newDataSourceIds),+ };+ }return child;}),});}}- const availablePortalDataSources = new Set(availableDataSources);- const newDataSourceIds = new Map<DataSource["id"], DataSource["id"]>();- for (const dataSource of slice.dataSources) {- const { scopeInstanceId } = dataSource;- // insert only data sources within portal content- if (scopeInstanceId && sliceInstanceIds.has(scopeInstanceId)) {- availablePortalDataSources.add(dataSource.id);- const newId = nanoid();- newDataSourceIds.set(dataSource.id, newId);- dataSources.set(newId, {- ...dataSource,- id: newId,- scopeInstanceId: newInstanceIds.get(scopeInstanceId),- });- }- }-for (let prop of slice.props) {if (sliceInstanceIds.has(prop.instanceId) === false) {continue;@@ -1098,7 +1135,7 @@ export const insertInstancesSliceCopy = ({if (prop.type === "expression") {const { code } = inlineUnavailableDataSources({code: prop.value,- availableDataSources: availablePortalDataSources,+ availableDataSources: availableFragmentDataSources,dataSources: sliceDataSources,});prop = { ...prop, value: replaceDataSources(code, newDataSourceIds) };@@ -1112,7 +1149,7 @@ export const insertInstancesSliceCopy = ({}const { code, isDiscarded } = inlineUnavailableDataSources({code: value.code,- availableDataSources: availablePortalDataSources,+ availableDataSources: availableFragmentDataSources,dataSources: sliceDataSources,});if (isDiscarded) {