store input keys in content script
This commit is contained in:
parent
a6b197ca73
commit
8ff302a1f2
7 changed files with 92 additions and 119 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue