Merge branch 'main' into feat/packages
This commit is contained in:
@@ -10,6 +10,8 @@ import { EdgeAnchor } from './EdgeAnchor';
|
||||
import { getMouseHandler } from './utils';
|
||||
import type { EdgeProps, WrapEdgeProps } from '../../types';
|
||||
|
||||
const alwaysValidConnection = () => true;
|
||||
|
||||
export default (EdgeComponent: ComponentType<EdgeProps>) => {
|
||||
const EdgeWrapper = ({
|
||||
id,
|
||||
@@ -93,12 +95,14 @@ export default (EdgeComponent: ComponentType<EdgeProps>) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const { edges, isValidConnection: isValidConnectionStore } = store.getState();
|
||||
const nodeId = isSourceHandle ? target : source;
|
||||
const handleId = (isSourceHandle ? targetHandleId : sourceHandleId) || null;
|
||||
const handleType = isSourceHandle ? 'target' : 'source';
|
||||
const isValidConnection = () => true;
|
||||
const isValidConnection = isValidConnectionStore || alwaysValidConnection;
|
||||
|
||||
const isTarget = isSourceHandle;
|
||||
const edge = store.getState().edges.find((e) => e.id === id)!;
|
||||
const edge = edges.find((e) => e.id === id)!;
|
||||
|
||||
setUpdating(true);
|
||||
onEdgeUpdateStart?.(event, edge, handleType);
|
||||
|
||||
@@ -26,7 +26,7 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
||||
{
|
||||
type = 'source',
|
||||
position = Position.Top,
|
||||
isValidConnection = alwaysValid,
|
||||
isValidConnection,
|
||||
isConnectable = true,
|
||||
id,
|
||||
onConnect,
|
||||
@@ -60,8 +60,8 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
||||
...params,
|
||||
};
|
||||
if (hasDefaultEdges) {
|
||||
const { edges } = store.getState();
|
||||
store.setState({ edges: addEdge(edgeParams, edges) });
|
||||
const { edges, setEdges } = store.getState();
|
||||
setEdges(addEdge(edgeParams, edges));
|
||||
}
|
||||
|
||||
onConnectAction?.(edgeParams);
|
||||
@@ -80,7 +80,7 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
||||
isTarget,
|
||||
getState: store.getState,
|
||||
setState: store.setState,
|
||||
isValidConnection,
|
||||
isValidConnection: isValidConnection || store.getState().isValidConnection || alwaysValid,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -92,7 +92,12 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
||||
};
|
||||
|
||||
const onClick = (event: ReactMouseEvent) => {
|
||||
const { onClickConnectStart, onClickConnectEnd, connectionMode } = store.getState();
|
||||
const {
|
||||
onClickConnectStart,
|
||||
onClickConnectEnd,
|
||||
connectionMode,
|
||||
isValidConnection: isValidConnectionStore,
|
||||
} = store.getState();
|
||||
if (!connectionStartHandle) {
|
||||
onClickConnectStart?.(event, { nodeId, handleId, handleType: type });
|
||||
store.setState({ connectionStartHandle: { nodeId, type, handleId } });
|
||||
@@ -100,6 +105,7 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
||||
}
|
||||
|
||||
const doc = getHostForElement(event.target as HTMLElement);
|
||||
const isValidConnectionHandler = isValidConnection || isValidConnectionStore || alwaysValid;
|
||||
const { connection, isValid } = isValidHandle(
|
||||
event.nativeEvent,
|
||||
{
|
||||
@@ -111,7 +117,7 @@ const Handle = forwardRef<HTMLDivElement, HandleComponentProps>(
|
||||
connectionStartHandle.nodeId,
|
||||
connectionStartHandle.handleId || null,
|
||||
connectionStartHandle.type,
|
||||
isValidConnection,
|
||||
isValidConnectionHandler,
|
||||
doc
|
||||
);
|
||||
|
||||
|
||||
@@ -112,9 +112,10 @@ export function isValidHandle(
|
||||
|
||||
// in strict mode we don't allow target to target or source to source connections
|
||||
const isValid =
|
||||
connectionMode === ConnectionMode.Strict
|
||||
handleToCheck.classList.contains('connectable') &&
|
||||
(connectionMode === ConnectionMode.Strict
|
||||
? (isTarget && handleType === 'source') || (!isTarget && handleType === 'target')
|
||||
: handleNodeId !== fromNodeId || handleId !== fromHandleId;
|
||||
: handleNodeId !== fromNodeId || handleId !== fromHandleId);
|
||||
|
||||
if (isValid) {
|
||||
result.isValid = isValidConnection(connection);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { MouseEvent } from 'react';
|
||||
import { MouseEvent, RefObject } from 'react';
|
||||
import { StoreApi } from 'zustand';
|
||||
import { getDimensions } from '@reactflow/utils';
|
||||
import { Position, type HandleElement, type NodeOrigin } from '@reactflow/system';
|
||||
@@ -58,6 +58,7 @@ export function handleNodeClick({
|
||||
id,
|
||||
store,
|
||||
unselect = false,
|
||||
nodeRef,
|
||||
}: {
|
||||
id: string;
|
||||
store: {
|
||||
@@ -65,6 +66,7 @@ export function handleNodeClick({
|
||||
setState: StoreApi<ReactFlowState>['setState'];
|
||||
};
|
||||
unselect?: boolean;
|
||||
nodeRef?: RefObject<HTMLDivElement>;
|
||||
}) {
|
||||
const { addSelectedNodes, unselectNodesAndEdges, multiSelectionActive, nodeInternals } = store.getState();
|
||||
const node = nodeInternals.get(id)!;
|
||||
@@ -75,5 +77,7 @@ export function handleNodeClick({
|
||||
addSelectedNodes([id]);
|
||||
} else if (unselect || (node.selected && multiSelectionActive)) {
|
||||
unselectNodesAndEdges({ nodes: [node] });
|
||||
|
||||
requestAnimationFrame(() => nodeRef?.current?.blur());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +74,7 @@ export default (NodeComponent: ComponentType<NodeProps>) => {
|
||||
handleNodeClick({
|
||||
id,
|
||||
store,
|
||||
nodeRef,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -90,13 +91,12 @@ export default (NodeComponent: ComponentType<NodeProps>) => {
|
||||
|
||||
if (elementSelectionKeys.includes(event.key) && isSelectable) {
|
||||
const unselect = event.key === 'Escape';
|
||||
if (unselect) {
|
||||
nodeRef.current?.blur();
|
||||
}
|
||||
|
||||
handleNodeClick({
|
||||
id,
|
||||
store,
|
||||
unselect,
|
||||
nodeRef,
|
||||
});
|
||||
} else if (
|
||||
!disableKeyboardA11y &&
|
||||
|
||||
@@ -50,6 +50,7 @@ type StoreUpdaterProps = Pick<
|
||||
| 'autoPanOnNodeDrag'
|
||||
| 'onError'
|
||||
| 'connectionRadius'
|
||||
| 'isValidConnection'
|
||||
> & { rfId: string };
|
||||
|
||||
const selector = (s: ReactFlowState) => ({
|
||||
@@ -128,6 +129,7 @@ const StoreUpdater = ({
|
||||
autoPanOnNodeDrag,
|
||||
onError,
|
||||
connectionRadius,
|
||||
isValidConnection,
|
||||
}: StoreUpdaterProps) => {
|
||||
const {
|
||||
setNodes,
|
||||
@@ -185,6 +187,7 @@ const StoreUpdater = ({
|
||||
useDirectStoreUpdater('autoPanOnNodeDrag', autoPanOnNodeDrag, store.setState);
|
||||
useDirectStoreUpdater('onError', onError, store.setState);
|
||||
useDirectStoreUpdater('connectionRadius', connectionRadius, store.setState);
|
||||
useDirectStoreUpdater('isValidConnection', isValidConnection, store.setState);
|
||||
|
||||
useStoreUpdater<Node[]>(nodes, setNodes);
|
||||
useStoreUpdater<Edge[]>(edges, setEdges);
|
||||
|
||||
Reference in New Issue
Block a user