feat(edges): add mouse handler closes #987

This commit is contained in:
moklick
2021-03-12 20:46:14 +01:00
parent bf6bce0c58
commit cd1baadf41
6 changed files with 66 additions and 5 deletions
+6
View File
@@ -41,6 +41,9 @@ const onLoad = (reactFlowInstance: OnLoadParams) => {
const onMoveEnd = (transform?: FlowTransform) => console.log('zoom/move end', transform); const onMoveEnd = (transform?: FlowTransform) => console.log('zoom/move end', transform);
const onEdgeContextMenu = (_: MouseEvent, edge: Edge) => console.log('edge context menu', edge); const onEdgeContextMenu = (_: MouseEvent, edge: Edge) => console.log('edge context menu', edge);
const onEdgeMouseEnter = (_: MouseEvent, edge: Edge) => console.log('edge mouse enter', edge);
const onEdgeMouseMove = (_: MouseEvent, edge: Edge) => console.log('edge mouse move', edge);
const onEdgeMouseLeave = (_: MouseEvent, edge: Edge) => console.log('edge mouse leave', edge);
const initialElements: Elements = [ const initialElements: Elements = [
{ {
@@ -179,6 +182,9 @@ const OverviewFlow = () => {
snapToGrid={true} snapToGrid={true}
snapGrid={snapGrid} snapGrid={snapGrid}
onEdgeContextMenu={onEdgeContextMenu} onEdgeContextMenu={onEdgeContextMenu}
onEdgeMouseEnter={onEdgeMouseEnter}
onEdgeMouseMove={onEdgeMouseMove}
onEdgeMouseLeave={onEdgeMouseLeave}
> >
<MiniMap nodeStrokeColor={nodeStrokeColor} nodeColor={nodeColor} nodeBorderRadius={2} /> <MiniMap nodeStrokeColor={nodeStrokeColor} nodeColor={nodeColor} nodeBorderRadius={2} />
<Controls /> <Controls />
+32 -1
View File
@@ -39,6 +39,9 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
handleEdgeUpdate, handleEdgeUpdate,
onConnectEdge, onConnectEdge,
onContextMenu, onContextMenu,
onMouseEnter,
onMouseMove,
onMouseLeave,
edgeUpdaterRadius, edgeUpdaterRadius,
}: WrapEdgeProps): JSX.Element | null => { }: WrapEdgeProps): JSX.Element | null => {
const addSelectedElements = useStoreActions((actions) => actions.addSelectedElements); const addSelectedElements = useStoreActions((actions) => actions.addSelectedElements);
@@ -99,6 +102,27 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
[edgeElement, onContextMenu] [edgeElement, onContextMenu]
); );
const onEdgeMouseEnter = useCallback(
(event: React.MouseEvent<SVGGElement, MouseEvent>): void => {
onMouseEnter?.(event, edgeElement);
},
[edgeElement, onContextMenu]
);
const onEdgeMouseMove = useCallback(
(event: React.MouseEvent<SVGGElement, MouseEvent>): void => {
onMouseMove?.(event, edgeElement);
},
[edgeElement, onContextMenu]
);
const onEdgeMouseLeave = useCallback(
(event: React.MouseEvent<SVGGElement, MouseEvent>): void => {
onMouseLeave?.(event, edgeElement);
},
[edgeElement, onContextMenu]
);
const handleEdgeUpdater = useCallback( const handleEdgeUpdater = useCallback(
(event: React.MouseEvent<SVGGElement, MouseEvent>, isSourceHandle: boolean) => { (event: React.MouseEvent<SVGGElement, MouseEvent>, isSourceHandle: boolean) => {
const nodeId = isSourceHandle ? target : source; const nodeId = isSourceHandle ? target : source;
@@ -143,7 +167,14 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
} }
return ( return (
<g className={edgeClasses} onClick={onEdgeClick} onContextMenu={onEdgeContextMenu}> <g
className={edgeClasses}
onClick={onEdgeClick}
onContextMenu={onEdgeContextMenu}
onMouseEnter={onEdgeMouseEnter}
onMouseMove={onEdgeMouseMove}
onMouseLeave={onEdgeMouseLeave}
>
{handleEdgeUpdate && ( {handleEdgeUpdate && (
<g <g
onMouseDown={onEdgeUpdaterSourceMouseDown} onMouseDown={onEdgeUpdaterSourceMouseDown}
+7 -1
View File
@@ -29,7 +29,10 @@ interface EdgeRendererProps {
markerEndId?: string; markerEndId?: string;
onlyRenderVisibleElements: boolean; onlyRenderVisibleElements: boolean;
onEdgeUpdate?: OnEdgeUpdateFunc; onEdgeUpdate?: OnEdgeUpdateFunc;
onEdgeContextMenu?: (event: React.MouseEvent, element: Edge) => void; onEdgeContextMenu?: (event: React.MouseEvent, edge: Edge) => void;
onEdgeMouseEnter?: (event: React.MouseEvent, edge: Edge) => void;
onEdgeMouseMove?: (event: React.MouseEvent, edge: Edge) => void;
onEdgeMouseLeave?: (event: React.MouseEvent, edge: Edge) => void;
edgeUpdaterRadius?: number; edgeUpdaterRadius?: number;
} }
@@ -166,6 +169,9 @@ const Edge = ({
onConnectEdge={onConnectEdge} onConnectEdge={onConnectEdge}
handleEdgeUpdate={typeof props.onEdgeUpdate !== 'undefined'} handleEdgeUpdate={typeof props.onEdgeUpdate !== 'undefined'}
onContextMenu={props.onEdgeContextMenu} onContextMenu={props.onEdgeContextMenu}
onMouseEnter={props.onEdgeMouseEnter}
onMouseMove={props.onEdgeMouseMove}
onMouseLeave={props.onEdgeMouseLeave}
edgeUpdaterRadius={props.edgeUpdaterRadius} edgeUpdaterRadius={props.edgeUpdaterRadius}
/> />
); );
+6
View File
@@ -86,6 +86,9 @@ const GraphView = ({
onPaneContextMenu, onPaneContextMenu,
onEdgeUpdate, onEdgeUpdate,
onEdgeContextMenu, onEdgeContextMenu,
onEdgeMouseEnter,
onEdgeMouseMove,
onEdgeMouseLeave,
edgeUpdaterRadius, edgeUpdaterRadius,
}: GraphViewProps) => { }: GraphViewProps) => {
const isInitialized = useRef<boolean>(false); const isInitialized = useRef<boolean>(false);
@@ -265,6 +268,9 @@ const GraphView = ({
onEdgeUpdate={onEdgeUpdate} onEdgeUpdate={onEdgeUpdate}
onlyRenderVisibleElements={onlyRenderVisibleElements} onlyRenderVisibleElements={onlyRenderVisibleElements}
onEdgeContextMenu={onEdgeContextMenu} onEdgeContextMenu={onEdgeContextMenu}
onEdgeMouseEnter={onEdgeMouseEnter}
onEdgeMouseMove={onEdgeMouseMove}
onEdgeMouseLeave={onEdgeMouseLeave}
edgeUpdaterRadius={edgeUpdaterRadius} edgeUpdaterRadius={edgeUpdaterRadius}
/> />
</FlowRenderer> </FlowRenderer>
+11 -2
View File
@@ -109,8 +109,11 @@ export interface ReactFlowProps extends Omit<HTMLAttributes<HTMLDivElement>, 'on
panOnScrollMode?: PanOnScrollMode; panOnScrollMode?: PanOnScrollMode;
zoomOnDoubleClick?: boolean; zoomOnDoubleClick?: boolean;
onEdgeUpdate?: OnEdgeUpdateFunc; onEdgeUpdate?: OnEdgeUpdateFunc;
onEdgeContextMenu?: (event: MouseEvent, nodes: Edge) => void; onEdgeContextMenu?: (event: MouseEvent, edge: Edge) => void;
edgeUpdaterRadius?: number onEdgeMouseEnter?: (event: MouseEvent, edge: Edge) => void;
onEdgeMouseMove?: (event: MouseEvent, edge: Edge) => void;
onEdgeMouseLeave?: (event: MouseEvent, edge: Edge) => void;
edgeUpdaterRadius?: number;
} }
const ReactFlow = ({ const ReactFlow = ({
@@ -176,6 +179,9 @@ const ReactFlow = ({
children, children,
onEdgeUpdate, onEdgeUpdate,
onEdgeContextMenu, onEdgeContextMenu,
onEdgeMouseEnter,
onEdgeMouseMove,
onEdgeMouseLeave,
edgeUpdaterRadius = 10, edgeUpdaterRadius = 10,
...rest ...rest
}: ReactFlowProps) => { }: ReactFlowProps) => {
@@ -245,6 +251,9 @@ const ReactFlow = ({
onSelectionContextMenu={onSelectionContextMenu} onSelectionContextMenu={onSelectionContextMenu}
onEdgeUpdate={onEdgeUpdate} onEdgeUpdate={onEdgeUpdate}
onEdgeContextMenu={onEdgeContextMenu} onEdgeContextMenu={onEdgeContextMenu}
onEdgeMouseEnter={onEdgeMouseEnter}
onEdgeMouseMove={onEdgeMouseMove}
onEdgeMouseLeave={onEdgeMouseLeave}
edgeUpdaterRadius={edgeUpdaterRadius} edgeUpdaterRadius={edgeUpdaterRadius}
/> />
<ElementUpdater elements={elements} /> <ElementUpdater elements={elements} />
+4 -1
View File
@@ -125,7 +125,10 @@ export interface WrapEdgeProps<T = any> {
isHidden?: boolean; isHidden?: boolean;
handleEdgeUpdate: boolean; handleEdgeUpdate: boolean;
onConnectEdge: OnConnectFunc; onConnectEdge: OnConnectFunc;
onContextMenu: (event: React.MouseEvent, edge: Edge) => void; onContextMenu?: (event: React.MouseEvent, edge: Edge) => void;
onMouseEnter?: (event: React.MouseEvent, edge: Edge) => void;
onMouseMove?: (event: React.MouseEvent, edge: Edge) => void;
onMouseLeave?: (event: React.MouseEvent, edge: Edge) => void;
edgeUpdaterRadius?: number; edgeUpdaterRadius?: number;
} }