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.

Turn instance copy paste into a plugin for copy/paste (#1076)

Oleg Isonen3836e2d2cbbe
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index 3a1152060b..7fb214f19d 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -36,7 +36,7 @@ import {
import { useClientSettings } from "./shared/client-settings";
import { Navigator } from "./features/sidebar-left";
import { getBuildUrl } from "~/shared/router-utils";
-import { useCopyPasteInstance } from "~/shared/copy-paste";
+import { useCopyPaste } from "~/shared/copy-paste";
import { AssetsProvider } from "./shared/assets";
registerContainers();
@@ -282,7 +282,7 @@ export const Builder = ({
useSubscribeCanvasReady(publish);
// We need to initialize this in both canvas and builder,
// because the events will fire in either one, depending on where the focus is
- useCopyPasteInstance();
+ useCopyPaste();
useSetWindowTitle();
const iframeRefCallback = useCallback(
(ref) => {
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsx
index 822d00d9e0..d1f031fbfa 100644
--- a/apps/builder/app/canvas/canvas.tsx
+++ b/apps/builder/app/canvas/canvas.tsx
@@ -36,7 +36,7 @@ import {
import { usePublishScrollState } from "./shared/use-publish-scroll-state";
import { useDragAndDrop } from "./shared/use-drag-drop";
import { useSubscribeBuilderReady } from "./shared/use-builder-ready";
-import { useCopyPasteInstance } from "~/shared/copy-paste";
+import { useCopyPaste } from "~/shared/copy-paste";
import { customComponents } from "./custom-components";
import { useHoveredInstanceConnector } from "./hovered-instance-connector";
@@ -74,7 +74,9 @@ const DesignMode = () => {
useDragAndDrop();
// We need to initialize this in both canvas and builder,
// because the events will fire in either one, depending on where the focus is
- useCopyPasteInstance();
+ // @todo we need to forward the events from canvas to builder and avoid importing this
+ // in both places
+ useCopyPaste();
useHoveredInstanceConnector();
return null;
diff --git a/apps/builder/app/shared/copy-paste/index.ts b/apps/builder/app/shared/copy-paste/index.ts
index 41cbf6716c..d4da3c3d53 100644
--- a/apps/builder/app/shared/copy-paste/index.ts
+++ b/apps/builder/app/shared/copy-paste/index.ts
@@ -1 +1 @@
-export * from "./copy-paste-instance";
+export { useCopyPaste } from "./use-copy-paste";
diff --git a/apps/builder/app/shared/copy-paste/copy-paste.ts b/apps/builder/app/shared/copy-paste/init-copy-paste.ts
similarity index 68%
rename from apps/builder/app/shared/copy-paste/copy-paste.ts
rename to apps/builder/app/shared/copy-paste/init-copy-paste.ts
index c91939afff..12e47e15cd 100644
--- a/apps/builder/app/shared/copy-paste/copy-paste.ts
+++ b/apps/builder/app/shared/copy-paste/init-copy-paste.ts
@@ -1,5 +1,3 @@
-import { z } from "zod";
-
const isValidClipboardEvent = (event: ClipboardEvent) => {
const selection = document.getSelection();
if (selection?.type === "Range") {
@@ -30,34 +28,15 @@ const isValidClipboardEvent = (event: ClipboardEvent) => {
return true;
};
-type Props<Data> = {
- version: string;
- type: z.ZodType<Data>;
- onCopy: () => undefined | Data;
- onCut: () => undefined | Data;
- onPaste: (data: Data) => void;
+type Props = {
+ mimeType?: string;
+ onCopy: () => undefined | string;
+ onCut: () => undefined | string;
+ onPaste: (data: string) => void;
};
-export const startCopyPaste = <Type>(props: Props<Type>) => {
- const { version, type } = props;
- const versionLiteral = version;
-
- const DataType = z.object({ [versionLiteral]: type });
-
- const serialize = (data: Type) => {
- return JSON.stringify({ [versionLiteral]: data });
- };
-
- const deserialize = (text: string) => {
- try {
- const data = DataType.parse(JSON.parse(text));
- // zod provides invalid type without versionLiteral
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return (data as any)[versionLiteral] as Type;
- } catch {
- return;
- }
- };
+export const initCopyPaste = (props: Props) => {
+ const { mimeType = "application/json" } = props;
const handleCopy = (event: ClipboardEvent) => {
if (
@@ -74,7 +53,7 @@ export const startCopyPaste = <Type>(props: Props<Type>) => {
// must prevent default, otherwise setData() will not work
event.preventDefault();
- event.clipboardData.setData("application/json", serialize(data));
+ event.clipboardData.setData(mimeType, data);
};
const handleCut = (event: ClipboardEvent) => {
@@ -92,7 +71,7 @@ export const startCopyPaste = <Type>(props: Props<Type>) => {
// must prevent default, otherwise setData() will not work
event.preventDefault();
- event.clipboardData.setData("application/json", serialize(data));
+ event.clipboardData.setData(mimeType, data);
};
const handlePaste = (event: ClipboardEvent) => {
@@ -107,12 +86,7 @@ export const startCopyPaste = <Type>(props: Props<Type>) => {
// this shouldn't matter, but just in case
event.preventDefault();
- const data = deserialize(event.clipboardData.getData("application/json"));
- if (data === undefined) {
- return;
- }
-
- props.onPaste(data);
+ props.onPaste(event.clipboardData.getData(mimeType));
};
document.addEventListener("copy", handleCopy);
diff --git a/apps/builder/app/shared/copy-paste/copy-paste-instance.ts b/apps/builder/app/shared/copy-paste/plugin-instance.ts
similarity index 73%
rename from apps/builder/app/shared/copy-paste/copy-paste-instance.ts
rename to apps/builder/app/shared/copy-paste/plugin-instance.ts
index 738982ec33..c841a51b94 100644
--- a/apps/builder/app/shared/copy-paste/copy-paste-instance.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-instance.ts
@@ -1,6 +1,5 @@
import store from "immerhin";
import { z } from "zod";
-import { useEffect } from "react";
import {
getStyleDeclKey,
Instance,
@@ -31,7 +30,9 @@ import {
insertInstanceMutable,
} from "../tree-utils";
import { deleteInstance } from "../instance-utils";
-import { startCopyPaste } from "./copy-paste";
+import { initCopyPaste } from "./init-copy-paste";
+
+const version = "@webstudio/instance/v0.1";
const InstanceData = z.object({
instance: Instance,
@@ -91,7 +92,26 @@ const copyInstanceData = (targetInstanceId: string) => {
};
};
-const pasteInstance = (data: InstanceData) => {
+const stringify = (data: InstanceData) => {
+ return JSON.stringify({ [version]: data });
+};
+
+const ClipboardData = z.object({ [version]: InstanceData });
+
+const parse = (text: string): InstanceData | undefined => {
+ try {
+ const data = ClipboardData.parse(JSON.parse(text));
+ return data[version];
+ } catch {
+ return;
+ }
+};
+
+const onPaste = (clipboardData: string) => {
+ const data = parse(clipboardData);
+ if (data === undefined) {
+ return;
+ }
const dropTarget = findClosestDroppableTarget(
instancesIndexStore.get(),
selectedInstanceIdStore.get()
@@ -127,38 +147,38 @@ const pasteInstance = (data: InstanceData) => {
selectedInstanceIdStore.set(data.instance.id);
};
-const startCopyPasteInstance = () => {
- return startCopyPaste({
- version: "@webstudio/instance/v0.1",
- type: InstanceData,
-
- onCopy: () => {
- const selectedInstanceId = selectedInstanceIdStore.get();
- if (selectedInstanceId === undefined) {
- return;
- }
- return copyInstanceData(selectedInstanceId);
- },
-
- onCut: () => {
- const selectedInstanceId = selectedInstanceIdStore.get();
- if (selectedInstanceId === undefined) {
- return;
- }
- const data = copyInstanceData(selectedInstanceId);
- if (data === undefined) {
- return;
- }
- deleteInstance(selectedInstanceId);
- return data;
- },
+const onCopy = () => {
+ const selectedInstanceId = selectedInstanceIdStore.get();
+ if (selectedInstanceId === undefined) {
+ return;
+ }
+ const data = copyInstanceData(selectedInstanceId);
+ if (data === undefined) {
+ return;
+ }
+ return stringify(data);
+};
- onPaste: pasteInstance,
- });
+const onCut = () => {
+ const selectedInstanceId = selectedInstanceIdStore.get();
+ if (selectedInstanceId === undefined) {
+ return;
+ }
+ const data = copyInstanceData(selectedInstanceId);
+ if (data === undefined) {
+ return;
+ }
+ deleteInstance(selectedInstanceId);
+ if (data === undefined) {
+ return;
+ }
+ return stringify(data);
};
-export const useCopyPasteInstance = () => {
- useEffect(() => {
- return startCopyPasteInstance();
- }, []);
+export const init = () => {
+ return initCopyPaste({
+ onCopy,
+ onCut,
+ onPaste,
+ });
};
diff --git a/apps/builder/app/shared/copy-paste/plugins.ts b/apps/builder/app/shared/copy-paste/plugins.ts
new file mode 100644
index 0000000000..f7ad251655
--- /dev/null
+++ b/apps/builder/app/shared/copy-paste/plugins.ts
@@ -0,0 +1 @@
+export * as instance from "./plugin-instance";
diff --git a/apps/builder/app/shared/copy-paste/use-copy-paste.ts b/apps/builder/app/shared/copy-paste/use-copy-paste.ts
new file mode 100644
index 0000000000..0f1ee4454d
--- /dev/null
+++ b/apps/builder/app/shared/copy-paste/use-copy-paste.ts
@@ -0,0 +1,24 @@
+import { useEffect } from "react";
+import * as plugins from "./plugins";
+
+const initPlugins = () => {
+ const cleanups: Array<() => void> = [];
+
+ for (const plugin of Object.values(plugins)) {
+ if (plugin.init) {
+ cleanups.push(plugin.init());
+ }
+ }
+
+ return () => {
+ for (const cleanup of cleanups) {
+ cleanup();
+ }
+ };
+};
+
+export const useCopyPaste = () => {
+ useEffect(() => {
+ return initPlugins();
+ }, []);
+};