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.

Support actions copy paste (#1976)

Bogdan Chadkin685ca245528f
diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.test.ts b/apps/builder/app/shared/copy-paste/plugin-instance.test.ts
index 677688209a..9e59ef84d6 100644
--- a/apps/builder/app/shared/copy-paste/plugin-instance.test.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.test.ts
@@ -173,6 +173,19 @@ describe("data sources", () => {
name: "show",
value: "box2$stateInitial",
},
+ {
+ id: "box2$onChange",
+ instanceId: "box2",
+ type: "action",
+ name: "onChange",
+ value: [
+ {
+ type: "execute",
+ args: ["value"],
+ code: `$ws$dataSource$box1$state = value`,
+ },
+ ],
+ },
] satisfies Prop[]);
test("are copy pasted when scoped to copied instances", () => {
@@ -212,7 +225,7 @@ describe("data sources", () => {
);
const propsDifference = getMapDifference(props, propsStore.get());
- const [newProp1, newProp2, newProp3] = propsDifference.keys();
+ const [newProp1, newProp2, newProp3, newProp4] = propsDifference.keys();
expect(propsDifference).toEqual(
toMap([
{
@@ -233,6 +246,18 @@ describe("data sources", () => {
instanceId: newBox2,
value: newDataSource2,
},
+ {
+ ...props.get("box2$onChange"),
+ id: newProp4,
+ instanceId: newBox2,
+ value: [
+ {
+ type: "execute",
+ args: ["value"],
+ code: `${encodeDataSourceVariable(newDataSource1)} = value`,
+ },
+ ],
+ },
])
);
});
@@ -259,7 +284,7 @@ describe("data sources", () => {
expect(dataSourcesDifference).toEqual(new Map());
const propsDifference = getMapDifference(props, propsStore.get());
- const [newProp1, newProp2] = propsDifference.keys();
+ const [newProp1, newProp2, newProp3] = propsDifference.keys();
expect(propsDifference).toEqual(
toMap([
{
@@ -276,6 +301,13 @@ describe("data sources", () => {
name: "show",
value: true,
},
+ {
+ id: newProp3,
+ instanceId: newBox2,
+ type: "action",
+ name: "onChange",
+ value: [],
+ },
])
);
});
diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.ts
index 21cc125064..5f8b916980 100644
--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts
@@ -266,6 +266,51 @@ const getTreeData = (targetInstanceSelector: InstanceSelector) => {
...getPropTypeAndValue(value),
} satisfies Prop;
}
+ if (prop.type === "action") {
+ return {
+ ...prop,
+ value: prop.value.flatMap((value) => {
+ if (value.type !== "execute") {
+ return [value];
+ }
+ let shouldKeepAction = true;
+ validateExpression(value.code, {
+ effectful: true,
+ transformIdentifier: (identifier) => {
+ if (value.args.includes(identifier)) {
+ return identifier;
+ }
+ const id = decodeDataSourceVariable(identifier);
+ if (id === undefined) {
+ return identifier;
+ }
+ if (treeDataSourceIds.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 (treeDataSourceIds.has(id) === false) {
+ shouldKeepAction = false;
+ return identifier;
+ }
+ }
+ }
+ return identifier;
+ },
+ });
+ if (shouldKeepAction) {
+ return [value];
+ }
+ return [];
+ }),
+ };
+ }
return prop;
});
@@ -480,7 +525,36 @@ export const onPaste = (clipboardData: string): boolean => {
insertPropsCopyMutable(
props,
- data.props,
+ data.props.map((prop) => {
+ if (prop.type === "action") {
+ return {
+ ...prop,
+ value: prop.value.map((value) => {
+ if (value.type !== "execute") {
+ 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);
+ },
+ }),
+ };
+ }),
+ };
+ }
+ return prop;
+ }),
copiedInstanceIds,
copiedDataSourceIds
);