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 = {
isSelectionMode: boolean;
onSelectionStart?: (e: React.MouseEvent) => void;
onSelectionEnd?: (e: React.MouseEvent) => void;
onClick?: (e: React.MouseEvent) => void;
onContextMenu?: (e: React.MouseEvent) => void;
onWheel?: (e: React.WheelEvent) => void;
@@ -55,7 +57,16 @@ const selector = (s: ReactFlowState) => ({
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 store = useStoreApi();
const prevSelectedNodesCount = useRef<number>(0);
@@ -93,6 +104,8 @@ const UserSelection = memo(({ isSelectionMode, onClick, onContextMenu, onWheel,
x: mousePos.x,
y: mousePos.y,
});
onSelectionStart?.(event);
};
const onMouseMove = (event: React.MouseEvent): void => {
@@ -150,11 +163,14 @@ const UserSelection = memo(({ isSelectionMode, onClick, onContextMenu, onWheel,
store.setState({ nodesSelectionActive: prevSelectedNodesCount.current > 0 });
resetUserSelection();
onSelectionEnd?.(event);
};
const onMouseLeave = () => {
const onMouseLeave = (event: React.MouseEvent) => {
if (userSelectionActive) {
store.setState({ nodesSelectionActive: prevSelectedNodesCount.current > 0 });
onSelectionEnd?.(event);
}
resetUserSelection();
};
@@ -192,7 +208,8 @@ const UserSelection = memo(({ isSelectionMode, onClick, onContextMenu, onWheel,
)}
</div>
);
});
}
);
UserSelection.displayName = 'UserSelection';
@@ -45,6 +45,8 @@ const FlowRenderer = ({
onMoveEnd,
selectionKeyCode,
selectBoxOnDrag,
onSelectionStart,
onSelectionEnd,
multiSelectionKeyCode,
zoomActivationKeyCode,
elementsSelectable,
@@ -112,6 +114,8 @@ const FlowRenderer = ({
noPanClassName={noPanClassName}
>
<UserSelection
onSelectionStart={onSelectionStart}
onSelectionEnd={onSelectionEnd}
onClick={onClick}
onMouseEnter={onPaneMouseEnter}
onMouseMove={onPaneMouseMove}
@@ -92,6 +92,8 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
onSelectionDrag,
onSelectionDragStop,
onSelectionContextMenu,
onSelectionStart,
onSelectionEnd,
connectionMode = ConnectionMode.Strict,
connectionLineType = ConnectionLineType.Bezier,
connectionLineStyle,
@@ -219,6 +221,8 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
onPaneScroll={onPaneScroll}
onPaneContextMenu={onPaneContextMenu}
onSelectionContextMenu={onSelectionContextMenu}
onSelectionStart={onSelectionStart}
onSelectionEnd={onSelectionEnd}
onEdgeUpdate={onEdgeUpdate}
onEdgeContextMenu={onEdgeContextMenu}
onEdgeDoubleClick={onEdgeDoubleClick}
@@ -68,6 +68,8 @@ export type ReactFlowProps = HTMLAttributes<HTMLDivElement> & {
onSelectionDragStart?: SelectionDragHandler;
onSelectionDrag?: SelectionDragHandler;
onSelectionDragStop?: SelectionDragHandler;
onSelectionStart?: (event: ReactMouseEvent) => void;
onSelectionEnd?: (event: ReactMouseEvent) => void;
onSelectionContextMenu?: (event: ReactMouseEvent, nodes: Node[]) => void;
onConnect?: OnConnect;
onConnectStart?: OnConnectStart;