Skip to content

Commit

Permalink
refactor(core): use correct end handle position in connection line (#…
Browse files Browse the repository at this point in the history
…1508)

* refactor(core): use correct end handle position in connection line

* chore(changeset): add
  • Loading branch information
bcakmakoglu committed Jul 1, 2024
1 parent 266f874 commit 3489fae
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-ligers-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-flow/core": minor
---

Use correct end handle position in connection line component and store handle positions during connections.
3 changes: 1 addition & 2 deletions packages/core/src/components/ConnectionLine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ const ConnectionLine = defineComponent({
}
}

// we assume the target position is opposite to the source position
const toPosition = fromPosition ? oppositePosition[fromPosition] : null
const toPosition = connectionEndHandle.value?.position ?? (fromPosition ? oppositePosition[fromPosition] : null)

if (!fromPosition || !toPosition) {
return null
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/composables/useHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
rendererPointToPoint,
resetRecentHandle,
} from '../utils'
import { Position } from '../types'
import { useVueFlow } from './useVueFlow'

export interface UseHandleProps {
Expand Down Expand Up @@ -129,6 +130,7 @@ export function useHandle({
nodeId: toValue(nodeId),
handleId: toValue(handleId),
type: handleType,
position: (clickedHandle?.getAttribute('data-handlepos') as Position) || Position.Top,
},
{
x: x - containerBounds.left,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export interface ValidHandleResult {
export interface ConnectingHandle {
nodeId: string
type: HandleType
handleId: string | null
handleId?: string | null
position?: Position | null
}

/** A valid connection function can determine if an attempted connection is valid or not, i.e. abort creating a new edge */
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/utils/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
GraphNode,
HandleType,
NodeHandleBounds,
Position,
ValidConnectionFunc,
ValidHandleResult,
XYPosition,
Expand Down Expand Up @@ -192,19 +193,20 @@ export function isValidHandle(
? (isTarget && handleType === 'source') || (!isTarget && handleType === 'target')
: handleNodeId !== fromNodeId || handleId !== fromHandleId)

result.endHandle = {
nodeId: handleNodeId,
handleId,
type: handleType as HandleType,
}

if (isValid) {
result.isValid = isValidConnection(connection, {
edges,
nodes,
sourceNode: findNode(connection.source)!,
targetNode: findNode(connection.target)!,
})

result.endHandle = {
nodeId: handleNodeId,
handleId,
type: handleType as HandleType,
position: result.isValid ? (handleToCheck.getAttribute('data-handlepos') as Position) : null,
}
}
}

Expand Down

0 comments on commit 3489fae

Please sign in to comment.