fix(nodes): jumping when drag is out of sync with last pos

This commit is contained in:
Braks
2022-05-27 23:36:01 +02:00
parent ce13007dbf
commit de60d924ff
2 changed files with 33 additions and 26 deletions
@@ -99,6 +99,8 @@ onMounted(() => {
onBeforeUnmount(() => observer.stop())
updateNodeDimensions([{ id, nodeElement: nodeElement.value, forceUpdate: true }])
watch(
[() => node.position, () => parentNode?.computedPosition],
([pos, parent]) => {
+31 -26
View File
@@ -61,6 +61,33 @@ function useDrag(params: UseDragParams) {
let lastPos = $ref<Partial<XYPosition>>({ x: undefined, y: undefined })
let parentPos = $ref<XYPosition>({ x: 0, y: 0 })
const handleDrag = (event: UseDragEvent) => {
const pos = pointToRendererPoint(
{
x: event.x - startPos.x,
y: event.y - startPos.y,
},
viewport.value,
snapToGrid.value,
snapGrid.value,
)
pos.x -= parentPos.x
pos.y -= parentPos.y
// skip events without movement
if (lastPos.x !== pos.x || lastPos.y !== pos.y) {
if (lastPos.x && lastPos.y) {
onDrag(event, {
dx: pos.x - lastPos.x,
dy: pos.y - lastPos.y,
})
}
}
lastPos = pos
}
return watch(
[() => disabled, () => noDragClassName, () => id, () => el],
() => {
@@ -83,33 +110,11 @@ function useDrag(params: UseDragParams) {
onStart(event)
})
.on('drag', (event: UseDragEvent) => {
const pos = pointToRendererPoint(
{
x: event.x - startPos.x,
y: event.y - startPos.y,
},
viewport.value,
snapToGrid.value,
snapGrid.value,
)
pos.x -= parentPos.x
pos.y -= parentPos.y
// skip events without movement
if (lastPos.x !== pos.x || lastPos.y !== pos.y) {
if (lastPos.x && lastPos.y) {
onDrag(event, {
dx: pos.x - lastPos.x,
dy: pos.y - lastPos.y,
})
}
lastPos = pos
}
.on('drag', handleDrag)
.on('end', (event: UseDragEvent) => {
if (node) lastPos = node.position
onStop(event)
})
.on('end', onStop)
.filter((event: any) => {
const filter = !event.ctrlKey && !event.button && !event.target.className.includes(noDragClassName)