All files / packages/tools/src/tools AnnotationEraserTool.ts

89.28% Statements 25/28
64.28% Branches 9/14
75% Functions 3/4
89.28% Lines 25/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                                  1x   1x 1x   1x                 1x   1x         1x       1x 1x   1x 2x   2x         1x     1x   1x         1x         1x 1x                 1x         1x 1x 1x     1x   1x       1x    
import { BaseTool } from './base';
import { EventTypes, PublicToolProps, ToolProps } from '../types';
import { ToolGroupManager } from '../store';
import {
  getAnnotations,
  removeAnnotation,
} from '../stateManagement/annotation/annotationState';
import { setAnnotationSelected } from '../stateManagement/annotation/annotationSelection';
 
class AnnotationEraserTool extends BaseTool {
  static toolName;
  constructor(
    toolProps: PublicToolProps = {},
    defaultToolProps: ToolProps = {
      supportedInteractionTypes: ['Mouse', 'Touch'],
    }
  ) {
    super(toolProps, defaultToolProps);
  }
  preMouseDownCallback = (evt: EventTypes.InteractionEventType): boolean => {
    return this._deleteNearbyAnnotations(evt, 'mouse');
  };
  preTouchStartCallback = (evt: EventTypes.InteractionEventType): boolean => {
    return this._deleteNearbyAnnotations(evt, 'touch');
  };
 
  _deleteNearbyAnnotations(
    evt: EventTypes.InteractionEventType,
    interactionType: string
  ): boolean {
    const { renderingEngineId, viewportId, element, currentPoints } =
      evt.detail;
 
    const toolGroup = ToolGroupManager.getToolGroupForViewport(
      viewportId,
      renderingEngineId
    );
 
    Iif (!toolGroup) {
      return false;
    }
 
    const tools = toolGroup._toolInstances;
    const annotationsToRemove = [];
 
    for (const toolName in tools) {
      const toolInstance = tools[toolName];
 
      if (
        typeof toolInstance.isPointNearTool !== 'function' ||
        typeof toolInstance.filterInteractableAnnotationsForElement !==
          'function'
      ) {
        continue;
      }
 
      const annotations = getAnnotations(toolName, element);
 
      Iif (!annotations) {
        continue;
      }
 
      const interactableAnnotations =
        toolInstance.filterInteractableAnnotationsForElement(
          element,
          annotations
        ) || [];
 
      for (const annotation of interactableAnnotations) {
        Eif (
          toolInstance.isPointNearTool(
            element,
            annotation,
            currentPoints.canvas,
            10,
            interactionType
          )
        ) {
          annotationsToRemove.push(annotation.annotationUID);
        }
      }
    }
 
    for (const annotationUID of annotationsToRemove) {
      setAnnotationSelected(annotationUID);
      removeAnnotation(annotationUID);
    }
 
    evt.preventDefault();
 
    return true;
  }
}
 
AnnotationEraserTool.toolName = 'Eraser';
export default AnnotationEraserTool;