fix(core): calculate position and computedPos correctly in calcNextPosition

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
braks
2023-01-05 13:06:26 +01:00
committed by Braks
parent 0c25796967
commit 6aec81532c
4 changed files with 28 additions and 11 deletions
@@ -49,6 +49,8 @@ const connectedEdges = $computed(() => getConnectedEdges([node], edges))
const nodeElement = ref() const nodeElement = ref()
const init = ref(false)
provide(NodeRef, nodeElement) provide(NodeRef, nodeElement)
const { emit, on } = useNodeHooks(node, emits) const { emit, on } = useNodeHooks(node, emits)
@@ -117,6 +119,7 @@ watch(
() => node.dimensions.width, () => node.dimensions.width,
() => parentNode?.dimensions.height, () => parentNode?.dimensions.height,
() => parentNode?.dimensions.width, () => parentNode?.dimensions.width,
init,
], ],
([newX, newY, parentX, parentY, parentZ]) => { ([newX, newY, parentX, parentY, parentZ]) => {
const xyzPos = { const xyzPos = {
@@ -126,23 +129,27 @@ watch(
z: (isNumber(getStyle.value.zIndex) ? getStyle.value.zIndex : 0) + (elevateNodesOnSelect ? (node.selected ? 1000 : 0) : 0), z: (isNumber(getStyle.value.zIndex) ? getStyle.value.zIndex : 0) + (elevateNodesOnSelect ? (node.selected ? 1000 : 0) : 0),
} }
console.log('foo')
updatePosition(xyzPos, parentX && parentY ? { x: parentX, y: parentY, z: parentZ || 0 } : undefined) updatePosition(xyzPos, parentX && parentY ? { x: parentX, y: parentY, z: parentZ || 0 } : undefined)
}, },
{ flush: 'pre', immediate: true }, { flush: 'post', immediate: true },
) )
watch([() => node.extent, () => nodeExtent], () => { watch([() => node.extent, () => nodeExtent], () => {
const { position } = calcNextPosition(node, node.computedPosition, nodeExtent, parentNode) const { computedPosition, position } = calcNextPosition(node, node.computedPosition, nodeExtent, parentNode)
node.computedPosition = { ...node.computedPosition, ...position } node.computedPosition = { ...node.computedPosition, ...computedPosition }
node.position = position
}) })
until(() => node.initialized) until(() => node.initialized)
.toBe(true) .toBe(true)
.then(() => { .then(() => {
const { position } = calcNextPosition(node, node.computedPosition, nodeExtent, parentNode) const { computedPosition, position } = calcNextPosition(node, node.computedPosition, nodeExtent, parentNode)
node.computedPosition = { ...node.computedPosition, ...position } node.computedPosition = { ...node.computedPosition, ...computedPosition }
node.position = position
}) })
function updatePosition(nodePos: XYZPosition, parentPos?: XYZPosition) { function updatePosition(nodePos: XYZPosition, parentPos?: XYZPosition) {
@@ -158,8 +165,9 @@ function updatePosition(nodePos: XYZPosition, parentPos?: XYZPosition) {
function updateInternals() { function updateInternals() {
if (nodeElement.value) updateNodeDimensions([{ id, nodeElement: nodeElement.value, forceUpdate: true }]) if (nodeElement.value) updateNodeDimensions([{ id, nodeElement: nodeElement.value, forceUpdate: true }])
const { position } = calcNextPosition(node, node.position, nodeExtent, parentNode) const { computedPosition, position } = calcNextPosition(node, node.computedPosition, nodeExtent, parentNode)
node.computedPosition = { ...node.computedPosition, ...computedPosition }
node.position = position node.position = position
} }
+2 -2
View File
@@ -94,7 +94,7 @@ function useDrag(params: UseDragParams) {
nextPosition.y = snapY * Math.round(nextPosition.y / snapY) nextPosition.y = snapY * Math.round(nextPosition.y / snapY)
} }
const { position } = calcNextPosition( const { computedPosition } = calcNextPosition(
n, n,
nextPosition, nextPosition,
nodeExtent, nodeExtent,
@@ -104,7 +104,7 @@ function useDrag(params: UseDragParams) {
// 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 changes
hasChange = hasChange || n.position.x !== nextPosition.x || n.position.y !== nextPosition.y hasChange = hasChange || n.position.x !== nextPosition.x || n.position.y !== nextPosition.y
n.position = position n.position = computedPosition
return n return n
}) })
@@ -16,11 +16,16 @@ function useUpdateNodePositions() {
const nodeUpdates = getSelectedNodes.value.map((n) => { const nodeUpdates = getSelectedNodes.value.map((n) => {
const nextPosition = { x: n.computedPosition.x + positionDiffX, y: n.computedPosition.y + positionDiffY } const nextPosition = { x: n.computedPosition.x + positionDiffX, y: n.computedPosition.y + positionDiffY }
const updatedPos = calcNextPosition(n, nextPosition, nodeExtent.value, n.parentNode ? findNode(n.parentNode) : undefined) const { computedPosition } = calcNextPosition(
n,
nextPosition,
nodeExtent.value,
n.parentNode ? findNode(n.parentNode) : undefined,
)
return { return {
id: n.id, id: n.id,
position: updatedPos.position, position: computedPosition,
from: n.position, from: n.position,
distance: { x: positionDiff.x, y: positionDiff.y }, distance: { x: positionDiff.x, y: positionDiff.y },
dimensions: n.dimensions, dimensions: n.dimensions,
+5 -1
View File
@@ -144,6 +144,10 @@ export const calcNextPosition = (
const clampedPos = clampPosition(nextPosition, extent) const clampedPos = clampPosition(nextPosition, extent)
return { return {
position: clampedPos, position: {
x: clampedPos.x - (parentNode?.computedPosition.x || 0),
y: clampedPos.y - (parentNode?.computedPosition.y || 0),
},
computedPosition: clampedPos,
} }
} }