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

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

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

  static fromJSON(json: any): Keymaps {
    if (typeof json !== 'object' || json === null) {
      throw new TypeError('invalid keymaps type: ' + JSON.stringify(json));
    }

    let data: KeymapsJSON = {};
    for (let key of Object.keys(json)) {
      data[key] = operations.valueOf(json[key]);
    }
    return new Keymaps(data);
  }

  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);
  }
}