fixed handle connections

This commit is contained in:
peterkogo
2024-02-27 16:54:21 +01:00
parent 14516ab061
commit 83e0a333b0
4 changed files with 24 additions and 10 deletions
+15 -4
View File
@@ -16,6 +16,7 @@ import {
type HandleProps, type HandleProps,
type Connection, type Connection,
type HandleType, type HandleType,
ConnectionMode,
} from '@xyflow/system'; } from '@xyflow/system';
import { useStore, useStoreApi } from '../../hooks/useStore'; import { useStore, useStoreApi } from '../../hooks/useStore';
@@ -36,14 +37,19 @@ const connectingSelector =
connectionStartHandle: startHandle, connectionStartHandle: startHandle,
connectionEndHandle: endHandle, connectionEndHandle: endHandle,
connectionClickStartHandle: clickHandle, connectionClickStartHandle: clickHandle,
connectionMode,
} = state; } = state;
return { return {
connecting: connecting:
(startHandle?.nodeId === nodeId && startHandle?.handleId === handleId && startHandle?.type === type) || (startHandle?.nodeId === nodeId && startHandle?.handleId === handleId && startHandle?.type === type) ||
(endHandle?.nodeId === nodeId && endHandle?.handleId === handleId && endHandle?.type === type), (endHandle?.nodeId === nodeId && endHandle?.handleId === handleId && endHandle?.type === type),
clickConnecting: clickConnecting:
clickHandle?.nodeId === nodeId && clickHandle?.handleId === handleId && clickHandle?.type === type, clickHandle?.nodeId === nodeId && clickHandle?.handleId === handleId && clickHandle?.type === type,
isPossibleEndHandle:
connectionMode === ConnectionMode.Strict
? startHandle?.type !== type
: nodeId !== startHandle?.nodeId || handleId !== startHandle?.handleId,
connectionInProcess: !!startHandle,
}; };
}; };
@@ -71,7 +77,10 @@ const HandleComponent = forwardRef<HTMLDivElement, HandleComponentProps>(
const store = useStoreApi(); const store = useStoreApi();
const nodeId = useNodeId(); const nodeId = useNodeId();
const { connectOnClick, noPanClassName, rfId } = useStore(selector, shallow); const { connectOnClick, noPanClassName, rfId } = useStore(selector, shallow);
const { connecting, clickConnecting } = useStore(connectingSelector(nodeId, handleId, type), shallow); const { connecting, clickConnecting, isPossibleEndHandle, connectionInProcess } = useStore(
connectingSelector(nodeId, handleId, type),
shallow
);
if (!nodeId) { if (!nodeId) {
store.getState().onError?.('010', errorMessages['error010']()); store.getState().onError?.('010', errorMessages['error010']());
@@ -201,10 +210,12 @@ const HandleComponent = forwardRef<HTMLDivElement, HandleComponentProps>(
connectable: isConnectable, connectable: isConnectable,
connectablestart: isConnectableStart, connectablestart: isConnectableStart,
connectableend: isConnectableEnd, connectableend: isConnectableEnd,
connecting: clickConnecting, connecting: connecting || clickConnecting,
// this class is used to style the handle when the user is connecting // this class is used to style the handle when the user is connecting
connectionindicator: connectionindicator:
isConnectable && ((isConnectableStart && !connecting) || (isConnectableEnd && connecting)), isConnectable &&
(!connectionInProcess || isPossibleEndHandle) &&
(connectionInProcess ? isConnectableEnd : isConnectableStart),
}, },
])} ])}
onMouseDown={onPointerDown} onMouseDown={onPointerDown}
+2 -4
View File
@@ -291,13 +291,11 @@ const createRFStore = ({
connectionEndHandle: null, connectionEndHandle: null,
}), }),
updateConnection: (params) => { updateConnection: (params) => {
const { connectionStatus, connectionStartHandle, connectionEndHandle, connectionPosition } = get(); const { connectionPosition } = get();
const currentConnection = { const currentConnection = {
...params,
connectionPosition: params.connectionPosition ?? connectionPosition, connectionPosition: params.connectionPosition ?? connectionPosition,
connectionStatus: params.connectionStatus ?? connectionStatus,
connectionStartHandle: params.connectionStartHandle ?? connectionStartHandle,
connectionEndHandle: params.connectionEndHandle ?? connectionEndHandle,
}; };
set(currentConnection); set(currentConnection);
+5
View File
@@ -223,9 +223,14 @@ svg.xy-flow__connectionline {
min-width: 5px; min-width: 5px;
min-height: 5px; min-height: 5px;
&.connecting {
pointer-events: all;
}
&.connectionindicator { &.connectionindicator {
pointer-events: all; pointer-events: all;
cursor: crosshair; cursor: crosshair;
background: green;
} }
&-bottom { &-bottom {
+2 -2
View File
@@ -38,7 +38,7 @@ export function getClosestHandle(
let closestHandles: ConnectionHandle[] = []; let closestHandles: ConnectionHandle[] = [];
let minDistance = Infinity; let minDistance = Infinity;
handles.forEach((handle) => { for (const handle of handles) {
const distance = Math.sqrt(Math.pow(handle.x - pos.x, 2) + Math.pow(handle.y - pos.y, 2)); const distance = Math.sqrt(Math.pow(handle.x - pos.x, 2) + Math.pow(handle.y - pos.y, 2));
if (distance <= connectionRadius) { if (distance <= connectionRadius) {
if (distance < minDistance) { if (distance < minDistance) {
@@ -49,7 +49,7 @@ export function getClosestHandle(
} }
minDistance = distance; minDistance = distance;
} }
}); }
if (!closestHandles.length) { if (!closestHandles.length) {
return null; return null;