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'; import Settings from '../Settings';
export class KbmBase { export class KbmBase {
listenFor: string[] = [];
logger: Logger; logger: Logger;
settings: Settings; settings: Settings;
eventBus: EventBus; eventBus: EventBus;
// eventBusCommands: { [x: string]: EventBusCommand } = { eventBusCommands: { [x: string]: EventBusCommand } = {
// 'kbm-enable': { 'kbm-enable': {
// function: () => this.enable() function: () => this.enable()
// }, },
// 'kbm-disable': { 'kbm-disable': {
// function: () => this.disable() function: () => this.disable()
// }, },
// 'kbm-set-config': { 'kbm-set-config': {
// function: (data: {config: any, temporary?: boolean}) => this.setConfig(data.config, data.temporary), function: (data: {config: any, temporary?: boolean}) => this.setConfig(data.config, data.temporary),
// }, },
// 'uw-enable': { 'uw-enable': {
// function: () => this.load() function: () => this.load()
// }, },
// 'uw-disable': { 'uw-disable': {
// function: () => this.disable() function: () => this.disable()
// }, },
// } }
constructor(eventBus: EventBus, settings: Settings, logger: Logger) { constructor(eventBus: EventBus, settings: Settings, logger: Logger) {
this.logger = logger; this.logger = logger;
this.settings = settings; this.settings = settings;
this.eventBus = eventBus; 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; export default KbmBase;

View File

@ -21,6 +21,7 @@ if(process.env.CHANNEL !== 'stable'){
* kbm-set-config sets configuration for this module. * kbm-set-config sets configuration for this module.
*/ */
export class KeyboardHandler extends KbmBase { export class KeyboardHandler extends KbmBase {
listenFor: string[] = ['keyup'];
logger: Logger; logger: Logger;
settings: Settings; settings: Settings;
eventBus: EventBus; eventBus: EventBus;
@ -38,21 +39,7 @@ export class KeyboardHandler extends KbmBase {
keypressActions: any[] = []; keypressActions: any[] = [];
eventBusCommands: { [x: string]: EventBusCommand } = { 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 //#region lifecycle
@ -77,31 +64,10 @@ export class KeyboardHandler extends KbmBase {
this.load(); this.load();
} }
load() {
if (! (this.settings.isEnabledForSite() && this.settings.active.kbm.enabled)) {
return;
}
this.addListener();
}
destroy() { destroy() {
this.removeListener(); 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. * Adds listeners for mouse events, for all player elements associated with the KeyboardHandler instance.
* @param element * @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) { setKeyboardLocal(state) {
if (state === ExtensionMode.Enabled) { if (state === ExtensionMode.Enabled) {
this.keyboardLocalDisabled = false; this.keyboardLocalDisabled = false;

View File

@ -12,6 +12,7 @@ if(process.env.CHANNEL !== 'stable'){
* Handles keypress * Handles keypress
*/ */
export class MouseHandler extends KbmBase { export class MouseHandler extends KbmBase {
listenFor: string[] = ['mousemove'];
playerElement: HTMLElement; playerElement: HTMLElement;
@ -64,16 +65,6 @@ export class MouseHandler extends KbmBase {
//#endregion //#endregion
//#region listener setup, teardown, handling //#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) { handleEvent(event: MouseEvent) {
switch (event.type) { switch (event.type) {
case 'mousemove': case 'mousemove':
@ -90,12 +81,6 @@ export class MouseHandler extends KbmBase {
this.removeListener(); this.removeListener();
} }
private setConfig(config, isTemporary?) {
}
private handleMouseMove(event: MouseEvent) { private handleMouseMove(event: MouseEvent) {
} }