From 685ff76ee8033a70400b1116a44db225a5c94a57 Mon Sep 17 00:00:00 2001 From: Jack Fishwick Date: Fri, 23 Sep 2022 14:25:04 +0100 Subject: [PATCH] feat: add a selectBoxOnDrag prop which allows select box to be triggered without an additional key press. --- .../core/src/container/FlowRenderer/index.tsx | 4 ++-- .../core/src/container/GraphView/index.tsx | 2 ++ .../core/src/container/ReactFlow/index.tsx | 2 ++ .../core/src/container/ZoomPane/index.tsx | 20 +++++++++++-------- packages/core/src/types/component-props.ts | 1 + 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/core/src/container/FlowRenderer/index.tsx b/packages/core/src/container/FlowRenderer/index.tsx index e3faed9c..fda81982 100644 --- a/packages/core/src/container/FlowRenderer/index.tsx +++ b/packages/core/src/container/FlowRenderer/index.tsx @@ -44,6 +44,7 @@ const FlowRenderer = ({ onMoveStart, onMoveEnd, selectionKeyCode, + selectBoxOnDrag, multiSelectionKeyCode, zoomActivationKeyCode, elementsSelectable, @@ -84,7 +85,6 @@ const FlowRenderer = ({ onMove={onMove} onMoveStart={onMoveStart} onMoveEnd={onMoveEnd} - selectionKeyPressed={selectionKeyPressed} elementsSelectable={elementsSelectable} zoomOnScroll={zoomOnScroll} zoomOnPinch={zoomOnPinch} @@ -103,7 +103,7 @@ const FlowRenderer = ({ noPanClassName={noPanClassName} > {children} - + {nodesSelectionActive && ( ( connectionLineContainerStyle, deleteKeyCode = 'Backspace', selectionKeyCode = 'Shift', + selectBoxOnDrag = false, multiSelectionKeyCode = 'Meta', zoomActivationKeyCode = 'Meta', snapToGrid = false, @@ -193,6 +194,7 @@ const ReactFlow = forwardRef( connectionLineComponent={connectionLineComponent} connectionLineContainerStyle={connectionLineContainerStyle} selectionKeyCode={selectionKeyCode} + selectBoxOnDrag={selectBoxOnDrag} deleteKeyCode={deleteKeyCode} multiSelectionKeyCode={multiSelectionKeyCode} zoomActivationKeyCode={zoomActivationKeyCode} diff --git a/packages/core/src/container/ZoomPane/index.tsx b/packages/core/src/container/ZoomPane/index.tsx index a5783a7a..fce3a35f 100644 --- a/packages/core/src/container/ZoomPane/index.tsx +++ b/packages/core/src/container/ZoomPane/index.tsx @@ -16,8 +16,13 @@ import type { Viewport, ReactFlowState } from '../../types'; type ZoomPaneProps = Omit< FlowRendererProps, - 'deleteKeyCode' | 'selectionKeyCode' | 'multiSelectionKeyCode' | 'noDragClassName' | 'disableKeyboardA11y' -> & { selectionKeyPressed: boolean }; + | 'deleteKeyCode' + | 'selectionKeyCode' + | 'multiSelectionKeyCode' + | 'noDragClassName' + | 'disableKeyboardA11y' + | 'selectBoxOnDrag' +>; const viewChanged = (prevViewport: Viewport, eventViewport: any): boolean => prevViewport.x !== eventViewport.x || prevViewport.y !== eventViewport.y || prevViewport.zoom !== eventViewport.k; @@ -46,7 +51,6 @@ const ZoomPane = ({ panOnScrollSpeed = 0.5, panOnScrollMode = PanOnScrollMode.Free, zoomOnDoubleClick = true, - selectionKeyPressed, elementsSelectable, panOnDrag = true, defaultViewport, @@ -151,9 +155,9 @@ const ZoomPane = ({ useEffect(() => { if (d3Zoom) { - if (selectionKeyPressed && !isZoomingOrPanning.current) { + if (store.getState().userSelectionActive && !isZoomingOrPanning.current) { d3Zoom.on('zoom', null); - } else if (!selectionKeyPressed) { + } else if (!store.getState().userSelectionActive) { d3Zoom.on('zoom', (event: D3ZoomEvent) => { const { onViewportChange } = store.getState(); @@ -168,7 +172,7 @@ const ZoomPane = ({ }); } } - }, [selectionKeyPressed, d3Zoom, onMove]); + }, [store, d3Zoom, onMove]); useEffect(() => { if (d3Zoom) { @@ -238,7 +242,7 @@ const ZoomPane = ({ } // during a selection we prevent all other interactions - if (selectionKeyPressed) { + if (store.getState().userSelectionActive) { return false; } @@ -276,13 +280,13 @@ const ZoomPane = ({ }); } }, [ + store, d3Zoom, zoomOnScroll, zoomOnPinch, panOnScroll, zoomOnDoubleClick, panOnDrag, - selectionKeyPressed, elementsSelectable, zoomActivationKeyPressed, ]); diff --git a/packages/core/src/types/component-props.ts b/packages/core/src/types/component-props.ts index 9b273641..456122ce 100644 --- a/packages/core/src/types/component-props.ts +++ b/packages/core/src/types/component-props.ts @@ -94,6 +94,7 @@ export type ReactFlowProps = HTMLAttributes & { connectionMode?: ConnectionMode; deleteKeyCode?: KeyCode | null; selectionKeyCode?: KeyCode | null; + selectBoxOnDrag?: boolean; multiSelectionKeyCode?: KeyCode | null; zoomActivationKeyCode?: KeyCode | null; snapToGrid?: boolean;