Move common things between mousehandler and kbhandler to kbmBase

This commit is contained in:
Tamius Han 2022-11-20 17:09:08 +01:00
parent 9600c4f1c9
commit 13bfd63dc2
3 changed files with 90 additions and 107 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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) {
}