aboutsummaryrefslogtreecommitdiff
path: root/src/shared/utils/keys.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-04-30 21:50:46 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-05-02 11:14:19 +0900
commit0cffb09e249832291be73be039dc1b9bb38115f9 (patch)
tree3b8783d7a8185ca1524d7b2f9231e43104121204 /src/shared/utils/keys.ts
parent2b8c37e57f1b55dcb562ccf9141ae29bac0e370a (diff)
Types on src/share
Diffstat (limited to 'src/shared/utils/keys.ts')
-rw-r--r--src/shared/utils/keys.ts22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/shared/utils/keys.ts b/src/shared/utils/keys.ts
index f024069..d9abef7 100644
--- a/src/shared/utils/keys.ts
+++ b/src/shared/utils/keys.ts
@@ -1,4 +1,12 @@
-const modifiedKeyName = (name) => {
+interface Key {
+ key: string;
+ shiftKey: boolean | undefined;
+ ctrlKey: boolean | undefined;
+ altKey: boolean | undefined;
+ metaKey: boolean | undefined;
+}
+
+const modifiedKeyName = (name: string): string => {
if (name === ' ') {
return 'Space';
}
@@ -10,7 +18,7 @@ const modifiedKeyName = (name) => {
return name;
};
-const fromKeyboardEvent = (e) => {
+const fromKeyboardEvent = (e: KeyboardEvent): Key => {
let key = modifiedKeyName(e.key);
let shift = e.shiftKey;
if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) {
@@ -28,7 +36,7 @@ const fromKeyboardEvent = (e) => {
};
};
-const fromMapKey = (key) => {
+const fromMapKey = (key: string): Key => {
if (key.startsWith('<') && key.endsWith('>')) {
let inner = key.slice(1, -1);
let shift = inner.includes('S-');
@@ -55,8 +63,10 @@ const fromMapKey = (key) => {
};
};
-const fromMapKeys = (keys) => {
- const fromMapKeysRecursive = (remainings, mappedKeys) => {
+const fromMapKeys = (keys: string): Key[] => {
+ const fromMapKeysRecursive = (
+ remainings: string, mappedKeys: Key[],
+ ): Key[] => {
if (remainings.length === 0) {
return mappedKeys;
}
@@ -78,7 +88,7 @@ const fromMapKeys = (keys) => {
return fromMapKeysRecursive(keys, []);
};
-const equals = (e1, e2) => {
+const equals = (e1: Key, e2: Key): boolean => {
return e1.key === e2.key &&
e1.ctrlKey === e2.ctrlKey &&
e1.metaKey === e2.metaKey &&