fix(core): avoid undraggable selected nodes being dragged

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
braks
2023-02-05 22:22:16 +01:00
committed by Braks
parent aaf46dc22c
commit c7e8bd7630
3 changed files with 28 additions and 19 deletions
+2 -1
View File
@@ -31,6 +31,7 @@ function useDrag(params: UseDragParams) {
nodeExtent, nodeExtent,
viewport, viewport,
autoPanOnNodeDrag, autoPanOnNodeDrag,
nodesDraggable,
panBy, panBy,
findNode, findNode,
multiSelectionActive, multiSelectionActive,
@@ -146,7 +147,7 @@ function useDrag(params: UseDragParams) {
const pointerPos = getPointerPosition(event) const pointerPos = getPointerPosition(event)
lastPos = pointerPos lastPos = pointerPos
dragItems = getDragItems(nodes, pointerPos, findNode, id) dragItems = getDragItems(nodes, nodesDraggable, pointerPos, findNode, id)
if (dragItems.length) { if (dragItems.length) {
const [currentNode, nodes] = getEventHandlerParams({ const [currentNode, nodes] = getEventHandlerParams({
@@ -1,7 +1,7 @@
import type { NodeDragItem, XYPosition } from '~/types' import type { NodeDragItem, XYPosition } from '~/types'
function useUpdateNodePositions() { function useUpdateNodePositions() {
const { getSelectedNodes, nodeExtent, updateNodePositions, findNode, snapGrid, snapToGrid } = useVueFlow() const { getSelectedNodes, nodeExtent, updateNodePositions, findNode, snapGrid, snapToGrid, nodesDraggable } = useVueFlow()
return (positionDiff: XYPosition, isShiftPressed = false) => { return (positionDiff: XYPosition, isShiftPressed = false) => {
// by default a node moves 5px on each key press, or 20px if shift is pressed // by default a node moves 5px on each key press, or 20px if shift is pressed
@@ -13,24 +13,26 @@ function useUpdateNodePositions() {
const positionDiffX = positionDiff.x * xVelo * factor const positionDiffX = positionDiff.x * xVelo * factor
const positionDiffY = positionDiff.y * yVelo * factor const positionDiffY = positionDiff.y * yVelo * factor
const nodeUpdates = getSelectedNodes.value.map((n) => { const nodeUpdates = getSelectedNodes.value
const nextPosition = { x: n.computedPosition.x + positionDiffX, y: n.computedPosition.y + positionDiffY } .filter((n) => n.draggable || (nodesDraggable && typeof n.draggable === 'undefined'))
.map((n) => {
const nextPosition = { x: n.computedPosition.x + positionDiffX, y: n.computedPosition.y + positionDiffY }
const { computedPosition } = calcNextPosition( const { computedPosition } = calcNextPosition(
n, n,
nextPosition, nextPosition,
nodeExtent.value, nodeExtent.value,
n.parentNode ? findNode(n.parentNode) : undefined, n.parentNode ? findNode(n.parentNode) : undefined,
) )
return { return {
id: n.id, id: n.id,
position: computedPosition, position: computedPosition,
from: n.position, from: n.position,
distance: { x: positionDiff.x, y: positionDiff.y }, distance: { x: positionDiff.x, y: positionDiff.y },
dimensions: n.dimensions, dimensions: n.dimensions,
} as NodeDragItem } as NodeDragItem
}) })
updateNodePositions(nodeUpdates, true, false) updateNodePositions(nodeUpdates, true, false)
} }
+7 -1
View File
@@ -16,12 +16,18 @@ export function hasSelector(target: Element, selector: string, node: Element): b
export function getDragItems( export function getDragItems(
nodes: GraphNode[], nodes: GraphNode[],
nodesDraggable: boolean,
mousePos: XYPosition, mousePos: XYPosition,
findNode: Actions['findNode'], findNode: Actions['findNode'],
nodeId?: string, nodeId?: string,
): NodeDragItem[] { ): NodeDragItem[] {
return nodes return nodes
.filter((n) => (n.selected || n.id === nodeId) && (!n.parentNode || !isParentSelected(n, findNode))) .filter(
(n) =>
(n.selected || n.id === nodeId) &&
(!n.parentNode || !isParentSelected(n, findNode)) &&
(n.draggable || (nodesDraggable && typeof n.draggable === 'undefined')),
)
.map((n) => .map((n) =>
markRaw({ markRaw({
id: n.id, id: n.id,