cancelNodeDrag method

void cancelNodeDrag(
  1. Map<String, Offset> originalPositions
)

Cancels a node drag operation and reverts to original positions.

Call this to abort a drag and restore nodes to their positions before the drag started. The caller provides the original positions since the widget that initiated the drag owns that state.

Parameters:

  • originalPositions: Map of node ID to original position before drag

Implementation

void cancelNodeDrag(Map<String, Offset> originalPositions) {
  // Capture dragged nodes before clearing state
  final draggedNodes = <Node<T>>[];
  for (final node in _nodes.values) {
    if (node.dragging.value) {
      draggedNodes.add(node);
    }
  }

  // Revert positions
  runInAction(() {
    for (final entry in originalPositions.entries) {
      final node = _nodes[entry.key];
      if (node != null) {
        node.position.value = entry.value;
        node.setVisualPosition(_config.snapToGridIfEnabled(entry.value));
      }
    }

    // Clear dragging state on nodes
    for (final node in draggedNodes) {
      node.dragging.value = false;
    }

    // Clear drag state
    interaction.draggedNodeId.value = null;
    interaction.lastPointerPosition.value = null;

    // Note: Canvas unlocking is now handled by DragSession
  });

  // Rebuild connection segments
  if (originalPositions.isNotEmpty) {
    rebuildConnectionSegmentsForNodes(originalPositions.keys.toList());
  }

  // Fire drag cancel event for all dragged nodes
  for (final node in draggedNodes) {
    events.node?.onDragCancel?.call(node);
  }
}