diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-08-13 12:24:55 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-08-13 12:24:55 +0900 |
commit | 3c1b33add3b8cddc29139f847b57c7023f0013c8 (patch) | |
tree | 791a4784e546b57465c863171aca187197f485be | |
parent | 082450928a35ea8d35fdbcd176dbd972e45acfec (diff) |
flush input queue when no key-maps matched
-rw-r--r-- | src/background/key-queue.js | 14 | ||||
-rw-r--r-- | src/background/keys.js | 19 |
2 files changed, 24 insertions, 9 deletions
diff --git a/src/background/key-queue.js b/src/background/key-queue.js index e14e995..cd8d865 100644 --- a/src/background/key-queue.js +++ b/src/background/key-queue.js @@ -17,10 +17,18 @@ export default class KeyQueue { push(key) { this.data.push(key); - for (let map of DEFAULT_KEYMAP) { - if (keys.keysEquals(map.keys, this.data)) { + let filtered = DEFAULT_KEYMAP.filter((map) => { + return keys.hasPrefix(map.keys, this.data) + }); + + if (filtered.length == 0) { + this.data = []; + return; + } else if (filtered.length == 1) { + let map = filtered[0]; + if (map.keys.length == this.data.length) { this.data = []; - return map.action + return map.action; } } return null; diff --git a/src/background/keys.js b/src/background/keys.js index d6a94fb..802340d 100644 --- a/src/background/keys.js +++ b/src/background/keys.js @@ -1,4 +1,4 @@ -const keyEquals = (key1, key2) => { +const identifyKey = (key1, key2) => { return (key1.code === key2.code) && ((key1.shift || false) === (key2.shift || false)) && ((key1.ctrl || false) === (key2.ctrl || false)) && @@ -6,16 +6,23 @@ const keyEquals = (key1, key2) => { ((key1.meta || false) === (key2.meta || false)); }; -const keysEquals = (keys1, keys2) => { - if (keys1.length !== keys2.length) { +const hasPrefix = (keys, prefix) => { + if (keys.length < prefix.length) { return false; } - for (let i = 0; i < keys1.length; ++i) { - if (!keyEquals(keys1[i], keys2[i])) { + for (let i = 0; i < keys.length; ++i) { + if (!identifyKey(keys[i], prefix[i])) { return false; } } return true; } -export { keyEquals, keysEquals }; +const identifyKeys = (keys1, keys2) => { + if (keys1.length !== keys2.length) { + return false; + } + return hasPrefix(keys1, keys2); +} + +export { identifyKey, identifyKeys, hasPrefix }; |