add inverse pan to minimap

This commit is contained in:
Artyom Sovetnikov
2023-03-21 16:27:01 +03:00
parent 29790476d8
commit 9303bf1b32
3 changed files with 10 additions and 4 deletions
@@ -1,4 +1,4 @@
import { MouseEvent, useCallback } from 'react'; import { MouseEvent, useCallback, useState } from "react";
import ReactFlow, { import ReactFlow, {
MiniMap, MiniMap,
Background, Background,
@@ -87,6 +87,7 @@ const defaultEdgeOptions = { zIndex: 0 };
const BasicFlow = () => { const BasicFlow = () => {
const instance = useReactFlow(); const instance = useReactFlow();
const [inverse, setInverse] = useState(false);
const updatePos = () => { const updatePos = () => {
instance.setNodes((nodes) => instance.setNodes((nodes) =>
@@ -137,7 +138,8 @@ const BasicFlow = () => {
fitView fitView
> >
<Background variant={BackgroundVariant.Dots} /> <Background variant={BackgroundVariant.Dots} />
<MiniMap onClick={onMiniMapClick} onNodeClick={onMiniMapNodeClick} pannable zoomable /> <MiniMap onClick={onMiniMapClick} onNodeClick={onMiniMapNodeClick} pannable zoomable
inversePan={inverse}/>
<Controls /> <Controls />
<div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}> <div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}>
@@ -151,6 +153,7 @@ const BasicFlow = () => {
toggle classnames toggle classnames
</button> </button>
<button onClick={logToObject}>toObject</button> <button onClick={logToObject}>toObject</button>
<button onClick={_ => setInverse(!inverse)}>{inverse ? 'un-inverse pan' : 'inverse pan'}</button>
</div> </div>
</ReactFlow> </ReactFlow>
); );
+4 -2
View File
@@ -67,6 +67,7 @@ function MiniMap({
pannable = false, pannable = false,
zoomable = false, zoomable = false,
ariaLabel = 'React Flow mini map', ariaLabel = 'React Flow mini map',
inversePan = false
}: MiniMapProps) { }: MiniMapProps) {
const store = useStoreApi(); const store = useStoreApi();
const svg = useRef<SVGSVGElement>(null); const svg = useRef<SVGSVGElement>(null);
@@ -120,9 +121,10 @@ function MiniMap({
} }
// @TODO: how to calculate the correct next position? Math.max(1, transform[2]) is a workaround. // @TODO: how to calculate the correct next position? Math.max(1, transform[2]) is a workaround.
const moveScale = viewScaleRef.current * Math.max(1, transform[2]) * (inversePan ? -1 : 1);
const position = { const position = {
x: transform[0] - event.sourceEvent.movementX * viewScaleRef.current * Math.max(1, transform[2]), x: transform[0] - event.sourceEvent.movementX * moveScale,
y: transform[1] - event.sourceEvent.movementY * viewScaleRef.current * Math.max(1, transform[2]), y: transform[1] - event.sourceEvent.movementY * moveScale,
}; };
const extent: CoordinateExtent = [ const extent: CoordinateExtent = [
[0, 0], [0, 0],
+1
View File
@@ -20,6 +20,7 @@ export type MiniMapProps<NodeData = any> = Omit<HTMLAttributes<SVGSVGElement>, '
pannable?: boolean; pannable?: boolean;
zoomable?: boolean; zoomable?: boolean;
ariaLabel?: string | null; ariaLabel?: string | null;
inversePan?: boolean;
}; };
export interface MiniMapNodeProps { export interface MiniMapNodeProps {