addAnnotation method

void addAnnotation(
  1. Annotation annotation
)

Implementation

void addAnnotation(Annotation annotation) {
  runInAction(() {
    _annotations[annotation.id] = annotation;

    // Only assign z-index if annotation doesn't have a meaningful one set
    // This preserves z-index values from loaded workflows while providing defaults for new annotations
    if (annotation.zIndex.value == -1) {
      // Find the current maximum z-index and add 1
      if (_annotations.length > 1) {
        final existingAnnotations = _annotations.values
            .where((a) => a.id != annotation.id)
            .toList();
        final maxZIndex = existingAnnotations
            .map((a) => a.zIndex.value)
            .fold(-1, math.max);
        annotation.setZIndex(maxZIndex + 1);
      } else {
        // First annotation gets z-index 0
        annotation.setZIndex(0);
      }
    }

    // Initialize visual position with snapping (identical to node behavior)
    annotation.setVisualPosition(
      _parentController.config.snapAnnotationsToGridIfEnabled(
        annotation.currentPosition,
      ),
    );

    // If this is a group annotation with dependencies, calculate initial bounds
    if (annotation is GroupAnnotation && annotation.hasAnyDependencies) {
      final dependentNodes = annotation.dependencies
          .map((nodeId) => _parentController.nodes[nodeId])
          .where((node) => node != null)
          .cast<Node<T>>()
          .toList();

      if (dependentNodes.isNotEmpty) {
        _updateGroupAnnotation(annotation, dependentNodes);
      }
    }
  });
}