fix(nodes): align snap grid to 0,0

This commit is contained in:
braks
2022-09-14 19:58:13 +02:00
committed by Braks
parent 32a98cd606
commit 87102eb8c2
2 changed files with 19 additions and 3 deletions

View File

@@ -97,13 +97,22 @@ function useDrag(params: UseDragParams) {
}
})
.on('drag', (event: UseDragEvent) => {
const mousePos = getMousePosition(event, hasSnapGrid(node?.snapGrid) as SnapGrid)
const snapGrid = hasSnapGrid(node?.snapGrid) as SnapGrid
const mousePos = getMousePosition(event, snapGrid)
// skip events without movement
if ((lastPos.x !== mousePos.x || lastPos.y !== mousePos.y) && dragItems) {
lastPos = mousePos
dragItems = dragItems.map((n) =>
updatePosition(n, mousePos, n.parentNode ? getNode(n.parentNode) : undefined, nodeExtent),
updatePosition(
n,
mousePos,
!!snapGrid ?? snapToGrid,
snapGrid ?? globalSnapGrid,
n.parentNode ? getNode(n.parentNode) : undefined,
nodeExtent,
),
)
updateNodePositions(dragItems, true, true)

View File

@@ -1,6 +1,6 @@
import type { Ref } from 'vue'
import { clampPosition, isParentSelected } from './graph'
import type { ComputedGetters, CoordinateExtent, Getters, GraphNode, NodeDragItem, XYPosition } from '~/types'
import type { ComputedGetters, CoordinateExtent, Getters, GraphNode, NodeDragItem, SnapGrid, XYPosition } from '~/types'
export function hasSelector(target: Element, selector: string, node: Ref<Element>): boolean {
let current = target
@@ -59,10 +59,17 @@ export function getEventHandlerParams({
export function updatePosition(
dragItem: NodeDragItem,
mousePos: XYPosition,
snapToGrid?: boolean,
snapGrid?: SnapGrid,
parent?: GraphNode,
nodeExtent?: CoordinateExtent,
): NodeDragItem {
const nextPosition = { x: mousePos.x - dragItem.distance.x, y: mousePos.y - dragItem.distance.y }
if (snapToGrid && snapGrid) {
const [snapX, snapY] = snapGrid
nextPosition.x = snapX * Math.round(nextPosition.x / snapX)
nextPosition.y = snapY * Math.round(nextPosition.y / snapY)
}
const currentExtent = applyExtent(dragItem, nodeExtent, parent)