All files / packages/tools/src/eventDispatchers/keyboardEventHandlers keyDown.ts

30.76% Statements 4/13
25% Branches 2/8
100% Functions 1/1
30.76% Lines 4/13

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                          12x   12x                           12x       12x                          
import { ToolGroupManager } from '../../store';
import getActiveToolForKeyboardEvent from '../shared/getActiveToolForKeyboardEvent';
import getToolsWithActionsForKeyboardEvent from '../shared/getToolsWithActionsForKeyboardEvents';
import { KeyDownEventType } from '../../types/EventTypes';
import ToolModes from '../../enums/ToolModes';
 
/**
 * KeyDown event listener to handle viewport cursor icon changes
 *
 * @param evt - The KeyboardEvent
 */
export default function keyDown(evt: KeyDownEventType): void {
  // get the active tool given the key and mouse button
  const activeTool = getActiveToolForKeyboardEvent(evt);
 
  Iif (activeTool) {
    const { renderingEngineId, viewportId } = evt.detail;
 
    const toolGroup = ToolGroupManager.getToolGroupForViewport(
      viewportId,
      renderingEngineId
    );
 
    const toolName = activeTool.getToolName();
    if (Object.keys(toolGroup.toolOptions).includes(toolName)) {
      toolGroup.setViewportsCursorByToolName(toolName);
    }
  }
 
  const activeToolsWithEventBinding = getToolsWithActionsForKeyboardEvent(evt, [
    ToolModes.Active,
  ]);
 
  Iif (activeToolsWithEventBinding?.size) {
    const { element } = evt.detail;
    for (const [key, value] of [...activeToolsWithEventBinding.entries()]) {
      // Calls the method that implements the action, which can be a string
      // in which case it belongs to the tool instance, or a function
      // Call it on the tool instance, with the element and configuration value
      // so that the method can depend on the specific configuration in use.
      const method =
        typeof value.method === 'function' ? value.method : key[value.method];
      method.call(key, element, value, evt);
    }
  }
}