chore(xydrag): cleanup
This commit is contained in:
@@ -134,16 +134,16 @@ export function XYDrag<OnNodeDrag extends (e: any, nodes: any, node: any) => voi
|
|||||||
let hasChange = false;
|
let hasChange = false;
|
||||||
|
|
||||||
const isMultiDrag = dragItems.size > 1;
|
const isMultiDrag = dragItems.size > 1;
|
||||||
const nodesBox =
|
const nodesBox = isMultiDrag && nodeExtent ? rectToBox(getInternalNodesBounds(dragItems)) : null;
|
||||||
isMultiDrag && nodeExtent ? rectToBox(getInternalNodesBounds(dragItems)) : { x: 0, y: 0, x2: 0, y2: 0 };
|
const multiDragSnapOffset =
|
||||||
const snapOffset = calculateSnapOffset({
|
isMultiDrag && snapToGrid
|
||||||
isMultiDrag,
|
? calculateSnapOffset({
|
||||||
dragItems,
|
dragItems,
|
||||||
snapToGrid,
|
snapGrid,
|
||||||
snapGrid,
|
x,
|
||||||
x,
|
y,
|
||||||
y,
|
})
|
||||||
});
|
: null;
|
||||||
|
|
||||||
for (const [id, dragItem] of dragItems) {
|
for (const [id, dragItem] of dragItems) {
|
||||||
/*
|
/*
|
||||||
@@ -156,17 +156,17 @@ export function XYDrag<OnNodeDrag extends (e: any, nodes: any, node: any) => voi
|
|||||||
let nextPosition = { x: x - dragItem.distance.x, y: y - dragItem.distance.y };
|
let nextPosition = { x: x - dragItem.distance.x, y: y - dragItem.distance.y };
|
||||||
|
|
||||||
if (snapToGrid) {
|
if (snapToGrid) {
|
||||||
nextPosition = isMultiDrag
|
nextPosition = multiDragSnapOffset
|
||||||
? {
|
? {
|
||||||
x: nextPosition.x + snapOffset.x,
|
x: nextPosition.x + multiDragSnapOffset.x,
|
||||||
y: nextPosition.y + snapOffset.y,
|
y: nextPosition.y + multiDragSnapOffset.y,
|
||||||
}
|
}
|
||||||
: snapPosition(nextPosition, snapGrid);
|
: snapPosition(nextPosition, snapGrid);
|
||||||
}
|
}
|
||||||
|
|
||||||
let adjustedNodeExtent: CoordinateExtent | null = null;
|
let adjustedNodeExtent: CoordinateExtent | null = null;
|
||||||
|
|
||||||
if (isMultiDrag && nodeExtent && !dragItem.extent) {
|
if (isMultiDrag && nodeExtent && !dragItem.extent && nodesBox) {
|
||||||
const { positionAbsolute } = dragItem.internals;
|
const { positionAbsolute } = dragItem.internals;
|
||||||
const x1 = positionAbsolute.x - nodesBox.x + nodeExtent[0][0];
|
const x1 = positionAbsolute.x - nodesBox.x + nodeExtent[0][0];
|
||||||
const x2 = positionAbsolute.x + dragItem.measured.width - nodesBox.x2 + nodeExtent[1][0];
|
const x2 = positionAbsolute.x + dragItem.measured.width - nodesBox.x2 + nodeExtent[1][0];
|
||||||
|
|||||||
@@ -128,38 +128,30 @@ export function getEventHandlerParams<NodeType extends InternalNodeBase>({
|
|||||||
* This function calculates the snap offset based on the first node in the selection.
|
* This function calculates the snap offset based on the first node in the selection.
|
||||||
*/
|
*/
|
||||||
export function calculateSnapOffset({
|
export function calculateSnapOffset({
|
||||||
isMultiDrag,
|
|
||||||
dragItems,
|
dragItems,
|
||||||
snapToGrid,
|
|
||||||
snapGrid,
|
snapGrid,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
}: {
|
}: {
|
||||||
isMultiDrag: boolean;
|
|
||||||
dragItems: Map<string, NodeDragItem>;
|
dragItems: Map<string, NodeDragItem>;
|
||||||
snapToGrid: boolean;
|
|
||||||
snapGrid: SnapGrid;
|
snapGrid: SnapGrid;
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
}) {
|
}) {
|
||||||
let snapOffset: XYPosition = { x: 0, y: 0 };
|
const refDragItem = dragItems.values().next().value;
|
||||||
|
|
||||||
if (snapToGrid && isMultiDrag) {
|
if (!refDragItem) {
|
||||||
const refDragItem = dragItems.values().next().value;
|
return null;
|
||||||
|
|
||||||
if (refDragItem) {
|
|
||||||
const refPos = {
|
|
||||||
x: x - refDragItem.distance.x,
|
|
||||||
y: y - refDragItem.distance.y,
|
|
||||||
};
|
|
||||||
const refPosSnapped = snapPosition(refPos, snapGrid);
|
|
||||||
|
|
||||||
snapOffset = {
|
|
||||||
x: refPosSnapped.x - refPos.x,
|
|
||||||
y: refPosSnapped.y - refPos.y,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return snapOffset;
|
const refPos = {
|
||||||
|
x: x - refDragItem.distance.x,
|
||||||
|
y: y - refDragItem.distance.y,
|
||||||
|
};
|
||||||
|
const refPosSnapped = snapPosition(refPos, snapGrid);
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: refPosSnapped.x - refPos.x,
|
||||||
|
y: refPosSnapped.y - refPos.y,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user