fix(nodes): undo handle validation wrapping itself

This commit is contained in:
braks
2022-10-07 22:55:38 +02:00
committed by Braks
parent 63dae2cd05
commit f508e6e571
2 changed files with 10 additions and 13 deletions
@@ -5,15 +5,7 @@ import { ConnectionMode } from '../../types'
import type { HandleProps } from '../../types/handle' import type { HandleProps } from '../../types/handle'
import { getDimensions } from '../../utils' import { getDimensions } from '../../utils'
const { const { type = 'source', position = 'top' as Position, connectable = true, id, isValidConnection } = defineProps<HandleProps>()
type = 'source',
position = 'top' as Position,
connectable = true,
id,
isValidConnection = function () {
return true
},
} = defineProps<HandleProps>()
const { connectionStartHandle, connectionMode, vueFlowRef } = $(useVueFlow()) const { connectionStartHandle, connectionMode, vueFlowRef } = $(useVueFlow())
@@ -1,3 +1,4 @@
import { isFunction } from '@vueuse/core'
import useVueFlow from './useVueFlow' import useVueFlow from './useVueFlow'
import { getHostForElement } from '~/utils' import { getHostForElement } from '~/utils'
import type { Connection, Getters, GraphEdge, HandleType, ValidConnectionFunc } from '~/types' import type { Connection, Getters, GraphEdge, HandleType, ValidConnectionFunc } from '~/types'
@@ -17,7 +18,7 @@ export const checkElementBelowIsValid = (
isTarget: boolean, isTarget: boolean,
nodeId: string, nodeId: string,
handleId: string | null, handleId: string | null,
isValidConnection: ValidConnectionFunc, isValidConnection: ValidConnectionFunc | undefined,
doc: Document, doc: Document,
edges: GraphEdge[], edges: GraphEdge[],
getNode: Getters['getNode'], getNode: Getters['getNode'],
@@ -57,8 +58,11 @@ export const checkElementBelowIsValid = (
} }
result.connection = connection result.connection = connection
result.isValid = result.isValid =
isValidConnection(connection, { edges, sourceNode: getNode(sourceId)!, targetNode: getNode(targetId)! }) || (isFunction(isValidConnection)
? isValidConnection(connection, { edges, sourceNode: getNode(sourceId)!, targetNode: getNode(targetId)! })
: true) ||
!result.connection.target || !result.connection.target ||
!result.connection.source !result.connection.source
} }
@@ -102,14 +106,14 @@ export default () => {
const doc = getHostForElement(event.target as HTMLElement) const doc = getHostForElement(event.target as HTMLElement)
if (!doc) return if (!doc) return
let validConnectFunc: ValidConnectionFunc = isValidConnection ?? (() => true) let validConnectFunc = isValidConnection
const node = getNode(nodeId) const node = getNode(nodeId)
if (node && (typeof node.connectable === 'undefined' ? nodesConnectable : node.connectable) === false) return if (node && (typeof node.connectable === 'undefined' ? nodesConnectable : node.connectable) === false) return
if (!isValidConnection) { if (!isValidConnection) {
if (node) validConnectFunc = (!isTarget ? node.isValidTargetPos : node.isValidSourcePos) ?? (() => true) if (node) validConnectFunc = !isTarget ? node.isValidTargetPos : node.isValidSourcePos
} }
const elementBelow = doc.elementFromPoint(event.clientX, event.clientY) const elementBelow = doc.elementFromPoint(event.clientX, event.clientY)
@@ -179,6 +183,7 @@ export default () => {
const isOwnHandle = connection.source === connection.target const isOwnHandle = connection.source === connection.target
console.log(isValid)
if (isValid && !isOwnHandle) { if (isValid && !isOwnHandle) {
if (!onEdgeUpdate) emits.connect(connection) if (!onEdgeUpdate) emits.connect(connection)
else onEdgeUpdate(connection) else onEdgeUpdate(connection)