refactor(handles): prefix with flow id for handling nested flows #3196

This commit is contained in:
moklick
2024-01-16 16:57:50 +01:00
parent 9e2c907526
commit bc69a6adb4
4 changed files with 18 additions and 6 deletions
@@ -55,6 +55,7 @@ export function EdgeUpdateAnchors({
onConnectEnd, onConnectEnd,
cancelConnection, cancelConnection,
nodes, nodes,
rfId: flowId,
panBy, panBy,
updateConnection, updateConnection,
} = store.getState(); } = store.getState();
@@ -85,6 +86,7 @@ export function EdgeUpdateAnchors({
isTarget, isTarget,
edgeUpdaterType: handleType, edgeUpdaterType: handleType,
lib, lib,
flowId,
cancelConnection, cancelConnection,
panBy, panBy,
isValidConnection, isValidConnection,
@@ -25,9 +25,9 @@ import { type ReactFlowState } from '../../types';
export type HandleComponentProps = HandleProps & Omit<HTMLAttributes<HTMLDivElement>, 'id'>; export type HandleComponentProps = HandleProps & Omit<HTMLAttributes<HTMLDivElement>, 'id'>;
const selector = (s: ReactFlowState) => ({ const selector = (s: ReactFlowState) => ({
connectionStartHandle: s.connectionStartHandle,
connectOnClick: s.connectOnClick, connectOnClick: s.connectOnClick,
noPanClassName: s.noPanClassName, noPanClassName: s.noPanClassName,
rfId: s.rfId,
}); });
const connectingSelector = const connectingSelector =
@@ -70,7 +70,7 @@ const HandleComponent = forwardRef<HTMLDivElement, HandleComponentProps>(
const isTarget = type === 'target'; const isTarget = type === 'target';
const store = useStoreApi(); const store = useStoreApi();
const nodeId = useNodeId(); const nodeId = useNodeId();
const { connectOnClick, noPanClassName } = useStore(selector, shallow); const { connectOnClick, noPanClassName, rfId } = useStore(selector, shallow);
const { connecting, clickConnecting } = useStore(connectingSelector(nodeId, handleId, type), shallow); const { connecting, clickConnecting } = useStore(connectingSelector(nodeId, handleId, type), shallow);
if (!nodeId) { if (!nodeId) {
@@ -116,6 +116,7 @@ const HandleComponent = forwardRef<HTMLDivElement, HandleComponentProps>(
isTarget, isTarget,
handleId, handleId,
nodeId, nodeId,
flowId: currentStore.rfId,
panBy: currentStore.panBy, panBy: currentStore.panBy,
cancelConnection: currentStore.cancelConnection, cancelConnection: currentStore.cancelConnection,
onConnectStart: currentStore.onConnectStart, onConnectStart: currentStore.onConnectStart,
@@ -142,6 +143,7 @@ const HandleComponent = forwardRef<HTMLDivElement, HandleComponentProps>(
connectionMode, connectionMode,
isValidConnection: isValidConnectionStore, isValidConnection: isValidConnectionStore,
lib, lib,
rfId: flowId,
} = store.getState(); } = store.getState();
if (!nodeId || (!connectionClickStartHandle && !isConnectableStart)) { if (!nodeId || (!connectionClickStartHandle && !isConnectableStart)) {
@@ -167,6 +169,7 @@ const HandleComponent = forwardRef<HTMLDivElement, HandleComponentProps>(
fromHandleId: connectionClickStartHandle.handleId || null, fromHandleId: connectionClickStartHandle.handleId || null,
fromType: connectionClickStartHandle.type, fromType: connectionClickStartHandle.type,
isValidConnection: isValidConnectionHandler, isValidConnection: isValidConnectionHandler,
flowId,
doc, doc,
lib, lib,
}); });
@@ -185,7 +188,7 @@ const HandleComponent = forwardRef<HTMLDivElement, HandleComponentProps>(
data-handleid={handleId} data-handleid={handleId}
data-nodeid={nodeId} data-nodeid={nodeId}
data-handlepos={position} data-handlepos={position}
data-id={`${nodeId}-${handleId}-${type}`} data-id={`${rfId}-${nodeId}-${handleId}-${type}`}
className={cc([ className={cc([
'react-flow__handle', 'react-flow__handle',
`react-flow__handle-${position}`, `react-flow__handle-${position}`,
@@ -55,7 +55,8 @@
connectionLookup, connectionLookup,
onconnect: onConnectAction, onconnect: onConnectAction,
onconnectstart: onConnectStartAction, onconnectstart: onConnectStartAction,
onconnectend: onConnectEndAction onconnectend: onConnectEndAction,
flowId
} = store; } = store;
function onPointerDown(event: MouseEvent | TouchEvent) { function onPointerDown(event: MouseEvent | TouchEvent) {
@@ -72,6 +73,7 @@
connectionMode: $connectionMode, connectionMode: $connectionMode,
lib: $lib, lib: $lib,
autoPanOnConnect: $autoPanOnConnect, autoPanOnConnect: $autoPanOnConnect,
flowId: $flowId,
isValidConnection: $isValidConnection, isValidConnection: $isValidConnection,
updateConnection, updateConnection,
cancelConnection, cancelConnection,
@@ -128,7 +130,7 @@
data-handleid={handleId} data-handleid={handleId}
data-nodeid={nodeId} data-nodeid={nodeId}
data-handlepos={position} data-handlepos={position}
data-id="{nodeId}-{id || null}-{type}" data-id="{flowId}-{nodeId}-{id || null}-{type}"
class={cc([ class={cc([
'svelte-flow__handle', 'svelte-flow__handle',
`svelte-flow__handle-${position}`, `svelte-flow__handle-${position}`,
+6 -1
View File
@@ -27,6 +27,7 @@ export type OnPointerDownParams = {
isTarget: boolean; isTarget: boolean;
nodes: NodeBase[]; nodes: NodeBase[];
lib: string; lib: string;
flowId: string | null;
edgeUpdaterType?: HandleType; edgeUpdaterType?: HandleType;
updateConnection: UpdateConnection; updateConnection: UpdateConnection;
panBy: PanBy; panBy: PanBy;
@@ -48,6 +49,7 @@ export type IsValidParams = {
isValidConnection?: IsValidConnection; isValidConnection?: IsValidConnection;
doc: Document | ShadowRoot; doc: Document | ShadowRoot;
lib: string; lib: string;
flowId: string | null;
}; };
export type XYHandleInstance = { export type XYHandleInstance = {
@@ -79,6 +81,7 @@ function onPointerDown(
nodes, nodes,
lib, lib,
autoPanOnConnect, autoPanOnConnect,
flowId,
panBy, panBy,
cancelConnection, cancelConnection,
onConnectStart, onConnectStart,
@@ -169,6 +172,7 @@ function onPointerDown(
isValidConnection, isValidConnection,
doc, doc,
lib, lib,
flowId,
}); });
handleDomNode = result.handleDomNode; handleDomNode = result.handleDomNode;
@@ -251,12 +255,13 @@ function isValidHandle(
fromType, fromType,
doc, doc,
lib, lib,
flowId,
isValidConnection = alwaysValid, isValidConnection = alwaysValid,
}: IsValidParams }: IsValidParams
) { ) {
const isTarget = fromType === 'target'; const isTarget = fromType === 'target';
const handleDomNode = doc.querySelector( const handleDomNode = doc.querySelector(
`.${lib}-flow__handle[data-id="${handle?.nodeId}-${handle?.id}-${handle?.type}"]` `.${lib}-flow__handle[data-id="${flowId}-${handle?.nodeId}-${handle?.id}-${handle?.type}"]`
); );
const { x, y } = getEventPosition(event); const { x, y } = getEventPosition(event);
const handleBelow = doc.elementFromPoint(x, y); const handleBelow = doc.elementFromPoint(x, y);