Skip to content

Commit cd5103e

Browse files
committed
refactor(core): reduce node bounds calculation (#1453)
* refactor(core): reduce node bounds calculation * chore(changeset): add * chore(core): cleanup
1 parent 49dc8ae commit cd5103e

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

.changeset/hip-kiwis-pretend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": minor
3+
---
4+
5+
Reduce node `getBoundingClientRect` calls by passing node-bounds directly to `getHandleBounds`

packages/core/src/store/actions.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ export function useActions(
142142
const style = window.getComputedStyle(viewportNode)
143143
const { m22: zoom } = new window.DOMMatrixReadOnly(style.transform)
144144

145-
const changes: NodeDimensionChange[] = updates.reduce<NodeDimensionChange[]>((res, update) => {
145+
const changes: NodeDimensionChange[] = []
146+
147+
for (const update of updates) {
146148
const node = findNode(update.id)
147149

148150
if (node) {
@@ -155,21 +157,20 @@ export function useActions(
155157
)
156158

157159
if (doUpdate) {
158-
node.handleBounds.source = getHandleBounds('.source', update.nodeElement, zoom)
159-
node.handleBounds.target = getHandleBounds('.target', update.nodeElement, zoom)
160+
const nodeBounds = update.nodeElement.getBoundingClientRect()
160161
node.dimensions = dimensions
162+
node.handleBounds.source = getHandleBounds('.source', update.nodeElement, nodeBounds, zoom)
163+
node.handleBounds.target = getHandleBounds('.target', update.nodeElement, nodeBounds, zoom)
161164
node.initialized = true
162165

163-
res.push({
166+
changes.push({
164167
id: node.id,
165168
type: 'dimensions',
166169
dimensions,
167170
})
168171
}
169172
}
170-
171-
return res
172-
}, [])
173+
}
173174

174175
if (!state.fitViewOnInitDone && state.fitViewOnInit) {
175176
nextTick(() => {

packages/core/src/utils/node.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { nextTick } from 'vue'
33
import type { Actions, GraphNode, HandleElement, Position } from '../types'
44
import { getDimensions } from '.'
55

6-
export function getHandleBounds(selector: string, nodeElement: HTMLDivElement, zoom: number): HandleElement[] | undefined {
6+
export function getHandleBounds(
7+
selector: string,
8+
nodeElement: HTMLDivElement,
9+
nodeBounds: DOMRect,
10+
zoom: number,
11+
): HandleElement[] {
712
const handles = nodeElement.querySelectorAll(`.vue-flow__handle${selector}`)
813

9-
if (!handles || !handles.length) {
10-
return undefined
11-
}
12-
1314
const handlesArray = Array.from(handles) as HTMLDivElement[]
14-
const nodeBounds = nodeElement.getBoundingClientRect()
1515

1616
return handlesArray.map((handle): HandleElement => {
1717
const handleBounds = handle.getBoundingClientRect()

0 commit comments

Comments
 (0)