All files / packages/tools/src/eventDispatchers/mouseEventHandlers mouseDownAnnotationAction.ts

65% Statements 13/20
37.5% Branches 3/8
100% Functions 1/1
65% Lines 13/20

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                    1x                         99x       99x 99x 99x 99x   99x         99x         99x     99x           99x               99x                     99x    
import { getEnabledElement } from '@cornerstonejs/core';
import { state } from '../../store';
import { ToolModes } from '../../enums';
import { EventTypes } from '../../types';
 
// Util
import filterToolsWithAnnotationsForElement from '../../store/filterToolsWithAnnotationsForElement';
import filterMoveableAnnotationTools from '../../store/filterMoveableAnnotationTools';
import getToolsWithActionsForMouseEvent from '../shared/getToolsWithActionsForMouseEvent';
 
const { Active, Passive } = ToolModes;
 
/**
 * Look for active or passive annotations with an action that could handle the
 * event based on the bindings and invoke the first one found.
 *
 * @param evt - The normalized mouseDown event.
 * @returns True if an action has executed or false otherwise
 */
export default function mouseDownAnnotationAction(
  evt: EventTypes.MouseDownEventType
): boolean {
  // If a tool has locked the current state it is dealing with an interaction within its own eventLoop.
  Iif (state.isInteractingWithTool) {
    return false;
  }
 
  const eventDetail = evt.detail;
  const { element } = eventDetail;
  const enabledElement = getEnabledElement(element);
  const { canvas: canvasCoords } = eventDetail.currentPoints;
 
  Iif (!enabledElement) {
    return false;
  }
 
  // Find all tools that might respond to this mouse down
  const toolsWithActions = getToolsWithActionsForMouseEvent(evt, [
    Active,
    Passive,
  ]);
 
  const tools = Array.from(toolsWithActions.keys());
 
  // Filter tools with annotations for this element
  const annotationToolsWithAnnotations = filterToolsWithAnnotationsForElement(
    element,
    tools
  );
 
  // Only moveable annotations (unlocked, visible and close to the canvas coordinates) may trigger actions
  const moveableAnnotationTools = filterMoveableAnnotationTools(
    element,
    annotationToolsWithAnnotations,
    canvasCoords
  );
 
  // If there are annotation tools that are interactable, select the first one
  // that isn't locked. If there's only one annotation tool, select it.
  Iif (moveableAnnotationTools.length > 0) {
    const { tool, annotation } = moveableAnnotationTools[0];
    const action = toolsWithActions.get(tool);
    const method =
      typeof action.method === 'string' ? tool[action.method] : action.method;
 
    method.call(tool, evt, annotation);
 
    return true;
  }
 
  return false;
}