From 13bfd63dc2dfdda11dc9256d9d8f3d2ada69030b Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sun, 20 Nov 2022 17:09:08 +0100 Subject: [PATCH] Move common things between mousehandler and kbhandler to kbmBase --- src/ext/lib/kbm/KbmBase.ts | 104 +++++++++++++++++++++----- src/ext/lib/kbm/KeyboardHandler.ts | 76 +------------------ src/ext/lib/kbm/PlayerMouseHandler.ts | 17 +---- 3 files changed, 90 insertions(+), 107 deletions(-) diff --git a/src/ext/lib/kbm/KbmBase.ts b/src/ext/lib/kbm/KbmBase.ts index 9aed36d..2aa4821 100644 --- a/src/ext/lib/kbm/KbmBase.ts +++ b/src/ext/lib/kbm/KbmBase.ts @@ -3,33 +3,103 @@ import Logger from '../Logger'; import Settings from '../Settings'; export class KbmBase { + listenFor: string[] = []; logger: Logger; settings: Settings; eventBus: EventBus; - // eventBusCommands: { [x: string]: EventBusCommand } = { - // 'kbm-enable': { - // function: () => this.enable() - // }, - // 'kbm-disable': { - // function: () => this.disable() - // }, - // 'kbm-set-config': { - // function: (data: {config: any, temporary?: boolean}) => this.setConfig(data.config, data.temporary), - // }, - // 'uw-enable': { - // function: () => this.load() - // }, - // 'uw-disable': { - // function: () => this.disable() - // }, - // } + eventBusCommands: { [x: string]: EventBusCommand } = { + 'kbm-enable': { + function: () => this.enable() + }, + 'kbm-disable': { + function: () => this.disable() + }, + 'kbm-set-config': { + function: (data: {config: any, temporary?: boolean}) => this.setConfig(data.config, data.temporary), + }, + 'uw-enable': { + function: () => this.load() + }, + 'uw-disable': { + function: () => this.disable() + }, + } constructor(eventBus: EventBus, settings: Settings, logger: Logger) { this.logger = logger; this.settings = settings; this.eventBus = eventBus; } + + /** + * Enables KeyboardHandler + */ + enable() { + this.load(); + } + + /** + * Disables KeyboardHandler + */ + disable() { + this.removeListener(); + } + + /** + * Sets configuration parameter for KeyboardHandler + * @param config + */ + setConfig(config, temporary = false) { + if (temporary) { + for (const confKey in config) { + switch (confKey) { + case 'enabled': + config[confKey] ? this.enable() : this.disable(); + break; + } + } + return; + } + + for (const confKey in config) { + this.settings.active.kbm[confKey] = config[confKey]; + } + + this.settings.save(); + this.load(); + } + + // convenience methods + addListener() { + // events should be handled in handleEvent function. We need to do things this + // way, otherwise we can't remove event listener + // https://stackoverflow.com/a/19507086 + + for (const ev of this.listenFor) { + if (ev.startsWith('key') ? this.settings.active.kbm.keyboardEnabled : this.settings.active.kbm.mouseEnabled) { + document.addEventListener(ev, this); + } + } + } + + removeListener() { + for (const ev of this.listenFor) { + document.removeEventListener(ev, this); + } + } + + load() { + if (! (this.settings.isEnabledForSite() && this.settings.active.kbm.enabled)) { + return; + } + this.addListener(); + } + + handleEvent(event) { + console.error('[KbmBase::handleEvent] — IF YOU SEE THIS, THEN YOU KINDA FORGOT TO DEFINE A FUNCTION. Classes that extend KbmBase should also override this function.'); + throw "KBM_BASE::HANDLE_EVENT - OVERRIDE_ME_PLS"; + } } export default KbmBase; diff --git a/src/ext/lib/kbm/KeyboardHandler.ts b/src/ext/lib/kbm/KeyboardHandler.ts index e3a39b9..376a788 100644 --- a/src/ext/lib/kbm/KeyboardHandler.ts +++ b/src/ext/lib/kbm/KeyboardHandler.ts @@ -21,6 +21,7 @@ if(process.env.CHANNEL !== 'stable'){ * kbm-set-config sets configuration for this module. */ export class KeyboardHandler extends KbmBase { + listenFor: string[] = ['keyup']; logger: Logger; settings: Settings; eventBus: EventBus; @@ -38,21 +39,7 @@ export class KeyboardHandler extends KbmBase { keypressActions: any[] = []; eventBusCommands: { [x: string]: EventBusCommand } = { - 'kbm-enable': { - function: () => this.enable() - }, - 'kbm-disable': { - function: () => this.disable() - }, - 'kbm-set-config': { - function: (data: {config: any, temporary?: boolean}) => this.setConfig(data.config, data.temporary), - }, - 'uw-enable': { - function: () => this.load() - }, - 'uw-disable': { - function: () => this.disable() - }, + } //#region lifecycle @@ -77,31 +64,10 @@ export class KeyboardHandler extends KbmBase { this.load(); } - load() { - if (! (this.settings.isEnabledForSite() && this.settings.active.kbm.enabled)) { - return; - } - this.addListener(); - } - destroy() { this.removeListener(); } - // convenience methods - addListener() { - // events should be handled in handleEvent function. We need to do things this - // way, otherwise we can't remove event listener - // https://stackoverflow.com/a/19507086 - - if (this.settings.active.kbm.keyboardEnabled) { - document.addEventListener('keyup', this ); - } - } - removeListener() { - document.removeEventListener('keyup', this); - } - /** * Adds listeners for mouse events, for all player elements associated with the KeyboardHandler instance. * @param element @@ -135,44 +101,6 @@ export class KeyboardHandler extends KbmBase { } } - /** - * Enables KeyboardHandler - */ - enable() { - this.load(); - } - - /** - * Disables KeyboardHandler - */ - disable() { - this.removeListener(); - } - - /** - * Sets configuration parameter for KeyboardHandler - * @param config - */ - setConfig(config, temporary = false) { - if (temporary) { - for (const confKey in config) { - switch (confKey) { - case 'enabled': - config[confKey] ? this.enable() : this.disable(); - break; - } - } - return; - } - - for (const confKey in config) { - this.settings.active.kbm[confKey] = config[confKey]; - } - - this.settings.save(); - this.load(); - } - setKeyboardLocal(state) { if (state === ExtensionMode.Enabled) { this.keyboardLocalDisabled = false; diff --git a/src/ext/lib/kbm/PlayerMouseHandler.ts b/src/ext/lib/kbm/PlayerMouseHandler.ts index e7c4bf6..2f7cdd8 100644 --- a/src/ext/lib/kbm/PlayerMouseHandler.ts +++ b/src/ext/lib/kbm/PlayerMouseHandler.ts @@ -12,6 +12,7 @@ if(process.env.CHANNEL !== 'stable'){ * Handles keypress */ export class MouseHandler extends KbmBase { + listenFor: string[] = ['mousemove']; playerElement: HTMLElement; @@ -64,16 +65,6 @@ export class MouseHandler extends KbmBase { //#endregion //#region listener setup, teardown, handling - private addListener() { - if (this.settings.active.kbm.enabled && this.settings.active.kbm.mouseEnabled) { - this.playerElement.addEventListener('mousemove', this); - } - } - - private removeListener() { - this.playerElement.removeEventListener('mousemove', this); - } - handleEvent(event: MouseEvent) { switch (event.type) { case 'mousemove': @@ -90,12 +81,6 @@ export class MouseHandler extends KbmBase { this.removeListener(); } - private setConfig(config, isTemporary?) { - - } - - - private handleMouseMove(event: MouseEvent) { }