Merge pull request #2662 from wbkd/feat/figma-controls
Feat: figma controls
This commit is contained in:
@@ -50,9 +50,9 @@ describe('Basic Flow Rendering', () => {
|
|||||||
cy.get('body')
|
cy.get('body')
|
||||||
.type('{shift}', { release: false })
|
.type('{shift}', { release: false })
|
||||||
.wait(50)
|
.wait(50)
|
||||||
.get('.react-flow__selectionpane')
|
.get('.react-flow__pane')
|
||||||
.trigger('mousedown', 1000, 50, { which: 1, force: true })
|
.trigger('mousedown', 1000, 50, { button: 0, force: true })
|
||||||
.trigger('mousemove', 1, 400, { which: 1 })
|
.trigger('mousemove', 1, 400, { button: 0 })
|
||||||
.wait(50)
|
.wait(50)
|
||||||
.trigger('mouseup', 1, 200, { force: true });
|
.trigger('mouseup', 1, 200, { force: true });
|
||||||
|
|
||||||
@@ -70,9 +70,9 @@ describe('Basic Flow Rendering', () => {
|
|||||||
it('selects all nodes', () => {
|
it('selects all nodes', () => {
|
||||||
cy.get('body')
|
cy.get('body')
|
||||||
.type('{shift}', { release: false })
|
.type('{shift}', { release: false })
|
||||||
.get('.react-flow__selectionpane')
|
.get('.react-flow__pane')
|
||||||
.trigger('mousedown', 'topRight', { which: 1, force: true })
|
.trigger('mousedown', 'topRight', { button: 0, force: true })
|
||||||
.trigger('mousemove', 'bottomLeft', { which: 1 })
|
.trigger('mousemove', 'bottomLeft', { button: 0 })
|
||||||
.wait(50)
|
.wait(50)
|
||||||
.trigger('mouseup', 'bottomLeft', { force: true })
|
.trigger('mouseup', 'bottomLeft', { force: true })
|
||||||
.wait(50)
|
.wait(50)
|
||||||
@@ -140,7 +140,7 @@ describe('Basic Flow Rendering', () => {
|
|||||||
// https://github.com/cypress-io/cypress/issues/3441
|
// https://github.com/cypress-io/cypress/issues/3441
|
||||||
cy.window().then((win) => {
|
cy.window().then((win) => {
|
||||||
cy.get('.react-flow__pane')
|
cy.get('.react-flow__pane')
|
||||||
.trigger('mousedown', 'topLeft', { which: 1, view: win })
|
.trigger('mousedown', 'topLeft', { button: 0, view: win })
|
||||||
.trigger('mousemove', 'bottomLeft')
|
.trigger('mousemove', 'bottomLeft')
|
||||||
.wait(50)
|
.wait(50)
|
||||||
.trigger('mouseup', { force: true, view: win })
|
.trigger('mouseup', { force: true, view: win })
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ describe('Controls Testing', () => {
|
|||||||
// https://github.com/cypress-io/cypress/issues/3441
|
// https://github.com/cypress-io/cypress/issues/3441
|
||||||
cy.window().then((win) => {
|
cy.window().then((win) => {
|
||||||
cy.get('.react-flow__renderer')
|
cy.get('.react-flow__renderer')
|
||||||
.trigger('mousedown', 'topLeft', { which: 1, view: win })
|
.trigger('mousedown', 'topLeft', { button: 0, view: win })
|
||||||
.trigger('mousemove', 10, 400)
|
.trigger('mousemove', 10, 400)
|
||||||
.wait(50)
|
.wait(50)
|
||||||
.trigger('mouseup', 10, 400, { force: true, view: win })
|
.trigger('mouseup', 10, 400, { force: true, view: win })
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ describe('Empty Flow Rendering', () => {
|
|||||||
cy.get('body')
|
cy.get('body')
|
||||||
.type('{shift}', { release: false })
|
.type('{shift}', { release: false })
|
||||||
.wait(50)
|
.wait(50)
|
||||||
.get('.react-flow__selectionpane')
|
.get('.react-flow__pane')
|
||||||
.trigger('mousedown', 400, 50, { which: 1, force: true })
|
.trigger('mousedown', 400, 50, { button: 0, force: true })
|
||||||
.trigger('mousemove', 200, 200, { which: 1 })
|
.trigger('mousemove', 200, 200, { button: 0 })
|
||||||
.wait(50)
|
.wait(50)
|
||||||
.trigger('mouseup', 200, 200, { force: true });
|
.trigger('mouseup', 200, 200, { force: true });
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
describe('Figma Flow UI', () => {
|
||||||
|
before(() => {
|
||||||
|
cy.visit('/figma');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders a flow with three nodes', () => {
|
||||||
|
cy.get('.react-flow__renderer');
|
||||||
|
cy.get('.react-flow__node').should('have.length', 4);
|
||||||
|
cy.get('.react-flow__edge').should('have.length', 2);
|
||||||
|
cy.get('.react-flow__node').children('.react-flow__handle');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('renders a grid', () => {
|
||||||
|
cy.get('.react-flow__background');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('selects all nodes by drag', () => {
|
||||||
|
cy.window().then((win) => {
|
||||||
|
cy.get('.react-flow__pane')
|
||||||
|
.trigger('mousedown', 'topLeft', { button: 0, view: win })
|
||||||
|
.trigger('mousemove', 'bottomRight', { force: true })
|
||||||
|
.wait(50)
|
||||||
|
.trigger('mouseup', { force: true, view: win })
|
||||||
|
.then(() => {
|
||||||
|
cy.get('.react-flow__node').should('have.class', 'selected');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes selection', () => {
|
||||||
|
cy.get('.react-flow__pane').click('topLeft');
|
||||||
|
cy.get('.react-flow__node').should('not.have.class', 'selected');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('drags using right click', () => {
|
||||||
|
cy.window().then((win) => {
|
||||||
|
cy.get('.react-flow__node:last').isWithinViewport();
|
||||||
|
cy.get('.react-flow__pane')
|
||||||
|
.trigger('mousedown', 'center', { button: 2, view: win })
|
||||||
|
.trigger('mousemove', 'bottom', { force: true })
|
||||||
|
.wait(50)
|
||||||
|
.trigger('mouseup', { force: true, view: win })
|
||||||
|
.then(() => {
|
||||||
|
cy.get('.react-flow__node').should('not.have.class', 'selected');
|
||||||
|
cy.get('.react-flow__node:last').isOutsideViewport();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
export {};
|
||||||
@@ -35,7 +35,18 @@ describe('Interaction Flow Rendering', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('tries to do a selection', () => {
|
it('tries to do a selection', () => {
|
||||||
cy.get('body').type('{shift}', { release: false }).get('.react-flow__selectionpane').should('not.exist');
|
cy.get('body')
|
||||||
|
.type('{shift}', { release: false })
|
||||||
|
.wait(50)
|
||||||
|
.get('.react-flow__pane')
|
||||||
|
.trigger('mousedown', 1000, 50, { button: 0, force: true })
|
||||||
|
.trigger('mousemove', 1, 400, { button: 0 })
|
||||||
|
.wait(50)
|
||||||
|
.get('.react-flow__selection')
|
||||||
|
.should('not.exist');
|
||||||
|
|
||||||
|
cy.get('.react-flow__pane').trigger('mouseup', 1, 200, { force: true });
|
||||||
|
|
||||||
cy.get('body').type('{shift}', { release: true });
|
cy.get('body').type('{shift}', { release: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,12 @@ describe('Minimap Testing', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('has same number of nodes as the pane', () => {
|
it('has same number of nodes as the pane', () => {
|
||||||
const paneNodes = Cypress.$('.react-flow__node').length;
|
cy.get('.react-flow__minimap-node').then(() => {
|
||||||
|
const paneNodes = Cypress.$('.react-flow__node').length;
|
||||||
|
const minimapNodes = Cypress.$('.react-flow__minimap-node').length;
|
||||||
|
|
||||||
cy.wait(200);
|
expect(paneNodes).equal(minimapNodes);
|
||||||
|
});
|
||||||
const minimapNodes = Cypress.$('.react-flow__minimap-node').length;
|
|
||||||
|
|
||||||
expect(paneNodes).equal(minimapNodes);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('changes zoom level', () => {
|
it('changes zoom level', () => {
|
||||||
@@ -60,7 +59,7 @@ describe('Minimap Testing', () => {
|
|||||||
// https://github.com/cypress-io/cypress/issues/3441
|
// https://github.com/cypress-io/cypress/issues/3441
|
||||||
cy.window().then((win) => {
|
cy.window().then((win) => {
|
||||||
cy.get('.react-flow__pane')
|
cy.get('.react-flow__pane')
|
||||||
.trigger('mousedown', 'topLeft', { which: 1, view: win })
|
.trigger('mousedown', 'topLeft', { button: 0, view: win })
|
||||||
.trigger('mousemove', 'bottomLeft')
|
.trigger('mousemove', 'bottomLeft')
|
||||||
.wait(50)
|
.wait(50)
|
||||||
.trigger('mouseup', { force: true, view: win })
|
.trigger('mouseup', { force: true, view: win })
|
||||||
|
|||||||
@@ -36,23 +36,25 @@ Cypress.Commands.add('zoomPane', (wheelDelta: number) =>
|
|||||||
Cypress.Commands.add('isWithinViewport', { prevSubject: true }, (subject) => {
|
Cypress.Commands.add('isWithinViewport', { prevSubject: true }, (subject) => {
|
||||||
const rect = subject[0].getBoundingClientRect();
|
const rect = subject[0].getBoundingClientRect();
|
||||||
|
|
||||||
expect(rect.top).to.be.within(0, window.innerHeight);
|
return cy.window().then((window) => {
|
||||||
expect(rect.right).to.be.within(0, window.innerWidth);
|
expect(rect.top).to.be.within(0, window.innerHeight);
|
||||||
expect(rect.bottom).to.be.within(0, window.innerHeight);
|
expect(rect.right).to.be.within(0, window.innerWidth);
|
||||||
expect(rect.left).to.be.within(0, window.innerWidth);
|
expect(rect.bottom).to.be.within(0, window.innerHeight);
|
||||||
|
expect(rect.left).to.be.within(0, window.innerWidth);
|
||||||
|
|
||||||
return subject;
|
return subject;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
Cypress.Commands.add('isOutsideViewport', { prevSubject: true }, (subject) => {
|
Cypress.Commands.add('isOutsideViewport', { prevSubject: true }, (subject) => {
|
||||||
const rect = subject[0].getBoundingClientRect();
|
const rect = subject[0].getBoundingClientRect();
|
||||||
|
|
||||||
expect(rect.top).not.to.be.within(0, window.innerHeight);
|
return cy.window().then((window) => {
|
||||||
expect(rect.right).not.to.be.within(0, window.innerWidth);
|
expect(window.innerHeight < rect.top || rect.bottom < 0 || window.innerWidth < rect.left || rect.right < 0).to.be
|
||||||
expect(rect.bottom).not.to.be.within(0, window.innerHeight);
|
.true;
|
||||||
expect(rect.left).not.to.be.within(0, window.innerWidth);
|
|
||||||
|
|
||||||
return subject;
|
return subject;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
export {};
|
export {};
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import Edges from '../examples/Edges';
|
|||||||
import EdgeRenderer from '../examples/EdgeRenderer';
|
import EdgeRenderer from '../examples/EdgeRenderer';
|
||||||
import EdgeTypes from '../examples/EdgeTypes';
|
import EdgeTypes from '../examples/EdgeTypes';
|
||||||
import Empty from '../examples/Empty';
|
import Empty from '../examples/Empty';
|
||||||
|
import Figma from '../examples/Figma';
|
||||||
import FloatingEdges from '../examples/FloatingEdges';
|
import FloatingEdges from '../examples/FloatingEdges';
|
||||||
import Hidden from '../examples/Hidden';
|
import Hidden from '../examples/Hidden';
|
||||||
import Interaction from '../examples/Interaction';
|
import Interaction from '../examples/Interaction';
|
||||||
@@ -120,6 +121,11 @@ const routes: IRoute[] = [
|
|||||||
path: '/empty',
|
path: '/empty',
|
||||||
component: Empty,
|
component: Empty,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Figma',
|
||||||
|
path: '/figma',
|
||||||
|
component: Figma,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'Floating Edges',
|
name: 'Floating Edges',
|
||||||
path: '/floating-edges',
|
path: '/floating-edges',
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import ReactFlow, { Background, BackgroundVariant, Node, Edge, SelectionMode, Viewport } from 'reactflow';
|
||||||
|
|
||||||
|
const MULTI_SELECT_KEY = ['Meta', 'Shift'];
|
||||||
|
|
||||||
|
const initialNodes: Node[] = [
|
||||||
|
{ id: '1', type: 'input', data: { label: 'Node 1' }, position: { x: 250, y: 5 }, className: 'light' },
|
||||||
|
{ id: '2', data: { label: 'Node 2' }, position: { x: 100, y: 100 }, className: 'light' },
|
||||||
|
{ id: '3', data: { label: 'Node 3' }, position: { x: 400, y: 100 }, className: 'light' },
|
||||||
|
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 }, className: 'light' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const initialEdges: Edge[] = [
|
||||||
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||||
|
{ id: 'e1-3', source: '1', target: '3' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const BasicFlow = () => {
|
||||||
|
return (
|
||||||
|
<ReactFlow
|
||||||
|
defaultNodes={initialNodes}
|
||||||
|
defaultEdges={initialEdges}
|
||||||
|
selectionOnDrag
|
||||||
|
selectionMode={SelectionMode.Partial}
|
||||||
|
panOnDrag="RightClick"
|
||||||
|
panOnScroll
|
||||||
|
zoomActivationKeyCode="Meta"
|
||||||
|
multiSelectionKeyCode={MULTI_SELECT_KEY}
|
||||||
|
fitView
|
||||||
|
selectNodesOnDrag={false}
|
||||||
|
>
|
||||||
|
<Background variant={BackgroundVariant.Cross} />
|
||||||
|
</ReactFlow>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default BasicFlow;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { CSSProperties, useCallback, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
import ReactFlow, { Controls, addEdge, Position, Connection, useNodesState, useEdgesState, Panel } from 'reactflow';
|
import ReactFlow, { Controls, addEdge, Position, Connection, useNodesState, useEdgesState, Panel } from 'reactflow';
|
||||||
|
|
||||||
import NodeResizerNode from './NodeResizerNode';
|
import NodeResizerNode from './NodeResizerNode';
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import shallow from 'zustand/shallow';
|
||||||
|
import { useStore } from '../../hooks/useStore';
|
||||||
|
import { ReactFlowState } from '../../types';
|
||||||
|
|
||||||
|
const selector = (s: ReactFlowState) => ({
|
||||||
|
userSelectionActive: s.userSelectionActive,
|
||||||
|
userSelectionRect: s.userSelectionRect,
|
||||||
|
});
|
||||||
|
|
||||||
|
function SelectionBox() {
|
||||||
|
const { userSelectionActive, userSelectionRect } = useStore(selector, shallow);
|
||||||
|
const showSelectionBox = userSelectionActive && userSelectionRect;
|
||||||
|
|
||||||
|
if (!showSelectionBox) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="react-flow__selection react-flow__container"
|
||||||
|
style={{
|
||||||
|
width: userSelectionRect.width,
|
||||||
|
height: userSelectionRect.height,
|
||||||
|
transform: `translate(${userSelectionRect.x}px, ${userSelectionRect.y}px)`,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default SelectionBox;
|
||||||
@@ -2,161 +2,232 @@
|
|||||||
* 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 { memo, useState, useRef } from 'react';
|
import { memo, useRef, MouseEvent as ReactMouseEvent } from 'react';
|
||||||
import shallow from 'zustand/shallow';
|
import shallow from 'zustand/shallow';
|
||||||
|
import cc from 'classcat';
|
||||||
|
|
||||||
|
import SelectionBox from './SelectionBox';
|
||||||
|
import { containerStyle } from '../../styles';
|
||||||
import { useStore, useStoreApi } from '../../hooks/useStore';
|
import { useStore, useStoreApi } from '../../hooks/useStore';
|
||||||
import { getSelectionChanges } from '../../utils/changes';
|
import { getSelectionChanges } from '../../utils/changes';
|
||||||
import { getConnectedEdges, getNodesInside } from '../../utils/graph';
|
import { getConnectedEdges, getNodesInside } from '../../utils/graph';
|
||||||
import type { XYPosition, ReactFlowState, NodeChange, EdgeChange, Rect } from '../../types';
|
import { SelectionMode } from '../../types';
|
||||||
|
import type { XYPosition, ReactFlowState, NodeChange, EdgeChange } from '../../types';
|
||||||
type SelectionRect = Rect & {
|
|
||||||
startX: number;
|
|
||||||
startY: number;
|
|
||||||
draw: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
type UserSelectionProps = {
|
type UserSelectionProps = {
|
||||||
selectionKeyPressed: boolean;
|
isSelecting: boolean;
|
||||||
|
selectionMode?: SelectionMode;
|
||||||
|
panOnDrag?: boolean | 'RightClick';
|
||||||
|
onSelectionStart?: (e: ReactMouseEvent) => void;
|
||||||
|
onSelectionEnd?: (e: ReactMouseEvent) => void;
|
||||||
|
onPaneClick?: (e: ReactMouseEvent) => void;
|
||||||
|
onPaneContextMenu?: (e: ReactMouseEvent) => void;
|
||||||
|
onPaneScroll?: (e: React.WheelEvent) => void;
|
||||||
|
onPaneMouseEnter?: (e: ReactMouseEvent) => void;
|
||||||
|
onPaneMouseMove?: (e: ReactMouseEvent) => void;
|
||||||
|
onPaneMouseLeave?: (e: ReactMouseEvent) => void;
|
||||||
|
children: React.ReactNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
function getMousePosition(event: React.MouseEvent, containerBounds: DOMRect): XYPosition {
|
function getMousePosition(event: ReactMouseEvent, containerBounds: DOMRect): XYPosition {
|
||||||
return {
|
return {
|
||||||
x: event.clientX - containerBounds.left,
|
x: event.clientX - containerBounds.left,
|
||||||
y: event.clientY - containerBounds.top,
|
y: event.clientY - containerBounds.top,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const wrapHandler = (
|
||||||
|
handler: React.MouseEventHandler | undefined,
|
||||||
|
containerRef: React.MutableRefObject<HTMLDivElement | null>
|
||||||
|
): React.MouseEventHandler => {
|
||||||
|
return (event: ReactMouseEvent) => {
|
||||||
|
if (event.target !== containerRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handler?.(event);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const selector = (s: ReactFlowState) => ({
|
const selector = (s: ReactFlowState) => ({
|
||||||
userSelectionActive: s.userSelectionActive,
|
userSelectionActive: s.userSelectionActive,
|
||||||
elementsSelectable: s.elementsSelectable,
|
elementsSelectable: s.elementsSelectable,
|
||||||
|
paneDragging: s.paneDragging,
|
||||||
});
|
});
|
||||||
|
|
||||||
const initialRect: SelectionRect = {
|
const UserSelection = memo(
|
||||||
startX: 0,
|
({
|
||||||
startY: 0,
|
isSelecting,
|
||||||
x: 0,
|
selectionMode = SelectionMode.Full,
|
||||||
y: 0,
|
panOnDrag,
|
||||||
width: 0,
|
onSelectionStart,
|
||||||
height: 0,
|
onSelectionEnd,
|
||||||
draw: false,
|
onPaneClick,
|
||||||
};
|
onPaneContextMenu,
|
||||||
|
onPaneScroll,
|
||||||
|
onPaneMouseEnter,
|
||||||
|
onPaneMouseMove,
|
||||||
|
onPaneMouseLeave,
|
||||||
|
children,
|
||||||
|
}: UserSelectionProps) => {
|
||||||
|
const container = useRef<HTMLDivElement | null>(null);
|
||||||
|
const store = useStoreApi();
|
||||||
|
const prevSelectedNodesCount = useRef<number>(0);
|
||||||
|
const prevSelectedEdgesCount = useRef<number>(0);
|
||||||
|
const containerBounds = useRef<DOMRect>();
|
||||||
|
const { userSelectionActive, elementsSelectable, paneDragging } = useStore(selector, shallow);
|
||||||
|
|
||||||
const UserSelection = memo(({ selectionKeyPressed }: UserSelectionProps) => {
|
const resetUserSelection = () => {
|
||||||
const store = useStoreApi();
|
store.setState({ userSelectionActive: false, userSelectionRect: null });
|
||||||
const prevSelectedNodesCount = useRef<number>(0);
|
|
||||||
const prevSelectedEdgesCount = useRef<number>(0);
|
|
||||||
const containerBounds = useRef<DOMRect>();
|
|
||||||
const [userSelectionRect, setUserSelectionRect] = useState<SelectionRect>(initialRect);
|
|
||||||
const { userSelectionActive, elementsSelectable } = useStore(selector, shallow);
|
|
||||||
|
|
||||||
const renderUserSelectionPane = userSelectionActive || selectionKeyPressed;
|
prevSelectedNodesCount.current = 0;
|
||||||
|
prevSelectedEdgesCount.current = 0;
|
||||||
if (!elementsSelectable || !renderUserSelectionPane) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const resetUserSelection = () => {
|
|
||||||
setUserSelectionRect(initialRect);
|
|
||||||
|
|
||||||
store.setState({ userSelectionActive: false });
|
|
||||||
|
|
||||||
prevSelectedNodesCount.current = 0;
|
|
||||||
prevSelectedEdgesCount.current = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
const onMouseDown = (event: React.MouseEvent): void => {
|
|
||||||
const reactFlowNode = (event.target as Element).closest('.react-flow')!;
|
|
||||||
containerBounds.current = reactFlowNode.getBoundingClientRect();
|
|
||||||
|
|
||||||
const mousePos = getMousePosition(event, containerBounds.current!);
|
|
||||||
|
|
||||||
setUserSelectionRect({
|
|
||||||
width: 0,
|
|
||||||
height: 0,
|
|
||||||
startX: mousePos.x,
|
|
||||||
startY: mousePos.y,
|
|
||||||
x: mousePos.x,
|
|
||||||
y: mousePos.y,
|
|
||||||
draw: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
store.setState({ userSelectionActive: true, nodesSelectionActive: false });
|
|
||||||
};
|
|
||||||
|
|
||||||
const onMouseMove = (event: React.MouseEvent): void => {
|
|
||||||
if (!selectionKeyPressed || !userSelectionRect.draw || !containerBounds.current) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const mousePos = getMousePosition(event, containerBounds.current!);
|
|
||||||
const startX = userSelectionRect.startX ?? 0;
|
|
||||||
const startY = userSelectionRect.startY ?? 0;
|
|
||||||
|
|
||||||
const nextUserSelectRect = {
|
|
||||||
...userSelectionRect,
|
|
||||||
x: mousePos.x < startX ? mousePos.x : startX,
|
|
||||||
y: mousePos.y < startY ? mousePos.y : startY,
|
|
||||||
width: Math.abs(mousePos.x - startX),
|
|
||||||
height: Math.abs(mousePos.y - startY),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const { nodeInternals, edges, transform, onNodesChange, onEdgesChange, nodeOrigin, getNodes } = store.getState();
|
const onClick = (event: ReactMouseEvent) => {
|
||||||
const selectedNodes = getNodesInside(nodeInternals, nextUserSelectRect, transform, false, true, nodeOrigin);
|
onPaneClick?.(event);
|
||||||
const selectedEdgeIds = getConnectedEdges(selectedNodes, edges).map((e) => e.id);
|
store.getState().resetSelectedElements();
|
||||||
const selectedNodeIds = selectedNodes.map((n) => n.id);
|
store.setState({ nodesSelectionActive: false });
|
||||||
|
};
|
||||||
|
|
||||||
|
const onContextMenu = (event: ReactMouseEvent) => {
|
||||||
|
if (panOnDrag === 'RightClick') {
|
||||||
|
event.preventDefault();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
onPaneContextMenu?.(event);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onWheel = onPaneScroll ? (event: React.WheelEvent) => onPaneScroll(event) : undefined;
|
||||||
|
|
||||||
|
const onMouseDown = (event: ReactMouseEvent): void => {
|
||||||
|
const { resetSelectedElements, domNode } = store.getState();
|
||||||
|
if (!elementsSelectable || !isSelecting || event.button !== 0 || event.target !== container.current || !domNode) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
containerBounds.current = domNode.getBoundingClientRect();
|
||||||
|
const { x, y } = getMousePosition(event, containerBounds.current!);
|
||||||
|
|
||||||
|
resetSelectedElements();
|
||||||
|
|
||||||
|
store.setState({
|
||||||
|
userSelectionRect: {
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
startX: x,
|
||||||
|
startY: y,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
onSelectionStart?.(event);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onMouseMove = (event: ReactMouseEvent): void => {
|
||||||
|
const { userSelectionRect, nodeInternals, edges, transform, onNodesChange, onEdgesChange, nodeOrigin, getNodes } =
|
||||||
|
store.getState();
|
||||||
|
if (!isSelecting || !containerBounds.current || !userSelectionRect) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
store.setState({ userSelectionActive: true, nodesSelectionActive: false });
|
||||||
|
|
||||||
|
const mousePos = getMousePosition(event, containerBounds.current!);
|
||||||
|
const startX = userSelectionRect.startX ?? 0;
|
||||||
|
const startY = userSelectionRect.startY ?? 0;
|
||||||
|
|
||||||
|
const nextUserSelectRect = {
|
||||||
|
...userSelectionRect,
|
||||||
|
x: mousePos.x < startX ? mousePos.x : startX,
|
||||||
|
y: mousePos.y < startY ? mousePos.y : startY,
|
||||||
|
width: Math.abs(mousePos.x - startX),
|
||||||
|
height: Math.abs(mousePos.y - startY),
|
||||||
|
};
|
||||||
|
|
||||||
if (prevSelectedNodesCount.current !== selectedNodeIds.length) {
|
|
||||||
const nodes = getNodes();
|
const nodes = getNodes();
|
||||||
prevSelectedNodesCount.current = selectedNodeIds.length;
|
const selectedNodes = getNodesInside(
|
||||||
const changes = getSelectionChanges(nodes, selectedNodeIds) as NodeChange[];
|
nodeInternals,
|
||||||
if (changes.length) {
|
nextUserSelectRect,
|
||||||
onNodesChange?.(changes);
|
transform,
|
||||||
|
selectionMode === SelectionMode.Partial,
|
||||||
|
true,
|
||||||
|
nodeOrigin
|
||||||
|
);
|
||||||
|
const selectedEdgeIds = getConnectedEdges(selectedNodes, edges).map((e) => e.id);
|
||||||
|
const selectedNodeIds = selectedNodes.map((n) => n.id);
|
||||||
|
|
||||||
|
if (prevSelectedNodesCount.current !== selectedNodeIds.length) {
|
||||||
|
prevSelectedNodesCount.current = selectedNodeIds.length;
|
||||||
|
const changes = getSelectionChanges(nodes, selectedNodeIds) as NodeChange[];
|
||||||
|
if (changes.length) {
|
||||||
|
onNodesChange?.(changes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (prevSelectedEdgesCount.current !== selectedEdgeIds.length) {
|
if (prevSelectedEdgesCount.current !== selectedEdgeIds.length) {
|
||||||
prevSelectedEdgesCount.current = selectedEdgeIds.length;
|
prevSelectedEdgesCount.current = selectedEdgeIds.length;
|
||||||
const changes = getSelectionChanges(edges, selectedEdgeIds) as EdgeChange[];
|
const changes = getSelectionChanges(edges, selectedEdgeIds) as EdgeChange[];
|
||||||
if (changes.length) {
|
if (changes.length) {
|
||||||
onEdgesChange?.(changes);
|
onEdgesChange?.(changes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
setUserSelectionRect(nextUserSelectRect);
|
store.setState({
|
||||||
};
|
userSelectionRect: nextUserSelectRect,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const onMouseUp = () => {
|
const onMouseUp = (event: ReactMouseEvent) => {
|
||||||
store.setState({ nodesSelectionActive: prevSelectedNodesCount.current > 0 });
|
const { userSelectionRect } = store.getState();
|
||||||
resetUserSelection();
|
// We only want to trigger click functions when in selection mode if
|
||||||
};
|
// the user did not move the mouse.
|
||||||
|
if (!userSelectionActive && userSelectionRect && event.target === container.current) {
|
||||||
|
onClick?.(event);
|
||||||
|
}
|
||||||
|
|
||||||
const onMouseLeave = () => {
|
store.setState({ nodesSelectionActive: prevSelectedNodesCount.current > 0 });
|
||||||
store.setState({ nodesSelectionActive: false });
|
|
||||||
resetUserSelection();
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
resetUserSelection();
|
||||||
<div
|
onSelectionEnd?.(event);
|
||||||
className="react-flow__selectionpane react-flow__container"
|
};
|
||||||
onMouseDown={onMouseDown}
|
|
||||||
onMouseMove={onMouseMove}
|
const onMouseLeave = (event: ReactMouseEvent) => {
|
||||||
onMouseUp={onMouseUp}
|
if (userSelectionActive) {
|
||||||
onMouseLeave={onMouseLeave}
|
store.setState({ nodesSelectionActive: prevSelectedNodesCount.current > 0 });
|
||||||
>
|
onSelectionEnd?.(event);
|
||||||
{userSelectionRect.draw && (
|
}
|
||||||
<div
|
|
||||||
className="react-flow__selection react-flow__container"
|
resetUserSelection();
|
||||||
style={{
|
};
|
||||||
width: userSelectionRect.width,
|
|
||||||
height: userSelectionRect.height,
|
const hasActiveSelection = elementsSelectable && (isSelecting || userSelectionActive);
|
||||||
transform: `translate(${userSelectionRect.x}px, ${userSelectionRect.y}px)`,
|
|
||||||
}}
|
return (
|
||||||
/>
|
<div
|
||||||
)}
|
className={cc([
|
||||||
</div>
|
'react-flow__pane',
|
||||||
);
|
'react-flow__container',
|
||||||
});
|
{ dragging: paneDragging, selection: isSelecting },
|
||||||
|
])}
|
||||||
|
onClick={hasActiveSelection ? undefined : wrapHandler(onClick, container)}
|
||||||
|
onContextMenu={wrapHandler(onContextMenu, container)}
|
||||||
|
onWheel={wrapHandler(onWheel, container)}
|
||||||
|
onMouseEnter={hasActiveSelection ? undefined : onPaneMouseEnter}
|
||||||
|
onMouseDown={hasActiveSelection ? onMouseDown : undefined}
|
||||||
|
onMouseMove={hasActiveSelection ? onMouseMove : onPaneMouseMove}
|
||||||
|
onMouseUp={hasActiveSelection ? onMouseUp : undefined}
|
||||||
|
onMouseLeave={hasActiveSelection ? onMouseLeave : onPaneMouseLeave}
|
||||||
|
ref={container}
|
||||||
|
style={containerStyle}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
<SelectionBox />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
UserSelection.displayName = 'UserSelection';
|
UserSelection.displayName = 'UserSelection';
|
||||||
|
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
import type { MouseEvent } from 'react';
|
|
||||||
import cc from 'classcat';
|
|
||||||
|
|
||||||
import { useStore } from '../../hooks/useStore';
|
|
||||||
import { containerStyle } from '../../styles';
|
|
||||||
import type { ReactFlowState } from '../../types';
|
|
||||||
import type { FlowRendererProps } from '.';
|
|
||||||
|
|
||||||
type PaneProps = Pick<FlowRendererProps, 'onClick' | 'onContextMenu' | 'onWheel'> & {
|
|
||||||
onMouseEnter?: (event: MouseEvent) => void;
|
|
||||||
onMouseMove?: (event: MouseEvent) => void;
|
|
||||||
onMouseLeave?: (event: MouseEvent) => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
const selector = (s: ReactFlowState) => s.paneDragging;
|
|
||||||
|
|
||||||
function Pane({ onClick, onMouseEnter, onMouseMove, onMouseLeave, onContextMenu, onWheel }: PaneProps) {
|
|
||||||
const dragging = useStore(selector);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className={cc(['react-flow__pane', { dragging }])}
|
|
||||||
onClick={onClick}
|
|
||||||
onMouseEnter={onMouseEnter}
|
|
||||||
onMouseMove={onMouseMove}
|
|
||||||
onMouseLeave={onMouseLeave}
|
|
||||||
onContextMenu={onContextMenu}
|
|
||||||
onWheel={onWheel}
|
|
||||||
style={containerStyle}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default Pane;
|
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
import { memo } from 'react';
|
import { memo } from 'react';
|
||||||
import type { ReactNode, WheelEvent, MouseEvent } from 'react';
|
import type { ReactNode } from 'react';
|
||||||
|
|
||||||
import { useStore, useStoreApi } from '../../hooks/useStore';
|
import { useStore } from '../../hooks/useStore';
|
||||||
import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler';
|
import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler';
|
||||||
import useKeyPress from '../../hooks/useKeyPress';
|
import useKeyPress from '../../hooks/useKeyPress';
|
||||||
import { GraphViewProps } from '../GraphView';
|
import { GraphViewProps } from '../GraphView';
|
||||||
import ZoomPane from '../ZoomPane';
|
import ZoomPane from '../ZoomPane';
|
||||||
import UserSelection from '../../components/UserSelection';
|
import UserSelection from '../../components/UserSelection';
|
||||||
import NodesSelection from '../../components/NodesSelection';
|
import NodesSelection from '../../components/NodesSelection';
|
||||||
import Pane from './Pane';
|
|
||||||
import type { ReactFlowState } from '../../types';
|
import type { ReactFlowState } from '../../types';
|
||||||
|
|
||||||
export type FlowRendererProps = Omit<
|
export type FlowRendererProps = Omit<
|
||||||
@@ -44,7 +43,12 @@ const FlowRenderer = ({
|
|||||||
onMoveStart,
|
onMoveStart,
|
||||||
onMoveEnd,
|
onMoveEnd,
|
||||||
selectionKeyCode,
|
selectionKeyCode,
|
||||||
|
selectionOnDrag,
|
||||||
|
selectionMode,
|
||||||
|
onSelectionStart,
|
||||||
|
onSelectionEnd,
|
||||||
multiSelectionKeyCode,
|
multiSelectionKeyCode,
|
||||||
|
panActivationKeyCode,
|
||||||
zoomActivationKeyCode,
|
zoomActivationKeyCode,
|
||||||
elementsSelectable,
|
elementsSelectable,
|
||||||
zoomOnScroll,
|
zoomOnScroll,
|
||||||
@@ -53,7 +57,7 @@ const FlowRenderer = ({
|
|||||||
panOnScrollSpeed,
|
panOnScrollSpeed,
|
||||||
panOnScrollMode,
|
panOnScrollMode,
|
||||||
zoomOnDoubleClick,
|
zoomOnDoubleClick,
|
||||||
panOnDrag,
|
panOnDrag: _panOnDrag,
|
||||||
defaultViewport,
|
defaultViewport,
|
||||||
translateExtent,
|
translateExtent,
|
||||||
minZoom,
|
minZoom,
|
||||||
@@ -64,27 +68,21 @@ const FlowRenderer = ({
|
|||||||
noPanClassName,
|
noPanClassName,
|
||||||
disableKeyboardA11y,
|
disableKeyboardA11y,
|
||||||
}: FlowRendererProps) => {
|
}: FlowRendererProps) => {
|
||||||
const store = useStoreApi();
|
|
||||||
const nodesSelectionActive = useStore(selector);
|
const nodesSelectionActive = useStore(selector);
|
||||||
const selectionKeyPressed = useKeyPress(selectionKeyCode);
|
const selectionKeyPressed = useKeyPress(selectionKeyCode);
|
||||||
|
const panActivationKeyPressed = useKeyPress(panActivationKeyCode);
|
||||||
|
|
||||||
|
const panOnDrag = panActivationKeyPressed || _panOnDrag;
|
||||||
|
const isSelecting = selectionKeyPressed || (selectionOnDrag && panOnDrag !== true);
|
||||||
|
|
||||||
useGlobalKeyHandler({ deleteKeyCode, multiSelectionKeyCode });
|
useGlobalKeyHandler({ deleteKeyCode, multiSelectionKeyCode });
|
||||||
|
|
||||||
const onClick = (event: MouseEvent) => {
|
|
||||||
onPaneClick?.(event);
|
|
||||||
store.getState().resetSelectedElements();
|
|
||||||
store.setState({ nodesSelectionActive: false });
|
|
||||||
};
|
|
||||||
|
|
||||||
const onContextMenu = onPaneContextMenu ? (event: MouseEvent) => onPaneContextMenu(event) : undefined;
|
|
||||||
const onWheel = onPaneScroll ? (event: WheelEvent) => onPaneScroll(event) : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ZoomPane
|
<ZoomPane
|
||||||
onMove={onMove}
|
onMove={onMove}
|
||||||
onMoveStart={onMoveStart}
|
onMoveStart={onMoveStart}
|
||||||
onMoveEnd={onMoveEnd}
|
onMoveEnd={onMoveEnd}
|
||||||
selectionKeyPressed={selectionKeyPressed}
|
onPaneContextMenu={onPaneContextMenu}
|
||||||
elementsSelectable={elementsSelectable}
|
elementsSelectable={elementsSelectable}
|
||||||
zoomOnScroll={zoomOnScroll}
|
zoomOnScroll={zoomOnScroll}
|
||||||
zoomOnPinch={zoomOnPinch}
|
zoomOnPinch={zoomOnPinch}
|
||||||
@@ -92,7 +90,7 @@ const FlowRenderer = ({
|
|||||||
panOnScrollSpeed={panOnScrollSpeed}
|
panOnScrollSpeed={panOnScrollSpeed}
|
||||||
panOnScrollMode={panOnScrollMode}
|
panOnScrollMode={panOnScrollMode}
|
||||||
zoomOnDoubleClick={zoomOnDoubleClick}
|
zoomOnDoubleClick={zoomOnDoubleClick}
|
||||||
panOnDrag={panOnDrag}
|
panOnDrag={!selectionKeyPressed && panOnDrag}
|
||||||
defaultViewport={defaultViewport}
|
defaultViewport={defaultViewport}
|
||||||
translateExtent={translateExtent}
|
translateExtent={translateExtent}
|
||||||
minZoom={minZoom}
|
minZoom={minZoom}
|
||||||
@@ -102,23 +100,28 @@ const FlowRenderer = ({
|
|||||||
noWheelClassName={noWheelClassName}
|
noWheelClassName={noWheelClassName}
|
||||||
noPanClassName={noPanClassName}
|
noPanClassName={noPanClassName}
|
||||||
>
|
>
|
||||||
{children}
|
<UserSelection
|
||||||
<UserSelection selectionKeyPressed={selectionKeyPressed} />
|
onSelectionStart={onSelectionStart}
|
||||||
{nodesSelectionActive && (
|
onSelectionEnd={onSelectionEnd}
|
||||||
<NodesSelection
|
onPaneClick={onPaneClick}
|
||||||
onSelectionContextMenu={onSelectionContextMenu}
|
onPaneMouseEnter={onPaneMouseEnter}
|
||||||
noPanClassName={noPanClassName}
|
onPaneMouseMove={onPaneMouseMove}
|
||||||
disableKeyboardA11y={disableKeyboardA11y}
|
onPaneMouseLeave={onPaneMouseLeave}
|
||||||
/>
|
onPaneContextMenu={onPaneContextMenu}
|
||||||
)}
|
onPaneScroll={onPaneScroll}
|
||||||
<Pane
|
panOnDrag={panOnDrag}
|
||||||
onClick={onClick}
|
isSelecting={!!isSelecting}
|
||||||
onMouseEnter={onPaneMouseEnter}
|
selectionMode={selectionMode}
|
||||||
onMouseMove={onPaneMouseMove}
|
>
|
||||||
onMouseLeave={onPaneMouseLeave}
|
{children}
|
||||||
onContextMenu={onContextMenu}
|
{nodesSelectionActive && (
|
||||||
onWheel={onWheel}
|
<NodesSelection
|
||||||
/>
|
onSelectionContextMenu={onSelectionContextMenu}
|
||||||
|
noPanClassName={noPanClassName}
|
||||||
|
disableKeyboardA11y={disableKeyboardA11y}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</UserSelection>
|
||||||
</ZoomPane>
|
</ZoomPane>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -51,12 +51,17 @@ const GraphView = ({
|
|||||||
onNodeMouseLeave,
|
onNodeMouseLeave,
|
||||||
onNodeContextMenu,
|
onNodeContextMenu,
|
||||||
onSelectionContextMenu,
|
onSelectionContextMenu,
|
||||||
|
onSelectionStart,
|
||||||
|
onSelectionEnd,
|
||||||
connectionLineType,
|
connectionLineType,
|
||||||
connectionLineStyle,
|
connectionLineStyle,
|
||||||
connectionLineComponent,
|
connectionLineComponent,
|
||||||
connectionLineContainerStyle,
|
connectionLineContainerStyle,
|
||||||
selectionKeyCode,
|
selectionKeyCode,
|
||||||
|
selectionOnDrag,
|
||||||
|
selectionMode,
|
||||||
multiSelectionKeyCode,
|
multiSelectionKeyCode,
|
||||||
|
panActivationKeyCode,
|
||||||
zoomActivationKeyCode,
|
zoomActivationKeyCode,
|
||||||
deleteKeyCode,
|
deleteKeyCode,
|
||||||
onlyRenderVisibleElements,
|
onlyRenderVisibleElements,
|
||||||
@@ -110,7 +115,12 @@ const GraphView = ({
|
|||||||
onPaneScroll={onPaneScroll}
|
onPaneScroll={onPaneScroll}
|
||||||
deleteKeyCode={deleteKeyCode}
|
deleteKeyCode={deleteKeyCode}
|
||||||
selectionKeyCode={selectionKeyCode}
|
selectionKeyCode={selectionKeyCode}
|
||||||
|
selectionOnDrag={selectionOnDrag}
|
||||||
|
selectionMode={selectionMode}
|
||||||
|
onSelectionStart={onSelectionStart}
|
||||||
|
onSelectionEnd={onSelectionEnd}
|
||||||
multiSelectionKeyCode={multiSelectionKeyCode}
|
multiSelectionKeyCode={multiSelectionKeyCode}
|
||||||
|
panActivationKeyCode={panActivationKeyCode}
|
||||||
zoomActivationKeyCode={zoomActivationKeyCode}
|
zoomActivationKeyCode={zoomActivationKeyCode}
|
||||||
elementsSelectable={elementsSelectable}
|
elementsSelectable={elementsSelectable}
|
||||||
onMove={onMove}
|
onMove={onMove}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import GraphView from '../GraphView';
|
|||||||
import Wrapper from './Wrapper';
|
import Wrapper from './Wrapper';
|
||||||
import { infiniteExtent } from '../../store/initialState';
|
import { infiniteExtent } from '../../store/initialState';
|
||||||
import { useNodeOrEdgeTypes } from './utils';
|
import { useNodeOrEdgeTypes } from './utils';
|
||||||
import { ConnectionLineType, ConnectionMode, PanOnScrollMode } from '../../types';
|
import { ConnectionLineType, ConnectionMode, PanOnScrollMode, SelectionMode } from '../../types';
|
||||||
import type {
|
import type {
|
||||||
EdgeTypes,
|
EdgeTypes,
|
||||||
EdgeTypesWrapped,
|
EdgeTypesWrapped,
|
||||||
@@ -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,
|
||||||
@@ -99,6 +101,9 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
|
|||||||
connectionLineContainerStyle,
|
connectionLineContainerStyle,
|
||||||
deleteKeyCode = 'Backspace',
|
deleteKeyCode = 'Backspace',
|
||||||
selectionKeyCode = 'Shift',
|
selectionKeyCode = 'Shift',
|
||||||
|
selectionOnDrag = false,
|
||||||
|
selectionMode = SelectionMode.Full,
|
||||||
|
panActivationKeyCode = 'Space',
|
||||||
multiSelectionKeyCode = 'Meta',
|
multiSelectionKeyCode = 'Meta',
|
||||||
zoomActivationKeyCode = 'Meta',
|
zoomActivationKeyCode = 'Meta',
|
||||||
snapToGrid = false,
|
snapToGrid = false,
|
||||||
@@ -193,8 +198,11 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
|
|||||||
connectionLineComponent={connectionLineComponent}
|
connectionLineComponent={connectionLineComponent}
|
||||||
connectionLineContainerStyle={connectionLineContainerStyle}
|
connectionLineContainerStyle={connectionLineContainerStyle}
|
||||||
selectionKeyCode={selectionKeyCode}
|
selectionKeyCode={selectionKeyCode}
|
||||||
|
selectionOnDrag={selectionOnDrag}
|
||||||
|
selectionMode={selectionMode}
|
||||||
deleteKeyCode={deleteKeyCode}
|
deleteKeyCode={deleteKeyCode}
|
||||||
multiSelectionKeyCode={multiSelectionKeyCode}
|
multiSelectionKeyCode={multiSelectionKeyCode}
|
||||||
|
panActivationKeyCode={panActivationKeyCode}
|
||||||
zoomActivationKeyCode={zoomActivationKeyCode}
|
zoomActivationKeyCode={zoomActivationKeyCode}
|
||||||
onlyRenderVisibleElements={onlyRenderVisibleElements}
|
onlyRenderVisibleElements={onlyRenderVisibleElements}
|
||||||
selectNodesOnDrag={selectNodesOnDrag}
|
selectNodesOnDrag={selectNodesOnDrag}
|
||||||
@@ -217,6 +225,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}
|
||||||
|
|||||||
@@ -9,14 +9,20 @@ import useKeyPress from '../../hooks/useKeyPress';
|
|||||||
import useResizeHandler from '../../hooks/useResizeHandler';
|
import useResizeHandler from '../../hooks/useResizeHandler';
|
||||||
import { useStore, useStoreApi } from '../../hooks/useStore';
|
import { useStore, useStoreApi } from '../../hooks/useStore';
|
||||||
import { containerStyle } from '../../styles';
|
import { containerStyle } from '../../styles';
|
||||||
import type { FlowRendererProps } from '../FlowRenderer';
|
import { clamp } from '../../utils';
|
||||||
import { CoordinateExtent, PanOnScrollMode } from '../../types';
|
import { CoordinateExtent, PanOnScrollMode } from '../../types';
|
||||||
|
import type { FlowRendererProps } from '../FlowRenderer';
|
||||||
import type { Viewport, ReactFlowState } from '../../types';
|
import type { Viewport, ReactFlowState } from '../../types';
|
||||||
|
|
||||||
type ZoomPaneProps = Omit<
|
type ZoomPaneProps = Omit<
|
||||||
FlowRendererProps,
|
FlowRendererProps,
|
||||||
'deleteKeyCode' | 'selectionKeyCode' | 'multiSelectionKeyCode' | 'noDragClassName' | 'disableKeyboardA11y'
|
| 'deleteKeyCode'
|
||||||
> & { selectionKeyPressed: boolean };
|
| 'selectionKeyCode'
|
||||||
|
| 'multiSelectionKeyCode'
|
||||||
|
| 'noDragClassName'
|
||||||
|
| 'disableKeyboardA11y'
|
||||||
|
| 'selectionOnDrag'
|
||||||
|
>;
|
||||||
|
|
||||||
const viewChanged = (prevViewport: Viewport, eventViewport: any): boolean =>
|
const viewChanged = (prevViewport: Viewport, eventViewport: any): boolean =>
|
||||||
prevViewport.x !== eventViewport.x || prevViewport.y !== eventViewport.y || prevViewport.zoom !== eventViewport.k;
|
prevViewport.x !== eventViewport.x || prevViewport.y !== eventViewport.y || prevViewport.zoom !== eventViewport.k;
|
||||||
@@ -33,19 +39,20 @@ const selector = (s: ReactFlowState) => ({
|
|||||||
d3Zoom: s.d3Zoom,
|
d3Zoom: s.d3Zoom,
|
||||||
d3Selection: s.d3Selection,
|
d3Selection: s.d3Selection,
|
||||||
d3ZoomHandler: s.d3ZoomHandler,
|
d3ZoomHandler: s.d3ZoomHandler,
|
||||||
|
userSelectionActive: s.userSelectionActive,
|
||||||
});
|
});
|
||||||
|
|
||||||
const ZoomPane = ({
|
const ZoomPane = ({
|
||||||
onMove,
|
onMove,
|
||||||
onMoveStart,
|
onMoveStart,
|
||||||
onMoveEnd,
|
onMoveEnd,
|
||||||
|
onPaneContextMenu,
|
||||||
zoomOnScroll = true,
|
zoomOnScroll = true,
|
||||||
zoomOnPinch = true,
|
zoomOnPinch = true,
|
||||||
panOnScroll = false,
|
panOnScroll = false,
|
||||||
panOnScrollSpeed = 0.5,
|
panOnScrollSpeed = 0.5,
|
||||||
panOnScrollMode = PanOnScrollMode.Free,
|
panOnScrollMode = PanOnScrollMode.Free,
|
||||||
zoomOnDoubleClick = true,
|
zoomOnDoubleClick = true,
|
||||||
selectionKeyPressed,
|
|
||||||
elementsSelectable,
|
elementsSelectable,
|
||||||
panOnDrag = true,
|
panOnDrag = true,
|
||||||
defaultViewport,
|
defaultViewport,
|
||||||
@@ -61,9 +68,10 @@ const ZoomPane = ({
|
|||||||
const timerId = useRef<ReturnType<typeof setTimeout>>();
|
const timerId = useRef<ReturnType<typeof setTimeout>>();
|
||||||
const store = useStoreApi();
|
const store = useStoreApi();
|
||||||
const isZoomingOrPanning = useRef(false);
|
const isZoomingOrPanning = useRef(false);
|
||||||
|
const hasMouseMoved = useRef(false);
|
||||||
const zoomPane = useRef<HTMLDivElement>(null);
|
const zoomPane = useRef<HTMLDivElement>(null);
|
||||||
const prevTransform = useRef<Viewport>({ x: 0, y: 0, zoom: 0 });
|
const prevTransform = useRef<Viewport>({ x: 0, y: 0, zoom: 0 });
|
||||||
const { d3Zoom, d3Selection, d3ZoomHandler } = useStore(selector, shallow);
|
const { d3Zoom, d3Selection, d3ZoomHandler, userSelectionActive } = useStore(selector, shallow);
|
||||||
const zoomActivationKeyPressed = useKeyPress(zoomActivationKeyCode);
|
const zoomActivationKeyPressed = useKeyPress(zoomActivationKeyCode);
|
||||||
|
|
||||||
useResizeHandler(zoomPane);
|
useResizeHandler(zoomPane);
|
||||||
@@ -73,7 +81,9 @@ const ZoomPane = ({
|
|||||||
const bbox = zoomPane.current.getBoundingClientRect();
|
const bbox = zoomPane.current.getBoundingClientRect();
|
||||||
const d3ZoomInstance = zoom().scaleExtent([minZoom, maxZoom]).translateExtent(translateExtent);
|
const d3ZoomInstance = zoom().scaleExtent([minZoom, maxZoom]).translateExtent(translateExtent);
|
||||||
const selection = select(zoomPane.current as Element).call(d3ZoomInstance);
|
const selection = select(zoomPane.current as Element).call(d3ZoomInstance);
|
||||||
const updatedTransform = zoomIdentity.translate(defaultViewport.x, defaultViewport.y).scale(defaultViewport.zoom);
|
const updatedTransform = zoomIdentity
|
||||||
|
.translate(defaultViewport.x, defaultViewport.y)
|
||||||
|
.scale(clamp(defaultViewport.zoom, minZoom, maxZoom));
|
||||||
const extent: CoordinateExtent = [
|
const extent: CoordinateExtent = [
|
||||||
[0, 0],
|
[0, 0],
|
||||||
[bbox.width, bbox.height],
|
[bbox.width, bbox.height],
|
||||||
@@ -95,7 +105,7 @@ const ZoomPane = ({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (d3Selection && d3Zoom) {
|
if (d3Selection && d3Zoom) {
|
||||||
if (panOnScroll && !zoomActivationKeyPressed) {
|
if (panOnScroll && !zoomActivationKeyPressed && !userSelectionActive) {
|
||||||
d3Selection.on('wheel.zoom', (event: any) => {
|
d3Selection.on('wheel.zoom', (event: any) => {
|
||||||
if (isWrappedWithClass(event, noWheelClassName)) {
|
if (isWrappedWithClass(event, noWheelClassName)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -139,6 +149,7 @@ const ZoomPane = ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
|
userSelectionActive,
|
||||||
panOnScroll,
|
panOnScroll,
|
||||||
panOnScrollMode,
|
panOnScrollMode,
|
||||||
d3Selection,
|
d3Selection,
|
||||||
@@ -152,9 +163,9 @@ const ZoomPane = ({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (d3Zoom) {
|
if (d3Zoom) {
|
||||||
if (selectionKeyPressed && !isZoomingOrPanning.current) {
|
if (userSelectionActive && !isZoomingOrPanning.current) {
|
||||||
d3Zoom.on('zoom', null);
|
d3Zoom.on('zoom', null);
|
||||||
} else if (!selectionKeyPressed) {
|
} else if (!userSelectionActive) {
|
||||||
d3Zoom.on('zoom', (event: D3ZoomEvent<HTMLDivElement, any>) => {
|
d3Zoom.on('zoom', (event: D3ZoomEvent<HTMLDivElement, any>) => {
|
||||||
const { onViewportChange } = store.getState();
|
const { onViewportChange } = store.getState();
|
||||||
store.setState({ transform: [event.transform.x, event.transform.y, event.transform.k] });
|
store.setState({ transform: [event.transform.x, event.transform.y, event.transform.k] });
|
||||||
@@ -165,10 +176,13 @@ const ZoomPane = ({
|
|||||||
onViewportChange?.(flowTransform);
|
onViewportChange?.(flowTransform);
|
||||||
onMove?.(event.sourceEvent as MouseEvent | TouchEvent, flowTransform);
|
onMove?.(event.sourceEvent as MouseEvent | TouchEvent, flowTransform);
|
||||||
}
|
}
|
||||||
|
if (panOnDrag === 'RightClick' && onPaneContextMenu) {
|
||||||
|
hasMouseMoved.current = true;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [selectionKeyPressed, d3Zoom, onMove]);
|
}, [userSelectionActive, d3Zoom, onMove, panOnDrag, onPaneContextMenu]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (d3Zoom) {
|
if (d3Zoom) {
|
||||||
@@ -218,9 +232,19 @@ const ZoomPane = ({
|
|||||||
panOnScroll ? 150 : 0
|
panOnScroll ? 150 : 0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
panOnDrag === 'RightClick' &&
|
||||||
|
onPaneContextMenu &&
|
||||||
|
!hasMouseMoved.current &&
|
||||||
|
event.sourceEvent?.button === 2
|
||||||
|
) {
|
||||||
|
onPaneContextMenu(event.sourceEvent);
|
||||||
|
}
|
||||||
|
hasMouseMoved.current = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [d3Zoom, onMoveEnd, panOnScroll]);
|
}, [d3Zoom, onMoveEnd, panOnScroll, panOnDrag, onPaneContextMenu]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (d3Zoom) {
|
if (d3Zoom) {
|
||||||
@@ -242,7 +266,7 @@ const ZoomPane = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// during a selection we prevent all other interactions
|
// during a selection we prevent all other interactions
|
||||||
if (selectionKeyPressed) {
|
if (userSelectionActive) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,18 +299,30 @@ const ZoomPane = ({
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if the pane is only movable using right clicks, prevent all other clicks
|
||||||
|
if (
|
||||||
|
panOnDrag === 'RightClick' &&
|
||||||
|
(event.type === 'mousedown' || event.type === 'touchstart') &&
|
||||||
|
event.button !== 2
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We only allow right clicks if pan on drag is set to right click
|
||||||
|
const buttonAllowed = panOnDrag === 'RightClick' ? 1 !== event.button : !event.button || event.button <= 1;
|
||||||
|
|
||||||
// default filter for d3-zoom
|
// default filter for d3-zoom
|
||||||
return (!event.ctrlKey || event.type === 'wheel') && (!event.button || event.button <= 1);
|
return (!event.ctrlKey || event.type === 'wheel') && buttonAllowed;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
|
userSelectionActive,
|
||||||
d3Zoom,
|
d3Zoom,
|
||||||
zoomOnScroll,
|
zoomOnScroll,
|
||||||
zoomOnPinch,
|
zoomOnPinch,
|
||||||
panOnScroll,
|
panOnScroll,
|
||||||
zoomOnDoubleClick,
|
zoomOnDoubleClick,
|
||||||
panOnDrag,
|
panOnDrag,
|
||||||
selectionKeyPressed,
|
|
||||||
elementsSelectable,
|
elementsSelectable,
|
||||||
zoomActivationKeyPressed,
|
zoomActivationKeyPressed,
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ const initialState: ReactFlowStore = {
|
|||||||
nodeExtent: infiniteExtent,
|
nodeExtent: infiniteExtent,
|
||||||
nodesSelectionActive: false,
|
nodesSelectionActive: false,
|
||||||
userSelectionActive: false,
|
userSelectionActive: false,
|
||||||
|
userSelectionRect: null,
|
||||||
connectionNodeId: null,
|
connectionNodeId: null,
|
||||||
connectionHandleId: null,
|
connectionHandleId: null,
|
||||||
connectionHandleType: 'source',
|
connectionHandleType: 'source',
|
||||||
|
|||||||
@@ -11,6 +11,10 @@
|
|||||||
z-index: 1;
|
z-index: 1;
|
||||||
cursor: grab;
|
cursor: grab;
|
||||||
|
|
||||||
|
&.selection {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
&.dragging {
|
&.dragging {
|
||||||
cursor: grabbing;
|
cursor: grabbing;
|
||||||
}
|
}
|
||||||
@@ -26,8 +30,8 @@
|
|||||||
z-index: 4;
|
z-index: 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
.react-flow__selectionpane {
|
.react-flow__selection {
|
||||||
z-index: 5;
|
z-index: 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
.react-flow__nodesselection-rect:focus,
|
.react-flow__nodesselection-rect:focus,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import type {
|
|||||||
NodeOrigin,
|
NodeOrigin,
|
||||||
EdgeMouseHandler,
|
EdgeMouseHandler,
|
||||||
HandleType,
|
HandleType,
|
||||||
|
SelectionMode,
|
||||||
} from '.';
|
} from '.';
|
||||||
|
|
||||||
export type ReactFlowProps = HTMLAttributes<HTMLDivElement> & {
|
export type ReactFlowProps = HTMLAttributes<HTMLDivElement> & {
|
||||||
@@ -68,6 +69,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;
|
||||||
@@ -94,6 +97,9 @@ export type ReactFlowProps = HTMLAttributes<HTMLDivElement> & {
|
|||||||
connectionMode?: ConnectionMode;
|
connectionMode?: ConnectionMode;
|
||||||
deleteKeyCode?: KeyCode | null;
|
deleteKeyCode?: KeyCode | null;
|
||||||
selectionKeyCode?: KeyCode | null;
|
selectionKeyCode?: KeyCode | null;
|
||||||
|
selectionOnDrag?: boolean;
|
||||||
|
selectionMode?: SelectionMode;
|
||||||
|
panActivationKeyCode?: KeyCode | null;
|
||||||
multiSelectionKeyCode?: KeyCode | null;
|
multiSelectionKeyCode?: KeyCode | null;
|
||||||
zoomActivationKeyCode?: KeyCode | null;
|
zoomActivationKeyCode?: KeyCode | null;
|
||||||
snapToGrid?: boolean;
|
snapToGrid?: boolean;
|
||||||
@@ -107,7 +113,7 @@ export type ReactFlowProps = HTMLAttributes<HTMLDivElement> & {
|
|||||||
initNodeOrigin?: NodeOrigin;
|
initNodeOrigin?: NodeOrigin;
|
||||||
elementsSelectable?: boolean;
|
elementsSelectable?: boolean;
|
||||||
selectNodesOnDrag?: boolean;
|
selectNodesOnDrag?: boolean;
|
||||||
panOnDrag?: boolean;
|
panOnDrag?: boolean | 'RightClick';
|
||||||
minZoom?: number;
|
minZoom?: number;
|
||||||
maxZoom?: number;
|
maxZoom?: number;
|
||||||
defaultViewport?: Viewport;
|
defaultViewport?: Viewport;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import type { MouseEvent as ReactMouseEvent, ComponentType, MemoExoticComponent
|
|||||||
import type { D3DragEvent, Selection as D3Selection, SubjectPosition, ZoomBehavior } from 'd3';
|
import type { D3DragEvent, Selection as D3Selection, SubjectPosition, ZoomBehavior } from 'd3';
|
||||||
|
|
||||||
import type { XYPosition, Rect, Transform, CoordinateExtent } from './utils';
|
import type { XYPosition, Rect, Transform, CoordinateExtent } from './utils';
|
||||||
import type { NodeChange, EdgeChange, NodePositionChange } from './changes';
|
import type { NodeChange, EdgeChange } from './changes';
|
||||||
import type {
|
import type {
|
||||||
Node,
|
Node,
|
||||||
NodeInternals,
|
NodeInternals,
|
||||||
@@ -155,6 +155,7 @@ export type ReactFlowStore = {
|
|||||||
|
|
||||||
nodesSelectionActive: boolean;
|
nodesSelectionActive: boolean;
|
||||||
userSelectionActive: boolean;
|
userSelectionActive: boolean;
|
||||||
|
userSelectionRect: SelectionRect | null;
|
||||||
|
|
||||||
connectionNodeId: string | null;
|
connectionNodeId: string | null;
|
||||||
connectionHandleId: string | null;
|
connectionHandleId: string | null;
|
||||||
@@ -249,3 +250,13 @@ export type ProOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type UseDragEvent = D3DragEvent<HTMLDivElement, null, SubjectPosition>;
|
export type UseDragEvent = D3DragEvent<HTMLDivElement, null, SubjectPosition>;
|
||||||
|
|
||||||
|
export enum SelectionMode {
|
||||||
|
Partial = 'partial',
|
||||||
|
Full = 'full',
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SelectionRect = Rect & {
|
||||||
|
startX: number;
|
||||||
|
startY: number;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user