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
copy/paste an instance together with props
Oleg Isonenfad69cbacadd
diff --git a/app/canvas/shared/copy-paste.ts b/app/canvas/shared/copy-paste.tsnew file mode 100644index 0000000000..20d3524a7d--- /dev/null+++ b/app/canvas/shared/copy-paste.ts@@ -0,0 +1,30 @@+import {+ type InstanceProps,+ type Instance,+ allUserPropsContainer,+} from "@webstudio-is/sdk";+import { selectedInstanceContainer } from "./nano-values";+import { cloneInstance } from "~/shared/tree-utils";+import { cloneProps } from "~/shared/props-utils";+import { publish } from "./pubsub";++let currentInstance: Instance;+let currentProps: InstanceProps;++export const copy = () => {+ if (selectedInstanceContainer.value === undefined) return;+ currentInstance = selectedInstanceContainer.value;+ currentProps = allUserPropsContainer.value[currentInstance.id];+};++export const paste = () => {+ if (currentInstance === undefined) return;+ const instance = cloneInstance(currentInstance);+ publish<"insertInstance", { instance: Instance; props: InstanceProps }>({+ type: "insertInstance",+ payload: {+ instance,+ props: cloneProps(currentProps, { instanceId: instance.id }),+ },+ });+};diff --git a/app/canvas/shared/nano-values.ts b/app/canvas/shared/nano-values.tsindex 03995efa21..8d1e800233 100644--- a/app/canvas/shared/nano-values.ts+++ b/app/canvas/shared/nano-values.ts@@ -6,7 +6,9 @@ import store from "immerhin";const dropDataContainer = createValueContainer<DropData | undefined>();export const useDropData = () => useValue(dropDataContainer);-const selectedInstanceContainer = createValueContainer<Instance | undefined>();+export const selectedInstanceContainer = createValueContainer<+ Instance | undefined+>();export const useSelectedInstance = () => useValue(selectedInstanceContainer);const selectedElementContainer = createValueContainer<Element | undefined>();diff --git a/app/canvas/shared/tree.ts b/app/canvas/shared/tree.tsindex c62d724ff6..93a970de30 100644--- a/app/canvas/shared/tree.ts+++ b/app/canvas/shared/tree.ts@@ -1,5 +1,10 @@import { useEffect } from "react";-import { type Instance, type Tree } from "@webstudio-is/sdk";+import {+ type InstanceProps,+ type Instance,+ type Tree,+ allUserPropsContainer,+} from "@webstudio-is/sdk";import {deleteInstanceMutable,populateInstance,@@ -26,10 +31,13 @@ export const usePopulateRootInstance = (tree: Tree) => {export const useInsertInstance = () => {const [selectedInstance, setSelectedInstance] = useSelectedInstance();- useSubscribe<"insertInstance", { instance: Instance; dropData?: DropData }>(+ useSubscribe<"insertInstance",- ({ instance, dropData }) => {- store.createTransaction([rootInstanceContainer], (rootInstance) => {+ { instance: Instance; dropData?: DropData; props?: InstanceProps }+ >("insertInstance", ({ instance, dropData, props }) => {+ store.createTransaction(+ [rootInstanceContainer, allUserPropsContainer],+ (rootInstance, allUserProps) => {if (rootInstance === undefined) return;const populatedInstance = populateInstance(instance);const hasInserted = insertInstanceMutable(@@ -44,9 +52,12 @@ export const useInsertInstance = () => {if (hasInserted) {setSelectedInstance(instance);}- });- }- );+ if (props !== undefined) {+ allUserProps[props.instanceId] = props;+ }+ }+ );+ });};export const useReparentInstance = () => {diff --git a/app/canvas/shared/use-shortcuts.ts b/app/canvas/shared/use-shortcuts.tsindex b467d5792e..3a3c253835 100644--- a/app/canvas/shared/use-shortcuts.ts+++ b/app/canvas/shared/use-shortcuts.ts@@ -1,9 +1,10 @@import { useHotkeys } from "react-hotkeys-hook";+import store from "immerhin";import { type Instance } from "@webstudio-is/sdk";+import { shortcuts } from "~/shared/shortcuts";import { useRootInstance, useSelectedInstance } from "./nano-values";import { publish, useSubscribe } from "./pubsub";-import store from "immerhin";-import { shortcuts } from "~/shared/shortcuts";+import { copy, paste } from "./copy-paste";const togglePreviewMode = () => {publish<"togglePreviewMode">({ type: "togglePreviewMode" });@@ -34,13 +35,15 @@ export const useShortcuts = () => {redo: store.redo.bind(store),delete: publishDeleteInstance,preview: togglePreviewMode,+ copy,+ paste,} as const;useHotkeys("backspace, delete",- publishDeleteInstance,+ shortcutHandlerMap.delete,{ enableOnTags: ["INPUT", "SELECT", "TEXTAREA"] },- [selectedInstance]+ [shortcutHandlerMap.delete]);useHotkeys(@@ -48,10 +51,7 @@ export const useShortcuts = () => {() => {if (selectedInstance === undefined) return;setSelectedInstance(undefined);- publish<"selectInstance", undefined>({- type: "selectInstance",- payload: undefined,- });+ publish<"selectInstance">({ type: "selectInstance" });},[selectedInstance]);@@ -62,6 +62,12 @@ export const useShortcuts = () => {useHotkeys(shortcuts.preview, shortcutHandlerMap.preview, []);+ useHotkeys(shortcuts.copy, shortcutHandlerMap.copy, [+ shortcutHandlerMap.copy,+ ]);++ useHotkeys(shortcuts.paste, shortcutHandlerMap.paste, []);+// Shortcuts from the parent windowuseSubscribe<"shortcut", keyof typeof shortcuts>("shortcut", (shortcut) => {shortcutHandlerMap[shortcut]();diff --git a/app/shared/props-utils/clone-props.test.ts b/app/shared/props-utils/clone-props.test.tsnew file mode 100644index 0000000000..743cf65433--- /dev/null+++ b/app/shared/props-utils/clone-props.test.ts@@ -0,0 +1,20 @@+import { cloneProps } from "./clone-props";++describe("Clone props", () => {+ test("update ids", () => {+ const instanceProps = {+ props: [{ id: "1", prop: "a", value: "1" }],+ id: "id",+ instanceId: "instanceId",+ treeId: "treeId",+ };++ const clonedProps = cloneProps(instanceProps, {+ instanceId: "newInstanceId",+ });+ expect(clonedProps.id).not.toBe(instanceProps.id);+ expect(clonedProps.instanceId).toBe("newInstanceId");+ expect(clonedProps.treeId).toBe(instanceProps.treeId);+ expect(clonedProps.props[0].id).not.toBe(instanceProps.props[0].id);+ });+});diff --git a/app/shared/props-utils/clone-props.ts b/app/shared/props-utils/clone-props.tsnew file mode 100644index 0000000000..7f8b51d9d5--- /dev/null+++ b/app/shared/props-utils/clone-props.ts@@ -0,0 +1,15 @@+import ObjectId from "bson-objectid";+import produce from "immer";+import { type InstanceProps, type Instance } from "@webstudio-is/sdk";++export const cloneProps = (+ instanceProps: InstanceProps,+ { instanceId }: { instanceId: Instance["id"] }+) =>+ produce((instanceProps: InstanceProps) => {+ instanceProps.id = ObjectId().toString();+ instanceProps.instanceId = instanceId;+ for (const prop of instanceProps.props) {+ prop.id = ObjectId().toString();+ }+ })(instanceProps);diff --git a/app/shared/props-utils/index.ts b/app/shared/props-utils/index.tsindex e76c58df03..ee931f4224 100644--- a/app/shared/props-utils/index.ts+++ b/app/shared/props-utils/index.ts@@ -1,2 +1,3 @@export * from "./update-props";export * from "./delete-prop";+export * from "./clone-props";diff --git a/app/shared/shortcuts/index.ts b/app/shared/shortcuts/index.tsindex 770a755f67..5d45af7099 100644--- a/app/shared/shortcuts/index.ts+++ b/app/shared/shortcuts/index.ts@@ -2,4 +2,6 @@ export const shortcuts = {undo: "cmd+z, ctrl+z",redo: "cmd+shift+z, ctrl+shift+z",preview: "cmd+shift+p, ctrl+shift+p",+ copy: "cmd+c, ctrl+c",+ paste: "cmd+v, ctrl+v",} as const;diff --git a/app/shared/tree-utils/clone-instance.test.ts b/app/shared/tree-utils/clone-instance.test.tsnew file mode 100644index 0000000000..58af6281ee--- /dev/null+++ b/app/shared/tree-utils/clone-instance.test.ts@@ -0,0 +1,42 @@+import { type Instance } from "@webstudio-is/sdk";+import { cloneInstance } from "./clone-instance";++const getIds = (instance: Instance, ids: Array<Instance["id"]> = []) => {+ ids.push(instance.id);+ for (const child of instance.children) {+ if (typeof child !== "string") getIds(child);+ }+ return ids;+};++describe("Clone instance", () => {+ test("ensure new ids", () => {+ const instance: Instance = {+ component: "Box",+ id: "1",+ style: {},+ children: [+ {+ component: "Box",+ id: "2",+ style: {},+ children: [+ {+ component: "Box",+ id: "3",+ style: {},+ children: [],+ },+ ],+ },+ ],+ };++ const ids = getIds(instance);+ const clone = cloneInstance(instance);+ const clonedIds = getIds(clone);+ ids.forEach((id, index) => {+ expect(id).not.toBe(clonedIds[index]);+ });+ });+});diff --git a/app/shared/tree-utils/clone-instance.ts b/app/shared/tree-utils/clone-instance.tsnew file mode 100644index 0000000000..6b3460ff54--- /dev/null+++ b/app/shared/tree-utils/clone-instance.ts@@ -0,0 +1,14 @@+import ObjectId from "bson-objectid";+import produce from "immer";+import { type Instance } from "@webstudio-is/sdk";++const updateIds = (instance: Instance) => {+ instance.id = ObjectId().toString();+ for (const child of instance.children) {+ if (typeof child === "string") continue;+ updateIds(child);+ }+};++export const cloneInstance = (instance: Instance): Instance =>+ produce(updateIds)(instance);diff --git a/app/shared/tree-utils/index.ts b/app/shared/tree-utils/index.tsindex 365552d5b4..add3a420da 100644--- a/app/shared/tree-utils/index.ts+++ b/app/shared/tree-utils/index.ts@@ -9,3 +9,4 @@ export * from "./set-instance-children";export * from "./get-instance-path";export * from "./find-closest-sibling-instance";export * from "./find-parent-instance";+export * from "./clone-instance";