aboutsummaryrefslogtreecommitdiff
path: root/src/content/components/keymapper.js
blob: 8f2cead599416f4679541f261c80a2622f316d59 (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
import * as inputActions from 'content/actions/input';
import * as operationActions from 'content/actions/operation';

export default class KeymapperComponent {
  constructor(store) {
    this.store = store;
  }

  update() {
  }

  key(key, ctrl) {
    let keymaps = this.keymaps();
    if (!keymaps) {
      return;
    }
    this.store.dispatch(inputActions.keyPress(key, ctrl));

    let input = this.store.getState().input;
    let matched = Object.keys(keymaps).filter((keyStr) => {
      return keyStr.startsWith(input.keys);
    });
    if (matched.length === 0) {
      this.store.dispatch(inputActions.clearKeys());
      return false;
    } else if (matched.length > 1 ||
      matched.length === 1 && input.keys !== matched[0]) {
      return true;
    }
    let operation = keymaps[matched];
    this.store.dispatch(operationActions.exec(operation));
    this.store.dispatch(inputActions.clearKeys());
    return true;
  }

  keymaps() {
    let settings = this.store.getState().setting.settings;
    if (!settings || !settings.json) {
      return null;
    }
    return JSON.parse(settings.json).keymaps;
  }
}