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 cross-project instance copy paste (#1326)

Bogdan Chadkin59b9c6fedc7c
diff --git a/apps/builder/app/shared/array-utils.test.ts b/apps/builder/app/shared/array-utils.test.ts
index 4610048c93..b8442a88b4 100644
--- a/apps/builder/app/shared/array-utils.test.ts
+++ b/apps/builder/app/shared/array-utils.test.ts
@@ -1,5 +1,9 @@
import { expect, test } from "@jest/globals";
-import { removeByMutable } from "./array-utils";
+import {
+ getMapValuesBy,
+ getMapValuesByKeysSet,
+ removeByMutable,
+} from "./array-utils";
test("removeByMutable", () => {
const array = [
@@ -33,3 +37,28 @@ test("removeByMutable", () => {
]
`);
});
+
+test("getMapValuesByKeysSet", () => {
+ const map = new Map([
+ [1, "value1"],
+ [2, "value2"],
+ [3, "value3"],
+ [4, "value4"],
+ [5, "value5"],
+ ]);
+ const keys = new Set([2, 4]);
+ expect(getMapValuesByKeysSet(map, keys)).toEqual(["value2", "value4"]);
+});
+
+test("getMapValuesBy", () => {
+ const map = new Map([
+ [1, "value1"],
+ [2, "value2"],
+ [3, "value3"],
+ [4, "value4"],
+ [5, "value5"],
+ ]);
+ expect(
+ getMapValuesBy(map, (value) => value.includes("3") || value.includes("5"))
+ ).toEqual(["value3", "value5"]);
+});
diff --git a/apps/builder/app/shared/array-utils.ts b/apps/builder/app/shared/array-utils.ts
index 2ca3ef00f7..52fbbff7c3 100644
--- a/apps/builder/app/shared/array-utils.ts
+++ b/apps/builder/app/shared/array-utils.ts
@@ -11,3 +11,30 @@ export const removeByMutable = <Item>(
}
}
};
+
+export const getMapValuesByKeysSet = <Key, Value>(
+ map: Map<Key, Value>,
+ keys: Set<Key>
+) => {
+ const values: Value[] = [];
+ for (const key of keys) {
+ const value = map.get(key);
+ if (value !== undefined) {
+ values.push(value);
+ }
+ }
+ return values;
+};
+
+export const getMapValuesBy = <Key, Value>(
+ map: Map<Key, Value>,
+ predicate: Predicate<Value>
+) => {
+ const values: Value[] = [];
+ for (const value of map.values()) {
+ if (predicate(value)) {
+ values.push(value);
+ }
+ }
+ return values;
+};
diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.ts
index 3cab431f16..bce9344e5f 100644
--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts
@@ -1,12 +1,14 @@
import store from "immerhin";
import { z } from "zod";
import {
- findTreeInstanceIdsExcludingSlotDescendants,
+ findTreeInstanceIds,
+ Instance,
InstancesItem,
Prop,
StyleDecl,
StyleSource,
StyleSourceSelection,
+ StyleSourceSelections,
} from "@webstudio-is/project-build";
import {
propsStore,
@@ -20,14 +22,15 @@ import {
import {
type InstanceSelector,
findClosestDroppableTarget,
- findSubtreeLocalStyleSources,
insertInstancesCopyMutable,
insertPropsCopyMutable,
insertStylesCopyMutable,
insertStyleSourcesCopyMutable,
insertStyleSourceSelectionsCopyMutable,
+ findSubtreeLocalStyleSources,
} from "../tree-utils";
import { deleteInstance } from "../instance-utils";
+import { getMapValuesBy, getMapValuesByKeysSet } from "../array-utils";
const version = "@webstudio/instance/v0.1";
@@ -41,6 +44,23 @@ const InstanceData = z.object({
type InstanceData = z.infer<typeof InstanceData>;
+const findTreeStyleSourceIds = (
+ styleSourceSelections: StyleSourceSelections,
+ treeInstanceIds: Set<Instance["id"]>
+) => {
+ const treeStyleSourceIds = new Set<StyleSource["id"]>();
+ for (const { instanceId, values } of styleSourceSelections.values()) {
+ // skip selections outside of tree
+ if (treeInstanceIds.has(instanceId) === false) {
+ continue;
+ }
+ for (const styleSourceId of values) {
+ treeStyleSourceIds.add(styleSourceId);
+ }
+ }
+ return treeStyleSourceIds;
+};
+
const getTreeData = (targetInstanceSelector: InstanceSelector) => {
// @todo tell user they can't copy or cut root
if (targetInstanceSelector.length === 1) {
@@ -49,57 +69,33 @@ const getTreeData = (targetInstanceSelector: InstanceSelector) => {
const [targetInstanceId] = targetInstanceSelector;
const instances = instancesStore.get();
- const treeInstanceIds = findTreeInstanceIdsExcludingSlotDescendants(
- instances,
- targetInstanceId
- );
- const styleSources = styleSourcesStore.get();
+ const treeInstanceIds = findTreeInstanceIds(instances, targetInstanceId);
const styleSourceSelections = styleSourceSelectionsStore.get();
- const subtreeLocalStyleSourceIds = findSubtreeLocalStyleSources(
- treeInstanceIds,
- styleSources,
- styleSourceSelections
+ const treeStyleSourceIds = findTreeStyleSourceIds(
+ styleSourceSelections,
+ treeInstanceIds
);
// first item is guaranteed root of copied tree
- const treeInstances: InstancesItem[] = [];
- for (const instanceId of treeInstanceIds) {
- const instance = instances.get(instanceId);
- if (instance) {
- treeInstances.push(instance);
- }
- }
+ const treeInstances = getMapValuesByKeysSet(instances, treeInstanceIds);
- const treeStyleSources: StyleSource[] = [];
- for (const styleSourceId of subtreeLocalStyleSourceIds) {
- const styleSource = styleSources.get(styleSourceId);
- if (styleSource) {
- treeStyleSources.push(styleSource);
- }
- }
+ const treeProps = getMapValuesBy(propsStore.get(), (prop) =>
+ treeInstanceIds.has(prop.instanceId)
+ );
- const props = propsStore.get();
- const treeProps: Prop[] = [];
- for (const prop of props.values()) {
- if (treeInstanceIds.has(prop.instanceId)) {
- treeProps.push(prop);
- }
- }
+ const treeStyleSourceSelections = getMapValuesByKeysSet(
+ styleSourceSelections,
+ treeInstanceIds
+ );
- const treeStyleSourceSelections: StyleSourceSelection[] = [];
- for (const styleSourceSelection of styleSourceSelections.values()) {
- if (treeInstanceIds.has(styleSourceSelection.instanceId)) {
- treeStyleSourceSelections.push(styleSourceSelection);
- }
- }
+ const treeStyleSources = getMapValuesByKeysSet(
+ styleSourcesStore.get(),
+ treeStyleSourceIds
+ );
- const styles = stylesStore.get();
- const treeStyles: StyleDecl[] = [];
- for (const styleDecl of styles.values()) {
- if (subtreeLocalStyleSourceIds.has(styleDecl.styleSourceId)) {
- treeStyles.push(styleDecl);
- }
- }
+ const treeStyles = getMapValuesBy(stylesStore.get(), (styleDecl) =>
+ treeStyleSourceIds.has(styleDecl.styleSourceId)
+ );
return {
instances: treeInstances,
@@ -155,10 +151,27 @@ export const onPaste = (clipboardData: string) => {
data.instances,
dropTarget
);
+
+ // find all local style sources of copied instances
+ const newStyleSourceIds = findSubtreeLocalStyleSources(
+ new Set(copiedInstanceIds.values()),
+ new Map(
+ data.styleSources.map((styleSource) => [styleSource.id, styleSource])
+ ),
+ new Map(
+ data.styleSourceSelections.map((styleSourceSelection) => [
+ styleSourceSelection.instanceId,
+ styleSourceSelection,
+ ])
+ )
+ );
+
const copiedStyleSourceIds = insertStyleSourcesCopyMutable(
styleSources,
- data.styleSources
+ data.styleSources,
+ newStyleSourceIds
);
+
insertPropsCopyMutable(props, data.props, copiedInstanceIds);
insertStyleSourceSelectionsCopyMutable(
styleSourceSelections,
diff --git a/apps/builder/app/shared/tree-utils.test.ts b/apps/builder/app/shared/tree-utils.test.ts
index 5d167c01a2..2eedf8aed0 100644
--- a/apps/builder/app/shared/tree-utils.test.ts
+++ b/apps/builder/app/shared/tree-utils.test.ts
@@ -47,13 +47,13 @@ const createInstancePair = (
return [id, createInstance(id, component, children)];
};
-const createProp = (id: string, instanceId: string): Prop => {
+const createProp = (id: string, instanceId: string, value?: string): Prop => {
return {
id,
instanceId,
type: "string",
name: "prop",
- value: "value",
+ value: value ?? "value",
};
};
@@ -86,7 +86,8 @@ const createStyleSourceSelection = (
const createStyleDecl = (
styleSourceId: string,
- breakpointId: string
+ breakpointId: string,
+ value?: string
): StyleDecl => {
return {
styleSourceId,
@@ -94,7 +95,7 @@ const createStyleDecl = (
property: "width",
value: {
type: "keyword",
- value: "value",
+ value: value ?? "value",
},
};
};
@@ -472,7 +473,7 @@ test("insert slot or do nothing when slot is cyclic", () => {
const instances = new Map([
createInstancePair("root", "Body", [
{ type: "id", value: "slot" },
- { type: "id", value: 'sibling",' },
+ { type: "id", value: "sibling" },
]),
createInstancePair("slot", "Slot", [{ type: "id", value: "fragment" }]),
createInstancePair("fragment", "Fragment", [
@@ -481,7 +482,7 @@ test("insert slot or do nothing when slot is cyclic", () => {
createInstancePair("child", "Box", []),
createInstancePair("sibling", "Box", []),
]);
- const copiedInstances = [
+ let copiedInstances = [
createInstance("slot", "Slot", [{ type: "id", value: "fragment" }]),
];
@@ -498,7 +499,7 @@ test("insert slot or do nothing when slot is cyclic", () => {
expect(Array.from(instances.entries())).toEqual([
createInstancePair("root", "Body", [
{ type: "id", value: "slot" },
- { type: "id", value: 'sibling",' },
+ { type: "id", value: "sibling" },
]),
createInstancePair("slot", "Slot", [{ type: "id", value: "fragment" }]),
createInstancePair("fragment", "Fragment", [
@@ -508,7 +509,7 @@ test("insert slot or do nothing when slot is cyclic", () => {
createInstancePair("sibling", "Box", []),
]);
- // insert slot into itself
+ // do not insert slot into itself
copiedInstanceIds = insertInstancesCopyMutable(instances, copiedInstances, {
parentSelector: ["slot", "root"],
position: "end",
@@ -517,7 +518,7 @@ test("insert slot or do nothing when slot is cyclic", () => {
expect(Array.from(instances.entries())).toEqual([
createInstancePair("root", "Body", [
{ type: "id", value: "slot" },
- { type: "id", value: 'sibling",' },
+ { type: "id", value: "sibling" },
]),
createInstancePair("slot", "Slot", [{ type: "id", value: "fragment" }]),
createInstancePair("fragment", "Fragment", [
@@ -536,7 +537,37 @@ test("insert slot or do nothing when slot is cyclic", () => {
expect(Array.from(instances.entries())).toEqual([
createInstancePair("root", "Body", [
{ type: "id", value: "slot" },
- { type: "id", value: 'sibling",' },
+ { type: "id", value: "sibling" },
+ ]),
+ createInstancePair("slot", "Slot", [{ type: "id", value: "fragment" }]),
+ createInstancePair("fragment", "Fragment", [
+ { type: "id", value: "child" },
+ ]),
+ createInstancePair("child", "Box", []),
+ createInstancePair("sibling", "Box", [{ type: "id", value: expectString }]),
+ createInstancePair(expectString, "Slot", [
+ { type: "id", value: "fragment" },
+ ]),
+ ]);
+
+ // insert new slot from other project
+ copiedInstances = [
+ createInstance("slot2", "Slot", [{ type: "id", value: "fragment2" }]),
+ createInstance("fragment2", "Fragment", [
+ { type: "id", value: "slot2box" },
+ ]),
+ createInstance("slot2box", "Box", []),
+ ];
+ copiedInstanceIds = insertInstancesCopyMutable(instances, copiedInstances, {
+ parentSelector: ["root"],
+ position: "end",
+ });
+ expect(copiedInstanceIds).toEqual(new Map([["slot2", expectString]]));
+ expect(Array.from(instances.entries())).toEqual([
+ createInstancePair("root", "Body", [
+ { type: "id", value: "slot" },
+ { type: "id", value: "sibling" },
+ { type: "id", value: expectString },
]),
createInstancePair("slot", "Slot", [{ type: "id", value: "fragment" }]),
createInstancePair("fragment", "Fragment", [
@@ -547,6 +578,13 @@ test("insert slot or do nothing when slot is cyclic", () => {
createInstancePair(expectString, "Slot", [
{ type: "id", value: "fragment" },
]),
+ createInstancePair("fragment2", "Fragment", [
+ { type: "id", value: "slot2box" },
+ ]),
+ createInstancePair("slot2box", "Box", []),
+ createInstancePair(expectString, "Slot", [
+ { type: "id", value: "fragment2" },
+ ]),
]);
});
@@ -554,14 +592,19 @@ test("insert style sources copy with new ids and provide map from old ids", () =
const styleSources = new Map([
["local1", createStyleSource("local", "local1")],
["local2", createStyleSource("local", "local2")],
+ ["token3", createStyleSource("token", "token3")],
]);
const copiedStyleSources = [
createStyleSource("local", "local1"),
createStyleSource("local", "local2"),
+ createStyleSource("local", "local4"),
+ createStyleSource("token", "token5"),
];
+ const newStyleSourceIds = new Set(["local1", "local2"]);
const copiedStyleSourceIds = insertStyleSourcesCopyMutable(
styleSources,
- copiedStyleSources
+ copiedStyleSources,
+ newStyleSourceIds
);
expect(Array.from(copiedStyleSourceIds.entries())).toEqual([
["local1", expectString],
@@ -570,8 +613,14 @@ test("insert style sources copy with new ids and provide map from old ids", () =
expect(Array.from(styleSources.entries())).toEqual([
["local1", createStyleSource("local", "local1")],
["local2", createStyleSource("local", "local2")],
+ // shared prop should not be overriden
+ ["token3", createStyleSource("token", "token3")],
+ // new style sources are copied
[expectString, createStyleSource("local", expectString)],
[expectString, createStyleSource("local", expectString)],
+ // shared style sources are inserted without changes
+ ["local4", createStyleSource("local", "local4")],
+ ["token5", createStyleSource("token", "token5")],
]);
});
@@ -580,10 +629,12 @@ test("insert props copy with new ids and apply new instance ids", () => {
["prop1", createProp("prop1", "instance1")],
["prop2", createProp("prop2", "instance2")],
["prop3", createProp("prop3", "instance3")],
+ ["existingSharedProp", createProp("existingSharedProp", "instance3")],
]);
const copiedProps = [
- createProp("prop1", "instance1"),
createProp("prop2", "instance2"),
+ createProp("sharedProp", "instance3", "newValue"),
+ createProp("existingSharedProp", "instance3", "newValue"),
];
const clonedInstanceIds = new Map<Instance["id"], Instance["id"]>([
["instance2", "newInstance2"],
@@ -593,8 +644,12 @@ test("insert props copy with new ids and apply new instance ids", () => {
["prop1", createProp("prop1", "instance1")],
["prop2", createProp("prop2", "instance2")],
["prop3", createProp("prop3", "instance3")],
- [expectString, createProp(expectString, "instance1")],
+ // shared prop is not overriden
+ ["existingSharedProp", createProp("existingSharedProp", "instance3")],
+ // new props are copied
[expectString, createProp(expectString, "newInstance2")],
+ // shared prop inserted without changes
+ ["sharedProp", createProp("sharedProp", "instance3", "newValue")],
]);
});
@@ -613,6 +668,8 @@ test("insert style source selections copy and apply new instance ids and style s
const copiedStyleSourceSelections = [
createStyleSourceSelection("instance1", ["local1", "token2"]),
createStyleSourceSelection("instance2", ["token3", "local4", "token5"]),
+ createStyleSourceSelection("instance3", ["local6", "token5"]),
+ createStyleSourceSelection("instance4", ["token5"]),
];
const copiedStyleSourceIds = new Map<StyleSource["id"], StyleSource["id"]>([
["local1", "newLocal1"],
@@ -650,6 +707,7 @@ test("insert style source selections copy and apply new instance ids and style s
"token5",
]),
],
+ ["instance4", createStyleSourceSelection("instance4", ["token5"])],
]);
});
@@ -659,10 +717,16 @@ test("insert styles copy and apply new style source ids", () => {
[`styleSource2:bp2:width`, createStyleDecl("styleSource2", "bp2")],
[`styleSource1:bp3:width`, createStyleDecl("styleSource1", "bp3")],
[`styleSource3:bp4:width`, createStyleDecl("styleSource3", "bp4")],
+ [
+ `existingSharedStyleSource:bp4:width`,
+ createStyleDecl("existingSharedStyleSource", "bp4"),
+ ],
]);
const copiedStyles = [
createStyleDecl("styleSource2", "bp2"),
createStyleDecl("styleSource3", "bp4"),
+ createStyleDecl("sharedStyleSource", "bp4"),
+ createStyleDecl("existingSharedStyleSource", "bp4", "newValue"),
];
const copiedStyleSourceIds = new Map<StyleSource["id"], StyleSource["id"]>([
["styleSource2", "newStyleSource2"],
@@ -674,8 +738,19 @@ test("insert styles copy and apply new style source ids", () => {
[`styleSource2:bp2:width`, createStyleDecl("styleSource2", "bp2")],
[`styleSource1:bp3:width`, createStyleDecl("styleSource1", "bp3")],
[`styleSource3:bp4:width`, createStyleDecl("styleSource3", "bp4")],
+ // shared style is not overriden
+ [
+ `existingSharedStyleSource:bp4:width`,
+ createStyleDecl("existingSharedStyleSource", "bp4"),
+ ],
+ // new styles are copied
[`newStyleSource2:bp2:width`, createStyleDecl("newStyleSource2", "bp2")],
[`newStyleSource3:bp4:width`, createStyleDecl("newStyleSource3", "bp4")],
+ // shared style inserted without changes
+ [
+ `sharedStyleSource:bp4:width`,
+ createStyleDecl("sharedStyleSource", "bp4"),
+ ],
]);
});
diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.ts
index 2fb049c0ec..27d3df2015 100644
--- a/apps/builder/app/shared/tree-utils.ts
+++ b/apps/builder/app/shared/tree-utils.ts
@@ -1,5 +1,6 @@
import { nanoid } from "nanoid";
import {
+ findTreeInstanceIdsExcludingSlotDescendants,
getStyleDeclKey,
Instance,
Instances,
@@ -337,22 +338,39 @@ export const insertInstancesCopyMutable = (
copiedInstances: InstancesItem[],
dropTarget: DroppableTarget
) => {
+ const newInstances: Instances = new Map();
+ for (const instance of copiedInstances) {
+ newInstances.set(instance.id, instance);
+ }
+ const newInstanceIds = findTreeInstanceIdsExcludingSlotDescendants(
+ newInstances,
+ copiedInstances[0].id
+ );
+
const copiedInstanceIds = new Map<Instance["id"], Instance["id"]>();
const copiedInstancesWithNewIds: InstancesItem[] = [];
- for (const instance of copiedInstances) {
+ for (const instanceId of newInstanceIds) {
const newInstanceId = nanoid();
- copiedInstanceIds.set(instance.id, newInstanceId);
+ copiedInstanceIds.set(instanceId, newInstanceId);
}
const preservedChildIds = new Set<Instance["id"]>();
for (const instance of copiedInstances) {
+ const newInstanceId = copiedInstanceIds.get(instance.id);
+ if (newInstanceId === undefined) {
+ if (instances.has(instance.id) === false) {
+ instances.set(instance.id, instance);
+ }
+ continue;
+ }
+
copiedInstancesWithNewIds.push({
...instance,
- id: copiedInstanceIds.get(instance.id) ?? instance.id,
+ id: newInstanceId,
children: instance.children.map((child) => {
if (child.type === "id") {
- if (copiedInstanceIds.has(child.value) === false) {
+ if (newInstanceIds.has(child.value) === false) {
preservedChildIds.add(child.value);
}
return {
@@ -391,11 +409,21 @@ export const insertInstancesCopyMutable = (
export const insertStyleSourcesCopyMutable = (
styleSources: StyleSources,
- copiedStyleSources: StyleSource[]
+ copiedStyleSources: StyleSource[],
+ newStyleSourceIds: Set<StyleSource["id"]>
) => {
// store map of old ids to new ids to copy dependant data
const copiedStyleSourceIds = new Map<StyleSource["id"], StyleSource["id"]>();
for (const styleSource of copiedStyleSources) {
+ // insert without changes when style source is shared
+ if (newStyleSourceIds.has(styleSource.id) === false) {
+ // prevent overriding shared style sources if already exist
+ if (styleSources.has(styleSource.id) === false) {
+ styleSources.set(styleSource.id, styleSource);
+ }
+ continue;
+ }
+
const newStyleSourceId = nanoid();
copiedStyleSourceIds.set(styleSource.id, newStyleSourceId);
styleSources.set(newStyleSourceId, {
@@ -412,8 +440,17 @@ export const insertPropsCopyMutable = (
copiedInstanceIds: Map<Instance["id"], Instance["id"]>
) => {
for (const prop of copiedProps) {
- const newInstanceId =
- copiedInstanceIds.get(prop.instanceId) ?? prop.instanceId;
+ const newInstanceId = copiedInstanceIds.get(prop.instanceId);
+ // insert without changes when instance does not have new id
+ if (newInstanceId === undefined) {
+ // prevent overriding shared props if already exist
+ if (props.has(prop.id) === false) {
+ props.set(prop.id, prop);
+ }
+ continue;
+ }
+
+ // copy prop before inserting
const newPropId = nanoid();
props.set(newPropId, {
...prop,
@@ -430,13 +467,20 @@ export const insertStyleSourceSelectionsCopyMutable = (
copiedStyleSourceIds: Map<StyleSource["id"], StyleSource["id"]>
) => {
for (const styleSourceSelection of copiedStyleSourceSelections) {
- // preserve ids when no new id provided
- // for example for non-local style source
- const newInstanceId =
- copiedInstanceIds.get(styleSourceSelection.instanceId) ??
- styleSourceSelection.instanceId;
+ // insert without changes when style source selection does not have new instance id
+ const { instanceId } = styleSourceSelection;
+ const newInstanceId = copiedInstanceIds.get(instanceId);
+ if (newInstanceId === undefined) {
+ // prevent overriding shared style source selections if already exist
+ if (styleSourceSelections.has(instanceId) === false) {
+ styleSourceSelections.set(instanceId, styleSourceSelection);
+ }
+ continue;
+ }
+
const newValues = styleSourceSelection.values.map(
(styleSourceId) =>
+ // preserve shared style source ids
copiedStyleSourceIds.get(styleSourceId) ?? styleSourceId
);
styleSourceSelections.set(newInstanceId, {
@@ -452,10 +496,17 @@ export const insertStylesCopyMutable = (
copiedStyleSourceIds: Map<StyleSource["id"], StyleSource["id"]>
) => {
for (const styleDecl of copiedStyles) {
- // preserve ids when no new id provided
- const newStyleSourceId =
- copiedStyleSourceIds.get(styleDecl.styleSourceId) ??
- styleDecl.styleSourceId;
+ const newStyleSourceId = copiedStyleSourceIds.get(styleDecl.styleSourceId);
+ // insert without changes when style source does not have new id
+ if (newStyleSourceId === undefined) {
+ const styleDeclKey = getStyleDeclKey(styleDecl);
+ // prevent overriding shared styles if already exist
+ if (styles.has(styleDeclKey) === false) {
+ styles.set(styleDeclKey, styleDecl);
+ }
+ continue;
+ }
+
const styleDeclCopy = {
...styleDecl,
styleSourceId: newStyleSourceId,