From fa6cb4c6d91b4b968e9ebb1610fdf722f7f9bebf Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Fri, 17 Feb 2023 16:50:16 +0100 Subject: [PATCH] refactor(core): use snap grid to clamp initial node pos Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com> --- packages/core/src/components/Nodes/NodeWrapper.vue | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/core/src/components/Nodes/NodeWrapper.vue b/packages/core/src/components/Nodes/NodeWrapper.vue index ae1b9ac1..5ede5e8a 100644 --- a/packages/core/src/components/Nodes/NodeWrapper.vue +++ b/packages/core/src/components/Nodes/NodeWrapper.vue @@ -36,6 +36,8 @@ const { elevateNodesOnSelect, disableKeyboardA11y, ariaLiveMessage, + snapToGrid, + snapGrid, } = $(useVueFlow()) const updateNodePositions = useUpdateNodePositions() @@ -162,7 +164,14 @@ else { /** this re-calculates the current position, necessary for clamping by a node's extent */ function clampPosition() { - const { computedPosition, position } = calcNextPosition(node, node.computedPosition, nodeExtent, parentNode) + const nextPos = node.computedPosition + + if (snapToGrid) { + nextPos.x = snapGrid[0] * Math.round(nextPos.x / snapGrid[0]) + nextPos.y = snapGrid[1] * Math.round(nextPos.y / snapGrid[1]) + } + + const { computedPosition, position } = calcNextPosition(node, nextPos, nodeExtent, parentNode) // only overwrite positions if there are changes when clamping if (node.computedPosition.x !== computedPosition.x || node.computedPosition.y !== computedPosition.y) {