diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-01 20:33:18 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-01 20:33:18 +0900 |
commit | 192e6fec1bbc540063bf7a56d5da98b61d1df6eb (patch) | |
tree | 64d5ae21d2510475af9516efb061ea912b8decff /src/components/background-input.js | |
parent | 1f3dc0ba5a3bc0f02a98e3e11fca6038b9fd8b87 (diff) | |
parent | a74a8b537e8a82f1af63667fd73869b83d8b7d0d (diff) |
Merge branch 'more-components'
Diffstat (limited to 'src/components/background-input.js')
-rw-r--r-- | src/components/background-input.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/components/background-input.js b/src/components/background-input.js new file mode 100644 index 0000000..9c6ef1c --- /dev/null +++ b/src/components/background-input.js @@ -0,0 +1,55 @@ +import * as inputActions from '../actions/input'; +import * as keys from '../shared/keys'; +import * as operationActions from '../actions/operation'; + +export default class BackgroundInputComponent { + constructor(store) { + this.store = store; + this.keymaps = {}; + this.prevInputs = []; + } + + update(sender) { + let state = this.store.getState(); + this.reloadSettings(state.setting); + this.handleKeyInputs(sender, state.input); + } + + reloadSettings(setting) { + if (!setting.settings.json) { + return; + } + this.keymaps = JSON.parse(setting.settings.json).keymaps; + } + + handleKeyInputs(sender, input) { + if (JSON.stringify(this.prevInputs) === JSON.stringify(input)) { + return; + } + this.prevInputs = input; + + if (input.keys.length === 0) { + return; + } + if (sender) { + return this.handleKeysChanged(sender, input); + } + } + + handleKeysChanged(sender, input) { + let prefix = keys.asKeymapChars(input.keys); + let matched = Object.keys(this.keymaps).filter((keyStr) => { + return keyStr.startsWith(prefix); + }); + if (matched.length === 0) { + this.store.dispatch(inputActions.clearKeys(), sender); + return Promise.resolve(); + } else if (matched.length > 1 || + matched.length === 1 && prefix !== matched[0]) { + return Promise.resolve(); + } + let operation = this.keymaps[matched]; + this.store.dispatch(operationActions.exec(operation, sender.tab), sender); + this.store.dispatch(inputActions.clearKeys(), sender); + } +} |