parent
825bb63476
commit
a74a8b537e
4 changed files with 60 additions and 51 deletions
@ -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); |
||||
} |
||||
} |
@ -1,5 +1,5 @@ |
||||
import { expect } from "chai"; |
||||
import * as keys from '../../src/background/keys'; |
||||
import * as keys from '../../src/shared/keys'; |
||||
|
||||
describe("keys", () => { |
||||
const KEYMAP = { |
Reference in new issue