refactor(core): reduce node bounds calculation (#1453)
* refactor(core): reduce node bounds calculation * chore(changeset): add * chore(core): cleanup
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@vue-flow/core": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Reduce node `getBoundingClientRect` calls by passing node-bounds directly to `getHandleBounds`
|
||||||
@@ -142,7 +142,9 @@ export function useActions(
|
|||||||
const style = window.getComputedStyle(viewportNode)
|
const style = window.getComputedStyle(viewportNode)
|
||||||
const { m22: zoom } = new window.DOMMatrixReadOnly(style.transform)
|
const { m22: zoom } = new window.DOMMatrixReadOnly(style.transform)
|
||||||
|
|
||||||
const changes: NodeDimensionChange[] = updates.reduce<NodeDimensionChange[]>((res, update) => {
|
const changes: NodeDimensionChange[] = []
|
||||||
|
|
||||||
|
for (const update of updates) {
|
||||||
const node = findNode(update.id)
|
const node = findNode(update.id)
|
||||||
|
|
||||||
if (node) {
|
if (node) {
|
||||||
@@ -155,21 +157,20 @@ export function useActions(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (doUpdate) {
|
if (doUpdate) {
|
||||||
node.handleBounds.source = getHandleBounds('.source', update.nodeElement, zoom)
|
const nodeBounds = update.nodeElement.getBoundingClientRect()
|
||||||
node.handleBounds.target = getHandleBounds('.target', update.nodeElement, zoom)
|
|
||||||
node.dimensions = dimensions
|
node.dimensions = dimensions
|
||||||
|
node.handleBounds.source = getHandleBounds('.source', update.nodeElement, nodeBounds, zoom)
|
||||||
|
node.handleBounds.target = getHandleBounds('.target', update.nodeElement, nodeBounds, zoom)
|
||||||
node.initialized = true
|
node.initialized = true
|
||||||
|
|
||||||
res.push({
|
changes.push({
|
||||||
id: node.id,
|
id: node.id,
|
||||||
type: 'dimensions',
|
type: 'dimensions',
|
||||||
dimensions,
|
dimensions,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return res
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
if (!state.fitViewOnInitDone && state.fitViewOnInit) {
|
if (!state.fitViewOnInitDone && state.fitViewOnInit) {
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
|
|||||||
@@ -3,15 +3,15 @@ import { nextTick } from 'vue'
|
|||||||
import type { Actions, GraphNode, HandleElement, Position } from '../types'
|
import type { Actions, GraphNode, HandleElement, Position } from '../types'
|
||||||
import { getDimensions } from '.'
|
import { getDimensions } from '.'
|
||||||
|
|
||||||
export function getHandleBounds(selector: string, nodeElement: HTMLDivElement, zoom: number): HandleElement[] | undefined {
|
export function getHandleBounds(
|
||||||
|
selector: string,
|
||||||
|
nodeElement: HTMLDivElement,
|
||||||
|
nodeBounds: DOMRect,
|
||||||
|
zoom: number,
|
||||||
|
): HandleElement[] {
|
||||||
const handles = nodeElement.querySelectorAll(`.vue-flow__handle${selector}`)
|
const handles = nodeElement.querySelectorAll(`.vue-flow__handle${selector}`)
|
||||||
|
|
||||||
if (!handles || !handles.length) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
const handlesArray = Array.from(handles) as HTMLDivElement[]
|
const handlesArray = Array.from(handles) as HTMLDivElement[]
|
||||||
const nodeBounds = nodeElement.getBoundingClientRect()
|
|
||||||
|
|
||||||
return handlesArray.map((handle): HandleElement => {
|
return handlesArray.map((handle): HandleElement => {
|
||||||
const handleBounds = handle.getBoundingClientRect()
|
const handleBounds = handle.getBoundingClientRect()
|
||||||
|
|||||||
Reference in New Issue
Block a user