aboutsummaryrefslogtreecommitdiff
path: root/source/library/state/keyboard.ts
blob: 5f07b2cc3db4965a8a6d840d5c7a6a1a923b8566 (plain) (blame)
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
/*** EXPORT ------------------------------------------- ***/

export type ShortcutAction =
  | { type: "closeTab" }
  | { type: "format" }
  | { type: "newTab" }
  | { type: "nextTab" }
  | { type: "prevTab" }
  | { type: "run" };

export function matchShortcut(event: KeyboardEvent): ShortcutAction | null {
  const meta = event.metaKey || event.ctrlKey;

  if (!meta)
    return null;

  if (event.key === "Enter") {
    if (event.shiftKey)
      return { type: "newTab" };

    if (!event.altKey)
      return { type: "run" };

    return null;
  }

  if (event.shiftKey && !event.altKey) {
    const key = event.key.toLowerCase();

    if (key === "w")
      return { type: "closeTab" };

    if (key === "f")
      return { type: "format" };
  }

  if (event.altKey && !event.shiftKey) {
    if (event.key === "ArrowRight")
      return { type: "nextTab" };

    if (event.key === "ArrowLeft")
      return { type: "prevTab" };
  }

  return null;
}