dont invoke connection updates if the snapped handle stays the same

This commit is contained in:
peterkogo
2024-06-26 18:12:56 +02:00
parent 41ebe46d77
commit 25dfeb1c02
2 changed files with 23 additions and 4 deletions
+1
View File
@@ -314,6 +314,7 @@ const createStore = ({
}); });
}, },
updateConnection: (connection) => { updateConnection: (connection) => {
console.log('newUpdate');
set({ connection }); set({ connection });
}, },
+22 -4
View File
@@ -14,6 +14,7 @@ import {
NodeLookup, NodeLookup,
Position, Position,
oppositePosition, oppositePosition,
ConnectionInProgress,
} from '../types'; } from '../types';
import { getClosestHandle, isConnectionValid, getHandleLookup, getHandleType } from './utils'; import { getClosestHandle, isConnectionValid, getHandleLookup, getHandleType } from './utils';
@@ -145,7 +146,7 @@ function onPointerDown(
const from = getHandlePosition(fromNodeInternal, fromHandle, Position.Left, true); const from = getHandlePosition(fromNodeInternal, fromHandle, Position.Left, true);
updateConnection({ const newConnection: ConnectionInProgress = {
inProgress: true, inProgress: true,
isValid: null, isValid: null,
@@ -158,7 +159,10 @@ function onPointerDown(
toHandle: null, toHandle: null,
toPosition: oppositePosition[fromHandle.position], toPosition: oppositePosition[fromHandle.position],
toNode: null, toNode: null,
}); };
updateConnection(newConnection);
let previousConnection: ConnectionInProgress = newConnection;
onConnectStart?.(event, { nodeId, handleId, handleType }); onConnectStart?.(event, { nodeId, handleId, handleType });
@@ -198,7 +202,7 @@ function onPointerDown(
connection = result.connection; connection = result.connection;
isValid = isConnectionValid(!!closestHandle, result.isValid); isValid = isConnectionValid(!!closestHandle, result.isValid);
updateConnection({ const newConnection: ConnectionInProgress = {
inProgress: true, inProgress: true,
isValid, isValid,
@@ -214,7 +218,21 @@ function onPointerDown(
toHandle: result.toHandle, toHandle: result.toHandle,
toPosition: isValid && result.toHandle ? result.toHandle.position : oppositePosition[fromHandle.position], toPosition: isValid && result.toHandle ? result.toHandle.position : oppositePosition[fromHandle.position],
toNode: result.toHandle ? nodeLookup.get(result.toHandle.nodeId)!.internals.userNode : null, toNode: result.toHandle ? nodeLookup.get(result.toHandle.nodeId)!.internals.userNode : null,
}); };
// we don't want to trigger an update when the connection
// is snapped to the same handle as before
if (
previousConnection.toHandle &&
newConnection.toHandle &&
previousConnection.toHandle.nodeId === newConnection.toHandle.nodeId &&
previousConnection.toHandle.id === newConnection.toHandle.id
) {
return;
}
updateConnection(newConnection);
previousConnection = newConnection;
} }
function onPointerUp(event: MouseEvent | TouchEvent) { function onPointerUp(event: MouseEvent | TouchEvent) {