Merge branch 'enhance-connection-more' of github.com:xyflow/xyflow into enhance-connection-more
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
|||||||
import { useStore } from '../../hooks/useStore';
|
import { useStore } from '../../hooks/useStore';
|
||||||
import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge';
|
import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge';
|
||||||
import type { ConnectionLineComponent, ReactFlowState } from '../../types';
|
import type { ConnectionLineComponent, ReactFlowState } from '../../types';
|
||||||
|
import { useConnection } from '../../hooks/useConnection';
|
||||||
|
|
||||||
type ConnectionLineWrapperProps = {
|
type ConnectionLineWrapperProps = {
|
||||||
type: ConnectionLineType;
|
type: ConnectionLineType;
|
||||||
@@ -58,10 +59,7 @@ type ConnectionLineProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const ConnectionLine = ({ style, type = ConnectionLineType.Bezier, CustomComponent, isValid }: ConnectionLineProps) => {
|
const ConnectionLine = ({ style, type = ConnectionLineType.Bezier, CustomComponent, isValid }: ConnectionLineProps) => {
|
||||||
const { inProgress, from, fromNode, fromHandle, fromPosition, to, toNode, toHandle, toPosition } = useStore(
|
const { inProgress, from, fromNode, fromHandle, fromPosition, to, toNode, toHandle, toPosition } = useConnection();
|
||||||
(s: ReactFlowState) => s.connection,
|
|
||||||
shallow
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!inProgress) {
|
if (!inProgress) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -2,12 +2,13 @@ import { shallow } from 'zustand/shallow';
|
|||||||
|
|
||||||
import { useStore } from './useStore';
|
import { useStore } from './useStore';
|
||||||
import type { ReactFlowStore } from '../types/store';
|
import type { ReactFlowStore } from '../types/store';
|
||||||
import { ConnectionState } from '@xyflow/system';
|
import { ConnectionState, pointToRendererPoint } from '@xyflow/system';
|
||||||
|
|
||||||
const selector = (s: ReactFlowStore) => ({
|
|
||||||
...s.connection,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
const selector = (s: ReactFlowStore): ConnectionState => {
|
||||||
|
return s.connection.inProgress
|
||||||
|
? { ...s.connection, to: pointToRendererPoint(s.connection.to, s.transform) }
|
||||||
|
: { ...s.connection };
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* Hook for accessing the connection state.
|
* Hook for accessing the connection state.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { getContext, setContext } from 'svelte';
|
import { getContext, setContext } from 'svelte';
|
||||||
import { derived, get } from 'svelte/store';
|
import { derived, get, writable } from 'svelte/store';
|
||||||
import {
|
import {
|
||||||
createMarkerIds,
|
createMarkerIds,
|
||||||
fitView as fitViewSystem,
|
fitView as fitViewSystem,
|
||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
addEdge as addEdgeUtil,
|
addEdge as addEdgeUtil,
|
||||||
initialConnection,
|
initialConnection,
|
||||||
errorMessages,
|
errorMessages,
|
||||||
|
pointToRendererPoint,
|
||||||
type UpdateNodePositions,
|
type UpdateNodePositions,
|
||||||
type InternalNodeUpdate,
|
type InternalNodeUpdate,
|
||||||
type ViewportHelperFunctionOptions,
|
type ViewportHelperFunctionOptions,
|
||||||
@@ -16,8 +17,8 @@ import {
|
|||||||
type XYPosition,
|
type XYPosition,
|
||||||
type CoordinateExtent,
|
type CoordinateExtent,
|
||||||
type UpdateConnection,
|
type UpdateConnection,
|
||||||
type NodeOrigin,
|
type ConnectionState,
|
||||||
type ConnectionState
|
type NodeOrigin
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import type { EdgeTypes, NodeTypes, Node, Edge, FitViewOptions } from '$lib/types';
|
import type { EdgeTypes, NodeTypes, Node, Edge, FitViewOptions } from '$lib/types';
|
||||||
@@ -341,12 +342,13 @@ export function createStore({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const _connection = writable<ConnectionState>(initialConnection);
|
||||||
const updateConnection: UpdateConnection = (newConnection: ConnectionState) => {
|
const updateConnection: UpdateConnection = (newConnection: ConnectionState) => {
|
||||||
store.connection.set({ ...newConnection });
|
_connection.set({ ...newConnection });
|
||||||
};
|
};
|
||||||
|
|
||||||
function cancelConnection() {
|
function cancelConnection() {
|
||||||
store.connection.set(initialConnection);
|
_connection.set(initialConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
function reset() {
|
function reset() {
|
||||||
@@ -367,6 +369,14 @@ export function createStore({
|
|||||||
// derived state
|
// derived state
|
||||||
visibleEdges: getVisibleEdges(store),
|
visibleEdges: getVisibleEdges(store),
|
||||||
visibleNodes: getVisibleNodes(store),
|
visibleNodes: getVisibleNodes(store),
|
||||||
|
connection: derived([_connection, store.viewport], ([connection, viewport]) => {
|
||||||
|
return connection.inProgress
|
||||||
|
? {
|
||||||
|
...connection,
|
||||||
|
to: pointToRendererPoint(connection.to, [viewport.x, viewport.y, viewport.zoom])
|
||||||
|
}
|
||||||
|
: { ...connection };
|
||||||
|
}),
|
||||||
markers: derived(
|
markers: derived(
|
||||||
[store.edges, store.defaultMarkerColor, store.flowId],
|
[store.edges, store.defaultMarkerColor, store.flowId],
|
||||||
([edges, defaultColor, id]) => createMarkerIds(edges, { defaultColor, id })
|
([edges, defaultColor, id]) => createMarkerIds(edges, { defaultColor, id })
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ export const getInitialStore = ({
|
|||||||
viewport: writable<Viewport>(viewport),
|
viewport: writable<Viewport>(viewport),
|
||||||
connectionMode: writable<ConnectionMode>(ConnectionMode.Strict),
|
connectionMode: writable<ConnectionMode>(ConnectionMode.Strict),
|
||||||
domNode: writable<HTMLDivElement | null>(null),
|
domNode: writable<HTMLDivElement | null>(null),
|
||||||
connection: writable<ConnectionState>(initialConnection),
|
connection: readable<ConnectionState>(initialConnection),
|
||||||
connectionLineType: writable<ConnectionLineType>(ConnectionLineType.Bezier),
|
connectionLineType: writable<ConnectionLineType>(ConnectionLineType.Bezier),
|
||||||
connectionRadius: writable<number>(20),
|
connectionRadius: writable<number>(20),
|
||||||
isValidConnection: writable<IsValidConnection>(() => true),
|
isValidConnection: writable<IsValidConnection>(() => true),
|
||||||
|
|||||||
@@ -1,4 +1,11 @@
|
|||||||
import { pointToRendererPoint, getHostForElement, calcAutoPan, getEventPosition, getHandlePosition } from '../utils';
|
import {
|
||||||
|
pointToRendererPoint,
|
||||||
|
getHostForElement,
|
||||||
|
calcAutoPan,
|
||||||
|
getEventPosition,
|
||||||
|
getHandlePosition,
|
||||||
|
rendererPointToPoint,
|
||||||
|
} from '../utils';
|
||||||
import {
|
import {
|
||||||
ConnectionMode,
|
ConnectionMode,
|
||||||
Position,
|
Position,
|
||||||
@@ -98,7 +105,7 @@ function onPointerDown(
|
|||||||
fromPosition: fromHandle.position,
|
fromPosition: fromHandle.position,
|
||||||
fromNode: fromNodeInternal.internals.userNode,
|
fromNode: fromNodeInternal.internals.userNode,
|
||||||
|
|
||||||
to: pointToRendererPoint(position, getTransform()),
|
to: position,
|
||||||
toHandle: null,
|
toHandle: null,
|
||||||
toPosition: oppositePosition[fromHandle.position],
|
toPosition: oppositePosition[fromHandle.position],
|
||||||
toNode: null,
|
toNode: null,
|
||||||
@@ -151,8 +158,8 @@ function onPointerDown(
|
|||||||
isValid,
|
isValid,
|
||||||
to:
|
to:
|
||||||
closestHandle && isValid
|
closestHandle && isValid
|
||||||
? { x: closestHandle.x, y: closestHandle.y }
|
? rendererPointToPoint({ x: closestHandle.x, y: closestHandle.y }, transform)
|
||||||
: pointToRendererPoint(position, transform),
|
: position,
|
||||||
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,
|
||||||
|
|||||||
Reference in New Issue
Block a user