feat(props): add panActivationKeyCode
This commit is contained in:
@@ -4,7 +4,6 @@ import ReactFlow, {
|
|||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
Node,
|
Node,
|
||||||
Edge,
|
Edge,
|
||||||
useReactFlow,
|
|
||||||
useKeyPress,
|
useKeyPress,
|
||||||
SelectionMode,
|
SelectionMode,
|
||||||
} from 'reactflow';
|
} from 'reactflow';
|
||||||
@@ -23,42 +22,9 @@ const initialEdges: Edge[] = [
|
|||||||
{ id: 'e1-3', source: '1', target: '3' },
|
{ id: 'e1-3', source: '1', target: '3' },
|
||||||
];
|
];
|
||||||
|
|
||||||
const defaultEdgeOptions = { zIndex: 0 };
|
|
||||||
|
|
||||||
const logEvent = (e: any) => console.log(e);
|
|
||||||
|
|
||||||
const onPaneClick = (e: any) => console.log('click pane', e);
|
|
||||||
|
|
||||||
const BasicFlow = () => {
|
const BasicFlow = () => {
|
||||||
const instance = useReactFlow();
|
|
||||||
const spaceBarPressed = useKeyPress('Space');
|
const spaceBarPressed = useKeyPress('Space');
|
||||||
|
|
||||||
const updatePos = () => {
|
|
||||||
instance.setNodes((nodes) =>
|
|
||||||
nodes.map((node) => {
|
|
||||||
node.position = {
|
|
||||||
x: Math.random() * 400,
|
|
||||||
y: Math.random() * 400,
|
|
||||||
};
|
|
||||||
|
|
||||||
return node;
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const logToObject = () => console.log(instance.toObject());
|
|
||||||
const resetTransform = () => instance.setViewport({ x: 0, y: 0, zoom: 1 });
|
|
||||||
|
|
||||||
const toggleClassnames = () => {
|
|
||||||
instance.setNodes((nodes) =>
|
|
||||||
nodes.map((node) => {
|
|
||||||
node.className = node.className === 'light' ? 'dark' : 'light';
|
|
||||||
|
|
||||||
return node;
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ReactFlow
|
<ReactFlow
|
||||||
defaultNodes={initialNodes}
|
defaultNodes={initialNodes}
|
||||||
@@ -67,31 +33,12 @@ const BasicFlow = () => {
|
|||||||
selectionMode={SelectionMode.Partial}
|
selectionMode={SelectionMode.Partial}
|
||||||
panOnDrag={spaceBarPressed ? true : 'RightClick'}
|
panOnDrag={spaceBarPressed ? true : 'RightClick'}
|
||||||
panOnScroll
|
panOnScroll
|
||||||
onPaneContextMenu={logEvent}
|
zoomActivationKeyCode="Meta"
|
||||||
zoomActivationKeyCode={'Meta'}
|
|
||||||
multiSelectionKeyCode={MULTI_SELECT_KEY}
|
multiSelectionKeyCode={MULTI_SELECT_KEY}
|
||||||
className="react-flow-basic-example"
|
|
||||||
minZoom={0.2}
|
|
||||||
maxZoom={4}
|
|
||||||
onPaneClick={onPaneClick}
|
|
||||||
fitView
|
fitView
|
||||||
defaultEdgeOptions={defaultEdgeOptions}
|
|
||||||
selectNodesOnDrag={false}
|
selectNodesOnDrag={false}
|
||||||
>
|
>
|
||||||
<Background variant={BackgroundVariant.Lines} />
|
<Background variant={BackgroundVariant.Cross} />
|
||||||
|
|
||||||
<div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}>
|
|
||||||
<button onClick={resetTransform} style={{ marginRight: 5 }}>
|
|
||||||
reset transform
|
|
||||||
</button>
|
|
||||||
<button onClick={updatePos} style={{ marginRight: 5 }}>
|
|
||||||
change pos
|
|
||||||
</button>
|
|
||||||
<button onClick={toggleClassnames} style={{ marginRight: 5 }}>
|
|
||||||
toggle classnames
|
|
||||||
</button>
|
|
||||||
<button onClick={logToObject}>toObject</button>
|
|
||||||
</div>
|
|
||||||
</ReactFlow>
|
</ReactFlow>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ const FlowRenderer = ({
|
|||||||
onSelectionStart,
|
onSelectionStart,
|
||||||
onSelectionEnd,
|
onSelectionEnd,
|
||||||
multiSelectionKeyCode,
|
multiSelectionKeyCode,
|
||||||
|
panActivationKeyCode,
|
||||||
zoomActivationKeyCode,
|
zoomActivationKeyCode,
|
||||||
elementsSelectable,
|
elementsSelectable,
|
||||||
zoomOnScroll,
|
zoomOnScroll,
|
||||||
@@ -91,6 +92,7 @@ const FlowRenderer = ({
|
|||||||
translateExtent={translateExtent}
|
translateExtent={translateExtent}
|
||||||
minZoom={minZoom}
|
minZoom={minZoom}
|
||||||
maxZoom={maxZoom}
|
maxZoom={maxZoom}
|
||||||
|
panActivationKeyCode={panActivationKeyCode}
|
||||||
zoomActivationKeyCode={zoomActivationKeyCode}
|
zoomActivationKeyCode={zoomActivationKeyCode}
|
||||||
preventScrolling={preventScrolling}
|
preventScrolling={preventScrolling}
|
||||||
noWheelClassName={noWheelClassName}
|
noWheelClassName={noWheelClassName}
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ const GraphView = ({
|
|||||||
selectionOnDrag,
|
selectionOnDrag,
|
||||||
selectionMode,
|
selectionMode,
|
||||||
multiSelectionKeyCode,
|
multiSelectionKeyCode,
|
||||||
|
panActivationKeyCode,
|
||||||
zoomActivationKeyCode,
|
zoomActivationKeyCode,
|
||||||
deleteKeyCode,
|
deleteKeyCode,
|
||||||
onlyRenderVisibleElements,
|
onlyRenderVisibleElements,
|
||||||
@@ -119,6 +120,7 @@ const GraphView = ({
|
|||||||
onSelectionStart={onSelectionStart}
|
onSelectionStart={onSelectionStart}
|
||||||
onSelectionEnd={onSelectionEnd}
|
onSelectionEnd={onSelectionEnd}
|
||||||
multiSelectionKeyCode={multiSelectionKeyCode}
|
multiSelectionKeyCode={multiSelectionKeyCode}
|
||||||
|
panActivationKeyCode={panActivationKeyCode}
|
||||||
zoomActivationKeyCode={zoomActivationKeyCode}
|
zoomActivationKeyCode={zoomActivationKeyCode}
|
||||||
elementsSelectable={elementsSelectable}
|
elementsSelectable={elementsSelectable}
|
||||||
onMove={onMove}
|
onMove={onMove}
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
|
|||||||
selectionKeyCode = 'Shift',
|
selectionKeyCode = 'Shift',
|
||||||
selectionOnDrag = false,
|
selectionOnDrag = false,
|
||||||
selectionMode = SelectionMode.Full,
|
selectionMode = SelectionMode.Full,
|
||||||
|
panActivationKeyCode = 'Space',
|
||||||
multiSelectionKeyCode = 'Meta',
|
multiSelectionKeyCode = 'Meta',
|
||||||
zoomActivationKeyCode = 'Meta',
|
zoomActivationKeyCode = 'Meta',
|
||||||
snapToGrid = false,
|
snapToGrid = false,
|
||||||
@@ -201,6 +202,7 @@ const ReactFlow = forwardRef<ReactFlowRefType, ReactFlowProps>(
|
|||||||
selectionMode={selectionMode}
|
selectionMode={selectionMode}
|
||||||
deleteKeyCode={deleteKeyCode}
|
deleteKeyCode={deleteKeyCode}
|
||||||
multiSelectionKeyCode={multiSelectionKeyCode}
|
multiSelectionKeyCode={multiSelectionKeyCode}
|
||||||
|
panActivationKeyCode={panActivationKeyCode}
|
||||||
zoomActivationKeyCode={zoomActivationKeyCode}
|
zoomActivationKeyCode={zoomActivationKeyCode}
|
||||||
onlyRenderVisibleElements={onlyRenderVisibleElements}
|
onlyRenderVisibleElements={onlyRenderVisibleElements}
|
||||||
selectNodesOnDrag={selectNodesOnDrag}
|
selectNodesOnDrag={selectNodesOnDrag}
|
||||||
|
|||||||
@@ -53,11 +53,12 @@ const ZoomPane = ({
|
|||||||
panOnScrollMode = PanOnScrollMode.Free,
|
panOnScrollMode = PanOnScrollMode.Free,
|
||||||
zoomOnDoubleClick = true,
|
zoomOnDoubleClick = true,
|
||||||
elementsSelectable,
|
elementsSelectable,
|
||||||
panOnDrag = true,
|
panOnDrag: _panOnDrag = true,
|
||||||
defaultViewport,
|
defaultViewport,
|
||||||
translateExtent,
|
translateExtent,
|
||||||
minZoom,
|
minZoom,
|
||||||
maxZoom,
|
maxZoom,
|
||||||
|
panActivationKeyCode,
|
||||||
zoomActivationKeyCode,
|
zoomActivationKeyCode,
|
||||||
preventScrolling = true,
|
preventScrolling = true,
|
||||||
children,
|
children,
|
||||||
@@ -72,6 +73,8 @@ const ZoomPane = ({
|
|||||||
const prevTransform = useRef<Viewport>({ x: 0, y: 0, zoom: 0 });
|
const prevTransform = useRef<Viewport>({ x: 0, y: 0, zoom: 0 });
|
||||||
const { d3Zoom, d3Selection, d3ZoomHandler, userSelectionActive } = useStore(selector, shallow);
|
const { d3Zoom, d3Selection, d3ZoomHandler, userSelectionActive } = useStore(selector, shallow);
|
||||||
const zoomActivationKeyPressed = useKeyPress(zoomActivationKeyCode);
|
const zoomActivationKeyPressed = useKeyPress(zoomActivationKeyCode);
|
||||||
|
const panActivationKeyPressed = useKeyPress(panActivationKeyCode);
|
||||||
|
const panOnDrag = _panOnDrag || panActivationKeyPressed;
|
||||||
|
|
||||||
useResizeHandler(zoomPane);
|
useResizeHandler(zoomPane);
|
||||||
|
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ export type ReactFlowProps = HTMLAttributes<HTMLDivElement> & {
|
|||||||
selectionKeyCode?: KeyCode | null;
|
selectionKeyCode?: KeyCode | null;
|
||||||
selectionOnDrag?: boolean;
|
selectionOnDrag?: boolean;
|
||||||
selectionMode?: SelectionMode;
|
selectionMode?: SelectionMode;
|
||||||
|
panActivationKeyCode?: KeyCode | null;
|
||||||
multiSelectionKeyCode?: KeyCode | null;
|
multiSelectionKeyCode?: KeyCode | null;
|
||||||
zoomActivationKeyCode?: KeyCode | null;
|
zoomActivationKeyCode?: KeyCode | null;
|
||||||
snapToGrid?: boolean;
|
snapToGrid?: boolean;
|
||||||
|
|||||||
Reference in New Issue
Block a user