Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

9.8 KB
Raw Blame
import { matchKeybindingPress, parseKeybinding } from 'tinykeys';

const MODIFIER_ORDER = ['mod', 'ctrl', 'meta', 'shift', 'alt'] as const;

const KEY_ALIASES: Record<string, string> = {
  ' ': 'space',
  arrowdown: 'arrowdown',
  arrowleft: 'arrowleft',
  arrowright: 'arrowright',
  arrowup: 'arrowup',
  del: 'delete',
  escape: 'esc',
  return: 'enter',
};

const TINY_KEY_ALIASES: Record<string, string> = {
  '[': 'BracketLeft',
  ']': 'BracketRight',
  '.': 'Period',
  ',': 'Comma',
  '/': 'Slash',
  '\\': 'Backslash',
  '-': 'Minus',
  '=': 'Equal',
  arrowdown: 'ArrowDown',
  arrowleft: 'ArrowLeft',
  arrowright: 'ArrowRight',
  arrowup: 'ArrowUp',
  backspace: 'Backspace',
  delete: 'Delete',
  enter: 'Enter',
  esc: 'Escape',
  space: 'Space',
};

const TINY_MODIFIER_ALIASES: Record<string, string> = {
  alt: 'Alt',
  ctrl: 'Control',
  meta: 'Meta',
  mod: '$mod',
  shift: 'Shift',
};

export const IS_MAC_OS =
  typeof navigator !== 'undefined' &&
  (
    (navigator as Navigator & { userAgentData?: { platform?: string } }).userAgentData?.platform ??
    navigator.platform ??
    ''
  )
    .toLowerCase()
    .includes('mac');

export const normalizeHotkeyString = (hotkey: string): string => {
  const parts = hotkey
    .split('+')
    .map((part) => part.trim().toLowerCase())
    .filter(Boolean)
    .map((part) => KEY_ALIASES[part] ?? part);
  const modifiers = parts
    .filter((part) => MODIFIER_ORDER.includes(part as (typeof MODIFIER_ORDER)[number]))
    .sort((left, right) => MODIFIER_ORDER.indexOf(left as never) - MODIFIER_ORDER.indexOf(right as never));
  const key = parts.find((part) => !MODIFIER_ORDER.includes(part as (typeof MODIFIER_ORDER)[number]));

  return key ? [...modifiers, key].join('+') : '';
};

/** A Latin letter or digit, as a binding names a key. */
const isLatinKey = (key: string): boolean => /^[a-z0-9]$/i.test(key);

/** The physical key of a Latin letter or digit (`KeyN`, `Digit1`). */
const toPhysicalKeyCode = (key: string): string => (/^[0-9]$/.test(key) ? `Digit${key}` : `Key${key.toUpperCase()}`);

/** `mod` is the platform's primary modifier (Cmd on macOS, Ctrl elsewhere); the other one, when held too, is kept. */
export const eventToHotkeyString = (event: KeyboardEvent, isMacOs = IS_MAC_OS): string => {
  if (event.isComposing || event.keyCode === 229 || ['Alt', 'Control', 'Meta', 'Shift'].includes(event.key)) {
    return '';
  }

  const modifiers: string[] = [];

  if (isMacOs ? event.metaKey : event.ctrlKey) {
    modifiers.push('mod');
  }
  if (isMacOs ? event.ctrlKey : event.metaKey) {
    modifiers.push(isMacOs ? 'ctrl' : 'meta');
  }
  if (event.shiftKey) {
    modifiers.push('shift');
  }
  if (event.altKey) {
    modifiers.push('alt');
  }

  // A key is recorded as typed, so the binding keeps its meaning on any layout.
  const fallsBackToPosition =
    event.altKey &&
    modifiers.some((modifier) => modifier !== 'shift' && modifier !== 'alt') &&
    event.getModifierState?.('AltGraph') !== true;
  const physical =
    fallsBackToPosition && !isLatinKey(event.key) ? /^(?:Key([A-Z])|Digit([0-9]))$/.exec(event.code ?? '') : null;
  const key = physical ? (physical[1] ?? physical[2]!).toLowerCase() : event.key;

  return normalizeHotkeyString([...modifiers, key].join('+'));
};

/** Alt chords with Cmd/Ctrl/Meta type no character, so they match by position (macOS Option alters `key`). */
const hasPositionFallback = (parts: readonly string[]): boolean => {
  const key = parts.at(-1) ?? '';

  return (
    parts.includes('alt') &&
    (parts.includes('mod') || parts.includes('ctrl') || parts.includes('meta')) &&
    isLatinKey(key) &&
    key.length === 1
  );
};

export const toTinykeysBinding = (hotkey: string): string => {
  const normalized = normalizeHotkeyString(hotkey);
  const parts = normalized.split('+').filter(Boolean);
  const key = parts.at(-1) ?? '';
  // The character, or its position (tinykeys tests a `(…)` pattern against both the key and the code);
  // `acceptsHotkeyPress` admits the position only when the press typed no Latin character.
  const positional = hasPositionFallback(parts) ? `(${key}|${toPhysicalKeyCode(key)})` : null;

  return parts
    .map((part, index) =>
      index === parts.length - 1 && positional
        ? positional
        : (TINY_MODIFIER_ALIASES[part] ?? TINY_KEY_ALIASES[part] ?? part)
    )
    .join('+');
};

/** A positional match also requires no typed Latin character and no AltGr, so other layouts keep their letters. */
export const acceptsHotkeyPress = (event: KeyboardEvent, hotkey: string): boolean => {
  const parts = normalizeHotkeyString(hotkey).split('+').filter(Boolean);
  const key = parts.at(-1) ?? '';

  if (!hasPositionFallback(parts) || event.key.toLowerCase() === key) {
    return true;
  }

  return (
    !isLatinKey(event.key) && event.code === toPhysicalKeyCode(key) && event.getModifierState?.('AltGraph') !== true
  );
};

/** Matches exactly as the runtime's tinykeys listener does, so local handlers and the runtime agree. */
export const isHotkeyPress = (event: KeyboardEvent, hotkey: string): boolean => {
  const binding = toTinykeysBinding(hotkey);
  const [press] = binding ? parseKeybinding(binding) : [];
  return press !== undefined && matchKeybindingPress(event, press) && acceptsHotkeyPress(event, hotkey);
};

/** The `mod` key's platform name; a bare modifier normalizes to nothing, so use this rather than formatting 'mod'. */
export const MOD_KEY_LABEL = IS_MAC_OS ? 'cmd' : 'ctrl';

const formatPart = (part: string, isMacOs: boolean): string => {
  const named = part
    .trim()
    .toLowerCase()
    .replace('mod', isMacOs ? 'cmd' : 'ctrl');
  return isMacOs ? named.replace('alt', 'option') : named;
};

/** Fixed pointer gestures may use a bare modifier instead of a complete hotkey chord. */
export const formatHotkeyPartForPlatform = (part: string): string => formatPart(part, IS_MAC_OS);

/** Apple's menu order, Control Option Shift Command; normalization keeps its own order for comparison. */
const MAC_DISPLAY_MODIFIER_RANK: Record<string, number> = { cmd: 3, ctrl: 0, meta: 3, option: 1, shift: 2 };

export const formatHotkeyForPlatform = (hotkey: string, isMacOs: boolean = IS_MAC_OS): string[] => {
  const parts = normalizeHotkeyString(hotkey)
    .split('+')
    .filter(Boolean)
    .map((part) => formatPart(part, isMacOs));

  if (!isMacOs) {
    return parts;
  }

  const key = parts.at(-1)!;
  const modifiers = parts.slice(0, -1).sort((a, b) => MAC_DISPLAY_MODIFIER_RANK[a]! - MAC_DISPLAY_MODIFIER_RANK[b]!);
  return [...modifiers, key];
};

/** Text labels: adjacent modifier glyphs on macOS, word labels joined by + elsewhere. */
const MAC_KEY_LABELS: Record<string, string> = {
  alt: '⌥',
  cmd: '⌘',
  ctrl: '⌃',
  enter: '↵',
  option: '⌥',
  shift: '⇧',
};

const OTHER_KEY_LABELS: Record<string, string> = {
  alt: 'Alt',
  ctrl: 'Ctrl',
  enter: 'Enter',
  meta: 'Win',
  shift: 'Shift',
};

/** One formatted part's text label, for render sites that draw icons for some keys and need text for the rest. */
export const formatHotkeyPartLabel = (part: string, isMacOs: boolean = IS_MAC_OS): string =>
  (isMacOs ? MAC_KEY_LABELS[part] : OTHER_KEY_LABELS[part]) ?? part.toUpperCase();

/** A binding as one line of text, e.g. ⌘K on macOS and Ctrl+K elsewhere, for tooltips and inline copy. */
export const formatHotkeyLabel = (hotkey: string, isMacOs: boolean = IS_MAC_OS): string =>
  formatHotkeyForPlatform(hotkey, isMacOs)
    .map((part) => formatHotkeyPartLabel(part, isMacOs))
    .join(isMacOs ? '' : '+');

const ARIA_KEY_LABELS: Record<string, string> = {
  alt: 'Alt',
  cmd: 'Meta',
  ctrl: 'Control',
  enter: 'Enter',
  option: 'Alt',
  shift: 'Shift',
};

export const formatHotkeyAriaLabel = (hotkey: string, isMacOs: boolean = IS_MAC_OS): string =>
  formatHotkeyForPlatform(hotkey, isMacOs)
    .map((part) => ARIA_KEY_LABELS[part] ?? (part.length === 1 ? part.toUpperCase() : part))
    .join('+');

/** Inputs that take no typed text: a switch or checkbox owning focus has no native undo/shortcut to protect. */
const NON_TEXT_INPUT_TYPES = new Set([
  'button',
  'checkbox',
  'color',
  'file',
  'image',
  'radio',
  'range',
  'reset',
  'submit',
]);

/** What a focused radio group or slider does with a key itself: move its selection or value. */
const NATIVE_CONTROL_KEYS = new Set(['ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'End', 'Home']);
const ACTIVATION_KEYS = new Set([' ', 'Enter']);
/** Controls a bare Space or Enter activates natively: buttons, links, toggles, tabs, menu items, options. */
const ACTIVATABLE_CONTROL_SELECTOR = [
  'a[href]',
  'button',
  'input[type="button"]',
  'input[type="checkbox"]',
  'input[type="radio"]',
  'input[type="reset"]',
  'input[type="submit"]',
  'summary',
  '[role="button"]',
  '[role="checkbox"]',
  '[role="link"]',
  '[role="menuitem"]',
  '[role="menuitemcheckbox"]',
  '[role="menuitemradio"]',
  '[role="option"]',
  '[role="switch"]',
  '[role="tab"]',
].join(', ');

/** A focused radio, slider, button, link, toggle or tab owns its unmodified navigation and activation keys. */
export const isNativeControlKeyPress = (event: KeyboardEvent): boolean =>
  !event.ctrlKey &&
  !event.metaKey &&
  !event.altKey &&
  typeof Element !== 'undefined' &&
  event.target instanceof Element &&
  ((NATIVE_CONTROL_KEYS.has(event.key) &&
    event.target.closest('input[type="radio"], input[type="range"], [role="radiogroup"], [role="slider"]') !== null) ||
    (ACTIVATION_KEYS.has(event.key) && event.target.matches(ACTIVATABLE_CONTROL_SELECTOR)));

export const isEditableHotkeyTarget = (target: EventTarget | null): boolean => {
  if (typeof HTMLElement === 'undefined') {
    return false;
  }

  if (!(target instanceof HTMLElement)) {
    return false;
  }

  const editable = target.closest('input, textarea, select, [contenteditable="true"]');

  return (
    editable !== null &&
    !(editable.tagName === 'INPUT' && NON_TEXT_INPUT_TYPES.has((editable as HTMLInputElement).type))
  );
};