diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-10-03 21:31:25 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-03 21:31:25 +0900 |
commit | 3244c3c35db7639dc1bb08d9b0bcd40323e9d626 (patch) | |
tree | 1acf04feeb4b246240e573744306d227620c7294 /src/components | |
parent | b5e52a75d760c23d2ac7257056f9ba592ad17b34 (diff) | |
parent | 4b5616b5244f14a8e6b1edf59e944edb7d8ae2d4 (diff) |
Merge pull request #16 from ueokande/enable-keys
Enable keys on github.com
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/background-input.js | 6 | ||||
-rw-r--r-- | src/components/background.js | 2 | ||||
-rw-r--r-- | src/components/content-input.js | 44 |
3 files changed, 47 insertions, 5 deletions
diff --git a/src/components/background-input.js b/src/components/background-input.js index 9c6ef1c..4735d5a 100644 --- a/src/components/background-input.js +++ b/src/components/background-input.js @@ -1,5 +1,4 @@ import * as inputActions from '../actions/input'; -import * as keys from '../shared/keys'; import * as operationActions from '../actions/operation'; export default class BackgroundInputComponent { @@ -37,15 +36,14 @@ export default class BackgroundInputComponent { } handleKeysChanged(sender, input) { - let prefix = keys.asKeymapChars(input.keys); let matched = Object.keys(this.keymaps).filter((keyStr) => { - return keyStr.startsWith(prefix); + 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 && prefix !== matched[0]) { + matched.length === 1 && input.keys !== matched[0]) { return Promise.resolve(); } let operation = this.keymaps[matched]; diff --git a/src/components/background.js b/src/components/background.js index 4c5bb19..0585a04 100644 --- a/src/components/background.js +++ b/src/components/background.js @@ -35,7 +35,7 @@ export default class BackgroundComponent { switch (message.type) { case messages.KEYDOWN: return this.store.dispatch( - inputActions.keyPress(message.code, message.ctrl), sender); + inputActions.keyPress(message.key, message.ctrl), sender); case messages.OPEN_URL: if (message.newTab) { return this.store.dispatch( diff --git a/src/components/content-input.js b/src/components/content-input.js new file mode 100644 index 0000000..6437128 --- /dev/null +++ b/src/components/content-input.js @@ -0,0 +1,44 @@ +import messages from '../content/messages'; + +export default class ContentInputComponent { + constructor(target) { + this.pressed = {}; + + target.addEventListener('keypress', this.onKeyPress.bind(this)); + target.addEventListener('keydown', this.onKeyDown.bind(this)); + target.addEventListener('keyup', this.onKeyUp.bind(this)); + } + + onKeyPress(e) { + this.capture(e); + } + + onKeyDown(e) { + this.capture(e); + } + + onKeyUp(e) { + this.pressed[e.key] = false; + } + + capture(e) { + if (this.pressed[e.key]) { + return; + } + this.pressed[e.key] = true; + + if (e.target instanceof HTMLInputElement || + e.target instanceof HTMLTextAreaElement || + e.target instanceof HTMLSelectElement) { + if (e.key === 'Escape' && e.target.blur) { + e.target.blur(); + } + return; + } + browser.runtime.sendMessage({ + type: messages.KEYDOWN, + key: e.key, + ctrl: e.ctrlKey + }); + } +} |