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

type OperationJson = {
  type: string
} | {
  type: string;
  [prop: string]: string | number | boolean;
};
export type KeymapsJSON = { [key: string]: OperationJson };

export default class Keymaps {
  constructor(
    private readonly data: { [key: string]: operations.Operation },
  ) {
  }

  static fromJSON(json: KeymapsJSON): Keymaps {
    const entries: { [key: string]: operations.Operation } = {};
    for (const key of Object.keys(json)) {
      entries[key] = operations.valueOf(json[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);
  }
}