fix(autopan): respect translate contraints
This commit is contained in:
@@ -119,8 +119,9 @@ function useDrag({
|
|||||||
lastPos.current.x = (lastPos.current.x ?? 0) - xMovement / transform[2];
|
lastPos.current.x = (lastPos.current.x ?? 0) - xMovement / transform[2];
|
||||||
lastPos.current.y = (lastPos.current.y ?? 0) - yMovement / transform[2];
|
lastPos.current.y = (lastPos.current.y ?? 0) - yMovement / transform[2];
|
||||||
|
|
||||||
updateNodes(lastPos.current as XYPosition);
|
if (panBy({ x: xMovement, y: yMovement })) {
|
||||||
panBy({ x: xMovement, y: yMovement });
|
updateNodes(lastPos.current as XYPosition);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
autoPanId.current = requestAnimationFrame(autoPan);
|
autoPanId.current = requestAnimationFrame(autoPan);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -252,11 +252,11 @@ const createRFStore = () =>
|
|||||||
nodeInternals: new Map(nodeInternals),
|
nodeInternals: new Map(nodeInternals),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
panBy: (delta: XYPosition) => {
|
panBy: (delta: XYPosition): boolean => {
|
||||||
const { transform, width, height, d3Zoom, d3Selection, translateExtent } = get();
|
const { transform, width, height, d3Zoom, d3Selection, translateExtent } = get();
|
||||||
|
|
||||||
if (!d3Zoom || !d3Selection || (!delta.x && !delta.y)) {
|
if (!d3Zoom || !d3Selection || (!delta.x && !delta.y)) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextTransform = zoomIdentity.translate(transform[0] + delta.x, transform[1] + delta.y).scale(transform[2]);
|
const nextTransform = zoomIdentity.translate(transform[0] + delta.x, transform[1] + delta.y).scale(transform[2]);
|
||||||
@@ -268,6 +268,13 @@ const createRFStore = () =>
|
|||||||
|
|
||||||
const constrainedTransform = d3Zoom?.constrain()(nextTransform, extent, translateExtent);
|
const constrainedTransform = d3Zoom?.constrain()(nextTransform, extent, translateExtent);
|
||||||
d3Zoom.transform(d3Selection, constrainedTransform);
|
d3Zoom.transform(d3Selection, constrainedTransform);
|
||||||
|
|
||||||
|
const transformChanged =
|
||||||
|
transform[0] !== constrainedTransform.x ||
|
||||||
|
transform[1] !== constrainedTransform.y ||
|
||||||
|
transform[2] !== constrainedTransform.k;
|
||||||
|
|
||||||
|
return transformChanged;
|
||||||
},
|
},
|
||||||
cancelConnection: () =>
|
cancelConnection: () =>
|
||||||
set({
|
set({
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ export type ReactFlowActions = {
|
|||||||
cancelConnection: () => void;
|
cancelConnection: () => void;
|
||||||
reset: () => void;
|
reset: () => void;
|
||||||
triggerNodeChanges: (changes: NodeChange[]) => void;
|
triggerNodeChanges: (changes: NodeChange[]) => void;
|
||||||
panBy: (delta: XYPosition) => void;
|
panBy: (delta: XYPosition) => boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ReactFlowState = ReactFlowStore & ReactFlowActions;
|
export type ReactFlowState = ReactFlowStore & ReactFlowActions;
|
||||||
|
|||||||
Reference in New Issue
Block a user