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 duplicating instances (#1995)

Bogdan Chadkinbe7468067460
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 645c6b89cf..677688209a 100644
--- a/apps/builder/app/shared/copy-paste/plugin-instance.test.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.test.ts
@@ -70,6 +70,62 @@ const getMapDifference = <Type extends Map<unknown, unknown>>(
return difference;
};
+describe("paste target", () => {
+ // body0
+ // box1
+ // box2
+ const instances: Instances = toMap([
+ createInstance("body0", "Body", [
+ { type: "id", value: "box1" },
+ { type: "id", value: "box2" },
+ ]),
+ createInstance("box1", "Box", []),
+ createInstance("box2", "Box", []),
+ ] satisfies Instance[]);
+
+ test("is inside selected instance", () => {
+ instancesStore.set(instances);
+ selectedInstanceSelectorStore.set(["box1", "body0"]);
+ const clipboardData = onCopy() ?? "";
+ selectedInstanceSelectorStore.set(["box2", "body0"]);
+ onPaste(clipboardData);
+
+ const instancesDifference = getMapDifference(
+ instances,
+ instancesStore.get()
+ );
+ const [newBox1] = instancesDifference.keys();
+ expect(instancesStore.get().get("box2")).toEqual(
+ createInstance("box2", "Box", [{ type: "id", value: newBox1 }])
+ );
+ expect(instancesDifference).toEqual(
+ toMap([createInstance(newBox1, "Box", [])])
+ );
+ });
+
+ test("is after selected instance when same as copied", () => {
+ instancesStore.set(instances);
+ selectedInstanceSelectorStore.set(["box1", "body0"]);
+ onPaste(onCopy() ?? "");
+
+ const instancesDifference = getMapDifference(
+ instances,
+ instancesStore.get()
+ );
+ const [newBox1] = instancesDifference.keys();
+ expect(instancesStore.get().get("body0")).toEqual(
+ createInstance("body0", "Body", [
+ { type: "id", value: "box1" },
+ { type: "id", value: newBox1 },
+ { type: "id", value: "box2" },
+ ])
+ );
+ expect(instancesDifference).toEqual(
+ toMap([createInstance(newBox1, "Box", [])])
+ );
+ });
+});
+
describe("data sources", () => {
// body0
// box1
diff --git a/apps/builder/app/shared/copy-paste/plugin-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.ts
index 88b5203cd4..eb57976d82 100644
--- a/apps/builder/app/shared/copy-paste/plugin-instance.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts
@@ -1,3 +1,4 @@
+import { shallowEqual } from "shallow-equal";
import { nanoid } from "nanoid";
import store from "immerhin";
import { z } from "zod";
@@ -43,6 +44,7 @@ import {
insertStyleSourceSelectionsCopyMutable,
findLocalStyleSourcesWithinInstances,
mergeNewBreakpointsMutable,
+ type DroppableTarget,
} from "../tree-utils";
import {
computeInstancesConstraints,
@@ -54,6 +56,7 @@ import { getMapValuesBy, getMapValuesByKeysSet } from "../array-utils";
const version = "@webstudio/instance/v0.1";
const InstanceData = z.object({
+ instanceSelector: z.array(z.string()),
breakpoints: z.array(Breakpoint),
instances: z.array(Instance),
props: z.array(Prop),
@@ -286,6 +289,7 @@ const getTreeData = (targetInstanceSelector: InstanceSelector) => {
);
return {
+ instanceSelector: targetInstanceSelector,
breakpoints: treeBreapoints,
instances: treeInstances,
styleSources: treeStyleSources,
@@ -337,15 +341,36 @@ export const onPaste = (clipboardData: string): boolean => {
const instanceSelector = selectedInstanceSelectorStore.get() ?? [
selectedPage.rootInstanceId,
];
- const dropTarget = findClosestDroppableTarget(
- metas,
- instancesStore.get(),
- instanceSelector,
- computeInstancesConstraints(metas, newInstances, [rootInstanceId])
- );
- if (dropTarget === undefined) {
+ let potentialDropTarget: undefined | DroppableTarget;
+ if (shallowEqual(instanceSelector, data.instanceSelector)) {
+ // paste after selected instance
+ const instances = instancesStore.get();
+ // body is not allowed to copy
+ // so clipboard always have at least two level instance selector
+ const [currentInstanceId, parentInstanceId] = instanceSelector;
+ const parentInstance = instances.get(parentInstanceId);
+ if (parentInstance === undefined) {
+ return false;
+ }
+ const indexWithinChildren = parentInstance.children.findIndex(
+ (child) => child.type === "id" && child.value === currentInstanceId
+ );
+ potentialDropTarget = {
+ parentSelector: instanceSelector.slice(1),
+ position: indexWithinChildren + 1,
+ };
+ } else {
+ potentialDropTarget = findClosestDroppableTarget(
+ metas,
+ instancesStore.get(),
+ instanceSelector,
+ computeInstancesConstraints(metas, newInstances, [rootInstanceId])
+ );
+ }
+ if (potentialDropTarget === undefined) {
return false;
}
+ const dropTarget = potentialDropTarget;
store.createTransaction(
[
diff --git a/apps/builder/app/shared/shortcuts/shortcuts.ts b/apps/builder/app/shared/shortcuts/shortcuts.ts
index 7611be8f11..bcdafb1611 100644
--- a/apps/builder/app/shared/shortcuts/shortcuts.ts
+++ b/apps/builder/app/shared/shortcuts/shortcuts.ts
@@ -6,6 +6,7 @@ import {
editingItemIdStore,
selectedInstanceSelectorStore,
} from "../nano-states";
+import { onCopy, onPaste } from "../copy-paste/plugin-instance";
export const shortcuts = {
esc: "esc",
@@ -55,6 +56,16 @@ export const useSharedShortcuts = ({
[]
);
+ useHotkeys(
+ "meta+d, ctrl+d",
+ (event) => {
+ event.preventDefault();
+ onPaste(onCopy() ?? "");
+ },
+ {},
+ []
+ );
+
useHotkeys(
"meta+e, 'ctrl+e",
() => {
diff --git a/apps/builder/app/shared/store-utils.test.ts b/apps/builder/app/shared/store-utils.test.ts
index 55d3435163..d75ed5347d 100644
--- a/apps/builder/app/shared/store-utils.test.ts
+++ b/apps/builder/app/shared/store-utils.test.ts
@@ -50,4 +50,7 @@ test("shallowComputed provides same reference when object/array values not chang
{ type: "a", value: 4 },
]);
prevList = computedList;
+
+ list.off();
+ filtered.off();
});