feat: add events for when selection starts and ends.

This commit is contained in:
Jack Fishwick
2022-11-22 11:04:01 +00:00
parent 4e874b90a8
commit 1de6de1f96
4 changed files with 150 additions and 123 deletions
@@ -19,6 +19,8 @@ type EventHandlers = { [key: string]: React.MouseEventHandler | React.WheelEvent
type UserSelectionProps = { type UserSelectionProps = {
isSelectionMode: boolean; isSelectionMode: boolean;
onSelectionStart?: (e: React.MouseEvent) => void;
onSelectionEnd?: (e: React.MouseEvent) => void;
onClick?: (e: React.MouseEvent) => void; onClick?: (e: React.MouseEvent) => void;
onContextMenu?: (e: React.MouseEvent) => void; onContextMenu?: (e: React.MouseEvent) => void;
onWheel?: (e: React.WheelEvent) => void; onWheel?: (e: React.WheelEvent) => void;
@@ -55,7 +57,16 @@ const selector = (s: ReactFlowState) => ({
elementsSelectable: s.elementsSelectable, elementsSelectable: s.elementsSelectable,
}); });
const UserSelection = memo(({ isSelectionMode, onClick, onContextMenu, onWheel, children }: UserSelectionProps) => { const UserSelection = memo(
({
isSelectionMode,
onSelectionStart,
onSelectionEnd,
onClick,
onContextMenu,
onWheel,
children,
}: UserSelectionProps) => {
const container = useRef<HTMLDivElement | null>(null); const container = useRef<HTMLDivElement | null>(null);
const store = useStoreApi(); const store = useStoreApi();
const prevSelectedNodesCount = useRef<number>(0); const prevSelectedNodesCount = useRef<number>(0);
@@ -93,6 +104,8 @@ const UserSelection = memo(({ isSelectionMode, onClick, onContextMenu, onWheel,
x: mousePos.x, x: mousePos.x,
y: mousePos.y, y: mousePos.y,
}); });
onSelectionStart?.(event);
}; };
const onMouseMove = (event: React.MouseEvent): void => { const onMouseMove = (event: React.MouseEvent): void => {
@@ -150,11 +163,14 @@ const UserSelection = memo(({ isSelectionMode, onClick, onContextMenu, onWheel,
store.setState({ nodesSelectionActive: prevSelectedNodesCount.current > 0 }); store.setState({ nodesSelectionActive: prevSelectedNodesCount.current > 0 });
resetUserSelection(); resetUserSelection();
onSelectionEnd?.(event);
}; };
const onMouseLeave = () => { const onMouseLeave = (event: React.MouseEvent) => {
if (userSelectionActive) { if (userSelectionActive) {
store.setState({ nodesSelectionActive: prevSelectedNodesCount.current > 0 }); store.setState({ nodesSelectionActive: prevSelectedNodesCount.current > 0 });
onSelectionEnd?.(event);
} }
resetUserSelection(); resetUserSelection();
}; };
@@ -192,7 +208,8 @@ const UserSelection = memo(({ isSelectionMode, onClick, onContextMenu, onWheel,
)} )}
</div> </div>
); );
}); }
);
UserSelection.displayName = 'UserSelection'; UserSelection.displayName = 'UserSelection';
@@ -45,6 +45,8 @@ const FlowRenderer = ({
onMoveEnd, onMoveEnd,
selectionKeyCode, selectionKeyCode,
selectBoxOnDrag, selectBoxOnDrag,
onSelectionStart,
onSelectionEnd,
multiSelectionKeyCode, multiSelectionKeyCode,
zoomActivationKeyCode, zoomActivationKeyCode,
elementsSelectable, elementsSelectable,
@@ -112,6 +114,8 @@ const FlowRenderer = ({
noPanClassName={noPanClassName} noPanClassName={noPanClassName}
> >
<UserSelection <UserSelection
onSelectionStart={onSelectionStart}
onSelectionEnd={onSelectionEnd}
onClick={onClick} onClick={onClick}
onMouseEnter={onPaneMouseEnter} onMouseEnter={onPaneMouseEnter}
onMouseMove={onPaneMouseMove} onMouseMove={onPaneMouseMove}
@@ -92,6 +92,8 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
onSelectionDrag, onSelectionDrag,
onSelectionDragStop, onSelectionDragStop,
onSelectionContextMenu, onSelectionContextMenu,
onSelectionStart,
onSelectionEnd,
connectionMode = ConnectionMode.Strict, connectionMode = ConnectionMode.Strict,
connectionLineType = ConnectionLineType.Bezier, connectionLineType = ConnectionLineType.Bezier,
connectionLineStyle, connectionLineStyle,
@@ -219,6 +221,8 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
onPaneScroll={onPaneScroll} onPaneScroll={onPaneScroll}
onPaneContextMenu={onPaneContextMenu} onPaneContextMenu={onPaneContextMenu}
onSelectionContextMenu={onSelectionContextMenu} onSelectionContextMenu={onSelectionContextMenu}
onSelectionStart={onSelectionStart}
onSelectionEnd={onSelectionEnd}
onEdgeUpdate={onEdgeUpdate} onEdgeUpdate={onEdgeUpdate}
onEdgeContextMenu={onEdgeContextMenu} onEdgeContextMenu={onEdgeContextMenu}
onEdgeDoubleClick={onEdgeDoubleClick} onEdgeDoubleClick={onEdgeDoubleClick}
@@ -68,6 +68,8 @@ export type ReactFlowProps = HTMLAttributes<HTMLDivElement> & {
onSelectionDragStart?: SelectionDragHandler; onSelectionDragStart?: SelectionDragHandler;
onSelectionDrag?: SelectionDragHandler; onSelectionDrag?: SelectionDragHandler;
onSelectionDragStop?: SelectionDragHandler; onSelectionDragStop?: SelectionDragHandler;
onSelectionStart?: (event: ReactMouseEvent) => void;
onSelectionEnd?: (event: ReactMouseEvent) => void;
onSelectionContextMenu?: (event: ReactMouseEvent, nodes: Node[]) => void; onSelectionContextMenu?: (event: ReactMouseEvent, nodes: Node[]) => void;
onConnect?: OnConnect; onConnect?: OnConnect;
onConnectStart?: OnConnectStart; onConnectStart?: OnConnectStart;