All files / tools/src/eventDispatchers/shared getToolsWithModesForMouseEvent.ts

100% Statements 16/16
100% Branches 10/10
100% Functions 2/2
100% Lines 15/15

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                                    6551x 6551x   6551x 1909x     4642x   4642x   4642x 16203x 16203x         16203x   348x   16203x           15386x 15386x       4642x    
import type { ToolModes } from '../../enums';
import { getToolGroupForViewport } from '../../store/ToolGroupManager';
import type { EventTypes } from '../../types';
 
type ModesFilter = Array<ToolModes>;
 
/**
 * Given the normalized mouse event and a filter of modes,
 * find all the tools on the element that are in one of the specified modes.
 * If the evtButton is specified, only tools with a matching binding will be returned.
 * @param evt - The normalized mouseDown event.
 * @param modesFilter - An array of entries from the `ToolModes` enum.
 */
export default function getToolsWithModesForMouseEvent(
  evt: EventTypes.MouseMoveEventType,
  modesFilter: ModesFilter,
  evtButton?: number
) {
  const { renderingEngineId, viewportId } = evt.detail;
  const toolGroup = getToolGroupForViewport(viewportId, renderingEngineId);
 
  if (!toolGroup) {
    return [];
  }
 
  const enabledTools = [];
 
  const toolGroupToolNames = Object.keys(toolGroup.toolOptions);
 
  for (let j = 0; j < toolGroupToolNames.length; j++) {
    const toolName = toolGroupToolNames[j];
    const tool = toolGroup.toolOptions[toolName];
 
    // tool has binding that matches the mouse button - we match those with
    // any modifier keys too since they can be passively interacted with
    const correctBinding =
      evtButton != null && // not null or undefined
      tool.bindings.length &&
      tool.bindings.some((binding) => binding.mouseButton === evtButton);
 
    if (
      modesFilter.includes(tool.mode) &&
      // Should not filter by event's button
      // or should, and the tool binding includes the event's button
      (!evtButton || correctBinding)
    ) {
      const toolInstance = toolGroup.getToolInstance(toolName);
      enabledTools.push(toolInstance);
    }
  }
 
  return enabledTools;
}