feat(core): add autoPanSpeed prop (#1535)

* feat(core): add `autoPanSpeed` prop

* chore(changeset): add
This commit is contained in:
Braks
2024-07-09 15:20:46 +02:00
parent 6aa82cbc4d
commit d4ccb11a4c
7 changed files with 29 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
---
"@vue-flow/core": minor
---
Add `autoPanSpeed` prop. Allows specifying at what speed the pane moves when auto-panning via node-drag, selection-drag or connection-drag

View File

@@ -46,6 +46,7 @@ export function useDrag(params: UseDragParams) {
nodeDragThreshold, nodeDragThreshold,
viewport, viewport,
autoPanOnNodeDrag, autoPanOnNodeDrag,
autoPanSpeed,
nodesDraggable, nodesDraggable,
panBy, panBy,
findNode, findNode,
@@ -132,7 +133,7 @@ export function useDrag(params: UseDragParams) {
return return
} }
const [xMovement, yMovement] = calcAutoPan(mousePosition, containerBounds) const [xMovement, yMovement] = calcAutoPan(mousePosition, containerBounds, autoPanSpeed.value)
if (xMovement !== 0 || yMovement !== 0) { if (xMovement !== 0 || yMovement !== 0) {
const nextPos = { const nextPos = {

View File

@@ -56,6 +56,7 @@ export function useHandle({
connectionClickStartHandle, connectionClickStartHandle,
nodesConnectable, nodesConnectable,
autoPanOnConnect, autoPanOnConnect,
autoPanSpeed,
findNode, findNode,
panBy, panBy,
startConnection, startConnection,
@@ -120,7 +121,7 @@ export function useHandle({
return return
} }
const [xMovement, yMovement] = calcAutoPan(connectionPosition, containerBounds) const [xMovement, yMovement] = calcAutoPan(connectionPosition, containerBounds, autoPanSpeed.value)
panBy({ x: xMovement, y: yMovement }) panBy({ x: xMovement, y: yMovement })
autoPanId = requestAnimationFrame(autoPan) autoPanId = requestAnimationFrame(autoPan)

View File

@@ -109,6 +109,7 @@ export function useState(): State {
autoPanOnNodeDrag: true, autoPanOnNodeDrag: true,
autoPanOnConnect: true, autoPanOnConnect: true,
autoPanSpeed: 15,
disableKeyboardA11y: false, disableKeyboardA11y: false,
ariaLiveMessage: '', ariaLiveMessage: '',

View File

@@ -223,6 +223,7 @@ export interface FlowProps {
autoPanOnConnect?: boolean autoPanOnConnect?: boolean
autoPanOnNodeDrag?: boolean autoPanOnNodeDrag?: boolean
autoPanSpeed?: number
} }
/** /**

View File

@@ -149,6 +149,11 @@ export interface State extends Omit<FlowProps, 'id' | 'modelValue'> {
autoPanOnConnect: boolean autoPanOnConnect: boolean
autoPanOnNodeDrag: boolean autoPanOnNodeDrag: boolean
/**
* The speed at which the viewport pans while dragging a node or a selection box.
* @default 15
*/
autoPanSpeed: number
disableKeyboardA11y: boolean disableKeyboardA11y: boolean

View File

@@ -5,17 +5,24 @@ import { clamp } from './graph'
// when the mouse is close to the edge of the canvas // when the mouse is close to the edge of the canvas
function calcAutoPanVelocity(value: number, min: number, max: number) { function calcAutoPanVelocity(value: number, min: number, max: number) {
if (value < min) { if (value < min) {
return clamp(Math.abs(value - min), 1, 50) / 50 return clamp(Math.abs(value - min), 1, min) / min
} else if (value > max) { }
return -clamp(Math.abs(value - max), 1, 50) / 50
if (value > max) {
return -clamp(Math.abs(value - max), 1, min) / min
} }
return 0 return 0
} }
export function calcAutoPan(pos: XYPosition, bounds: Dimensions) { export function calcAutoPan(
const xMovement = calcAutoPanVelocity(pos.x, 35, bounds.width - 35) * 20 pos: XYPosition,
const yMovement = calcAutoPanVelocity(pos.y, 35, bounds.height - 35) * 20 bounds: Dimensions,
speed = 15,
distance = 40,
): [xMovement: number, yMovement: number] {
const xMovement = calcAutoPanVelocity(pos.x, distance, bounds.width - distance) * speed
const yMovement = calcAutoPanVelocity(pos.y, distance, bounds.height - distance) * speed
return [xMovement, yMovement] return [xMovement, yMovement]
} }