addAnnotation method

void addAnnotation(
  1. Annotation annotation
)

Implementation

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

    // Update spatial index
    _parentController._spatialIndex.updateAnnotation(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 == -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)
            .fold(-1, math.max);
        annotation.zIndex = maxZIndex + 1;
      } else {
        // First annotation gets z-index 0
        annotation.zIndex = 0;
      }
    }

    // Initialize visual position with snapping (identical to node behavior)
    annotation.visualPosition = _parentController.config
        .snapAnnotationsToGridIfEnabled(annotation.position);

    // Set up node monitoring reactions if requested
    if (annotation.monitorNodes) {
      _setupNodeMonitoringForAnnotation(annotation);
    }

    // Watch for monitorNodes state changes (e.g., behavior changes in GroupAnnotation)
    // MobX tracks the underlying observable through the getter chain
    _setupMonitorNodesChangeReaction(annotation);
  });
}