feat(core): add isValidConnection prop

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
braks
2023-03-08 16:32:11 +01:00
committed by Braks
parent 7519b31b8f
commit 05bc748100
6 changed files with 37 additions and 11 deletions
@@ -29,6 +29,7 @@ const EdgeWrapper = defineComponent({
removeSelectedEdges, removeSelectedEdges,
findEdge, findEdge,
findNode, findNode,
isValidConnection,
} = useVueFlow() } = useVueFlow()
const hooks = useEdgeHooks(props.edge, emits) const hooks = useEdgeHooks(props.edge, emits)
@@ -60,7 +61,7 @@ const EdgeWrapper = defineComponent({
nodeId, nodeId,
handleId, handleId,
type, type,
isValidConnection: undefined, isValidConnection: isValidConnection.value,
edgeUpdaterType, edgeUpdaterType,
onEdgeUpdate, onEdgeUpdate,
onEdgeUpdateEnd, onEdgeUpdateEnd,
+8 -7
View File
@@ -5,7 +5,7 @@ interface UseHandleProps {
handleId: MaybeRef<string | null> handleId: MaybeRef<string | null>
nodeId: MaybeRef<string> nodeId: MaybeRef<string>
type: MaybeRef<HandleType> type: MaybeRef<HandleType>
isValidConnection?: ValidConnectionFunc isValidConnection?: ValidConnectionFunc | null
edgeUpdaterType?: MaybeRef<HandleType> edgeUpdaterType?: MaybeRef<HandleType>
onEdgeUpdate?: (event: MouseTouchEvent, connection: Connection) => void onEdgeUpdate?: (event: MouseTouchEvent, connection: Connection) => void
onEdgeUpdateEnd?: (event: MouseTouchEvent) => void onEdgeUpdateEnd?: (event: MouseTouchEvent) => void
@@ -44,6 +44,7 @@ export default function useHandle({
emits, emits,
viewport, viewport,
edges, edges,
isValidConnection: isValidConnectionProp,
} = useVueFlow() } = useVueFlow()
let connection: Connection | null = null let connection: Connection | null = null
@@ -59,10 +60,10 @@ export default function useHandle({
const node = findNode(unref(nodeId)) const node = findNode(unref(nodeId))
let validConnectFunc = isValidConnection || alwaysValid let isValidConnectionHandler = isValidConnection || isValidConnectionProp.value || alwaysValid
if (!isValidConnection) { if (!isValidConnection) {
if (node) validConnectFunc = (!isTarget ? node.isValidTargetPos : node.isValidSourcePos) || alwaysValid if (node) isValidConnectionHandler = (!isTarget ? node.isValidTargetPos : node.isValidSourcePos) || alwaysValid
} }
let prevClosestHandle: ConnectionHandle | null let prevClosestHandle: ConnectionHandle | null
@@ -136,7 +137,7 @@ export default function useHandle({
nodeId, nodeId,
handleId, handleId,
isTarget ? 'target' : 'source', isTarget ? 'target' : 'source',
validConnectFunc, isValidConnectionHandler,
doc, doc,
edges.value, edges.value,
findNode, findNode,
@@ -218,12 +219,12 @@ export default function useHandle({
if (!connectionClickStartHandle.value) { if (!connectionClickStartHandle.value) {
startConnection({ nodeId: unref(nodeId), type: unref(type), handleId: unref(handleId) }, undefined, event, true) startConnection({ nodeId: unref(nodeId), type: unref(type), handleId: unref(handleId) }, undefined, event, true)
} else { } else {
let validConnectFunc = isValidConnection ?? alwaysValid let isValidConnectionHandler = isValidConnection || isValidConnectionProp.value || alwaysValid
const node = findNode(unref(nodeId)) const node = findNode(unref(nodeId))
if (!isValidConnection) { if (!isValidConnection) {
if (node) validConnectFunc = (!isTarget ? node.isValidTargetPos : node.isValidSourcePos) || alwaysValid if (node) isValidConnectionHandler = (!isTarget ? node.isValidTargetPos : node.isValidSourcePos) || alwaysValid
} }
if (node && (typeof node.connectable === 'undefined' ? nodesConnectable : node.connectable) === false) return if (node && (typeof node.connectable === 'undefined' ? nodesConnectable : node.connectable) === false) return
@@ -241,7 +242,7 @@ export default function useHandle({
connectionClickStartHandle.value.nodeId, connectionClickStartHandle.value.nodeId,
connectionClickStartHandle.value.handleId || null, connectionClickStartHandle.value.handleId || null,
connectionClickStartHandle.value.type, connectionClickStartHandle.value.type,
validConnectFunc, isValidConnectionHandler,
doc, doc,
edges.value, edges.value,
findNode, findNode,
@@ -45,6 +45,7 @@ const props = withDefaults(defineProps<FlowProps>(), {
nodesFocusable: undefined, nodesFocusable: undefined,
autoPanOnConnect: undefined, autoPanOnConnect: undefined,
autoPanOnNodeDrag: undefined, autoPanOnNodeDrag: undefined,
isValidConnection: undefined,
}) })
const emit = defineEmits<{ const emit = defineEmits<{
+22 -2
View File
@@ -298,7 +298,17 @@ export function useActions(state: State, getters: ComputedGetters): Actions {
if (!state.initialized && !nextEdges.length) return if (!state.initialized && !nextEdges.length) return
state.edges = nextEdges.reduce<GraphEdge[]>((res, edge) => { const validEdges = state.isValidConnection
? nextEdges.filter((edge) =>
state.isValidConnection!(edge, {
edges: state.edges,
sourceNode: findNode(edge.source)!,
targetNode: findNode(edge.target)!,
}),
)
: nextEdges
state.edges = validEdges.reduce<GraphEdge[]>((res, edge) => {
const sourceNode = findNode(edge.source)! const sourceNode = findNode(edge.source)!
const targetNode = findNode(edge.target)! const targetNode = findNode(edge.target)!
@@ -343,7 +353,17 @@ export function useActions(state: State, getters: ComputedGetters): Actions {
const addEdges: Actions['addEdges'] = (params) => { const addEdges: Actions['addEdges'] = (params) => {
const nextEdges = params instanceof Function ? params(state.edges) : params const nextEdges = params instanceof Function ? params(state.edges) : params
const changes = nextEdges.reduce((acc, param) => { const validEdges = state.isValidConnection
? nextEdges.filter((edge) =>
state.isValidConnection!(edge, {
edges: state.edges,
sourceNode: findNode(edge.source)!,
targetNode: findNode(edge.target)!,
}),
)
: nextEdges
const changes = validEdges.reduce((acc, param) => {
const edge = addEdgeToStore( const edge = addEdgeToStore(
{ {
...param, ...param,
+2
View File
@@ -6,6 +6,7 @@ import type { Connection, ConnectionLineOptions, ConnectionLineType, ConnectionM
import type { PanOnScrollMode, ViewportTransform } from './zoom' import type { PanOnScrollMode, ViewportTransform } from './zoom'
import type { EdgeTypesObject, NodeTypesObject } from './components' import type { EdgeTypesObject, NodeTypesObject } from './components'
import type { CustomEvent } from './hooks' import type { CustomEvent } from './hooks'
import type { ValidConnectionFunc } from '~/types/handle'
export type ElementData = any export type ElementData = any
@@ -119,6 +120,7 @@ export interface FlowProps {
connectionLineStyle?: CSSProperties | null connectionLineStyle?: CSSProperties | null
connectionLineOptions?: ConnectionLineOptions connectionLineOptions?: ConnectionLineOptions
connectionRadius?: number connectionRadius?: number
isValidConnection?: ValidConnectionFunc | null
deleteKeyCode?: KeyFilter | null deleteKeyCode?: KeyFilter | null
selectionKeyCode?: KeyFilter | null selectionKeyCode?: KeyFilter | null
multiSelectionKeyCode?: KeyFilter | null multiSelectionKeyCode?: KeyFilter | null
+2 -1
View File
@@ -27,7 +27,7 @@ import type { CoordinateExtent, GraphNode, Node } from './node'
import type { D3Selection, D3Zoom, D3ZoomHandler, PanOnScrollMode, ViewportFunctions, ViewportTransform } from './zoom' import type { D3Selection, D3Zoom, D3ZoomHandler, PanOnScrollMode, ViewportFunctions, ViewportTransform } from './zoom'
import type { CustomEvent, FlowHooks, FlowHooksEmit, FlowHooksOn } from './hooks' import type { CustomEvent, FlowHooks, FlowHooksEmit, FlowHooksOn } from './hooks'
import type { EdgeChange, NodeChange, NodeDragItem } from './changes' import type { EdgeChange, NodeChange, NodeDragItem } from './changes'
import type { StartHandle, ValidHandleResult } from './handle' import type { StartHandle, ValidConnectionFunc, ValidHandleResult } from './handle'
export interface UpdateNodeDimensionsParams { export interface UpdateNodeDimensionsParams {
id: string id: string
@@ -89,6 +89,7 @@ export interface State extends Omit<FlowOptions, 'id' | 'modelValue'> {
connectionPosition: XYPosition connectionPosition: XYPosition
connectionRadius: number connectionRadius: number
connectionStatus: ConnectionStatus | null connectionStatus: ConnectionStatus | null
isValidConnection: ValidConnectionFunc | null
connectOnClick: boolean connectOnClick: boolean
edgeUpdaterRadius: number edgeUpdaterRadius: number