Skip to content

Commit c8b5522

Browse files
committed
fix(core): correct calculation of nodes inside selection (#1509)
* fix(core): correct calculation of nodes inside selection * chore(changeset): add
1 parent 3ed281b commit c8b5522

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

.changeset/fast-ghosts-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-flow/core": patch
3+
---
4+
5+
Correct calculation of nodes inside selection rect

packages/core/src/utils/graph.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ export function rendererPointToPoint({ x, y }: XYPosition, { x: tx, y: ty, zoom:
298298
export function pointToRendererPoint(
299299
{ x, y }: XYPosition,
300300
{ x: tx, y: ty, zoom: tScale }: ViewportTransform,
301-
snapToGrid: boolean,
302-
[snapX, snapY]: [snapX: number, snapY: number],
301+
snapToGrid: boolean = false,
302+
[snapX, snapY]: [snapX: number, snapY: number] = [1, 1],
303303
): XYPosition {
304304
const position: XYPosition = {
305305
x: (x - tx) / tScale,
@@ -373,37 +373,41 @@ export function getRectOfNodes(nodes: GraphNode[]) {
373373
export function getNodesInside(
374374
nodes: GraphNode[],
375375
rect: Rect,
376-
{ x: tx, y: ty, zoom: tScale }: ViewportTransform = { x: 0, y: 0, zoom: 1 },
376+
viewport: ViewportTransform = { x: 0, y: 0, zoom: 1 },
377377
partially = false,
378378
// set excludeNonSelectableNodes if you want to pay attention to the nodes "selectable" attribute
379379
excludeNonSelectableNodes = false,
380380
) {
381381
const paneRect = {
382-
x: (rect.x - tx) / tScale,
383-
y: (rect.y - ty) / tScale,
384-
width: rect.width / tScale,
385-
height: rect.height / tScale,
382+
...pointToRendererPoint(rect, viewport),
383+
width: rect.width / viewport.zoom,
384+
height: rect.height / viewport.zoom,
386385
}
387386

388-
return nodes.filter((node) => {
389-
const { computedPosition = { x: 0, y: 0 }, dimensions = { width: 0, height: 0 }, selectable } = node
387+
const visibleNodes: GraphNode[] = []
390388

391-
if (excludeNonSelectableNodes && !selectable) {
392-
return false
389+
for (const node of nodes) {
390+
const { dimensions, selectable = true, hidden = false } = node
391+
const width = dimensions.width ?? node.width ?? null
392+
const height = dimensions.height ?? node.height ?? null
393+
394+
if ((excludeNonSelectableNodes && !selectable) || hidden) {
395+
continue
393396
}
394397

395-
const nodeRect = { ...computedPosition, width: dimensions.width || 0, height: dimensions.height || 0 }
396-
const overlappingArea = getOverlappingArea(paneRect, nodeRect)
397-
const notInitialized =
398-
typeof dimensions.width === 'undefined' ||
399-
typeof dimensions.height === 'undefined' ||
400-
dimensions.width === 0 ||
401-
dimensions.height === 0
398+
const overlappingArea = getOverlappingArea(paneRect, nodeToRect(node))
399+
const notInitialized = width === null || height === null
402400

403401
const partiallyVisible = partially && overlappingArea > 0
404-
const area = dimensions.width * dimensions.height
405-
return notInitialized || partiallyVisible || overlappingArea >= area
406-
})
402+
const area = (width ?? 0) * (height ?? 0)
403+
const isVisible = notInitialized || partiallyVisible || overlappingArea >= area
404+
405+
if (isVisible || node.dragging) {
406+
visibleNodes.push(node)
407+
}
408+
}
409+
410+
return visibleNodes
407411
}
408412

409413
export function getConnectedEdges<E extends Edge>(nodesOrId: Node[] | string, edges: E[]) {

0 commit comments

Comments
 (0)