fix(userselection): dont render user selection when its not active

This commit is contained in:
moklick
2020-06-05 12:37:54 +02:00
parent c2fad7e680
commit 33d2770b23
4 changed files with 30 additions and 12 deletions
+5
View File
@@ -38,9 +38,14 @@ export default memo(() => {
const nodes = useStoreState((s) => s.nodes); const nodes = useStoreState((s) => s.nodes);
const updateNodePos = useStoreActions((a) => a.updateNodePos); const updateNodePos = useStoreActions((a) => a.updateNodePos);
const position = selectedNodesBbox; const position = selectedNodesBbox;
const grid = (snapToGrid ? snapGrid : [1, 1])! as [number, number]; const grid = (snapToGrid ? snapGrid : [1, 1])! as [number, number];
if (!selectedElements) {
return null;
}
const onStart = (evt: MouseEvent) => { const onStart = (evt: MouseEvent) => {
const scaledClient: XYPosition = { const scaledClient: XYPosition = {
x: evt.clientX / tScale, x: evt.clientX / tScale,
+16 -3
View File
@@ -2,13 +2,14 @@
* The user selection rectangle gets displayed when a user drags the mouse while pressing shift * The user selection rectangle gets displayed when a user drags the mouse while pressing shift
*/ */
import React, { memo } from 'react'; import React, { memo, useEffect } from 'react';
import { useStoreActions, useStoreState } from '../../store/hooks'; import { useStoreActions, useStoreState } from '../../store/hooks';
import { XYPosition } from '../../types'; import { XYPosition } from '../../types';
type UserSelectionProps = { type UserSelectionProps = {
isInteractive: boolean; isInteractive: boolean;
selectionKeyPressed: boolean;
}; };
function getMousePosition(evt: React.MouseEvent): XYPosition | void { function getMousePosition(evt: React.MouseEvent): XYPosition | void {
@@ -44,12 +45,21 @@ const SelectionRect = () => {
); );
}; };
export default memo(({ isInteractive }: UserSelectionProps) => { export default memo(({ isInteractive, selectionKeyPressed }: UserSelectionProps) => {
const selectionActive = useStoreState((s) => s.selectionActive);
const setUserSelection = useStoreActions((a) => a.setUserSelection); const setUserSelection = useStoreActions((a) => a.setUserSelection);
const updateUserSelection = useStoreActions((a) => a.updateUserSelection); const updateUserSelection = useStoreActions((a) => a.updateUserSelection);
const unsetUserSelection = useStoreActions((a) => a.unsetUserSelection); const unsetUserSelection = useStoreActions((a) => a.unsetUserSelection);
const renderUserSelectionPane = selectionActive || selectionKeyPressed;
if (!isInteractive) { useEffect(() => {
if (!selectionKeyPressed) {
unsetUserSelection();
}
}, [selectionKeyPressed]);
if (!isInteractive || !renderUserSelectionPane) {
return null; return null;
} }
@@ -63,6 +73,9 @@ export default memo(({ isInteractive }: UserSelectionProps) => {
}; };
const onMouseMove = (evt: React.MouseEvent): void => { const onMouseMove = (evt: React.MouseEvent): void => {
if (!selectionKeyPressed || !selectionActive) {
return;
}
const mousePos = getMousePosition(evt); const mousePos = getMousePosition(evt);
if (!mousePos) { if (!mousePos) {
+7 -7
View File
@@ -12,7 +12,7 @@ import useD3Zoom from '../../hooks/useD3Zoom';
import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler'; import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler';
import useElementUpdater from '../../hooks/useElementUpdater'; import useElementUpdater from '../../hooks/useElementUpdater';
import { getDimensions } from '../../utils'; import { getDimensions } from '../../utils';
import { fitView, zoomIn, zoomOut, project, getElements } from '../../utils/graph'; import { project, getElements } from '../../utils/graph';
import { import {
Elements, Elements,
NodeTypesType, NodeTypesType,
@@ -80,7 +80,6 @@ const GraphView = memo(
const height = useStoreState((s) => s.height); const height = useStoreState((s) => s.height);
const d3Initialised = useStoreState((s) => s.d3Initialised); const d3Initialised = useStoreState((s) => s.d3Initialised);
const nodesSelectionActive = useStoreState((s) => s.nodesSelectionActive); const nodesSelectionActive = useStoreState((s) => s.nodesSelectionActive);
const selectionActive = useStoreState((s) => s.selectionActive);
const updateSize = useStoreActions((actions) => actions.updateSize); const updateSize = useStoreActions((actions) => actions.updateSize);
const setNodesSelection = useStoreActions((actions) => actions.setNodesSelection); const setNodesSelection = useStoreActions((actions) => actions.setNodesSelection);
const setOnConnect = useStoreActions((a) => a.setOnConnect); const setOnConnect = useStoreActions((a) => a.setOnConnect);
@@ -88,10 +87,11 @@ const GraphView = memo(
const setInteractive = useStoreActions((actions) => actions.setInteractive); const setInteractive = useStoreActions((actions) => actions.setInteractive);
const updateTransform = useStoreActions((actions) => actions.updateTransform); const updateTransform = useStoreActions((actions) => actions.updateTransform);
const setMinMaxZoom = useStoreActions((actions) => actions.setMinMaxZoom); const setMinMaxZoom = useStoreActions((actions) => actions.setMinMaxZoom);
const fitView = useStoreActions((actions) => actions.fitView);
const zoom = useStoreActions((actions) => actions.zoom);
const selectionKeyPressed = useKeyPress(selectionKeyCode); const selectionKeyPressed = useKeyPress(selectionKeyCode);
const rendererClasses = classnames('react-flow__renderer', { 'is-interactive': isInteractive }); const rendererClasses = classnames('react-flow__renderer', { 'is-interactive': isInteractive });
const userSelectionActive = selectionKeyPressed || selectionActive;
const onZoomPaneClick = () => setNodesSelection({ isActive: false }); const onZoomPaneClick = () => setNodesSelection({ isActive: false });
@@ -147,9 +147,9 @@ const GraphView = memo(
useEffect(() => { useEffect(() => {
if (d3Initialised && onLoad) { if (d3Initialised && onLoad) {
onLoad({ onLoad({
fitView, fitView: (params = { padding: 0.1 }) => fitView(params),
zoomIn, zoomIn: () => zoom(0.2),
zoomOut, zoomOut: () => zoom(-0.2),
project, project,
getElements, getElements,
}); });
@@ -189,7 +189,7 @@ const GraphView = memo(
connectionLineType={connectionLineType} connectionLineType={connectionLineType}
connectionLineStyle={connectionLineStyle} connectionLineStyle={connectionLineStyle}
/> />
{userSelectionActive && <UserSelection isInteractive={isInteractive} />} <UserSelection selectionKeyPressed={selectionKeyPressed} isInteractive={isInteractive} />
{nodesSelectionActive && <NodesSelection />} {nodesSelectionActive && <NodesSelection />}
<div className="react-flow__zoompane" onClick={onZoomPaneClick} ref={zoomPane} /> <div className="react-flow__zoompane" onClick={onZoomPaneClick} ref={zoomPane} />
</div> </div>
+1 -1
View File
@@ -69,6 +69,7 @@
.react-flow__edge-text { .react-flow__edge-text {
font-size: 12px; font-size: 12px;
pointer-events: none; pointer-events: none;
user-select: none;
} }
.react-flow__edge-textbg { .react-flow__edge-textbg {
@@ -132,7 +133,6 @@
.react-flow__node-output { .react-flow__node-output {
background: #55dd99; background: #55dd99;
} }
.react-flow__nodesselection { .react-flow__nodesselection {