aboutsummaryrefslogtreecommitdiff
path: root/src/content/components/common/keymapper.ts
blob: c901ffee57b7c5c5c45b3f08e0f15bf16a16b487 (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
84
85
86
87
import * as inputActions from '../../actions/input';
import * as operationActions from '../../actions/operation';
import * as operations from '../../../shared/operations';
import * as keyUtils from '../../../shared/utils/keys';

import AddonEnabledUseCase from '../../usecases/AddonEnabledUseCase';
import { SettingRepositoryImpl } from '../../repositories/SettingRepository';
import { Keymaps } from '../../../shared/Settings';

type KeymapEntityMap = Map<keyUtils.Key[], operations.Operation>;

let addonEnabledUseCase = new AddonEnabledUseCase();
let settingRepository = new SettingRepositoryImpl();

const reservedKeymaps: Keymaps = {
  '<Esc>': { type: operations.CANCEL },
  '<C-[>': { type: operations.CANCEL },
};

const mapStartsWith = (
  mapping: keyUtils.Key[],
  keys: keyUtils.Key[],
): boolean => {
  if (mapping.length < keys.length) {
    return false;
  }
  for (let i = 0; i < keys.length; ++i) {
    if (!keyUtils.equals(mapping[i], keys[i])) {
      return false;
    }
  }
  return true;
};

export default class KeymapperComponent {
  private store: any;

  constructor(store: any) {
    this.store = store;
  }

  key(key: keyUtils.Key): boolean {
    this.store.dispatch(inputActions.keyPress(key));

    let input = this.store.getState().input;
    let keymaps = this.keymapEntityMap();
    let matched = Array.from(keymaps.keys()).filter(
      (mapping: keyUtils.Key[]) => {
        return mapStartsWith(mapping, input.keys);
      });
    if (!addonEnabledUseCase.getEnabled()) {
      // available keymaps are only ADDON_ENABLE and ADDON_TOGGLE_ENABLED if
      // the addon disabled
      matched = matched.filter((keys) => {
        let type = (keymaps.get(keys) as operations.Operation).type;
        return type === operations.ADDON_ENABLE ||
          type === operations.ADDON_TOGGLE_ENABLED;
      });
    }
    if (matched.length === 0) {
      this.store.dispatch(inputActions.clearKeys());
      return false;
    } else if (matched.length > 1 ||
      matched.length === 1 && input.keys.length < matched[0].length) {
      return true;
    }
    let operation = keymaps.get(matched[0]) as operations.Operation;
    let act = operationActions.exec(operation);
    this.store.dispatch(act);
    this.store.dispatch(inputActions.clearKeys());
    return true;
  }

  private keymapEntityMap(): KeymapEntityMap {
    let keymaps = {
      ...settingRepository.get().keymaps,
      ...reservedKeymaps,
    };
    let entries = Object.entries(keymaps).map((entry) => {
      return [
        keyUtils.fromMapKeys(entry[0]),
        entry[1],
      ];
    }) as [keyUtils.Key[], operations.Operation][];
    return new Map<keyUtils.Key[], operations.Operation>(entries);
  }
}