Merge pull request #2410 from wardoost/node-snap-relative-to-center
Snap node to grid relative to 0,0 instead start position of node
This commit is contained in:
@@ -96,14 +96,14 @@ function useDrag({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('drag', (event: UseDragEvent) => {
|
.on('drag', (event: UseDragEvent) => {
|
||||||
const { updateNodePositions, nodeInternals, nodeExtent, onNodeDrag, onSelectionDrag } = store.getState();
|
const { updateNodePositions, snapToGrid, snapGrid, nodeInternals, nodeExtent, onNodeDrag, onSelectionDrag } = store.getState();
|
||||||
const pointerPos = getPointerPosition(event);
|
const pointerPos = getPointerPosition(event);
|
||||||
|
|
||||||
// skip events without movement
|
// skip events without movement
|
||||||
if ((lastPos.current.x !== pointerPos.x || lastPos.current.y !== pointerPos.y) && dragItems.current) {
|
if ((lastPos.current.x !== pointerPos.x || lastPos.current.y !== pointerPos.y) && dragItems.current) {
|
||||||
lastPos.current = pointerPos;
|
lastPos.current = pointerPos;
|
||||||
dragItems.current = dragItems.current.map((n) =>
|
dragItems.current = dragItems.current.map((n) =>
|
||||||
updatePosition(n, pointerPos, nodeInternals, nodeExtent)
|
updatePosition(n, pointerPos, snapToGrid, snapGrid, nodeInternals, nodeExtent)
|
||||||
);
|
);
|
||||||
|
|
||||||
const onDrag = nodeId ? onNodeDrag : wrapSelectionDragFunc(onSelectionDrag);
|
const onDrag = nodeId ? onNodeDrag : wrapSelectionDragFunc(onSelectionDrag);
|
||||||
|
|||||||
@@ -59,11 +59,18 @@ export function getDragItems(nodeInternals: NodeInternals, mousePos: XYPosition,
|
|||||||
export function updatePosition(
|
export function updatePosition(
|
||||||
dragItem: NodeDragItem,
|
dragItem: NodeDragItem,
|
||||||
mousePos: XYPosition,
|
mousePos: XYPosition,
|
||||||
|
snapToGrid: boolean,
|
||||||
|
[snapX, snapY]: [number, number],
|
||||||
nodeInternals: NodeInternals,
|
nodeInternals: NodeInternals,
|
||||||
nodeExtent?: CoordinateExtent
|
nodeExtent?: CoordinateExtent
|
||||||
): NodeDragItem {
|
): NodeDragItem {
|
||||||
let currentExtent = dragItem.extent || nodeExtent;
|
let currentExtent = dragItem.extent || nodeExtent;
|
||||||
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) {
|
||||||
|
nextPosition.x = snapX * Math.round(nextPosition.x / snapX)
|
||||||
|
nextPosition.y = snapY * Math.round(nextPosition.y / snapY)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (dragItem.extent === 'parent') {
|
if (dragItem.extent === 'parent') {
|
||||||
if (dragItem.parentNode && dragItem.width && dragItem.height) {
|
if (dragItem.parentNode && dragItem.width && dragItem.height) {
|
||||||
|
|||||||
Reference in New Issue
Block a user