chore(core): cleanup getHandleBounds (#1828)

chore(core): cleanup getHandleBounds

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
Braks
2025-04-23 19:47:17 +02:00
parent 7a09afa12a
commit 7d9ffe3f35
6 changed files with 23 additions and 19 deletions

View File

@@ -17,7 +17,8 @@
"lint": "turbo lint --filter='./packages/*'", "lint": "turbo lint --filter='./packages/*'",
"typedocs": "pnpm build && pnpm --dir docs typedocs", "typedocs": "pnpm build && pnpm --dir docs typedocs",
"ci:version": "changeset version", "ci:version": "changeset version",
"ci:publish": "pnpm lint && pnpm build && pnpm test && changeset publish" "ci:publish": "pnpm lint && pnpm build && pnpm test && changeset publish",
"changeset:add": "pnpm changeset && git add .changeset && git commit -m 'chore(changeset): add'"
}, },
"devDependencies": { "devDependencies": {
"@changesets/changelog-github": "^0.5.0", "@changesets/changelog-github": "^0.5.0",

View File

@@ -55,11 +55,11 @@ const ConnectionLine = defineComponent({
const handleType = connectionStartHandle.value.type const handleType = connectionStartHandle.value.type
const fromHandleBounds = fromNode.value.handleBounds const fromHandleBounds = fromNode.value.handleBounds
let handleBounds = fromHandleBounds?.[handleType] || [] let handleBounds = fromHandleBounds?.[handleType] ?? []
if (connectionMode.value === ConnectionMode.Loose) { if (connectionMode.value === ConnectionMode.Loose) {
const oppositeBounds = fromHandleBounds?.[handleType === 'source' ? 'target' : 'source'] || [] const oppositeBounds = fromHandleBounds?.[handleType === 'source' ? 'target' : 'source'] ?? []
handleBounds = [...handleBounds, ...oppositeBounds] || oppositeBounds handleBounds = [...handleBounds, ...oppositeBounds]
} }
if (!handleBounds) { if (!handleBounds) {
@@ -67,7 +67,7 @@ const ConnectionLine = defineComponent({
} }
const fromHandle = (startHandleId ? handleBounds.find((d) => d.id === startHandleId) : handleBounds[0]) ?? null const fromHandle = (startHandleId ? handleBounds.find((d) => d.id === startHandleId) : handleBounds[0]) ?? null
const fromPosition = fromHandle?.position || Position.Top const fromPosition = fromHandle?.position ?? Position.Top
const { x: fromX, y: fromY } = getHandlePosition(fromNode.value, fromHandle, fromPosition) const { x: fromX, y: fromY } = getHandlePosition(fromNode.value, fromHandle, fromPosition)
let toHandle: HandleElement | null = null let toHandle: HandleElement | null = null
@@ -81,7 +81,7 @@ const ConnectionLine = defineComponent({
} else { } else {
// if connection mode is loose, look for the handle in both source and target bounds // if connection mode is loose, look for the handle in both source and target bounds
toHandle = toHandle =
[...(toNode.value.handleBounds.source || []), ...(toNode.value.handleBounds.target || [])]?.find( [...(toNode.value.handleBounds.source ?? []), ...(toNode.value.handleBounds.target ?? [])]?.find(
(d) => d.id === connectionEndHandle.value?.id, (d) => d.id === connectionEndHandle.value?.id,
) || null ) || null
} }

View File

@@ -140,8 +140,8 @@ export function useActions(state: State, nodeLookup: ComputedRef<NodeLookup>, ed
const changes: NodeDimensionChange[] = [] const changes: NodeDimensionChange[] = []
for (let i = 0; i < updates.length; ++i) { for (const element of updates) {
const update = updates[i] const update = element
const node = findNode(update.id) const node = findNode(update.id)
@@ -157,8 +157,8 @@ export function useActions(state: State, nodeLookup: ComputedRef<NodeLookup>, ed
if (doUpdate) { if (doUpdate) {
const nodeBounds = update.nodeElement.getBoundingClientRect() const nodeBounds = update.nodeElement.getBoundingClientRect()
node.dimensions = dimensions node.dimensions = dimensions
node.handleBounds.source = getHandleBounds('source', update.nodeElement, nodeBounds, zoom) node.handleBounds.source = getHandleBounds('source', update.nodeElement, nodeBounds, zoom, node.id)
node.handleBounds.target = getHandleBounds('target', update.nodeElement, nodeBounds, zoom) node.handleBounds.target = getHandleBounds('target', update.nodeElement, nodeBounds, zoom, node.id)
changes.push({ changes.push({
id: node.id, id: node.id,

View File

@@ -19,8 +19,8 @@ export interface CoordinateExtentRange {
} }
export interface NodeHandleBounds { export interface NodeHandleBounds {
source?: HandleElement[] source: HandleElement[] | null
target?: HandleElement[] target: HandleElement[] | null
} }
/** @deprecated will be removed in next major release */ /** @deprecated will be removed in next major release */

View File

@@ -30,7 +30,7 @@ export function getHandlePosition(
} }
} }
export function getEdgeHandle(bounds: HandleElement[] | undefined, handleId?: string | null): HandleElement | null { export function getEdgeHandle(bounds: HandleElement[] | null, handleId?: string | null): HandleElement | null {
if (!bounds) { if (!bounds) {
return null return null
} }

View File

@@ -8,22 +8,25 @@ export function getHandleBounds(
nodeElement: HTMLDivElement, nodeElement: HTMLDivElement,
nodeBounds: DOMRect, nodeBounds: DOMRect,
zoom: number, zoom: number,
): HandleElement[] { nodeId: string,
): HandleElement[] | null {
const handles = nodeElement.querySelectorAll(`.vue-flow__handle.${type}`) const handles = nodeElement.querySelectorAll(`.vue-flow__handle.${type}`)
const handlesArray = Array.from(handles) as HTMLDivElement[] if (!handles?.length) {
return null
}
return handlesArray.map((handle): HandleElement => { return Array.from(handles).map((handle): HandleElement => {
const handleBounds = handle.getBoundingClientRect() const handleBounds = handle.getBoundingClientRect()
return { return {
id: handle.getAttribute('data-handleid'), id: handle.getAttribute('data-handleid'),
position: handle.getAttribute('data-handlepos') as unknown as Position,
nodeId: handle.getAttribute('data-nodeid') as string,
type, type,
nodeId,
position: handle.getAttribute('data-handlepos') as unknown as Position,
x: (handleBounds.left - nodeBounds.left) / zoom, x: (handleBounds.left - nodeBounds.left) / zoom,
y: (handleBounds.top - nodeBounds.top) / zoom, y: (handleBounds.top - nodeBounds.top) / zoom,
...getDimensions(handle), ...getDimensions(handle as HTMLDivElement),
} }
}) })
} }