Skip to content

Commit d4ccb11

Browse files
committed
feat(core): add autoPanSpeed prop (#1535)
* feat(core): add `autoPanSpeed` prop * chore(changeset): add
1 parent 6aa82cb commit d4ccb11

File tree

7 files changed

+29
-8
lines changed

7 files changed

+29
-8
lines changed

.changeset/good-turtles-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": minor
3+
---
4+
5+
Add `autoPanSpeed` prop. Allows specifying at what speed the pane moves when auto-panning via node-drag, selection-drag or connection-drag

packages/core/src/composables/useDrag.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function useDrag(params: UseDragParams) {
4646
nodeDragThreshold,
4747
viewport,
4848
autoPanOnNodeDrag,
49+
autoPanSpeed,
4950
nodesDraggable,
5051
panBy,
5152
findNode,
@@ -132,7 +133,7 @@ export function useDrag(params: UseDragParams) {
132133
return
133134
}
134135

135-
const [xMovement, yMovement] = calcAutoPan(mousePosition, containerBounds)
136+
const [xMovement, yMovement] = calcAutoPan(mousePosition, containerBounds, autoPanSpeed.value)
136137

137138
if (xMovement !== 0 || yMovement !== 0) {
138139
const nextPos = {

packages/core/src/composables/useHandle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function useHandle({
5656
connectionClickStartHandle,
5757
nodesConnectable,
5858
autoPanOnConnect,
59+
autoPanSpeed,
5960
findNode,
6061
panBy,
6162
startConnection,
@@ -120,7 +121,7 @@ export function useHandle({
120121
return
121122
}
122123

123-
const [xMovement, yMovement] = calcAutoPan(connectionPosition, containerBounds)
124+
const [xMovement, yMovement] = calcAutoPan(connectionPosition, containerBounds, autoPanSpeed.value)
124125

125126
panBy({ x: xMovement, y: yMovement })
126127
autoPanId = requestAnimationFrame(autoPan)

packages/core/src/store/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export function useState(): State {
109109

110110
autoPanOnNodeDrag: true,
111111
autoPanOnConnect: true,
112+
autoPanSpeed: 15,
112113

113114
disableKeyboardA11y: false,
114115
ariaLiveMessage: '',

packages/core/src/types/flow.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ export interface FlowProps {
223223

224224
autoPanOnConnect?: boolean
225225
autoPanOnNodeDrag?: boolean
226+
autoPanSpeed?: number
226227
}
227228

228229
/**

packages/core/src/types/store.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ export interface State extends Omit<FlowProps, 'id' | 'modelValue'> {
149149

150150
autoPanOnConnect: boolean
151151
autoPanOnNodeDrag: boolean
152+
/**
153+
* The speed at which the viewport pans while dragging a node or a selection box.
154+
* @default 15
155+
*/
156+
autoPanSpeed: number
152157

153158
disableKeyboardA11y: boolean
154159

packages/core/src/utils/autopan.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ import { clamp } from './graph'
55
// when the mouse is close to the edge of the canvas
66
function calcAutoPanVelocity(value: number, min: number, max: number) {
77
if (value < min) {
8-
return clamp(Math.abs(value - min), 1, 50) / 50
9-
} else if (value > max) {
10-
return -clamp(Math.abs(value - max), 1, 50) / 50
8+
return clamp(Math.abs(value - min), 1, min) / min
9+
}
10+
11+
if (value > max) {
12+
return -clamp(Math.abs(value - max), 1, min) / min
1113
}
1214

1315
return 0
1416
}
1517

16-
export function calcAutoPan(pos: XYPosition, bounds: Dimensions) {
17-
const xMovement = calcAutoPanVelocity(pos.x, 35, bounds.width - 35) * 20
18-
const yMovement = calcAutoPanVelocity(pos.y, 35, bounds.height - 35) * 20
18+
export function calcAutoPan(
19+
pos: XYPosition,
20+
bounds: Dimensions,
21+
speed = 15,
22+
distance = 40,
23+
): [xMovement: number, yMovement: number] {
24+
const xMovement = calcAutoPanVelocity(pos.x, distance, bounds.width - distance) * speed
25+
const yMovement = calcAutoPanVelocity(pos.y, distance, bounds.height - distance) * speed
1926

2027
return [xMovement, yMovement]
2128
}

0 commit comments

Comments
 (0)