From 71cc9092f8e1edb0d18ee77d75b2af44cb15bd66 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Wed, 31 May 2023 12:25:12 -0300 Subject: [PATCH] Added duplicate function on menu --- src/frontend/src/contexts/tabsContext.tsx | 11 +++-- .../components/nodeToolbarComponent/index.tsx | 46 ++++++++++++++----- src/frontend/src/types/tabs/index.ts | 2 +- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/frontend/src/contexts/tabsContext.tsx b/src/frontend/src/contexts/tabsContext.tsx index 14880a2db..6f26ee697 100644 --- a/src/frontend/src/contexts/tabsContext.tsx +++ b/src/frontend/src/contexts/tabsContext.tsx @@ -41,7 +41,7 @@ const TabsContextInitialValue: TabsContextType = { getNodeId: () => "", paste: ( selection: { nodes: any; edges: any }, - position: { x: number; y: number } + position: { x: number; y: number; paneX?: number; paneY?: number } ) => {}, }; @@ -221,7 +221,10 @@ export function TabsProvider({ children }: { children: ReactNode }) { * @param flow Optional flow to add. */ - function paste(selectionInstance, position) { + function paste( + selectionInstance, + position: { x: number; y: number; paneX?: number; paneY?: number } + ) { let minimumX = Infinity; let minimumY = Infinity; let idsMap = {}; @@ -236,7 +239,9 @@ export function TabsProvider({ children }: { children: ReactNode }) { } }); - const insidePosition = reactFlowInstance.project(position); + const insidePosition = position.paneX + ? { x: position.paneX + position.x, y: position.paneY + position.y } + : reactFlowInstance.project({ x: position.x, y: position.y }); selectionInstance.nodes.forEach((n) => { // Generate a unique node ID diff --git a/src/frontend/src/pages/FlowPage/components/nodeToolbarComponent/index.tsx b/src/frontend/src/pages/FlowPage/components/nodeToolbarComponent/index.tsx index 529a98b33..f30de16f7 100644 --- a/src/frontend/src/pages/FlowPage/components/nodeToolbarComponent/index.tsx +++ b/src/frontend/src/pages/FlowPage/components/nodeToolbarComponent/index.tsx @@ -7,6 +7,7 @@ import { PencilSquareIcon, DocumentDuplicateIcon, DocumentPlusIcon, + Square2StackIcon, } from "@heroicons/react/24/outline"; import { classNames } from "../../../../utils"; import { Fragment } from "react"; @@ -15,7 +16,7 @@ import { TabsContext } from "../../../../contexts/tabsContext"; import { useReactFlow } from "reactflow"; const NodeToolbarComponent = (props) => { - const { setLastCopiedSelection } = useContext(TabsContext); + const { setLastCopiedSelection, paste } = useContext(TabsContext); const reactFlowInstance = useReactFlow(); return ( <> @@ -78,7 +79,7 @@ const NodeToolbarComponent = (props) => { }); }} > - + @@ -105,13 +106,15 @@ const NodeToolbarComponent = (props) => {
{({ active }) => ( - { + event.preventDefault(); + }} className={classNames( active ? "bg-gray-100 text-gray-900" : "text-gray-700", - "group flex items-center px-4 py-2 text-sm" + "w-full group flex items-center px-4 py-2 text-sm" )} > { aria-hidden="true" /> Edit - + )} {({ active }) => ( - { + event.preventDefault(); + console.log( + reactFlowInstance.getNode(props.data.id) + ); + paste( + { + nodes: [ + reactFlowInstance.getNode(props.data.id), + ], + edges: [], + }, + { + x: 50, + y: 10, + paneX: reactFlowInstance.getNode(props.data.id) + .position.x, + paneY: reactFlowInstance.getNode(props.data.id) + .position.y, + } + ); + }} className={classNames( active ? "bg-gray-100 text-gray-900" : "text-gray-700", - "group flex items-center px-4 py-2 text-sm" + "w-full group flex items-center px-4 py-2 text-sm" )} > - + )}
diff --git a/src/frontend/src/types/tabs/index.ts b/src/frontend/src/types/tabs/index.ts index eb7d3fc6f..ba61b5ff2 100644 --- a/src/frontend/src/types/tabs/index.ts +++ b/src/frontend/src/types/tabs/index.ts @@ -18,7 +18,7 @@ export type TabsContextType = { getNodeId: () => string; paste: ( selection: { nodes: any; edges: any }, - position: { x: number; y: number } + position: { x: number; y: number; paneX?: number; paneY?: number } ) => void; lastCopiedSelection: { nodes: any; edges: any }; setLastCopiedSelection: (selection: { nodes: any; edges: any }) => void;