refactor(core): avoid passing reactive values to position changes (#1937)
Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
@@ -42,7 +42,7 @@ export function useDrag(params: UseDragParams) {
|
|||||||
snapToGrid,
|
snapToGrid,
|
||||||
snapGrid,
|
snapGrid,
|
||||||
noDragClassName,
|
noDragClassName,
|
||||||
nodes,
|
nodeLookup,
|
||||||
nodeExtent,
|
nodeExtent,
|
||||||
nodeDragThreshold,
|
nodeDragThreshold,
|
||||||
viewport,
|
viewport,
|
||||||
@@ -169,7 +169,7 @@ export function useDrag(params: UseDragParams) {
|
|||||||
|
|
||||||
const pointerPos = getPointerPosition(event.sourceEvent)
|
const pointerPos = getPointerPosition(event.sourceEvent)
|
||||||
lastPos = pointerPos
|
lastPos = pointerPos
|
||||||
dragItems = getDragItems(nodes.value, nodesDraggable.value, pointerPos, findNode, id)
|
dragItems = getDragItems(nodeLookup.value, nodesDraggable.value, pointerPos, id)
|
||||||
|
|
||||||
if (dragItems.length) {
|
if (dragItems.length) {
|
||||||
const [currentNode, nodes] = getEventHandlerParams({
|
const [currentNode, nodes] = getEventHandlerParams({
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { markRaw } from 'vue'
|
|
||||||
import type {
|
import type {
|
||||||
Actions,
|
Actions,
|
||||||
CoordinateExtent,
|
CoordinateExtent,
|
||||||
@@ -6,6 +5,7 @@ import type {
|
|||||||
Dimensions,
|
Dimensions,
|
||||||
GraphNode,
|
GraphNode,
|
||||||
NodeDragItem,
|
NodeDragItem,
|
||||||
|
NodeLookup,
|
||||||
State,
|
State,
|
||||||
XYPosition,
|
XYPosition,
|
||||||
} from '../types'
|
} from '../types'
|
||||||
@@ -27,39 +27,38 @@ export function hasSelector(target: Element, selector: string, node: Element): b
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDragItems(
|
// looks for all selected nodes and created a NodeDragItem for each of them
|
||||||
nodes: GraphNode[],
|
export function getDragItems(nodeLookup: NodeLookup, nodesDraggable: boolean, mousePos: XYPosition, nodeId?: string) {
|
||||||
nodesDraggable: boolean,
|
const dragItems = new Map<string, NodeDragItem>()
|
||||||
mousePos: XYPosition,
|
|
||||||
findNode: Actions['findNode'],
|
for (const [id, node] of nodeLookup) {
|
||||||
nodeId?: string,
|
|
||||||
): NodeDragItem[] {
|
|
||||||
const dragItems: NodeDragItem[] = []
|
|
||||||
for (const node of nodes) {
|
|
||||||
if (
|
if (
|
||||||
(node.selected || node.id === nodeId) &&
|
(node.selected || node.id === nodeId) &&
|
||||||
(!node.parentNode || !isParentSelected(node, findNode)) &&
|
(!node.parentNode || !isParentSelected(node, nodeLookup)) &&
|
||||||
(node.draggable || (nodesDraggable && typeof node.draggable === 'undefined'))
|
(node.draggable || (nodesDraggable && typeof node.draggable === 'undefined'))
|
||||||
) {
|
) {
|
||||||
dragItems.push(
|
const internalNode = nodeLookup.get(id)
|
||||||
markRaw({
|
|
||||||
|
if (internalNode) {
|
||||||
|
dragItems.set(id, {
|
||||||
id: node.id,
|
id: node.id,
|
||||||
position: node.position || { x: 0, y: 0 },
|
position: node.position || { x: 0, y: 0 },
|
||||||
distance: {
|
distance: {
|
||||||
x: mousePos.x - node.computedPosition?.x || 0,
|
x: mousePos.x - node.computedPosition?.x || 0,
|
||||||
y: mousePos.y - node.computedPosition?.y || 0,
|
y: mousePos.y - node.computedPosition?.y || 0,
|
||||||
},
|
},
|
||||||
from: node.computedPosition,
|
from: { x: node.computedPosition.x, y: node.computedPosition.y },
|
||||||
extent: node.extent,
|
extent: node.extent,
|
||||||
parentNode: node.parentNode,
|
parentNode: node.parentNode,
|
||||||
dimensions: node.dimensions,
|
dimensions: { ...node.dimensions },
|
||||||
expandParent: node.expandParent,
|
expandParent: node.expandParent,
|
||||||
}),
|
})
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dragItems
|
// todo: work with map in `useDrag` instead of array
|
||||||
|
return Array.from(dragItems.values())
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getEventHandlerParams({
|
export function getEventHandlerParams({
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { markRaw } from 'vue'
|
import { markRaw } from 'vue'
|
||||||
import type {
|
import type {
|
||||||
Actions,
|
|
||||||
Box,
|
Box,
|
||||||
Connection,
|
Connection,
|
||||||
CoordinateExtent,
|
CoordinateExtent,
|
||||||
@@ -16,6 +15,7 @@ import type {
|
|||||||
GraphNode,
|
GraphNode,
|
||||||
MaybeElement,
|
MaybeElement,
|
||||||
Node,
|
Node,
|
||||||
|
NodeLookup,
|
||||||
Rect,
|
Rect,
|
||||||
ViewportTransform,
|
ViewportTransform,
|
||||||
XYPosition,
|
XYPosition,
|
||||||
@@ -471,12 +471,12 @@ export function getXYZPos(parentPos: XYZPosition, computedPosition: XYZPosition)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isParentSelected(node: GraphNode, findNode: Actions['findNode']): boolean {
|
export function isParentSelected(node: GraphNode, nodeLookup: NodeLookup): boolean {
|
||||||
if (!node.parentNode) {
|
if (!node.parentNode) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const parent = findNode(node.parentNode)
|
const parent = nodeLookup.get(node.parentNode)
|
||||||
if (!parent) {
|
if (!parent) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -485,7 +485,7 @@ export function isParentSelected(node: GraphNode, findNode: Actions['findNode'])
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return isParentSelected(parent, findNode)
|
return isParentSelected(parent, nodeLookup)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMarkerId(marker: EdgeMarkerType | undefined, vueFlowId?: string) {
|
export function getMarkerId(marker: EdgeMarkerType | undefined, vueFlowId?: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user