feat(xyhandle): add dragThreshold #5315
This commit is contained in:
@@ -45,6 +45,7 @@ function onPointerDown(
|
|||||||
getTransform,
|
getTransform,
|
||||||
getFromHandle,
|
getFromHandle,
|
||||||
autoPanSpeed,
|
autoPanSpeed,
|
||||||
|
dragThreshold = 1,
|
||||||
}: OnPointerDownParams
|
}: OnPointerDownParams
|
||||||
) {
|
) {
|
||||||
// when xyflow is used inside a shadow root we can't use document
|
// when xyflow is used inside a shadow root we can't use document
|
||||||
@@ -56,6 +57,7 @@ function onPointerDown(
|
|||||||
const clickedHandle = doc?.elementFromPoint(x, y);
|
const clickedHandle = doc?.elementFromPoint(x, y);
|
||||||
const handleType = getHandleType(edgeUpdaterType, clickedHandle);
|
const handleType = getHandleType(edgeUpdaterType, clickedHandle);
|
||||||
const containerBounds = domNode?.getBoundingClientRect();
|
const containerBounds = domNode?.getBoundingClientRect();
|
||||||
|
let connectionStarted = false;
|
||||||
|
|
||||||
if (!containerBounds || !handleType) {
|
if (!containerBounds || !handleType) {
|
||||||
return;
|
return;
|
||||||
@@ -92,10 +94,9 @@ function onPointerDown(
|
|||||||
};
|
};
|
||||||
|
|
||||||
const fromNodeInternal = nodeLookup.get(nodeId)!;
|
const fromNodeInternal = nodeLookup.get(nodeId)!;
|
||||||
|
|
||||||
const from = getHandlePosition(fromNodeInternal, fromHandle, Position.Left, true);
|
const from = getHandlePosition(fromNodeInternal, fromHandle, Position.Left, true);
|
||||||
|
|
||||||
const newConnection: ConnectionInProgress = {
|
let previousConnection: ConnectionInProgress = {
|
||||||
inProgress: true,
|
inProgress: true,
|
||||||
isValid: null,
|
isValid: null,
|
||||||
|
|
||||||
@@ -110,12 +111,31 @@ function onPointerDown(
|
|||||||
toNode: null,
|
toNode: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
updateConnection(newConnection);
|
function startConnection() {
|
||||||
let previousConnection: ConnectionInProgress = newConnection;
|
updateConnection(previousConnection);
|
||||||
|
onConnectStart?.(event, { nodeId, handleId, handleType });
|
||||||
|
}
|
||||||
|
|
||||||
onConnectStart?.(event, { nodeId, handleId, handleType });
|
if (dragThreshold === 0) {
|
||||||
|
startConnection();
|
||||||
|
}
|
||||||
|
|
||||||
function onPointerMove(event: MouseEvent | TouchEvent) {
|
function onPointerMove(event: MouseEvent | TouchEvent) {
|
||||||
|
if (!connectionStarted) {
|
||||||
|
const { x: evtX, y: evtY } = getEventPosition(event);
|
||||||
|
const dx = evtX - x;
|
||||||
|
const dy = evtY - y;
|
||||||
|
const nextConnectionStarted = dx * dx + dy * dy > dragThreshold * dragThreshold;
|
||||||
|
|
||||||
|
if (!nextConnectionStarted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
startConnection();
|
||||||
|
|
||||||
|
connectionStarted = nextConnectionStarted;
|
||||||
|
}
|
||||||
|
|
||||||
if (!getFromHandle() || !fromHandle) {
|
if (!getFromHandle() || !fromHandle) {
|
||||||
onPointerUp(event);
|
onPointerUp(event);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ export type OnPointerDownParams = {
|
|||||||
getTransform: () => Transform;
|
getTransform: () => Transform;
|
||||||
getFromHandle: () => Handle | null;
|
getFromHandle: () => Handle | null;
|
||||||
autoPanSpeed?: number;
|
autoPanSpeed?: number;
|
||||||
|
dragThreshold?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type IsValidParams = {
|
export type IsValidParams = {
|
||||||
|
|||||||
Reference in New Issue
Block a user