aboutsummaryrefslogtreecommitdiff
path: root/src/background/key-queue.js
blob: 25f1d244711112ee7fc372687dbeff817b92eb77 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import newActions from '../actions';
import * as actions from '../shared/actions';

const DEFAULT_KEYMAP = {
  ':': { type: actions.CMD_OPEN },
  'o': { type: actions.CMD_TABS_OPEN, alter: false },
  'O': { type: actions.CMD_TABS_OPEN, alter: true },
  'b': { type: actions.CMD_BUFFER },
  'k': { type: actions.SCROLL_LINES, count: -1 },
  'j': { type: actions.SCROLL_LINES, count: 1 },
  '<C-E>': { type: actions.SCROLL_LINES, count: -1 },
  '<C-Y>': { type: actions.SCROLL_LINES, count: 1 },
  '<C-U>': { type: actions.SCROLL_PAGES, count: -0.5 },
  '<C-D>': { type: actions.SCROLL_PAGES, count: 0.5 },
  '<C-B>': { type: actions.SCROLL_PAGES, count: -1 },
  '<C-F>': { type: actions.SCROLL_PAGES, count: 1 },
  'gg': { type: actions.SCROLL_TOP },
  'G': { type: actions.SCROLL_BOTTOM },
  '0': { type: actions.SCROLL_LEFT },
  '$': { type: actions.SCROLL_RIGHT },
  'd': { type: newActions.TABS_CLOSE },
  'u': { type: newActions.TABS_REOPEN },
  'h': { type: newActions.TABS_PREV, count: 1 },
  'l': { type: newActions.TABS_NEXT, count: 1 },
  'r': { type: newActions.TABS_RELOAD, cache: false },
  'R': { type: newActions.TABS_RELOAD, cache: true },
  'zi': { type: newActions.ZOOM_IN },
  'zo': { type: newActions.ZOOM_OUT },
  'zz': { type: newActions.ZOOM_NEUTRAL },
  'f': { type: actions.FOLLOW_START, newTab: false },
  'F': { type: actions.FOLLOW_START, newTab: true },
  'H': { type: actions.HISTORY_PREV },
  'L': { type: actions.HISTORY_NEXT },
}

export default class KeyQueue {

  constructor(keymap = DEFAULT_KEYMAP) {
    this.data = [];
    this.keymap = keymap;
  }

  push(key) {
    this.data.push(key);

    let current = this.asKeymapChars();
    let filtered = Object.keys(this.keymap).filter((keys) => {
      return keys.startsWith(current);
    });

    if (filtered.length == 0) {
      this.data = [];
      return null;
    } else if (filtered.length === 1 && current === filtered[0]) {
      let action = this.keymap[filtered[0]];
      this.data = [];
      return action;
    }
    return null;
  }

  asKeymapChars() {
    return this.data.map((k) => {
      let c = String.fromCharCode(k.code);
      if (k.ctrl) {
        return '<C-' + c.toUpperCase() + '>';
      } else {
        return c
      }
    }).join('');
  }

  asCaretChars() {
    return this.data.map((k) => {
      let c = String.fromCharCode(k.code);
      if (k.ctrl) {
        return '^' + c.toUpperCase();
      } else {
        return c;
      }
    }).join('');
  }
}