aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/Key.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-10-07 12:54:32 +0000
committerGitHub <noreply@github.com>2019-10-07 12:54:32 +0000
commit8eddcc1785a85bbe74be254d1055ebe5125dad10 (patch)
treef3f51320d12a90a1b421ed8b1f811c576996ea8e /src/shared/settings/Key.ts
parent7fc2bb615f530fc6adfade54b9553568f5d50ceb (diff)
parentb77a4734985722e96066e713f3b1b9e81a6e1811 (diff)
Merge pull request #654 from ueokande/settings-as-a-class
Refactor settings on shared logics
Diffstat (limited to 'src/shared/settings/Key.ts')
-rw-r--r--src/shared/settings/Key.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/shared/settings/Key.ts b/src/shared/settings/Key.ts
new file mode 100644
index 0000000..b11eeb2
--- /dev/null
+++ b/src/shared/settings/Key.ts
@@ -0,0 +1,61 @@
+export default class Key {
+ public readonly key: string;
+
+ public readonly shift: boolean;
+
+ public readonly ctrl: boolean;
+
+ public readonly alt: boolean;
+
+ public readonly meta: boolean;
+
+ constructor({ key, shift, ctrl, alt, meta }: {
+ key: string;
+ shift: boolean;
+ ctrl: boolean;
+ alt: boolean;
+ meta: boolean;
+ }) {
+ this.key = key;
+ this.shift = shift;
+ this.ctrl = ctrl;
+ this.alt = alt;
+ this.meta = meta;
+ }
+
+ static fromMapKey(str: string): Key {
+ if (str.startsWith('<') && str.endsWith('>')) {
+ let inner = str.slice(1, -1);
+ let shift = inner.includes('S-');
+ let base = inner.slice(inner.lastIndexOf('-') + 1);
+ if (shift && base.length === 1) {
+ base = base.toUpperCase();
+ } else if (!shift && base.length === 1) {
+ base = base.toLowerCase();
+ }
+ return new Key({
+ key: base,
+ shift: shift,
+ ctrl: inner.includes('C-'),
+ alt: inner.includes('A-'),
+ meta: inner.includes('M-'),
+ });
+ }
+
+ return new Key({
+ key: str,
+ shift: str.toLowerCase() !== str,
+ ctrl: false,
+ alt: false,
+ meta: false,
+ });
+ }
+
+ equals(key: Key) {
+ return this.key === key.key &&
+ this.ctrl === key.ctrl &&
+ this.meta === key.meta &&
+ this.alt === key.alt &&
+ this.shift === key.shift;
+ }
+}