All files / packages/tools/src/eventDispatchers/touchEventHandlers touchStart.ts

4.08% Statements 2/49
0% Branches 0/37
0% Functions 0/4
2.32% Lines 1/43

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                                              1x                                                                                                                                                                                                                                                                                                              
import { state } from '../../store';
import { ToolModes } from '../../enums';
import { EventTypes } from '../../types';
import {
  ToolAnnotationPair,
  ToolsWithMoveableHandles,
} from '../../types/InternalToolTypes';
 
import {
  setAnnotationSelected,
  isAnnotationSelected,
} from '../../stateManagement/annotation/annotationSelection';
 
import { isAnnotationLocked } from '../../stateManagement/annotation/annotationLocking';
import { isAnnotationVisible } from '../../stateManagement/annotation/annotationVisibility';
 
// Util
import filterToolsWithMoveableHandles from '../../store/filterToolsWithMoveableHandles';
import filterToolsWithAnnotationsForElement from '../../store/filterToolsWithAnnotationsForElement';
import filterMoveableAnnotationTools from '../../store/filterMoveableAnnotationTools';
import getActiveToolForTouchEvent from '../shared/getActiveToolForTouchEvent';
import getToolsWithModesForTouchEvent from '../shared/getToolsWithModesForTouchEvent';
 
const { Active, Passive } = ToolModes;
 
/**
 * touchStart - Event handler for touchStart events. Uses `customCallbackHandler` to fire
 * the `touchStartCallback` function on active tools.
 */
export default function touchStart(evt: EventTypes.TouchStartEventType) {
  if (state.isInteractingWithTool) {
    return;
  }
  const activeTool = getActiveToolForTouchEvent(evt);
 
  // Check for preTouchStartCallbacks,
  // If the tool claims it consumed the event, prevent further checks.
  if (activeTool && typeof activeTool.preTouchStartCallback === 'function') {
    const consumedEvent = activeTool.preTouchStartCallback(evt);
 
    if (consumedEvent) {
      return;
    }
  }
 
  const isPrimaryClick = Object.keys(evt.detail.event.touches).length === 1;
  const activeToolsWithEventBinding = getToolsWithModesForTouchEvent(
    evt,
    [Active],
    Object.keys(evt.detail.event.touches).length
  );
  const passiveToolsIfEventWasPrimaryTouchButton = isPrimaryClick
    ? getToolsWithModesForTouchEvent(evt, [Passive])
    : undefined;
  const applicableTools = [
    ...(activeToolsWithEventBinding || []),
    ...(passiveToolsIfEventWasPrimaryTouchButton || []),
    activeTool,
  ];
 
  const eventDetail = evt.detail;
  const { element } = eventDetail;
 
  // Filter tools with annotations for this element
  const annotationToolsWithAnnotations = filterToolsWithAnnotationsForElement(
    element,
    applicableTools
  );
 
  const canvasCoords = eventDetail.currentPoints.canvas;
 
  // For the canvas coordinates, find all tools that might respond to this touch start
  // on their handles. This filter will call getHandleNearImagePoint for each tool
  // instance (each annotation)
  const annotationToolsWithMoveableHandles = filterToolsWithMoveableHandles(
    element,
    annotationToolsWithAnnotations,
    canvasCoords,
    'touch'
  );
 
  const isMultiSelect = false;
 
  // If there are annotation tools whose handle is near the touch, select the first one
  // that isn't locked. If there's only one annotation tool, select it.
  if (annotationToolsWithMoveableHandles.length > 0) {
    const { tool, annotation, handle } = getAnnotationForSelection(
      annotationToolsWithMoveableHandles
    ) as ToolsWithMoveableHandles;
 
    toggleAnnotationSelection(annotation.annotationUID, isMultiSelect);
    tool.handleSelectedCallback(evt, annotation, handle, 'Touch');
 
    return;
  }
 
  // If there were no annotation tools whose handle was near the touch, try to check
  // if any of the annotation tools are interactable (e.g. moving an entire length annotation)
  const moveableAnnotationTools = filterMoveableAnnotationTools(
    element,
    annotationToolsWithAnnotations,
    canvasCoords,
    'touch'
  );
 
  // 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.
  if (moveableAnnotationTools.length > 0) {
    const { tool, annotation } = getAnnotationForSelection(
      moveableAnnotationTools
    );
 
    toggleAnnotationSelection(annotation.annotationUID, isMultiSelect);
    tool.toolSelectedCallback(evt, annotation, 'Touch');
 
    return;
  }
 
  // Run the postTouchStartCallback for the active tool if it exists
  if (activeTool && typeof activeTool.postTouchStartCallback === 'function') {
    const consumedEvent = activeTool.postTouchStartCallback(evt);
 
    if (consumedEvent) {
      // If the tool claims it consumed the event, prevent further checks.
      return;
    }
  }
 
  // Don't stop propagation so that touchStartActivate can handle the event
}
 
/**
 * If there are multiple annotation tools, return the first one that isn't locked neither hidden.
 * If there's only one annotation tool, return it
 * @param annotationTools - An array of tools and annotation.
 * @returns The candidate for selection
 */
function getAnnotationForSelection(
  toolsWithMovableHandles: ToolAnnotationPair[]
): ToolAnnotationPair {
  return (
    (toolsWithMovableHandles.length > 1 &&
      toolsWithMovableHandles.find(
        (item) =>
          !isAnnotationLocked(item.annotation) &&
          isAnnotationVisible(item.annotation.annotationUID)
      )) ||
    toolsWithMovableHandles[0]
  );
}
 
/**
 * If the annotation is selected, deselect it. If it's not selected, select it
 * @param annotationUID - The AnnotationUID that we
 * want to toggle the selection of.
 * @param isMultiSelect - If true, the annotation. will be deselected if it is
 * already selected, or deselected if it is selected.
 */
function toggleAnnotationSelection(
  annotationUID: string,
  isMultiSelect = false
): void {
  if (isMultiSelect) {
    if (isAnnotationSelected(annotationUID)) {
      setAnnotationSelected(annotationUID, false);
    } else {
      const preserveSelected = true;
      setAnnotationSelected(annotationUID, true, preserveSelected);
    }
  } else {
    const preserveSelected = false;
    setAnnotationSelected(annotationUID, true, preserveSelected);
  }
}