commit
0f54a203db
12 changed files with 188 additions and 197 deletions
@ -1,53 +0,0 @@ |
||||
import * as inputActions from 'actions/input'; |
||||
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 matched = Object.keys(this.keymaps).filter((keyStr) => { |
||||
return keyStr.startsWith(input.keys); |
||||
}); |
||||
if (matched.length === 0) { |
||||
this.store.dispatch(inputActions.clearKeys(), sender); |
||||
return Promise.resolve(); |
||||
} else if (matched.length > 1 || |
||||
matched.length === 1 && input.keys !== 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); |
||||
} |
||||
} |
@ -0,0 +1,43 @@ |
||||
import * as inputActions from 'actions/input'; |
||||
import * as operationActions from 'actions/operation'; |
||||
|
||||
export default class KeymapperComponent { |
||||
constructor(store) { |
||||
this.store = store; |
||||
} |
||||
|
||||
update() { |
||||
} |
||||
|
||||
key(key, ctrl) { |
||||
let keymaps = this.keymaps(); |
||||
if (!keymaps) { |
||||
return; |
||||
} |
||||
this.store.dispatch(inputActions.keyPress(key, ctrl)); |
||||
|
||||
let input = this.store.getState().input; |
||||
let matched = Object.keys(keymaps).filter((keyStr) => { |
||||
return keyStr.startsWith(input.keys); |
||||
}); |
||||
if (matched.length === 0) { |
||||
this.store.dispatch(inputActions.clearKeys()); |
||||
return false; |
||||
} else if (matched.length > 1 || |
||||
matched.length === 1 && input.keys !== matched[0]) { |
||||
return true; |
||||
} |
||||
let operation = keymaps[matched]; |
||||
this.store.dispatch(operationActions.exec(operation)); |
||||
this.store.dispatch(inputActions.clearKeys()); |
||||
return true; |
||||
} |
||||
|
||||
keymaps() { |
||||
let settings = this.store.getState().setting.settings; |
||||
if (!settings || !settings.json) { |
||||
return null; |
||||
} |
||||
return JSON.parse(settings.json).keymaps; |
||||
} |
||||
} |
Reference in new issue