faet(handle): add isConnectableStart and isConnectableEnd
This commit is contained in:
@@ -29,6 +29,8 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
|||||||
position = Position.Top,
|
position = Position.Top,
|
||||||
isValidConnection,
|
isValidConnection,
|
||||||
isConnectable = true,
|
isConnectable = true,
|
||||||
|
isConnectableStart = true,
|
||||||
|
isConnectableEnd = true,
|
||||||
id,
|
id,
|
||||||
onConnect,
|
onConnect,
|
||||||
children,
|
children,
|
||||||
@@ -72,7 +74,7 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
|||||||
const onPointerDown = (event: ReactMouseEvent<HTMLDivElement> | ReactTouchEvent<HTMLDivElement>) => {
|
const onPointerDown = (event: ReactMouseEvent<HTMLDivElement> | ReactTouchEvent<HTMLDivElement>) => {
|
||||||
const isMouseTriggered = isMouseEvent(event);
|
const isMouseTriggered = isMouseEvent(event);
|
||||||
|
|
||||||
if ((isMouseTriggered && event.button === 0) || !isMouseTriggered) {
|
if (isConnectableStart && ((isMouseTriggered && event.button === 0) || !isMouseTriggered)) {
|
||||||
handlePointerDown({
|
handlePointerDown({
|
||||||
event,
|
event,
|
||||||
handleId,
|
handleId,
|
||||||
@@ -99,6 +101,11 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
|||||||
connectionMode,
|
connectionMode,
|
||||||
isValidConnection: isValidConnectionStore,
|
isValidConnection: isValidConnectionStore,
|
||||||
} = store.getState();
|
} = store.getState();
|
||||||
|
|
||||||
|
if (!connectionStartHandle && !isConnectableStart) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!connectionStartHandle) {
|
if (!connectionStartHandle) {
|
||||||
onClickConnectStart?.(event, { nodeId, handleId, handleType: type });
|
onClickConnectStart?.(event, { nodeId, handleId, handleType: type });
|
||||||
store.setState({ connectionStartHandle: { nodeId, type, handleId } });
|
store.setState({ connectionStartHandle: { nodeId, type, handleId } });
|
||||||
@@ -131,6 +138,11 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
|||||||
store.setState({ connectionStartHandle: null });
|
store.setState({ connectionStartHandle: null });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const connecting =
|
||||||
|
connectionStartHandle?.nodeId === nodeId &&
|
||||||
|
connectionStartHandle?.handleId === handleId &&
|
||||||
|
connectionStartHandle?.type === type;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
data-handleid={handleId}
|
data-handleid={handleId}
|
||||||
@@ -147,10 +159,11 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
|||||||
source: !isTarget,
|
source: !isTarget,
|
||||||
target: isTarget,
|
target: isTarget,
|
||||||
connectable: isConnectable,
|
connectable: isConnectable,
|
||||||
connecting:
|
connectablestart: isConnectableStart,
|
||||||
connectionStartHandle?.nodeId === nodeId &&
|
connectableend: isConnectableEnd,
|
||||||
connectionStartHandle?.handleId === handleId &&
|
connecting,
|
||||||
connectionStartHandle?.type === type,
|
connectionindicator:
|
||||||
|
(isConnectable && isConnectableStart && !connecting) || (isConnectableEnd && connecting),
|
||||||
},
|
},
|
||||||
])}
|
])}
|
||||||
onMouseDown={onPointerDown}
|
onMouseDown={onPointerDown}
|
||||||
|
|||||||
@@ -92,6 +92,8 @@ export function isValidHandle(
|
|||||||
const handleType = getHandleType(undefined, handleToCheck);
|
const handleType = getHandleType(undefined, handleToCheck);
|
||||||
const handleNodeId = handleToCheck.getAttribute('data-nodeid');
|
const handleNodeId = handleToCheck.getAttribute('data-nodeid');
|
||||||
const handleId = handleToCheck.getAttribute('data-handleid');
|
const handleId = handleToCheck.getAttribute('data-handleid');
|
||||||
|
const connectable = handleToCheck.classList.contains('connectable');
|
||||||
|
const connectableEnd = handleToCheck.classList.contains('connectableend');
|
||||||
|
|
||||||
const connection: Connection = {
|
const connection: Connection = {
|
||||||
source: isTarget ? handleNodeId : fromNodeId,
|
source: isTarget ? handleNodeId : fromNodeId,
|
||||||
@@ -102,9 +104,10 @@ export function isValidHandle(
|
|||||||
|
|
||||||
result.connection = connection;
|
result.connection = connection;
|
||||||
|
|
||||||
|
const isConnectable = connectable && connectableEnd;
|
||||||
// in strict mode we don't allow target to target or source to source connections
|
// in strict mode we don't allow target to target or source to source connections
|
||||||
const isValid =
|
const isValid =
|
||||||
handleToCheck.classList.contains('connectable') &&
|
isConnectable &&
|
||||||
(connectionMode === ConnectionMode.Strict
|
(connectionMode === ConnectionMode.Strict
|
||||||
? (isTarget && handleType === 'source') || (!isTarget && handleType === 'target')
|
? (isTarget && handleType === 'source') || (!isTarget && handleType === 'target')
|
||||||
: handleNodeId !== fromNodeId || handleId !== fromHandleId);
|
: handleNodeId !== fromNodeId || handleId !== fromHandleId);
|
||||||
|
|||||||
@@ -144,7 +144,7 @@
|
|||||||
min-width: 5px;
|
min-width: 5px;
|
||||||
min-height: 5px;
|
min-height: 5px;
|
||||||
|
|
||||||
&.connectable {
|
&.connectionindicator {
|
||||||
pointer-events: all;
|
pointer-events: all;
|
||||||
cursor: crosshair;
|
cursor: crosshair;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ export interface HandleProps {
|
|||||||
type: HandleType;
|
type: HandleType;
|
||||||
position: Position;
|
position: Position;
|
||||||
isConnectable?: boolean;
|
isConnectable?: boolean;
|
||||||
|
isConnectableStart?: boolean;
|
||||||
|
isConnectableEnd?: boolean;
|
||||||
onConnect?: OnConnect;
|
onConnect?: OnConnect;
|
||||||
isValidConnection?: (connection: Connection) => boolean;
|
isValidConnection?: (connection: Connection) => boolean;
|
||||||
id?: string;
|
id?: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user