refactor(core): add onBeforeTransform arg to panBy action
Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -18,26 +18,28 @@ interface UseDragParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function useDrag(params: UseDragParams) {
|
function useDrag(params: UseDragParams) {
|
||||||
const {
|
|
||||||
vueFlowRef,
|
const {
|
||||||
snapToGrid,
|
vueFlowRef,
|
||||||
snapGrid,
|
snapToGrid,
|
||||||
noDragClassName,
|
snapGrid,
|
||||||
nodes,
|
noDragClassName,
|
||||||
nodeExtent,
|
nodes,
|
||||||
viewport,
|
nodeExtent,
|
||||||
autoPanOnNodeDrag,
|
viewport,
|
||||||
nodesDraggable,
|
autoPanOnNodeDrag,
|
||||||
panBy,
|
nodesDraggable,
|
||||||
findNode,
|
panBy,
|
||||||
multiSelectionActive,
|
findNode,
|
||||||
nodesSelectionActive,
|
multiSelectionActive,
|
||||||
selectNodesOnDrag,
|
nodesSelectionActive,
|
||||||
removeSelectedElements,
|
selectNodesOnDrag,
|
||||||
addSelectedNodes,
|
removeSelectedElements,
|
||||||
updateNodePositions,
|
addSelectedNodes,
|
||||||
emits,
|
updateNodePositions,
|
||||||
} = $(useVueFlow())
|
emits,
|
||||||
|
translateExtent,
|
||||||
|
} = $(useVueFlow())
|
||||||
|
|
||||||
const { onStart, onDrag, onStop, el, disabled, id, selectable } = params
|
const { onStart, onDrag, onStop, el, disabled, id, selectable } = params
|
||||||
|
|
||||||
@@ -58,7 +60,7 @@ function useDrag(params: UseDragParams) {
|
|||||||
|
|
||||||
const getPointerPosition = useGetPointerPosition()
|
const getPointerPosition = useGetPointerPosition()
|
||||||
|
|
||||||
const updateNodes = ({ x, y }: XYPosition) => {
|
const updateNodes = ({ x, y }: XYPosition, onBeforeUpdate: (position: XYPosition) => XYPosition = (xyPos) => xyPos) => {
|
||||||
lastPos = { x, y }
|
lastPos = { x, y }
|
||||||
|
|
||||||
let hasChange = false
|
let hasChange = false
|
||||||
@@ -71,7 +73,7 @@ function useDrag(params: UseDragParams) {
|
|||||||
nextPosition.y = snapGrid[1] * Math.round(nextPosition.y / snapGrid[1])
|
nextPosition.y = snapGrid[1] * Math.round(nextPosition.y / snapGrid[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
const { computedPosition } = calcNextPosition(
|
let { computedPosition } = calcNextPosition(
|
||||||
n,
|
n,
|
||||||
nextPosition,
|
nextPosition,
|
||||||
emits.error,
|
emits.error,
|
||||||
@@ -79,7 +81,9 @@ function useDrag(params: UseDragParams) {
|
|||||||
n.parentNode ? findNode(n.parentNode) : undefined,
|
n.parentNode ? findNode(n.parentNode) : undefined,
|
||||||
)
|
)
|
||||||
|
|
||||||
// we want to make sure that we only fire a change event when there is a changes
|
computedPosition = onBeforeUpdate(computedPosition)
|
||||||
|
|
||||||
|
// 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
|
hasChange = hasChange || n.position.x !== computedPosition.x || n.position.y !== computedPosition.y
|
||||||
|
|
||||||
n.position = computedPosition
|
n.position = computedPosition
|
||||||
@@ -108,19 +112,23 @@ function useDrag(params: UseDragParams) {
|
|||||||
|
|
||||||
const autoPan = (): void => {
|
const autoPan = (): void => {
|
||||||
if (!containerBounds) {
|
if (!containerBounds) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const [xMovement, yMovement] = calcAutoPan(mousePosition, containerBounds)
|
const [xMovement, yMovement] = calcAutoPan(mousePosition, containerBounds)
|
||||||
|
|
||||||
if (xMovement !== 0 || yMovement !== 0) {
|
if (xMovement !== 0 || yMovement !== 0) {
|
||||||
lastPos.x = (lastPos.x ?? 0) - xMovement / viewport.zoom
|
const nextPos = {
|
||||||
lastPos.y = (lastPos.y ?? 0) - yMovement / viewport.zoom
|
x: (lastPos.x ?? 0) - xMovement / viewport.zoom,
|
||||||
|
y: (lastPos.y ?? 0) - yMovement / viewport.zoom,
|
||||||
|
}
|
||||||
|
|
||||||
updateNodes(lastPos as XYPosition)
|
updateNodes(nextPos)
|
||||||
|
|
||||||
panBy({ x: xMovement, y: yMovement })
|
panBy({ x: xMovement, y: yMovement })
|
||||||
}
|
|
||||||
|
lastPos = nextPos
|
||||||
|
}
|
||||||
|
|
||||||
autoPanId = requestAnimationFrame(autoPan)
|
autoPanId = requestAnimationFrame(autoPan)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -629,7 +629,7 @@ export function useActions(
|
|||||||
return partiallyVisible || overlappingArea >= Number(nodeOrRect.width) * Number(nodeOrRect.height)
|
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
|
const { viewport, dimensions, d3Zoom, d3Selection, translateExtent } = state
|
||||||
|
|
||||||
if (!d3Zoom || !d3Selection || (!delta.x && !delta.y)) {
|
if (!d3Zoom || !d3Selection || (!delta.x && !delta.y)) {
|
||||||
@@ -645,6 +645,8 @@ export function useActions(
|
|||||||
|
|
||||||
const constrainedTransform = d3Zoom.constrain()(nextTransform, extent, translateExtent)
|
const constrainedTransform = d3Zoom.constrain()(nextTransform, extent, translateExtent)
|
||||||
|
|
||||||
|
onBeforeTransform?.(constrainedTransform)
|
||||||
|
|
||||||
d3Zoom.transform(d3Selection, constrainedTransform)
|
d3Zoom.transform(d3Selection, constrainedTransform)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import type { CSSProperties, ComputedRef, ToRefs } from 'vue'
|
import type { CSSProperties, ComputedRef, ToRefs } from 'vue'
|
||||||
import type { KeyFilter } from '@vueuse/core'
|
import type { KeyFilter } from '@vueuse/core'
|
||||||
|
import type { ZoomTransform } from 'd3-zoom'
|
||||||
import type {
|
import type {
|
||||||
Dimensions,
|
Dimensions,
|
||||||
ElementData,
|
ElementData,
|
||||||
@@ -269,7 +270,7 @@ export interface Actions extends ViewportFunctions {
|
|||||||
/** check if a node is intersecting with a defined area */
|
/** check if a node is intersecting with a defined area */
|
||||||
isNodeIntersecting: IsNodeIntersecting
|
isNodeIntersecting: IsNodeIntersecting
|
||||||
/** pan the viewport */
|
/** pan the viewport */
|
||||||
panBy: (delta: XYPosition) => void
|
panBy: (delta: XYPosition, onBeforeTransform?: (transform: ZoomTransform) => void) => void
|
||||||
|
|
||||||
/** reset state to defaults */
|
/** reset state to defaults */
|
||||||
$reset: () => void
|
$reset: () => void
|
||||||
|
|||||||
Reference in New Issue
Block a user