Copy / Paste Nodes and Edges #1020
Replies: 12 comments 7 replies
-
|
I am currently planing to implement this feature for my app. I am thinking about using the clipboard api and defining a custom Content-Type for my app. This should also allow me to copy and paste nodes across flows. Similar to the drag-and-drop example. For this behavior one also needs custom validation to check, if the pasted node is a valid one. I think that another prop like onNodeValidation should receive a function with the following definition: (node: Node) => boolean;It returns true, if the to be inserted node is valid and and false if not. Only if the node is valid, it will be inserted into the elements array. |
Beta Was this translation helpful? Give feedback.
-
|
there should be a way to copy and paste single and multiple nodes without having to create an external solution. Im already creating a solution since we need this asap but I think a simple implementation only for elements in the canvas should be available. |
Beta Was this translation helpful? Give feedback.
-
|
@moklick, I can throw up a pull request for it, if you haven't already started on it |
Beta Was this translation helpful? Give feedback.
-
|
What is the status on this? |
Beta Was this translation helpful? Give feedback.
-
|
Hi is this now possible to do with the latest version? if not can someone please share your working fork? |
Beta Was this translation helpful? Give feedback.
-
|
Bumping to say this would be a very much appreciated feature! |
Beta Was this translation helpful? Give feedback.
-
|
Would love to have this feature! |
Beta Was this translation helpful? Give feedback.
-
|
I think this feature is cool! |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
-
|
Copy/Paste/Cut is now available as a pro example https://reactflow.dev/docs/examples/interaction/copy-and-paste/ |
Beta Was this translation helpful? Give feedback.
-
|
Example hook import { ReactFlowInstance, Node } from "@xyflow/react";
import { useCallback, useEffect } from "react";
export const useCopyPaste = (
rfInstance: ReactFlowInstance | null,
) => {
const onCopyCapture = useCallback(
(event: ClipboardEvent) => {
event.preventDefault();
const nodes = JSON.stringify(
rfInstance?.getNodes().filter((n) => n.selected)
);
event.clipboardData?.setData("flowchart:nodes", nodes);
},
[rfInstance]
);
const onPasteCapture = useCallback(
(event: ClipboardEvent) => {
event.preventDefault();
const nodes = JSON.parse(
event.clipboardData?.getData("flowchart:nodes") || "[]"
) as Node[] | undefined;
if (nodes) {
const randomId = () => Math.random().toString(16).slice(2);
rfInstance?.setNodes(
[
...rfInstance.getNodes().map((n) => ({ ...n, selected: false })),
...nodes.map((n) => ({
...n,
selected: true,
id: randomId(),
position: { x: n.position.x + 10, y: n.position.y + 10 },
}))
]);
}
},
[rfInstance]
);
useEffect(() => {
window.addEventListener("copy", onCopyCapture);
return () => {
window.removeEventListener("copy", onCopyCapture);
};
}, [onCopyCapture]);
useEffect(() => {
window.addEventListener("paste", onPasteCapture);
return () => {
window.removeEventListener("paste", onPasteCapture);
};
}, [onPasteCapture]);
}; |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Add the ability to copy/paste nodes and edges with cmd+c, cmd+v and change these keys via
copyKeyCodeandpasteKeyCodefor example.Beta Was this translation helpful? Give feedback.
All reactions