Skip to content

Commit 7d9ffe3

Browse files
committed
chore(core): cleanup getHandleBounds (#1828)
chore(core): cleanup getHandleBounds Signed-off-by: braks <[email protected]>
1 parent 7a09afa commit 7d9ffe3

File tree

6 files changed

+23
-19
lines changed

6 files changed

+23
-19
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"lint": "turbo lint --filter='./packages/*'",
1818
"typedocs": "pnpm build && pnpm --dir docs typedocs",
1919
"ci:version": "changeset version",
20-
"ci:publish": "pnpm lint && pnpm build && pnpm test && changeset publish"
20+
"ci:publish": "pnpm lint && pnpm build && pnpm test && changeset publish",
21+
"changeset:add": "pnpm changeset && git add .changeset && git commit -m 'chore(changeset): add'"
2122
},
2223
"devDependencies": {
2324
"@changesets/changelog-github": "^0.5.0",

packages/core/src/components/ConnectionLine/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ const ConnectionLine = defineComponent({
5555
const handleType = connectionStartHandle.value.type
5656

5757
const fromHandleBounds = fromNode.value.handleBounds
58-
let handleBounds = fromHandleBounds?.[handleType] || []
58+
let handleBounds = fromHandleBounds?.[handleType] ?? []
5959

6060
if (connectionMode.value === ConnectionMode.Loose) {
61-
const oppositeBounds = fromHandleBounds?.[handleType === 'source' ? 'target' : 'source'] || []
62-
handleBounds = [...handleBounds, ...oppositeBounds] || oppositeBounds
61+
const oppositeBounds = fromHandleBounds?.[handleType === 'source' ? 'target' : 'source'] ?? []
62+
handleBounds = [...handleBounds, ...oppositeBounds]
6363
}
6464

6565
if (!handleBounds) {
6666
return null
6767
}
6868

6969
const fromHandle = (startHandleId ? handleBounds.find((d) => d.id === startHandleId) : handleBounds[0]) ?? null
70-
const fromPosition = fromHandle?.position || Position.Top
70+
const fromPosition = fromHandle?.position ?? Position.Top
7171
const { x: fromX, y: fromY } = getHandlePosition(fromNode.value, fromHandle, fromPosition)
7272

7373
let toHandle: HandleElement | null = null
@@ -81,7 +81,7 @@ const ConnectionLine = defineComponent({
8181
} else {
8282
// if connection mode is loose, look for the handle in both source and target bounds
8383
toHandle =
84-
[...(toNode.value.handleBounds.source || []), ...(toNode.value.handleBounds.target || [])]?.find(
84+
[...(toNode.value.handleBounds.source ?? []), ...(toNode.value.handleBounds.target ?? [])]?.find(
8585
(d) => d.id === connectionEndHandle.value?.id,
8686
) || null
8787
}

packages/core/src/store/actions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ export function useActions(state: State, nodeLookup: ComputedRef<NodeLookup>, ed
140140

141141
const changes: NodeDimensionChange[] = []
142142

143-
for (let i = 0; i < updates.length; ++i) {
144-
const update = updates[i]
143+
for (const element of updates) {
144+
const update = element
145145

146146
const node = findNode(update.id)
147147

@@ -157,8 +157,8 @@ export function useActions(state: State, nodeLookup: ComputedRef<NodeLookup>, ed
157157
if (doUpdate) {
158158
const nodeBounds = update.nodeElement.getBoundingClientRect()
159159
node.dimensions = dimensions
160-
node.handleBounds.source = getHandleBounds('source', update.nodeElement, nodeBounds, zoom)
161-
node.handleBounds.target = getHandleBounds('target', update.nodeElement, nodeBounds, zoom)
160+
node.handleBounds.source = getHandleBounds('source', update.nodeElement, nodeBounds, zoom, node.id)
161+
node.handleBounds.target = getHandleBounds('target', update.nodeElement, nodeBounds, zoom, node.id)
162162

163163
changes.push({
164164
id: node.id,

packages/core/src/types/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export interface CoordinateExtentRange {
1919
}
2020

2121
export interface NodeHandleBounds {
22-
source?: HandleElement[]
23-
target?: HandleElement[]
22+
source: HandleElement[] | null
23+
target: HandleElement[] | null
2424
}
2525

2626
/** @deprecated will be removed in next major release */

packages/core/src/utils/edge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function getHandlePosition(
3030
}
3131
}
3232

33-
export function getEdgeHandle(bounds: HandleElement[] | undefined, handleId?: string | null): HandleElement | null {
33+
export function getEdgeHandle(bounds: HandleElement[] | null, handleId?: string | null): HandleElement | null {
3434
if (!bounds) {
3535
return null
3636
}

packages/core/src/utils/node.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ export function getHandleBounds(
88
nodeElement: HTMLDivElement,
99
nodeBounds: DOMRect,
1010
zoom: number,
11-
): HandleElement[] {
11+
nodeId: string,
12+
): HandleElement[] | null {
1213
const handles = nodeElement.querySelectorAll(`.vue-flow__handle.${type}`)
1314

14-
const handlesArray = Array.from(handles) as HTMLDivElement[]
15+
if (!handles?.length) {
16+
return null
17+
}
1518

16-
return handlesArray.map((handle): HandleElement => {
19+
return Array.from(handles).map((handle): HandleElement => {
1720
const handleBounds = handle.getBoundingClientRect()
1821

1922
return {
2023
id: handle.getAttribute('data-handleid'),
21-
position: handle.getAttribute('data-handlepos') as unknown as Position,
22-
nodeId: handle.getAttribute('data-nodeid') as string,
2324
type,
25+
nodeId,
26+
position: handle.getAttribute('data-handlepos') as unknown as Position,
2427
x: (handleBounds.left - nodeBounds.left) / zoom,
2528
y: (handleBounds.top - nodeBounds.top) / zoom,
26-
...getDimensions(handle),
29+
...getDimensions(handle as HTMLDivElement),
2730
}
2831
})
2932
}

0 commit comments

Comments
 (0)