2024-05-23 18:53:04 +08:00
|
|
|
import { useSetModalState } from '@/hooks/commonHooks';
|
|
|
|
|
import React, { Dispatch, SetStateAction, useCallback, useState } from 'react';
|
2024-05-27 08:21:30 +08:00
|
|
|
import { Node, Position, ReactFlowInstance } from 'reactflow';
|
2024-05-23 18:53:04 +08:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2024-04-28 19:03:54 +08:00
|
|
|
|
|
|
|
|
export const useHandleDrag = () => {
|
2024-05-23 18:53:04 +08:00
|
|
|
const handleDragStart = useCallback(
|
2024-04-28 19:03:54 +08:00
|
|
|
(operatorId: string) => (ev: React.DragEvent<HTMLDivElement>) => {
|
2024-05-23 18:53:04 +08:00
|
|
|
ev.dataTransfer.setData('application/reactflow', operatorId);
|
|
|
|
|
ev.dataTransfer.effectAllowed = 'move';
|
2024-04-28 19:03:54 +08:00
|
|
|
},
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-23 18:53:04 +08:00
|
|
|
return { handleDragStart };
|
2024-04-28 19:03:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useHandleDrop = (setNodes: Dispatch<SetStateAction<Node[]>>) => {
|
2024-05-23 18:53:04 +08:00
|
|
|
const [reactFlowInstance, setReactFlowInstance] =
|
|
|
|
|
useState<ReactFlowInstance<any, any>>();
|
|
|
|
|
|
|
|
|
|
const onDragOver = useCallback((event: React.DragEvent<HTMLDivElement>) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.dataTransfer.dropEffect = 'move';
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const onDrop = useCallback(
|
|
|
|
|
(event: React.DragEvent<HTMLDivElement>) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
const type = event.dataTransfer.getData('application/reactflow');
|
|
|
|
|
|
|
|
|
|
// check if the dropped element is valid
|
|
|
|
|
if (typeof type === 'undefined' || !type) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-28 19:03:54 +08:00
|
|
|
|
2024-05-23 18:53:04 +08:00
|
|
|
// reactFlowInstance.project was renamed to reactFlowInstance.screenToFlowPosition
|
|
|
|
|
// and you don't need to subtract the reactFlowBounds.left/top anymore
|
|
|
|
|
// details: https://reactflow.dev/whats-new/2023-11-10
|
|
|
|
|
const position = reactFlowInstance?.screenToFlowPosition({
|
|
|
|
|
x: event.clientX,
|
|
|
|
|
y: event.clientY,
|
2024-04-28 19:03:54 +08:00
|
|
|
});
|
2024-05-23 18:53:04 +08:00
|
|
|
const newNode = {
|
|
|
|
|
id: uuidv4(),
|
2024-05-27 08:21:30 +08:00
|
|
|
type: 'textUpdater',
|
2024-05-23 18:53:04 +08:00
|
|
|
position: position || {
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
},
|
2024-05-27 08:21:30 +08:00
|
|
|
data: { label: `${type}` },
|
|
|
|
|
sourcePosition: Position.Right,
|
|
|
|
|
targetPosition: Position.Left,
|
2024-05-23 18:53:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setNodes((nds) => nds.concat(newNode));
|
2024-04-28 19:03:54 +08:00
|
|
|
},
|
2024-05-23 18:53:04 +08:00
|
|
|
[reactFlowInstance, setNodes],
|
2024-04-28 19:03:54 +08:00
|
|
|
);
|
|
|
|
|
|
2024-05-23 18:53:04 +08:00
|
|
|
return { onDrop, onDragOver, setReactFlowInstance };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useShowDrawer = () => {
|
|
|
|
|
const {
|
|
|
|
|
visible: drawerVisible,
|
|
|
|
|
hideModal: hideDrawer,
|
|
|
|
|
showModal: showDrawer,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
drawerVisible,
|
|
|
|
|
hideDrawer,
|
|
|
|
|
showDrawer,
|
|
|
|
|
};
|
2024-04-28 19:03:54 +08:00
|
|
|
};
|