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

This commit is contained in:
braks
2022-09-14 20:07:58 +02:00
committed by Braks
parent 32a98cd606
commit 87102eb8c2
2 changed files with 19 additions and 3 deletions
+11 -2
View File
@@ -97,13 +97,22 @@ function useDrag(params: UseDragParams) {
} }
}) })
.on('drag', (event: UseDragEvent) => { .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 // skip events without movement
if ((lastPos.x !== mousePos.x || lastPos.y !== mousePos.y) && dragItems) { if ((lastPos.x !== mousePos.x || lastPos.y !== mousePos.y) && dragItems) {
lastPos = mousePos lastPos = mousePos
dragItems = dragItems.map((n) => 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) updateNodePositions(dragItems, true, true)
+8 -1
View File
@@ -1,6 +1,6 @@
import type { Ref } from 'vue' import type { Ref } from 'vue'
import { clampPosition, isParentSelected } from './graph' 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 { export function hasSelector(target: Element, selector: string, node: Ref<Element>): boolean {
let current = target let current = target
@@ -59,10 +59,17 @@ export function getEventHandlerParams({
export function updatePosition( export function updatePosition(
dragItem: NodeDragItem, dragItem: NodeDragItem,
mousePos: XYPosition, mousePos: XYPosition,
snapToGrid?: boolean,
snapGrid?: SnapGrid,
parent?: GraphNode, parent?: GraphNode,
nodeExtent?: CoordinateExtent, nodeExtent?: CoordinateExtent,
): NodeDragItem { ): NodeDragItem {
const nextPosition = { x: mousePos.x - dragItem.distance.x, y: mousePos.y - dragItem.distance.y } 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) const currentExtent = applyExtent(dragItem, nodeExtent, parent)