aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/Keymaps.ts
blob: 7e510d1be27fad5679177619b166c094463009c2 (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
import * as operations from '../operations';
import Validator from './Validator';

const Schema = {
  type: 'object',
  patternProperties: {
    '.*': {
      type: 'object',
      properties: {
        type: { type: 'string' },
      },
      required: ['type'],
    },
  }
};

export type KeymapsJSON = { [key: string]: operations.Operation };

export default class Keymaps {
  constructor(
    private readonly data: KeymapsJSON,
  ) {
  }

  static fromJSON(json: unknown): Keymaps {
    let obj = new Validator<KeymapsJSON>(Schema).validate(json);
    let entries: KeymapsJSON = {};
    for (let key of Object.keys(obj)) {
      entries[key] = operations.valueOf(obj[key]);
    }
    return new Keymaps(entries);
  }

  combine(other: Keymaps): Keymaps {
    return new Keymaps({
      ...this.data,
      ...other.data,
    });
  }

  toJSON(): KeymapsJSON {
    return this.data;
  }

  entries(): [string, operations.Operation][] {
    return Object.entries(this.data);
  }
}