Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
ca52d06c60 | |||
d33366c487 | |||
3bc5e1d0e2 | |||
96a88112dc | |||
6c0c1e165f | |||
653a4138a9 | |||
d776843494 | |||
79e64c4cc2 |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@designestate/dees-domtools",
|
"name": "@designestate/dees-domtools",
|
||||||
"version": "2.0.14",
|
"version": "2.0.18",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@designestate/dees-domtools",
|
"name": "@designestate/dees-domtools",
|
||||||
"version": "2.0.14",
|
"version": "2.0.18",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apiglobal/typedrequest": "^2.0.3",
|
"@apiglobal/typedrequest": "^2.0.3",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@designestate/dees-domtools",
|
"name": "@designestate/dees-domtools",
|
||||||
"version": "2.0.14",
|
"version": "2.0.18",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "tools to simplify complex css structures",
|
"description": "tools to simplify complex css structures",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
@ -146,6 +146,23 @@ export class DomTools {
|
|||||||
this.elements.headElement.appendChild(styleElement);
|
this.elements.headElement.appendChild(styleElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* allows to set global styles
|
||||||
|
* @param stylesText the css text you want to set
|
||||||
|
*/
|
||||||
|
public async setExternalScript(scriptLinkArg: string) {
|
||||||
|
await this.domReady.promise;
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
|
const script = document.createElement('script')
|
||||||
|
script.src = scriptLinkArg;
|
||||||
|
script.addEventListener('load', function() {
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
|
const parentNode = document.head || document.body;
|
||||||
|
parentNode.append(script);
|
||||||
|
await done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* allows setting external css files
|
* allows setting external css files
|
||||||
* @param cssLinkArg a url to an external stylesheet
|
* @param cssLinkArg a url to an external stylesheet
|
||||||
|
@ -128,8 +128,6 @@ export enum Key {
|
|||||||
Quote = 222,
|
Quote = 222,
|
||||||
}
|
}
|
||||||
|
|
||||||
type KeyCombo = Array<Key>;
|
|
||||||
|
|
||||||
export class Keyboard {
|
export class Keyboard {
|
||||||
private mapCombosToHandlers = new Map<number[], plugins.smartrx.rxjs.Subject<KeyboardEvent>>();
|
private mapCombosToHandlers = new Map<number[], plugins.smartrx.rxjs.Subject<KeyboardEvent>>();
|
||||||
private pressedKeys = new Set<Key>();
|
private pressedKeys = new Set<Key>();
|
||||||
@ -140,12 +138,9 @@ export class Keyboard {
|
|||||||
|
|
||||||
public keyEnum = Key;
|
public keyEnum = Key;
|
||||||
|
|
||||||
public on(keys: Key[] | KeyCombo[]) {
|
public on(keys: Key[]) {
|
||||||
const combos = this.toCombos(keys);
|
|
||||||
const subject = new plugins.smartrx.rxjs.Subject<KeyboardEvent>();
|
const subject = new plugins.smartrx.rxjs.Subject<KeyboardEvent>();
|
||||||
combos.forEach((combo) => {
|
this.registerKeys(keys, subject);
|
||||||
this.registerComboCallback(combo, subject);
|
|
||||||
});
|
|
||||||
return subject;
|
return subject;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,8 +163,8 @@ export class Keyboard {
|
|||||||
private handleKeyDown = (event: KeyboardEvent) => {
|
private handleKeyDown = (event: KeyboardEvent) => {
|
||||||
this.pressedKeys.add(event.keyCode);
|
this.pressedKeys.add(event.keyCode);
|
||||||
|
|
||||||
this.mapCombosToHandlers.forEach((subjectArg, comboArg) => {
|
this.mapCombosToHandlers.forEach((subjectArg, keysArg) => {
|
||||||
if (this.isComboPressed(comboArg)) {
|
if (this.areAllKeysPressed(keysArg)) {
|
||||||
subjectArg.next(event);
|
subjectArg.next(event);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -179,10 +174,10 @@ export class Keyboard {
|
|||||||
this.pressedKeys.delete(event.keyCode);
|
this.pressedKeys.delete(event.keyCode);
|
||||||
};
|
};
|
||||||
|
|
||||||
private isComboPressed(combo: number[]) {
|
private areAllKeysPressed(keysArg: Key[]) {
|
||||||
let result = true;
|
let result = true;
|
||||||
|
|
||||||
combo.forEach((key) => {
|
keysArg.forEach((key) => {
|
||||||
if (!this.pressedKeys.has(key)) {
|
if (!this.pressedKeys.has(key)) {
|
||||||
result = false;
|
result = false;
|
||||||
}
|
}
|
||||||
@ -191,33 +186,15 @@ export class Keyboard {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerComboCallback(
|
private registerKeys(
|
||||||
comboArg: Array<Key>,
|
keysArg: Array<Key>,
|
||||||
subjectArg: plugins.smartrx.rxjs.Subject<KeyboardEvent>
|
subjectArg: plugins.smartrx.rxjs.Subject<KeyboardEvent>
|
||||||
) {
|
) {
|
||||||
if (!this.mapCombosToHandlers.has(comboArg)) {
|
if (!this.mapCombosToHandlers.has(keysArg)) {
|
||||||
this.mapCombosToHandlers.set(comboArg, subjectArg);
|
this.mapCombosToHandlers.set(keysArg, subjectArg);
|
||||||
} else {
|
} else {
|
||||||
const subject = this.mapCombosToHandlers.get(comboArg);
|
const subject = this.mapCombosToHandlers.get(keysArg);
|
||||||
return subject;
|
return subject;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private toCombos(keys: KeyCombo[] | Key[]) {
|
|
||||||
if (keys.length === 0) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const isKeys = !Array.isArray(keys[0]);
|
|
||||||
let combos: KeyCombo[] = [];
|
|
||||||
|
|
||||||
if (isKeys) {
|
|
||||||
combos = (keys as Key[]).map((key) => [key]);
|
|
||||||
} else {
|
|
||||||
combos = keys as KeyCombo[];
|
|
||||||
combos = combos.filter((combo) => combo.length > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return combos;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user