diff --git a/src/App.css b/src/App.css index b4adae6..6e6a4b1 100644 --- a/src/App.css +++ b/src/App.css @@ -27,7 +27,6 @@ left: 50%; top: 50%; touch-action: none; - background: white; border-radius: 50%; } .handle.left { diff --git a/src/components/Diagram/handle.tsx b/src/components/Diagram/handle.tsx index 3c1d647..f87e5f9 100644 --- a/src/components/Diagram/handle.tsx +++ b/src/components/Diagram/handle.tsx @@ -12,138 +12,146 @@ interface HandleProps extends React.HTMLAttributes { id: string; type: "target" | "source"; placement: Placement; + children?: React.ReactNode; + ghostSize?: number; } -export const Handle: FC = memo(({ id, type, placement }) => { - const ref = useRef(null); - const { diagramId } = useDiagramContext(); - const { nodeId } = useNodeContext(); - const useDiagram = useGetDiagramStore(); +export const Handle: FC = memo( + ({ id, type, placement, children, ghostSize, ...props }) => { + const ref = useRef(null); + const { diagramId } = useDiagramContext(); + const { nodeId } = useNodeContext(); + const useDiagram = useGetDiagramStore(); - useLayoutEffect(() => { - useDiagram.getState().setHandlePlacement(nodeId, id, placement); - }, [placement]); + useLayoutEffect(() => { + useDiagram.getState().setHandlePlacement(nodeId, id, placement); + }, [placement]); - useLayoutEffect(() => { - ref.current && resizeObserver.observe(ref.current); - return () => { - ref.current && resizeObserver.unobserve(ref.current); - useDiagram.getState().clearHandleDimensions(nodeId, id); - }; - }, []); + useLayoutEffect(() => { + ref.current && resizeObserver.observe(ref.current); + return () => { + ref.current && resizeObserver.unobserve(ref.current); + useDiagram.getState().clearHandleDimensions(nodeId, id); + }; + }, []); - useGesture( - { - onDrag: ({ xy, event, first, canceled, pinching, tap }) => { - if (canceled || pinching) return; - const state = useDiagram.getState(); - if (first) { - const handleCenter = state.getHandleCenter(nodeId, id); + useGesture( + { + onDrag: ({ xy, event, first, canceled, pinching, tap }) => { + if (canceled || pinching) return; + const state = useDiagram.getState(); + if (first) { + const handleCenter = state.getHandleCenter(nodeId, id); - state.updateDraggedEdgePosition({ - start: handleCenter, - end: handleCenter, - }); + state.updateDraggedEdgePosition({ + start: handleCenter, + end: handleCenter, + }); - state.setDraggedEdgeVisible(true); - state.setDraggedEdge({ - handleId: id, - nodeId, - handleType: type, - }); - } + state.setDraggedEdgeVisible(true); + state.setDraggedEdge({ + handleId: id, + nodeId, + handleType: type, + }); + } - console.log("state.viewport:", state.viewport); - state.updateDraggedEdgePosition({ - end: getInDiagramPosition({ x: xy[0], y: xy[1] }, state.viewport), - }); + state.updateDraggedEdgePosition({ + end: getInDiagramPosition({ x: xy[0], y: xy[1] }, state.viewport), + }); - const element = document.elementFromPoint(...xy) as HTMLElement | null; - if ( - element && - element.dataset.elementType === "handle" && - element.dataset.type !== type && - element.dataset.id !== id - ) { - const handleCenter = state.getHandleCenter( - element.dataset.nodeId as string, - element.dataset.id as string - ); + const element = document.elementFromPoint( + ...xy + ) as HTMLElement | null; + if ( + element && + element.dataset.elementType === "handle" && + element.dataset.type !== type && + element.dataset.id !== id + ) { + const handleCenter = state.getHandleCenter( + element.dataset.nodeId as string, + element.dataset.id as string + ); - useDiagram.getState().updateDraggedEdgePosition({ - end: handleCenter, - }); - } - }, - onDragEnd: ({ xy }) => { - const element = document.elementFromPoint(...xy); + useDiagram.getState().updateDraggedEdgePosition({ + end: handleCenter, + }); + } + }, + onDragEnd: ({ xy }) => { + const element = document.elementFromPoint(...xy); - const [from, to] = [ref.current, element] as HTMLElement[]; - if ( - from && - to && - to.dataset.elementType === "handle" && - from.dataset.nodeId !== to.dataset.nodeId && - from.dataset.id !== to.dataset.id && - to.dataset.type !== type - ) { - const [source, target] = type === "source" ? [from, to] : [to, from]; - const onConnection = useDiagram.getState().onConnection; - onConnection - ? onConnection({ - source: source.dataset.nodeId as string, - target: target.dataset.nodeId as string, - sourceHandle: source.dataset.id as string, - targetHandle: target.dataset.id as string, - }) - : useDiagram.getState().addEdge( - createEdge({ + const [from, to] = [ref.current, element] as HTMLElement[]; + if ( + from && + to && + to.dataset.elementType === "handle" && + from.dataset.nodeId !== to.dataset.nodeId && + from.dataset.id !== to.dataset.id && + to.dataset.type !== type + ) { + const [source, target] = + type === "source" ? [from, to] : [to, from]; + const onConnection = useDiagram.getState().onConnection; + onConnection + ? onConnection({ source: source.dataset.nodeId as string, target: target.dataset.nodeId as string, sourceHandle: source.dataset.id as string, targetHandle: target.dataset.id as string, - data: "", - animated: true, }) - ); - } - useDiagram.getState().clearDraggedEdge(); - }, - }, - { - target: ref, - eventOptions: { - passive: false, + : useDiagram.getState().addEdge( + createEdge({ + source: source.dataset.nodeId as string, + target: target.dataset.nodeId as string, + sourceHandle: source.dataset.id as string, + targetHandle: target.dataset.id as string, + data: "", + animated: true, + }) + ); + } + useDiagram.getState().clearDraggedEdge(); + }, }, - } - ); - const draggedEdge = useDraggedEdge(); - if (draggedEdge?.handleType === type && draggedEdge?.nodeId !== nodeId) - return null; - return ( -
- {/* {draggedEdge && ( - - )} */} -
- ); -}); + { + target: ref, + eventOptions: { + passive: false, + }, + } + ); + const draggedEdge = useDraggedEdge(); + if (draggedEdge?.handleType === type && draggedEdge?.nodeId !== nodeId) + return null; + return ( +
+
{children}
+ {draggedEdge && ( +
+ )} +
+ ); + } +); diff --git a/src/components/Node/DefaultNode.tsx b/src/components/Node/DefaultNode.tsx index 2b28dde..b8600e1 100644 --- a/src/components/Node/DefaultNode.tsx +++ b/src/components/Node/DefaultNode.tsx @@ -18,7 +18,9 @@ export const DefaultNode: NodeFC = memo(({ id }) => {

{(data as Partial<{ label: string }>)?.label || `Default: ${id}`}

- + +
+
);