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: reuse variables on copy/paste within same tree (#2580)
Bogdan Chadkinfa06b7d49534
diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.test.ts b/apps/builder/app/shared/copy-paste/plugin-instance.test.tsindex 8d2407aede..7d996401b6 100644--- a/apps/builder/app/shared/copy-paste/plugin-instance.test.ts+++ b/apps/builder/app/shared/copy-paste/plugin-instance.test.ts@@ -154,28 +154,28 @@ describe("data sources", () => {] satisfies DataSource[]);const props: Props = toMap([{- id: "box1$state",+ id: "box1$stateProp",instanceId: "box1",type: "dataSource",name: "state",value: "box1$state",},{- id: "box2$state",+ id: "box2$stateProp",instanceId: "box2",type: "dataSource",name: "state",value: "box1$state",},{- id: "box2$show",+ id: "box2$showProp",instanceId: "box2",type: "dataSource",name: "show",value: "box2$stateInitial",},{- id: "box2$onChange",+ id: "box2$onChangeProp",instanceId: "box2",type: "action",name: "onChange",@@ -230,27 +230,30 @@ describe("data sources", () => {expect(propsDifference).toEqual(toMap([{- ...props.get("box1$state"),+ ...props.get("box1$stateProp"),id: newProp1,instanceId: newBox1,value: newDataSource1,},{- ...props.get("box2$state"),id: newProp2,instanceId: newBox2,+ name: "state",+ type: "dataSource",value: newDataSource1,},{- ...props.get("box2$show"),id: newProp3,instanceId: newBox2,+ name: "show",+ type: "dataSource",value: newDataSource2,},{- ...props.get("box2$onChange"),id: newProp4,instanceId: newBox2,+ name: "onChange",+ type: "action",value: [{type: "execute",@@ -282,7 +285,18 @@ describe("data sources", () => {dataSources,dataSourcesStore.get());- expect(dataSourcesDifference).toEqual(new Map());+ const [dataSourceId] = dataSourcesDifference.keys();+ expect(dataSourcesDifference).toEqual(+ toMap([+ {+ id: dataSourceId,+ scopeInstanceId: newBox2,+ name: "stateInitial",+ type: "expression",+ code: `"initial" === 'initial'`,+ },+ ])+ );const propsDifference = getMapDifference(props, propsStore.get());const [newProp1, newProp2, newProp3] = propsDifference.keys();@@ -298,9 +312,9 @@ describe("data sources", () => {{id: newProp2,instanceId: newBox2,- type: "boolean",name: "show",- value: true,+ type: "dataSource",+ value: dataSourceId,},{id: newProp3,@@ -313,7 +327,70 @@ describe("data sources", () => {);});- test.todo(- "preserve data sources not scoped to new instances within same scope"- );+ test("preserve data sources outside of scope when pasted within their scope", () => {+ instancesStore.set(instances);+ propsStore.set(props);+ dataSourcesStore.set(dataSources);+ selectedInstanceSelectorStore.set(["box2", "box1", "body0"]);+ const clipboardData = onCopy() ?? "";+ selectedInstanceSelectorStore.set(["box1", "body0"]);+ onPaste(clipboardData);++ const instancesDifference = getMapDifference(+ instances,+ instancesStore.get()+ );+ const [newBox2] = instancesDifference.keys();++ const dataSourcesDifference = getMapDifference(+ dataSources,+ dataSourcesStore.get()+ );+ const [dataSourceId] = dataSourcesDifference.keys();+ expect(dataSourcesDifference).toEqual(+ toMap([+ {+ id: dataSourceId,+ scopeInstanceId: newBox2,+ name: "stateInitial",+ type: "expression",+ code: "$ws$dataSource$box1$state === 'initial'",+ },+ ])+ );++ const propsDifference = getMapDifference(props, propsStore.get());+ const [newProp1, newProp2, newProp3] = propsDifference.keys();+ expect(propsDifference).toEqual(+ toMap([+ {+ id: newProp1,+ instanceId: newBox2,+ name: "state",+ type: "dataSource",+ value: "box1$state",+ },+ {+ id: newProp2,+ instanceId: newBox2,+ name: "show",+ type: "dataSource",+ value: dataSourceId,+ },+ {+ id: newProp3,+ instanceId: newBox2,+ type: "action",+ name: "onChange",+ value: [+ {+ args: ["value"],+ code: "$ws$dataSource$box1$state = value",+ type: "execute",+ },+ ],+ },+ ])+ );+ });});diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.tsindex 47b3f5aa81..38ed405d47 100644--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts@@ -201,16 +201,34 @@ export const onPaste = (clipboardData: string): boolean => {dropTarget);+ // duplicate data sources only within copied treeconst copiedDataSourceIds = new Map<DataSource["id"], DataSource["id"]>();+ const copiedDataSources = new Map<DataSource["id"], DataSource>();for (const dataSource of data.dataSources) {- copiedDataSourceIds.set(dataSource.id, nanoid());+ const { scopeInstanceId } = dataSource;+ if (+ scopeInstanceId !== undefined &&+ copiedInstanceIds.has(scopeInstanceId)+ ) {+ copiedDataSourceIds.set(dataSource.id, nanoid());+ }+ copiedDataSources.set(dataSource.id, dataSource);}for (const dataSource of data.dataSources) {let { scopeInstanceId } = dataSource;- if (scopeInstanceId !== undefined) {- scopeInstanceId = copiedInstanceIds.get(scopeInstanceId);+ if (scopeInstanceId === undefined) {+ continue;}+ // reject data sources outside of copied tree and not scoped to ancestors+ if (+ copiedInstanceIds.has(scopeInstanceId) === false &&+ instanceSelector.includes(scopeInstanceId) === false+ ) {+ continue;+ }+ scopeInstanceId = copiedInstanceIds.get(scopeInstanceId);+const newId = copiedDataSourceIds.get(dataSource.id);if (newId === undefined) {continue;@@ -222,26 +240,6 @@ export const onPaste = (clipboardData: string): boolean => {scopeInstanceId,});}- if (dataSource.type === "expression") {- dataSources.set(newId, {- ...dataSource,- id: newId,- scopeInstanceId,- code: validateExpression(dataSource.code, {- transformIdentifier: (id) => {- const dataSourceId = decodeDataSourceVariable(id);- if (dataSourceId === undefined) {- return id;- }- const newId = copiedDataSourceIds.get(dataSourceId);- if (newId === undefined) {- return id;- }- return encodeDataSourceVariable(newId);- },- }),- });- }}const localStyleSourceIds = findLocalStyleSourcesWithinInstances(@@ -259,30 +257,92 @@ export const onPaste = (clipboardData: string): boolean => {insertPropsCopyMutable(props,data.props.map((prop) => {+ let actionDiscarded = false;+ const transformIdentifier = (+ identifier: string,+ assignee: boolean+ ) => {+ const dataSourceId = decodeDataSourceVariable(identifier);+ if (dataSourceId === undefined) {+ return identifier;+ }+ // data source is within copied tree+ const newId = copiedDataSourceIds.get(dataSourceId);+ if (newId !== undefined) {+ return encodeDataSourceVariable(newId);+ }+ const dataSource = copiedDataSources.get(dataSourceId);+ // data source is within paste target+ if (+ dataSource?.scopeInstanceId !== undefined &&+ instanceSelector.includes(dataSource.scopeInstanceId)+ ) {+ return identifier;+ }+ // left operand of assign operator cannot be inlined+ if (assignee) {+ actionDiscarded = true;+ }+ // inline variable not scoped to copied tree or paste target+ if (dataSource?.type === "variable") {+ return JSON.stringify(dataSource.value.value);+ }+ return identifier;+ };++ if (prop.type === "dataSource") {+ const dataSource = copiedDataSources.get(prop.value);+ // convert to value prop when variable is not scoped to copied tree or paste target+ if (dataSource?.type === "variable") {+ // data source is within copied tree+ if (copiedDataSourceIds.has(dataSource.id)) {+ return prop;+ }+ // data source is within paste target+ if (+ dataSource.scopeInstanceId !== undefined &&+ instanceSelector.includes(dataSource.scopeInstanceId)+ ) {+ return prop;+ }+ return {+ ...prop,+ ...dataSource.value,+ };+ }+ if (dataSource?.type === "expression") {+ const newDataSourceId =+ copiedDataSourceIds.get(dataSource.id) ?? dataSource.id;+ const newInstanceId =+ copiedInstanceIds.get(dataSource.scopeInstanceId) ??+ dataSource.scopeInstanceId;+ dataSources.set(newDataSourceId, {+ ...dataSource,+ id: newDataSourceId,+ scopeInstanceId: newInstanceId,+ code: validateExpression(dataSource.code, {+ transformIdentifier,+ }),+ });+ return prop;+ }+ }if (prop.type === "action") {return {...prop,- value: prop.value.map((value) => {+ value: prop.value.flatMap((value) => {if (value.type !== "execute") {- return value;+ return [value];}- return {- ...value,- code: validateExpression(value.code, {- effectful: true,- transformIdentifier: (id) => {- const dataSourceId = decodeDataSourceVariable(id);- if (dataSourceId === undefined) {- return id;- }- const newId = copiedDataSourceIds.get(dataSourceId);- if (newId === undefined) {- return id;- }- return encodeDataSourceVariable(newId);- },- }),- };+ actionDiscarded = false;+ const newCode = validateExpression(value.code, {+ effectful: true,+ transformIdentifier,+ });+ if (actionDiscarded) {+ return [];+ }+ return [{ ...value, code: newCode }];}),};}@@ -291,6 +351,7 @@ export const onPaste = (clipboardData: string): boolean => {copiedInstanceIds,copiedDataSourceIds);+insertStyleSourceSelectionsCopyMutable(styleSourceSelections,data.styleSourceSelections,diff --git a/apps/builder/app/shared/instance-utils.test.ts b/apps/builder/app/shared/instance-utils.test.tsindex cb9f0f76aa..25c26eb70e 100644--- a/apps/builder/app/shared/instance-utils.test.ts+++ b/apps/builder/app/shared/instance-utils.test.ts@@ -754,7 +754,7 @@ describe("get instances slice", () => {]);});- test("collect data sources within instances slice and convert others to value prop", () => {+ test("collect data sources used in expressions within instances", () => {// body// box1// box2@@ -793,28 +793,28 @@ describe("get instances slice", () => {propsStore.set(toMap([{- id: "box1$state",+ id: "box1$stateProp",instanceId: "box1",type: "dataSource",name: "state",value: "box1$state",},{- id: "box2$state",+ id: "box2$stateProp",instanceId: "box2",type: "dataSource",name: "state",value: "box1$state",},{- id: "box2$show",+ id: "box2$showProp",instanceId: "box2",type: "dataSource",name: "show",value: "box2$stateInitial",},{- id: "box2$true",+ id: "box2$trueProp",instanceId: "box2",type: "dataSource",name: "bool-prop",@@ -822,34 +822,57 @@ describe("get instances slice", () => {},]));- const { props } = getInstancesSlice("box2");+ const { props, dataSources } = getInstancesSlice("box2");+ expect(dataSources).toEqual([+ {+ id: "box1$state",+ scopeInstanceId: "box1",+ type: "variable",+ name: "state",+ value: { type: "string", value: "initial" },+ },+ {+ id: "box2$stateInitial",+ scopeInstanceId: "box2",+ type: "expression",+ name: "stateInitial",+ code: `$ws$dataSource$box1$state === 'initial'`,+ },+ {+ id: "box2$true",+ scopeInstanceId: "box2",+ type: "expression",+ name: "trueValue",+ code: `true`,+ },+ ]);expect(props).toEqual([{- id: "box2$state",+ id: "box2$stateProp",instanceId: "box2",- type: "string",name: "state",- value: "initial",+ type: "dataSource",+ value: "box1$state",},{- id: "box2$show",+ id: "box2$showProp",instanceId: "box2",- type: "boolean",name: "show",- value: true,+ type: "dataSource",+ value: "box2$stateInitial",},{- id: "box2$true",+ id: "box2$trueProp",instanceId: "box2",- type: "dataSource",name: "bool-prop",+ type: "dataSource",value: "box2$true",},]);});- test("clear actions which depend on data sources outside of instances slice", () => {+ test("collect data sources used in actions within instances", () => {// body// box1// box2@@ -908,15 +931,37 @@ describe("get instances slice", () => {},]));- const { props } = getInstancesSlice("box2");+ const { props, dataSources } = getInstancesSlice("box2");+ expect(dataSources).toEqual([+ {+ id: "box1$state",+ scopeInstanceId: "box1",+ type: "variable",+ name: "state",+ value: { type: "string", value: "initial" },+ },+ {+ id: "box2$state",+ scopeInstanceId: "box2",+ type: "variable",+ name: "state",+ value: { type: "string", value: "initial" },+ },+ ]);expect(props).toEqual([{id: "box2$onChange1",instanceId: "box2",type: "action",name: "onChange",- value: [],+ value: [+ {+ args: ["value"],+ code: "$ws$dataSource$box1$state = value",+ type: "execute",+ },+ ],},{id: "box2$onChange2",diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex 48fec784d7..349a15951f 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -12,14 +12,13 @@ import {Breakpoints,DataSources,Props,+ DataSource,} from "@webstudio-is/sdk";import { findTreeInstanceIdsExcludingSlotDescendants } from "@webstudio-is/sdk";import {type WsComponentMeta,generateDataFromEmbedTemplate,type EmbedTemplateData,- encodeDataSourceVariable,- computeExpressionsDependencies,decodeDataSourceVariable,validateExpression,} from "@webstudio-is/react-sdk";@@ -35,7 +34,6 @@ import {registeredComponentMetasStore,dataSourcesStore,assetsStore,- dataSourcesLogicStore,} from "./nano-states";import {type DroppableTarget,@@ -535,22 +533,6 @@ const traverseStyleValue = (value satisfies never;};-const getPropTypeAndValue = (name: string, value: unknown) => {- if (typeof value === "boolean") {- return { type: "boolean", value } as const;- }- if (typeof value === "number") {- return { type: "number", value } as const;- }- if (typeof value === "string") {- return { type: "string", value } as const;- }- if (Array.isArray(value)) {- return { type: "string[]", value } as const;- }- throw Error(`Unexpected "${name}" prop value ${value}`);-};-export const getInstancesSlice = (rootInstanceId: string) => {const assets = assetsStore.get();const instances = instancesStore.get();@@ -561,8 +543,6 @@ export const getInstancesSlice = (rootInstanceId: string) => {const breakpoints = breakpointsStore.get();const styles = stylesStore.get();- const dataSourcesLogic = dataSourcesLogicStore.get();-// collect the instance by id and all its descendants including portal instancesconst slicedInstanceIds = findTreeInstanceIds(instances, rootInstanceId);const slicedInstances: Instance[] = [];@@ -626,63 +606,9 @@ export const getInstancesSlice = (rootInstanceId: string) => {});}- // compute dependencies of expressions to make sure all data sources- // are inside of instances slice and can be copied- const expressions = new Map<string, string>();- for (const dataSource of dataSources.values()) {- if (dataSource.type === "expression") {- expressions.set(encodeDataSourceVariable(dataSource.id), dataSource.code);- }- }- const dependencies = computeExpressionsDependencies(expressions);-- // collect data sources scoped to instances slice- //- // @todo copy data sources outside of slice tree and bind to copied root- // to preserve bindings and let users easily rebind variables- // blocked by lack of UI to manage data sources- const slicedDataSources: DataSources = new Map();- for (const dataSource of dataSources.values()) {- let canBeCopied = true;- if (dataSource.type === "expression") {- const expressionDeps = dependencies.get(- encodeDataSourceVariable(dataSource.id)- );- // check if all expression dependencies can be copied- if (expressionDeps) {- for (const dependency of expressionDeps) {- const dataSourceId = decodeDataSourceVariable(dependency);- if (dataSourceId === undefined) {- continue;- }- const dataSource = dataSources.get(dataSourceId);- if (dataSource === undefined) {- continue;- }- if (- dataSource.scopeInstanceId === undefined ||- slicedInstanceIds.has(dataSource.scopeInstanceId) === false- ) {- canBeCopied = false;- }- }- }- }- if (- canBeCopied &&- // check if data source itself can be copied- dataSource.scopeInstanceId !== undefined &&- slicedInstanceIds.has(dataSource.scopeInstanceId)- ) {- slicedDataSources.set(dataSource.id, dataSource);- }- }-- // @todo convert to value props- // when variables are outside of slice and cannot be copied-// 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;@@ -690,72 +616,40 @@ export const getInstancesSlice = (rootInstanceId: string) => {slicedProps.set(prop.id, prop);- // convert data source prop to value prop- // when not scoped to sliced instances- //- // @todo stop converting to value props once UI is readyif (prop.type === "dataSource") {- const dataSourceId = prop.value;- if (slicedDataSources.has(dataSourceId) === false) {- const value =- dataSourcesLogic.get(prop.id) ?? dataSourcesLogic.get(dataSourceId);- slicedProps.set(prop.id, {- id: prop.id,- instanceId: prop.instanceId,- name: prop.name,- ...getPropTypeAndValue(prop.name, value),+ const dataSource = dataSources.get(prop.value);+ if (dataSource?.type === "variable") {+ usedDataSourceIds.add(dataSource.id);+ }+ if (dataSource?.type === "expression") {+ usedDataSourceIds.add(dataSource.id);+ validateExpression(dataSource.code, {+ transformIdentifier(identifier) {+ const id = decodeDataSourceVariable(identifier);+ if (id !== undefined) {+ usedDataSourceIds.add(id);+ }+ return identifier;+ },});}}- // clear effectful expressions when depend on data sources- // outside of instances slice- //- // @todo should not be necessary once bindings UI is availableif (prop.type === "action") {- slicedProps.set(prop.id, {- ...prop,- value: prop.value.flatMap((value) => {- if (value.type !== "execute") {- return [value];- }- let shouldKeepAction = true;+ for (const value of prop.value) {+ if (value.type === "execute") {validateExpression(value.code, {effectful: true,- transformIdentifier: (identifier) => {- if (value.args.includes(identifier)) {- return identifier;- }+ transformIdentifier(identifier) {const id = decodeDataSourceVariable(identifier);- if (id === undefined) {- return identifier;- }- if (slicedDataSources.has(id) === false) {- shouldKeepAction = false;- return identifier;- }- const identifierDeps = dependencies.get(id);- if (identifierDeps) {- for (const dependency of identifierDeps) {- const id = decodeDataSourceVariable(dependency);- if (id === undefined) {- continue;- }- if (slicedDataSources.has(id) === false) {- shouldKeepAction = false;- return identifier;- }- }+ if (id !== undefined) {+ usedDataSourceIds.add(id);}return identifier;},});- if (shouldKeepAction) {- return [value];- }- return [];- }),- });+ }+ }}// collect assets@@ -764,6 +658,21 @@ export const getInstancesSlice = (rootInstanceId: string) => {}}+ // collect variables scoped to instances slice+ // or used by expressions or actions even outside of the tree+ // such variables can be bound to sliced root on paste+ const slicedDataSources: DataSources = new Map();+ for (const dataSource of dataSources.values()) {+ if (+ // check if data source itself can be copied+ (dataSource.scopeInstanceId !== undefined &&+ slicedInstanceIds.has(dataSource.scopeInstanceId)) ||+ usedDataSourceIds.has(dataSource.id)+ ) {+ slicedDataSources.set(dataSource.id, dataSource);+ }+ }+const slicedAssets: Asset[] = [];for (const asset of assets.values()) {if (diff --git a/packages/react-sdk/src/expression.test.ts b/packages/react-sdk/src/expression.test.tsindex b8435b4131..cdbdeda2ad 100644--- a/packages/react-sdk/src/expression.test.ts+++ b/packages/react-sdk/src/expression.test.ts@@ -2,7 +2,6 @@ import { expect, test } from "@jest/globals";import {decodeDataSourceVariable,encodeDataSourceVariable,- computeExpressionsDependencies,validateExpression,generateDataSources,} from "./expression";@@ -162,36 +161,6 @@ test("encode/decode variable names", () => {expect(decodeDataSourceVariable("myVarName")).toEqual(undefined);});-test("compute expressions dependencies", () => {- const expressions = new Map([- ["exp1", `var1`],- ["exp2", `exp1 + exp1`],- ["exp3", `exp1 + exp2`],- ["exp4", `var1 + exp1`],- ]);- expect(computeExpressionsDependencies(expressions)).toEqual(- new Map([- ["exp4", new Set(["var1", "exp1"])],- ["exp3", new Set(["var1", "exp1", "exp2"])],- ["exp2", new Set(["var1", "exp1"])],- ["exp1", new Set(["var1"])],- ])- );-});--test("handle cyclic dependencies", () => {- const expressions = new Map([- ["exp1", `exp2 + var1`],- ["exp2", `exp1 + var1`],- ]);- expect(computeExpressionsDependencies(expressions)).toEqual(- new Map([- ["exp2", new Set(["var1", "exp1", "exp2"])],- ["exp1", new Set(["var1", "exp1", "exp2"])],- ])- );-});-test("generate variables with actions", () => {const generated = generateDataSources({scope: createScope(),diff --git a/packages/react-sdk/src/expression.ts b/packages/react-sdk/src/expression.tsindex a00ef2b2d9..ff2c81af05 100644--- a/packages/react-sdk/src/expression.ts+++ b/packages/react-sdk/src/expression.ts@@ -166,50 +166,6 @@ export const validateExpression = (});};-const computeExpressionDependencies = (- expressions: Map<string, string>,- expressionId: string,- dependencies: Map<string, Set<string>>-) => {- // prevent recalculating expressions over again- const depsById = dependencies.get(expressionId);- if (depsById) {- return depsById;- }- const parentDeps = new Set<string>();- const code = expressions.get(expressionId);- if (code === undefined) {- return parentDeps;- }- // write before recursive call to avoid infinite cycle- dependencies.set(expressionId, parentDeps);- validateExpression(code, {- transformIdentifier: (id) => {- parentDeps.add(id);- const childDeps = computeExpressionDependencies(- expressions,- id,- dependencies- );- for (const depId of childDeps) {- parentDeps.add(depId);- }- return id;- },- });- return parentDeps;-};--export const computeExpressionsDependencies = (- expressions: Map<string, string>-) => {- const dependencies = new Map<string, Set<string>>();- for (const id of expressions.keys()) {- computeExpressionDependencies(expressions, id, dependencies);- }- return dependencies;-};-const dataSourceVariablePrefix = "$ws$dataSource$";// data source id is generated with nanoid which has "-" in alphabetadiff --git a/packages/react-sdk/src/index.ts b/packages/react-sdk/src/index.tsindex 89035a15c8..a08a2cc757 100644--- a/packages/react-sdk/src/index.ts+++ b/packages/react-sdk/src/index.ts@@ -22,7 +22,6 @@ export {export { type Params, ReactSdkContext } from "./context";export {validateExpression,- computeExpressionsDependencies,encodeDataSourceVariable,decodeDataSourceVariable,generateDataSources,