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.

cross page copy-paste

Roman Pominovfbf8f7db8750
diff --git a/apps/designer/app/canvas/shared/copy-paste.ts b/apps/designer/app/canvas/shared/copy-paste.ts
deleted file mode 100644
index 68f2eca6b1..0000000000
--- a/apps/designer/app/canvas/shared/copy-paste.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import {
- type InstanceProps,
- type Instance,
- allUserPropsContainer,
-} from "@webstudio-is/react-sdk";
-import { selectedInstanceContainer } from "./nano-states";
-import { utils } from "@webstudio-is/project";
-import { cloneProps } from "~/shared/props-utils";
-import { publish } from "~/shared/pubsub";
-
-let currentInstance: Instance | undefined;
-let currentProps: InstanceProps | undefined;
-
-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 = utils.tree.cloneInstance(currentInstance);
- const props = currentProps
- ? cloneProps(currentProps, { instanceId: instance.id })
- : undefined;
- publish({
- type: "insertInstance",
- payload: {
- instance,
- props,
- },
- });
-};
diff --git a/apps/designer/app/canvas/shared/use-shortcuts.ts b/apps/designer/app/canvas/shared/use-shortcuts.ts
index 29972b5730..d9fe167cae 100644
--- a/apps/designer/app/canvas/shared/use-shortcuts.ts
+++ b/apps/designer/app/canvas/shared/use-shortcuts.ts
@@ -4,7 +4,6 @@ import { type Instance, getComponentMeta } from "@webstudio-is/react-sdk";
import { shortcuts, options } from "~/shared/shortcuts";
import { publish, useSubscribe } from "~/shared/pubsub";
import { useSelectedInstance } from "./nano-states";
-import { copy, paste } from "./copy-paste";
import { useTextEditingInstanceId } from "~/shared/nano-states";
import { type SelectedInstanceData } from "@webstudio-is/project";
@@ -19,6 +18,8 @@ declare module "~/shared/pubsub" {
selectInstance?: SelectedInstanceData;
togglePreviewMode: undefined;
zoom: "zoomOut" | "zoomIn";
+ copy: undefined;
+ paste: undefined;
}
}
@@ -66,6 +67,14 @@ const publishCancelCurrentDrag = () => {
publish({ type: "cancelCurrentDrag" });
};
+const publishCopy = () => {
+ publish({ type: "copy" });
+};
+
+const publishPaste = () => {
+ publish({ type: "paste" });
+};
+
export const useShortcuts = () => {
const [selectedInstance, setSelectedInstance] = useSelectedInstance();
const [editingInstanceId, setEditingInstanceId] = useTextEditingInstanceId();
@@ -85,8 +94,8 @@ export const useShortcuts = () => {
redo: store.redo.bind(store),
delete: publishDeleteInstance,
preview: togglePreviewMode,
- copy,
- paste,
+ copy: publishCopy,
+ paste: publishPaste,
breakpointsMenu: publishOpenBreakpointsMenu,
breakpoint: publishSelectBreakpoint,
zoom: publishZoom,
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index 7a36a5215d..cf8261e369 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -40,6 +40,7 @@ import { useClientSettings } from "./shared/client-settings";
import { Navigator } from "./features/sidebar-left";
import { useSetAssets } from "./shared/assets";
import { getBuildUrl } from "~/shared/router-utils";
+import { useSubscribeCopyPaste } from "~/shared/copy-paste";
export const links = () => {
return [
@@ -272,6 +273,7 @@ export const Designer = ({
const { onRef: onRefReadCanvas, onTransitionEnd } = useReadCanvasRect();
const [dragAndDropState] = useDragAndDropState();
useSubscribeCanvasReady(publish);
+ useSubscribeCopyPaste(publish);
const iframeRefCallback = useCallback(
(ref) => {
diff --git a/apps/designer/app/shared/copy-paste.ts b/apps/designer/app/shared/copy-paste.ts
new file mode 100644
index 0000000000..750769caa2
--- /dev/null
+++ b/apps/designer/app/shared/copy-paste.ts
@@ -0,0 +1,45 @@
+import { type InstanceProps, type Instance } from "@webstudio-is/react-sdk";
+import { utils } from "@webstudio-is/project";
+import { cloneProps } from "~/shared/props-utils";
+import { useSubscribe, type Publish } from "~/shared/pubsub";
+import { useSelectedInstanceData } from "~/designer/shared/nano-states";
+import { useRootInstance } from "./nano-states";
+
+let currentData: { instance: Instance; props: InstanceProps } | undefined;
+
+export const useSubscribeCopyPaste = (publish: Publish) => {
+ const [selectedInstanceData] = useSelectedInstanceData();
+ const [rootInstance] = useRootInstance();
+
+ useSubscribe("copy", () => {
+ if (selectedInstanceData === undefined || rootInstance === undefined) {
+ return;
+ }
+ const instance = utils.tree.findInstanceById(
+ rootInstance,
+ selectedInstanceData.id
+ );
+ if (instance === undefined) {
+ return;
+ }
+ currentData = { instance, props: selectedInstanceData.props };
+ });
+
+ useSubscribe("paste", () => {
+ if (currentData === undefined) {
+ return;
+ }
+
+ const instance = utils.tree.cloneInstance(currentData.instance);
+
+ // @todo: look into why in the old code they were undefined sometimes
+ const props = currentData.props
+ ? cloneProps(currentData.props, { instanceId: instance.id })
+ : undefined;
+
+ publish({
+ type: "insertInstance",
+ payload: { instance, props },
+ });
+ });
+};