keybindings.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { onBeforeUnmount, onMounted } from "@nuxtjs/composition-api"
  2. import { HoppAction, invokeAction } from "./actions"
  3. import { isAppleDevice } from "./platformutils"
  4. import { isDOMElement, isTypableElement } from "./utils/dom"
  5. /**
  6. * This variable keeps track whether keybindings are being accepted
  7. * true -> Keybindings are checked
  8. * false -> Key presses are ignored (Keybindings are not checked)
  9. */
  10. let keybindingsEnabled = true
  11. /**
  12. * Alt is also regarded as macOS OPTION (⌥) key
  13. * Ctrl is also regarded as macOS COMMAND (⌘) key (NOTE: this differs from HTML Keyboard spec where COMMAND is Meta key!)
  14. */
  15. type ModifierKeys = "ctrl" | "alt" | "ctrl-shift" | "alt-shift"
  16. /* eslint-disable prettier/prettier */
  17. // prettier-ignore
  18. type Key =
  19. | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j"
  20. | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t"
  21. | "u" | "v" | "w" | "x" | "y" | "z" | "0" | "1" | "2" | "3"
  22. | "4" | "5" | "6" | "7" | "8" | "9" | "up" | "down" | "left"
  23. | "right" | "/" | "?"
  24. /* eslint-enable */
  25. type ModifierBasedShortcutKey = `${ModifierKeys}-${Key}`
  26. // Singular keybindings (these will be disabled when an input-ish area has been focused)
  27. type SingleCharacterShortcutKey = `${Key}`
  28. type ShortcutKey = ModifierBasedShortcutKey | SingleCharacterShortcutKey
  29. export const bindings: {
  30. // eslint-disable-next-line no-unused-vars
  31. [_ in ShortcutKey]?: HoppAction
  32. } = {
  33. "ctrl-g": "request.send-cancel",
  34. "ctrl-i": "request.reset",
  35. "ctrl-u": "request.copy-link",
  36. "ctrl-s": "request.save",
  37. "ctrl-shift-s": "request.save-as",
  38. "alt-up": "request.method.next",
  39. "alt-down": "request.method.prev",
  40. "alt-g": "request.method.get",
  41. "alt-h": "request.method.head",
  42. "alt-p": "request.method.post",
  43. "alt-u": "request.method.put",
  44. "alt-x": "request.method.delete",
  45. "ctrl-k": "flyouts.keybinds.toggle",
  46. "/": "modals.search.toggle",
  47. "?": "modals.support.toggle",
  48. "ctrl-m": "modals.share.toggle",
  49. "alt-r": "navigation.jump.rest",
  50. "alt-q": "navigation.jump.graphql",
  51. "alt-w": "navigation.jump.realtime",
  52. "alt-d": "navigation.jump.documentation",
  53. "alt-m": "navigation.jump.profile",
  54. "alt-s": "navigation.jump.settings",
  55. }
  56. /**
  57. * A composable that hooks to the caller component's
  58. * lifecycle and hooks to the keyboard events to fire
  59. * the appropriate actions based on keybindings
  60. */
  61. export function hookKeybindingsListener() {
  62. onMounted(() => {
  63. document.addEventListener("keydown", handleKeyDown)
  64. })
  65. onBeforeUnmount(() => {
  66. document.removeEventListener("keydown", handleKeyDown)
  67. })
  68. }
  69. function handleKeyDown(ev: KeyboardEvent) {
  70. // Do not check keybinds if the mode is disabled
  71. if (!keybindingsEnabled) return
  72. const binding = generateKeybindingString(ev)
  73. if (!binding) return
  74. const boundAction = bindings[binding]
  75. if (!boundAction) return
  76. ev.preventDefault()
  77. invokeAction(boundAction)
  78. }
  79. function generateKeybindingString(ev: KeyboardEvent): ShortcutKey | null {
  80. // We may or may not have a modifier key
  81. const modifierKey = getActiveModifier(ev)
  82. // We will always have a non-modifier key
  83. const key = getPressedKey(ev)
  84. if (!key) return null
  85. // All key combos backed by modifiers are valid shortcuts (whether currently typing or not)
  86. if (modifierKey) return `${modifierKey}-${key}`
  87. const target = ev.target
  88. // no modifier key here then we do not do anything while on input
  89. if (isDOMElement(target) && isTypableElement(target)) return null
  90. // single key while not input
  91. return `${key}`
  92. }
  93. function getPressedKey(ev: KeyboardEvent): Key | null {
  94. const val = ev.key.toLowerCase()
  95. // Check arrow keys
  96. if (val === "arrowup") return "up"
  97. else if (val === "arrowdown") return "down"
  98. else if (val === "arrowleft") return "left"
  99. else if (val === "arrowright") return "right"
  100. // Check letter keys
  101. if (val.length === 1 && val.toUpperCase() !== val.toLowerCase())
  102. return val as Key
  103. // Check if number keys
  104. if (val.length === 1 && !isNaN(val as any)) return val as Key
  105. // Check if question mark
  106. if (val === "?") return "?"
  107. // Check if question mark
  108. if (val === "/") return "/"
  109. // If no other cases match, this is not a valid key
  110. return null
  111. }
  112. function getActiveModifier(ev: KeyboardEvent): ModifierKeys | null {
  113. const isShiftKey = ev.shiftKey
  114. // We only allow one modifier key to be pressed (for now)
  115. // Control key (+ Command) gets priority and if Alt is also pressed, it is ignored
  116. if (isAppleDevice() && ev.metaKey) return isShiftKey ? "ctrl-shift" : "ctrl"
  117. else if (!isAppleDevice() && ev.ctrlKey)
  118. return isShiftKey ? "ctrl-shift" : "ctrl"
  119. // Test for Alt key
  120. if (ev.altKey) return isShiftKey ? "alt-shift" : "alt"
  121. return null
  122. }
  123. /**
  124. * This composable allows for the UI component to be disabled if the component in question is mounted
  125. */
  126. export function useKeybindingDisabler() {
  127. // TODO: Move to a lock based system that keeps the bindings disabled until all locks are lifted
  128. const disableKeybindings = () => {
  129. keybindingsEnabled = false
  130. }
  131. const enableKeybindings = () => {
  132. keybindingsEnabled = true
  133. }
  134. return {
  135. disableKeybindings,
  136. enableKeybindings,
  137. }
  138. }