diff options
Diffstat (limited to 'src/components/content-input.js')
-rw-r--r-- | src/components/content-input.js | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/src/components/content-input.js b/src/components/content-input.js index 38d57fd..9568caf 100644 --- a/src/components/content-input.js +++ b/src/components/content-input.js @@ -1,14 +1,20 @@ -import messages from 'content/messages'; - export default class ContentInputComponent { constructor(target) { this.pressed = {}; + this.onKeyListeners = []; target.addEventListener('keypress', this.onKeyPress.bind(this)); target.addEventListener('keydown', this.onKeyDown.bind(this)); target.addEventListener('keyup', this.onKeyUp.bind(this)); } + update() { + } + + onKey(cb) { + this.onKeyListeners.push(cb); + } + onKeyPress(e) { if (this.pressed[e.key] && this.pressed[e.key] !== 'keypress') { return; @@ -30,18 +36,32 @@ 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; + } + + let stop = false; + for (let listener of this.onKeyListeners) { + stop = stop || listener(e.key, e.ctrlKey); + if (stop) { + break; + } + } + if (stop) { + e.preventDefault(); + e.stopPropagation(); + } + } + + fromInput(e) { + return e.target instanceof HTMLInputElement || + e.target instanceof HTMLTextAreaElement || + e.target instanceof HTMLSelectElement; } } |