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

8% Statements 2/25
0% Branches 0/12
0% Functions 0/4
4.76% Lines 1/21

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                    1x                                                                                                                        
// // State
import { state } from '../../store';
import { ToolModes } from '../../enums';
 
// // Util
import filterToolsWithAnnotationsForElement from '../../store/filterToolsWithAnnotationsForElement';
import getToolsWithModesForMouseEvent from '../shared/getToolsWithModesForMouseEvent';
import triggerAnnotationRender from '../../utilities/triggerAnnotationRender';
import { MouseMoveEventType } from '../../types/EventTypes';
 
const { Active, Passive } = ToolModes;
 
/**
 * mouseMove - On mouse move when not dragging, fire tools `mouseMoveCallback`s.
 * This is mostly used to update the [un]hover state
 * of a tool.
 *
 * @param evt - The normalized mouseDown event.
 */
export default function mouseMove(evt: MouseMoveEventType) {
  // Tool interactions when mouse moved are handled inside each tool.
  // This function is mostly used to update the [un]hover state
  if (state.isInteractingWithTool || state.isMultiPartToolActive) {
    return;
  }
 
  const activeAndPassiveTools = getToolsWithModesForMouseEvent(evt, [
    Active,
    Passive,
  ]);
 
  const eventDetail = evt.detail;
  const { element } = eventDetail;
 
  // Annotation tool specific
  const toolsWithAnnotations = filterToolsWithAnnotationsForElement(
    element,
    activeAndPassiveTools
  );
 
  const toolsWithoutAnnotations = activeAndPassiveTools.filter((tool) => {
    const doesNotHaveAnnotations = !toolsWithAnnotations.some(
      (toolAndAnnotation) =>
        toolAndAnnotation.tool.getToolName() === tool.getToolName()
    );
 
    return doesNotHaveAnnotations;
  });
 
  let annotationsNeedToBeRedrawn = false;
 
  for (const { tool, annotations } of toolsWithAnnotations) {
    if (typeof tool.mouseMoveCallback === 'function') {
      annotationsNeedToBeRedrawn =
        tool.mouseMoveCallback(evt, annotations) || annotationsNeedToBeRedrawn;
    }
  }
 
  // Run mouse move handlers for non-annotation tools
  toolsWithoutAnnotations.forEach((tool) => {
    if (typeof tool.mouseMoveCallback === 'function') {
      tool.mouseMoveCallback(evt);
    }
  });
 
  // Annotation activation status changed, redraw the annotations
  if (annotationsNeedToBeRedrawn === true) {
    triggerAnnotationRender(element);
  }
}