Skip to content

Commit fa6cb4c

Browse files
committed
refactor(core): use snap grid to clamp initial node pos
Signed-off-by: braks <[email protected]>
1 parent 58a1ce8 commit fa6cb4c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

packages/core/src/components/Nodes/NodeWrapper.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const {
3636
elevateNodesOnSelect,
3737
disableKeyboardA11y,
3838
ariaLiveMessage,
39+
snapToGrid,
40+
snapGrid,
3941
} = $(useVueFlow())
4042
4143
const updateNodePositions = useUpdateNodePositions()
@@ -162,7 +164,14 @@ else {
162164
163165
/** this re-calculates the current position, necessary for clamping by a node's extent */
164166
function clampPosition() {
165-
const { computedPosition, position } = calcNextPosition(node, node.computedPosition, nodeExtent, parentNode)
167+
const nextPos = node.computedPosition
168+
169+
if (snapToGrid) {
170+
nextPos.x = snapGrid[0] * Math.round(nextPos.x / snapGrid[0])
171+
nextPos.y = snapGrid[1] * Math.round(nextPos.y / snapGrid[1])
172+
}
173+
174+
const { computedPosition, position } = calcNextPosition(node, nextPos, nodeExtent, parentNode)
166175
167176
// only overwrite positions if there are changes when clamping
168177
if (node.computedPosition.x !== computedPosition.x || node.computedPosition.y !== computedPosition.y) {

0 commit comments

Comments
 (0)