All files / packages/tools/src/eventDispatchers/shared getToolsWithActionsForKeyboardEvents.ts

60.86% Statements 14/23
20% Branches 2/10
33.33% Functions 1/3
61.9% Lines 13/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                              12x 12x 12x         12x       12x 12x   12x 12x 12x 12x 12x 12x                                 12x    
import { ToolGroupManager } from '../../store';
import { ToolModes } from '../../enums';
import { EventTypes } from '../../types';
 
/**
 * 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 getToolsWithModesForKeyboardEvent(
  evt: EventTypes.KeyDownEventType,
  toolModes: ToolModes[]
) {
  const toolsWithActions = new Map();
  const { renderingEngineId, viewportId } = evt.detail;
  const toolGroup = ToolGroupManager.getToolGroupForViewport(
    viewportId,
    renderingEngineId
  );
 
  Iif (!toolGroup) {
    return toolsWithActions;
  }
 
  const toolGroupToolNames = Object.keys(toolGroup.toolOptions);
  const key = evt.detail.key;
 
  for (let j = 0; j < toolGroupToolNames.length; j++) {
    const toolName = toolGroupToolNames[j];
    const tool = toolGroup.getToolInstance(toolName);
    const actionsConfig = tool.configuration?.actions;
    Eif (!actionsConfig) {
      continue;
    }
    const actions = Object.values(actionsConfig);
 
    if (!actions?.length || !toolModes.includes(tool.mode)) {
      continue;
    }
 
    const action = actions.find((action: any) =>
      action.bindings.some((binding) => binding.key === key)
    );
 
    if (action) {
      toolsWithActions.set(tool, action);
    }
  }
 
  return toolsWithActions;
}