Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): add onBeforeTransform arg to panBy action #860

Merged
merged 4 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-pots-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/core": patch
---

Limit auto-pan when dragging a node by translate extent, so nodes cannot be dragged infinitely into the background.
22 changes: 16 additions & 6 deletions packages/core/src/composables/useDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function useDrag(params: UseDragParams) {

let autoPanId = $ref(0)
let autoPanStarted = $ref(false)
let previousTransform = $ref<XYPosition>({ x: 0, y: 0 })

const getPointerPosition = useGetPointerPosition()

Expand All @@ -79,7 +80,7 @@ function useDrag(params: UseDragParams) {
n.parentNode ? findNode(n.parentNode) : undefined,
)

// we want to make sure that we only fire a change event when there is a changes
// we want to make sure that we only fire a change event when there is a change
hasChange = hasChange || n.position.x !== computedPosition.x || n.position.y !== computedPosition.y

n.position = computedPosition
Expand Down Expand Up @@ -114,12 +115,21 @@ function useDrag(params: UseDragParams) {
const [xMovement, yMovement] = calcAutoPan(mousePosition, containerBounds)

if (xMovement !== 0 || yMovement !== 0) {
lastPos.x = (lastPos.x ?? 0) - xMovement / viewport.zoom
lastPos.y = (lastPos.y ?? 0) - yMovement / viewport.zoom

updateNodes(lastPos as XYPosition)
const nextPos = {
x: (lastPos.x ?? 0) - xMovement / viewport.zoom,
y: (lastPos.y ?? 0) - yMovement / viewport.zoom,
}

panBy({ x: xMovement, y: yMovement })
panBy({ x: xMovement, y: yMovement }, (transform) => {
if (
Math.round(transform.x) !== Math.round(previousTransform.x) ||
Math.round(transform.y) !== Math.round(previousTransform.y)
) {
updateNodes(nextPos)
previousTransform = transform
lastPos = nextPos
}
})
}

autoPanId = requestAnimationFrame(autoPan)
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ export function useActions(
return partiallyVisible || overlappingArea >= Number(nodeOrRect.width) * Number(nodeOrRect.height)
}

const panBy: Actions['panBy'] = (delta) => {
const panBy: Actions['panBy'] = (delta, onBeforeTransform) => {
const { viewport, dimensions, d3Zoom, d3Selection, translateExtent } = state

if (!d3Zoom || !d3Selection || (!delta.x && !delta.y)) {
Expand All @@ -645,6 +645,8 @@ export function useActions(

const constrainedTransform = d3Zoom.constrain()(nextTransform, extent, translateExtent)

onBeforeTransform?.(constrainedTransform)

d3Zoom.transform(d3Selection, constrainedTransform)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CSSProperties, ComputedRef, ToRefs } from 'vue'
import type { KeyFilter } from '@vueuse/core'
import type { ZoomTransform } from 'd3-zoom'
import type {
Dimensions,
ElementData,
Expand Down Expand Up @@ -269,7 +270,7 @@ export interface Actions extends ViewportFunctions {
/** check if a node is intersecting with a defined area */
isNodeIntersecting: IsNodeIntersecting
/** pan the viewport */
panBy: (delta: XYPosition) => void
panBy: (delta: XYPosition, onBeforeTransform?: (transform: ZoomTransform) => void) => void

/** reset state to defaults */
$reset: () => void
Expand Down