Skip to content

Commit

Permalink
Feat: Zoom on node, on single click on node. Double click delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
mahesh-gfx committed Aug 20, 2024
1 parent 7366333 commit 2c42025
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
2 changes: 2 additions & 0 deletions packages/frontend/src/components/WorkflowCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const WorkflowCanvas: React.FC = () => {
onNodesDelete,
transformEdges,
onNodeDragStop,
handleNodeClick,
}: any = useContext(WorkflowContext);

//useEffects
Expand Down Expand Up @@ -81,6 +82,7 @@ const WorkflowCanvas: React.FC = () => {
onInit={setReactFlowInstance}
onDrop={onDrop}
onDragOver={onDragOver}
onNodeClick={handleNodeClick}
onNodeDoubleClick={onNodeDoubleClick}
onNodeDragStop={onNodeDragStop}
nodeTypes={nodeTypes}
Expand Down
43 changes: 30 additions & 13 deletions packages/frontend/src/context/WorkflowContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ const WorkflowProvider = ({ children }: any) => {
const [NodeConfigModalIsOpen, setNodeConfigModalIsOpen] = useState(false);
const callbackRef = useRef<(() => void) | null>(null);
const prevWorkflowIdRef = useRef<string | null>(null);
const [clickTimeout, setClickTimeout] = useState<ReturnType<
typeof setTimeout
> | null>(null);
const CLICK_DELAY = 150; // Fixed timeout duration in milliseconds for single click on node

useEffect(() => {
// Extract the workflow ID from the URL
Expand Down Expand Up @@ -233,14 +237,35 @@ const WorkflowProvider = ({ children }: any) => {
[reactFlowInstance, nodes, setNodes, nodeDefinitions]
);

const handleNodeClick = useCallback(
(_: any, node: Node<NodeData>) => {
// Clear any existing timeout
if (clickTimeout) {
clearTimeout(clickTimeout);
setClickTimeout(null);
}
const timeout = setTimeout(() => {
reactFlowInstance?.fitView({ nodes: [node], duration: 150 }); // Adjust duration as needed
}, CLICK_DELAY);

setClickTimeout(timeout);
},
[reactFlowInstance?.fitView, clickTimeout]
);

const onNodeDoubleClick = useCallback(
(event: React.MouseEvent, node: Node<NodeData>) => {
// Clear the single click timeout
if (clickTimeout) {
clearTimeout(clickTimeout);
setClickTimeout(null);
}
setSelectedNode(node);
setTimeout(() => {
setNodeConfigModalIsOpen(true);
}, 100);
},
[]
[clickTimeout]
);

const onDragStart = useCallback(
Expand Down Expand Up @@ -479,18 +504,6 @@ const WorkflowProvider = ({ children }: any) => {
});
};

const getNodeById = useCallback(
(nodeId: string) => {
const node = nodes.find((node) => node.id === nodeId);
if (!node) {
console.warn(`Node with ID ${nodeId} not found.`);
return null;
}
return node;
},
[nodes]
);

const onNodeDragStop = (event: Event, node: any) => {
console.log("On node drag stop: ", event, node);
// const updatedEdges = edges.map((edge: any) => {
Expand Down Expand Up @@ -550,6 +563,10 @@ const WorkflowProvider = ({ children }: any) => {
openModal,
closeModal,
onNodeDragStop,
handleNodeClick,
clickTimeout,
setClickTimeout,
CLICK_DELAY,
}}
>
{children}
Expand Down

0 comments on commit 2c42025

Please sign in to comment.