diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-07 18:38:57 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-07 18:50:43 +0900 |
commit | 8ff302a1f2870994cddc36fd461879eac951203d (patch) | |
tree | 3a07b64768937516dc7d43958b77dfcf77856126 /src/components | |
parent | a6b197ca73a6be50c5c5bf391391c9971ff8c5e2 (diff) |
store input keys in content script
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/background-input.js | 53 | ||||
-rw-r--r-- | src/components/background.js | 9 | ||||
-rw-r--r-- | src/components/content-input.js | 52 |
3 files changed, 47 insertions, 67 deletions
diff --git a/src/components/background-input.js b/src/components/background-input.js deleted file mode 100644 index bd6ecf9..0000000 --- a/src/components/background-input.js +++ /dev/null @@ -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); - } -} diff --git a/src/components/background.js b/src/components/background.js index de44dae..79a7a6a 100644 --- a/src/components/background.js +++ b/src/components/background.js @@ -1,5 +1,5 @@ import messages from 'content/messages'; -import * as inputActions from 'actions/input'; +import * as operationActions from 'actions/operation'; import * as settingsActions from 'actions/setting'; import * as tabActions from 'actions/tab'; import * as commands from 'shared/commands'; @@ -37,9 +37,10 @@ export default class BackgroundComponent { onMessage(message, sender) { switch (message.type) { - case messages.KEYDOWN: + case messages.BACKGROUND_OPERATION: return this.store.dispatch( - inputActions.keyPress(message.key, message.ctrl), sender); + operationActions.execBackground(message.operation, sender.tab), + sender); case messages.OPEN_URL: if (message.newTab) { return this.store.dispatch( @@ -70,7 +71,7 @@ export default class BackgroundComponent { } onTabUpdated(id, info) { - if (info.url) { + if (info.status === 'complete') { this.syncSettings(id); } } diff --git a/src/components/content-input.js b/src/components/content-input.js index 38d57fd..1dbf48f 100644 --- a/src/components/content-input.js +++ b/src/components/content-input.js @@ -1,14 +1,43 @@ -import messages from 'content/messages'; +import * as inputActions from 'actions/input'; +import * as operationActions from 'actions/operation'; export default class ContentInputComponent { - constructor(target) { + constructor(target, store) { this.pressed = {}; + this.store = store; target.addEventListener('keypress', this.onKeyPress.bind(this)); target.addEventListener('keydown', this.onKeyDown.bind(this)); target.addEventListener('keyup', this.onKeyUp.bind(this)); } + update() { + let settings = this.store.getState().setting.settings; + if (!settings) { + return; + } + let input = this.store.getState().input; + let keymaps = JSON.parse(settings.json).keymaps; + + let matched = Object.keys(keymaps).filter((keyStr) => { + return keyStr.startsWith(input.keys); + }); + if (matched.length === 0) { + this.store.dispatch(inputActions.clearKeys()); + return Promise.resolve(); + } else if (matched.length > 1 || + matched.length === 1 && input.keys !== matched[0]) { + return Promise.resolve(); + } + let operation = keymaps[matched]; + try { + this.store.dispatch(operationActions.exec(operation)); + } catch (e) { + console.error(e); + } + this.store.dispatch(inputActions.clearKeys()); + } + onKeyPress(e) { if (this.pressed[e.key] && this.pressed[e.key] !== 'keypress') { return; @@ -30,18 +59,21 @@ export default class ContentInputComponent { } capture(e) { - if (e.target instanceof HTMLInputElement || - e.target instanceof HTMLTextAreaElement || - e.target instanceof HTMLSelectElement) { + if (this.fromInput(e)) { if (e.key === 'Escape' && e.target.blur) { e.target.blur(); } return; } - browser.runtime.sendMessage({ - type: messages.KEYDOWN, - key: e.key, - ctrl: e.ctrlKey - }); + if (e.key === 'OS') { + return; + } + this.store.dispatch(inputActions.keyPress(e.key, e.ctrlKey)); + } + + fromInput(e) { + return e.target instanceof HTMLInputElement || + e.target instanceof HTMLTextAreaElement || + e.target instanceof HTMLSelectElement; } } |