fix(core): update

This commit is contained in:
Philipp Kunz 2022-04-21 18:01:54 +02:00
parent d58272a604
commit 6145bd66dc

View File

@ -1,3 +1,5 @@
import * as plugins from './domtools.plugins.js';
export enum Key {
Backspace = 8,
Tab = 9,
@ -123,102 +125,96 @@ export enum Key {
OpenBracket = 219,
ClosedBracket = 221,
Quote = 222
Quote = 222,
}
type KeyCombo = Array<Key>
/**
* @param {KeyboardEvent} event - pressed key event, in case of multi-key combos
* the last pressed key event is passed to this handler
*/
type Handler = (event: KeyboardEvent) => void
type KeyCombo = Array<Key>;
export class Keyboard {
private mapCombosToHandlers = new Map<number[], Handler[]>();
private mapCombosToHandlers = new Map<number[], plugins.smartrx.rxjs.Subject<KeyboardEvent>>();
private pressedKeys = new Set<Key>();
constructor(
private domNode: Element | Document
) {
this.startListening()
constructor(private domNode: Element | Document) {
this.startListening();
}
on(keys: Key[] | KeyCombo[], callback: Handler) {
const combos = this.toCombos(keys)
combos.forEach(combo => {
this.registerComboCallback(combo, callback)
})
public on(keys: Key[] | KeyCombo[]) {
const combos = this.toCombos(keys);
const subject = new plugins.smartrx.rxjs.Subject<KeyboardEvent>();
combos.forEach((combo) => {
this.registerComboCallback(combo, subject);
});
}
startListening() {
this.domNode.addEventListener('keydown', this.handleKeyDown)
this.domNode.addEventListener('keyup', this.handleKeyUp)
public startListening() {
this.domNode.addEventListener('keydown', this.handleKeyDown);
this.domNode.addEventListener('keyup', this.handleKeyUp);
}
stopListening() {
this.domNode.removeEventListener('keydown', this.handleKeyDown)
this.domNode.removeEventListener('keyup', this.handleKeyUp)
public stopListening() {
this.domNode.removeEventListener('keydown', this.handleKeyDown);
this.domNode.removeEventListener('keyup', this.handleKeyUp);
}
clear() {
this.stopListening()
this.mapCombosToHandlers.clear()
this.pressedKeys.clear()
public clear() {
this.stopListening();
this.mapCombosToHandlers.clear();
this.pressedKeys.clear();
}
private handleKeyDown = (event: KeyboardEvent) => {
this.pressedKeys.add(event.keyCode)
this.pressedKeys.add(event.keyCode);
this.mapCombosToHandlers.forEach((handlers, combo) => {
if (this.isComboPressed(combo)) {
handlers.forEach(handler => handler(event))
}
})
this.mapCombosToHandlers.forEach((subjectArg, comboArg) => {
if (this.isComboPressed(comboArg)) {
subjectArg.next(event);
}
});
};
private handleKeyUp = (event: KeyboardEvent) => {
this.pressedKeys.delete(event.keyCode)
}
this.pressedKeys.delete(event.keyCode);
};
private isComboPressed(combo: number[]) {
let result = true
let result = true;
combo.forEach(key => {
combo.forEach((key) => {
if (!this.pressedKeys.has(key)) {
result = false
result = false;
}
})
});
return result
return result;
}
private registerComboCallback(combo: Array<Key>, callback: Handler) {
if (!this.mapCombosToHandlers.has(combo)) {
this.mapCombosToHandlers.set(combo, [])
private registerComboCallback(
comboArg: Array<Key>,
subjectArg: plugins.smartrx.rxjs.Subject<KeyboardEvent>
) {
if (!this.mapCombosToHandlers.has(comboArg)) {
this.mapCombosToHandlers.set(comboArg, subjectArg);
} else {
const subject = this.mapCombosToHandlers.get(comboArg);
return subject;
}
const handlers = this.mapCombosToHandlers.get(combo)
handlers!.push(callback)
}
private toCombos(keys: KeyCombo[] | Key[]) {
if (keys.length === 0) {
return []
return [];
}
const isKeys = !Array.isArray(keys[0])
let combos: KeyCombo[] = []
const isKeys = !Array.isArray(keys[0]);
let combos: KeyCombo[] = [];
if (isKeys) {
combos = (keys as Key[]).map(key => [key])
combos = (keys as Key[]).map((key) => [key]);
} else {
combos = keys as KeyCombo[]
combos = combos.filter(combo => combo.length > 0)
combos = keys as KeyCombo[];
combos = combos.filter((combo) => combo.length > 0);
}
return combos
return combos;
}
}