diff options
Diffstat (limited to 'src/content/components')
-rw-r--r-- | src/content/components/common/index.ts | 4 | ||||
-rw-r--r-- | src/content/components/common/input.ts | 80 |
2 files changed, 2 insertions, 82 deletions
diff --git a/src/content/components/common/index.ts b/src/content/components/common/index.ts index b2f48a3..c74020e 100644 --- a/src/content/components/common/index.ts +++ b/src/content/components/common/index.ts @@ -1,4 +1,4 @@ -import InputComponent from './input'; +import InputDriver from './../../InputDriver'; import FollowComponent from './follow'; import MarkComponent from './mark'; import KeymapperComponent from './keymapper'; @@ -15,7 +15,7 @@ let settingUseCase = new SettingUseCase(); export default class Common { constructor(win: Window, store: any) { - const input = new InputComponent(win.document.body); + const input = new InputDriver(win.document.body); const follow = new FollowComponent(); const mark = new MarkComponent(store); const keymapper = new KeymapperComponent(store); diff --git a/src/content/components/common/input.ts b/src/content/components/common/input.ts deleted file mode 100644 index 1fe34c9..0000000 --- a/src/content/components/common/input.ts +++ /dev/null @@ -1,80 +0,0 @@ -import * as dom from '../../../shared/utils/dom'; -import * as keys from '../../../shared/utils/keys'; - -const cancelKey = (e: KeyboardEvent): boolean => { - return e.key === 'Escape' || e.key === '[' && e.ctrlKey; -}; - -export default class InputComponent { - private pressed: {[key: string]: string} = {}; - - private onKeyListeners: ((key: keys.Key) => boolean)[] = []; - - constructor(target: HTMLElement) { - 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)); - } - - onKey(cb: (key: keys.Key) => boolean) { - this.onKeyListeners.push(cb); - } - - onKeyPress(e: KeyboardEvent) { - if (this.pressed[e.key] && this.pressed[e.key] !== 'keypress') { - return; - } - this.pressed[e.key] = 'keypress'; - this.capture(e); - } - - onKeyDown(e: KeyboardEvent) { - if (this.pressed[e.key] && this.pressed[e.key] !== 'keydown') { - return; - } - this.pressed[e.key] = 'keydown'; - this.capture(e); - } - - onKeyUp(e: KeyboardEvent) { - delete this.pressed[e.key]; - } - - // eslint-disable-next-line max-statements - capture(e: KeyboardEvent) { - let target = e.target; - if (!(target instanceof HTMLElement)) { - return; - } - if (this.fromInput(target)) { - if (cancelKey(e) && target.blur) { - target.blur(); - } - return; - } - if (['Shift', 'Control', 'Alt', 'OS'].includes(e.key)) { - // pressing only meta key is ignored - return; - } - - let key = keys.fromKeyboardEvent(e); - for (let listener of this.onKeyListeners) { - let stop = listener(key); - if (stop) { - e.preventDefault(); - e.stopPropagation(); - break; - } - } - } - - fromInput(e: Element) { - return e instanceof HTMLInputElement || - e instanceof HTMLTextAreaElement || - e instanceof HTMLSelectElement || - dom.isContentEditable(e); - } -} |