Skip to content

Commit bd606b9

Browse files
committed
refactor(core): avoid passing reactive values to position changes (#1937)
Signed-off-by: braks <[email protected]>
1 parent 490473d commit bd606b9

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

packages/core/src/composables/useDrag.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function useDrag(params: UseDragParams) {
4242
snapToGrid,
4343
snapGrid,
4444
noDragClassName,
45-
nodes,
45+
nodeLookup,
4646
nodeExtent,
4747
nodeDragThreshold,
4848
viewport,
@@ -169,7 +169,7 @@ export function useDrag(params: UseDragParams) {
169169

170170
const pointerPos = getPointerPosition(event.sourceEvent)
171171
lastPos = pointerPos
172-
dragItems = getDragItems(nodes.value, nodesDraggable.value, pointerPos, findNode, id)
172+
dragItems = getDragItems(nodeLookup.value, nodesDraggable.value, pointerPos, id)
173173

174174
if (dragItems.length) {
175175
const [currentNode, nodes] = getEventHandlerParams({

packages/core/src/utils/drag.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { markRaw } from 'vue'
21
import type {
32
Actions,
43
CoordinateExtent,
54
CoordinateExtentRange,
65
Dimensions,
76
GraphNode,
87
NodeDragItem,
8+
NodeLookup,
99
State,
1010
XYPosition,
1111
} from '../types'
@@ -27,39 +27,38 @@ export function hasSelector(target: Element, selector: string, node: Element): b
2727
return false
2828
}
2929

30-
export function getDragItems(
31-
nodes: GraphNode[],
32-
nodesDraggable: boolean,
33-
mousePos: XYPosition,
34-
findNode: Actions['findNode'],
35-
nodeId?: string,
36-
): NodeDragItem[] {
37-
const dragItems: NodeDragItem[] = []
38-
for (const node of nodes) {
30+
// looks for all selected nodes and created a NodeDragItem for each of them
31+
export function getDragItems(nodeLookup: NodeLookup, nodesDraggable: boolean, mousePos: XYPosition, nodeId?: string) {
32+
const dragItems = new Map<string, NodeDragItem>()
33+
34+
for (const [id, node] of nodeLookup) {
3935
if (
4036
(node.selected || node.id === nodeId) &&
41-
(!node.parentNode || !isParentSelected(node, findNode)) &&
37+
(!node.parentNode || !isParentSelected(node, nodeLookup)) &&
4238
(node.draggable || (nodesDraggable && typeof node.draggable === 'undefined'))
4339
) {
44-
dragItems.push(
45-
markRaw({
40+
const internalNode = nodeLookup.get(id)
41+
42+
if (internalNode) {
43+
dragItems.set(id, {
4644
id: node.id,
4745
position: node.position || { x: 0, y: 0 },
4846
distance: {
4947
x: mousePos.x - node.computedPosition?.x || 0,
5048
y: mousePos.y - node.computedPosition?.y || 0,
5149
},
52-
from: node.computedPosition,
50+
from: { x: node.computedPosition.x, y: node.computedPosition.y },
5351
extent: node.extent,
5452
parentNode: node.parentNode,
55-
dimensions: node.dimensions,
53+
dimensions: { ...node.dimensions },
5654
expandParent: node.expandParent,
57-
}),
58-
)
55+
})
56+
}
5957
}
6058
}
6159

62-
return dragItems
60+
// todo: work with map in `useDrag` instead of array
61+
return Array.from(dragItems.values())
6362
}
6463

6564
export function getEventHandlerParams({

packages/core/src/utils/graph.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { markRaw } from 'vue'
22
import type {
3-
Actions,
43
Box,
54
Connection,
65
CoordinateExtent,
@@ -16,6 +15,7 @@ import type {
1615
GraphNode,
1716
MaybeElement,
1817
Node,
18+
NodeLookup,
1919
Rect,
2020
ViewportTransform,
2121
XYPosition,
@@ -471,12 +471,12 @@ export function getXYZPos(parentPos: XYZPosition, computedPosition: XYZPosition)
471471
}
472472
}
473473

474-
export function isParentSelected(node: GraphNode, findNode: Actions['findNode']): boolean {
474+
export function isParentSelected(node: GraphNode, nodeLookup: NodeLookup): boolean {
475475
if (!node.parentNode) {
476476
return false
477477
}
478478

479-
const parent = findNode(node.parentNode)
479+
const parent = nodeLookup.get(node.parentNode)
480480
if (!parent) {
481481
return false
482482
}
@@ -485,7 +485,7 @@ export function isParentSelected(node: GraphNode, findNode: Actions['findNode'])
485485
return true
486486
}
487487

488-
return isParentSelected(parent, findNode)
488+
return isParentSelected(parent, nodeLookup)
489489
}
490490

491491
export function getMarkerId(marker: EdgeMarkerType | undefined, vueFlowId?: string) {

0 commit comments

Comments
 (0)