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

61.53% Statements 8/13
37.5% Branches 3/8
100% Functions 1/1
61.53% Lines 8/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                          2x   2x                     2x       2x 2x 2x           2x 2x        
import getActiveToolForKeyboardEvent from '../shared/getActiveToolForKeyboardEvent';
import getToolsWithActionsForKeyboardEvent from '../shared/getToolsWithActionsForKeyboardEvents';
import type { KeyDownEventType } from '../../types/EventTypes';
import ToolModes from '../../enums/ToolModes';
import { getToolGroupForViewport } from '../../store/ToolGroupManager';
 
/**
 * 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 = getToolGroupForViewport(viewportId, renderingEngineId);
 
    const toolName = activeTool.getToolName();
    if (Object.keys(toolGroup.toolOptions).includes(toolName)) {
      toolGroup.setViewportsCursorByToolName(toolName);
    }
  }
 
  const activeToolsWithEventBinding = getToolsWithActionsForKeyboardEvent(evt, [
    ToolModes.Active,
  ]);
 
  Eif (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);
    }
  }
}