Major controls rework

This commit is contained in:
Davide Passoni
2024-11-21 17:35:51 +01:00
parent 73c8ce2fe7
commit 402a1457aa
25 changed files with 1001 additions and 506 deletions

View File

@@ -11,33 +11,31 @@ export class Shortcut {
this.#id = id;
this.#options = options;
AppStateChangedEvent.on(() => this.#keydown = false)
/* Key up event is mandatory */
AppStateChangedEvent.on(() => (this.#keydown = false));
/* On keyup, it is enough to check the code only, not the entire combination */
document.addEventListener("keyup", (ev: any) => {
this.#keydown = false;
if (keyEventWasInInput(ev) || options.code !== ev.code) return;
if (
ev.altKey === (options.altKey ?? ev.code.indexOf("Alt") >= 0) &&
ev.ctrlKey === (options.ctrlKey ?? ev.code.indexOf("Ctrl") >= 0) &&
ev.shiftKey === (options.shiftKey ?? ev.code.indexOf("Shift") >= 0)
)
if (this.#keydown && options.code === ev.code) {
ev.preventDefault();
options.keyUpCallback(ev);
this.#keydown = false;
}
});
/* Key down event is optional */
if (options.keyDownCallback) {
document.addEventListener("keydown", (ev: any) => {
if (this.#keydown || keyEventWasInInput(ev) || options.code !== ev.code) return;
/* On keydown, check exactly if the requested key combination is being pressed */
document.addEventListener("keydown", (ev: any) => {
if (
!(this.#keydown || keyEventWasInInput(ev) || options.code !== ev.code) &&
(options.altKey === undefined || ev.altKey === (options.altKey ?? ev.code.indexOf("Alt") >= 0)) &&
(options.ctrlKey === undefined || ev.ctrlKey === (options.ctrlKey ?? ev.code.indexOf("Control") >= 0)) &&
(options.shiftKey === undefined || ev.shiftKey === (options.shiftKey ?? ev.code.indexOf("Shift") >= 0))
) {
ev.preventDefault();
this.#keydown = true;
if (
ev.altKey === (options.altKey ?? ev.code.indexOf("Alt") >= 0) &&
ev.ctrlKey === (options.ctrlKey ?? ev.code.indexOf("Control") >= 0) &&
ev.shiftKey === (options.shiftKey ?? ev.code.indexOf("Shift") >= 0)
)
if (options.keyDownCallback) options.keyDownCallback(ev);
});
}
if (options.keyDownCallback) options.keyDownCallback(ev); /* Key down event is optional */
}
});
}
getOptions() {
@@ -51,4 +49,19 @@ export class Shortcut {
getId() {
return this.#id;
}
toActions() {
let actions: string[] = [];
if (this.getOptions().shiftKey) actions.push("Shift");
if (this.getOptions().altKey) actions.push("Alt");
if (this.getOptions().ctrlKey) actions.push("Ctrl")
actions.push(this.getOptions().code.replace("Key", "")
.replace("ControlLeft", "Left Ctrl")
.replace("AltLeft", "Left Alt")
.replace("ShiftLeft", "Left Shift")
.replace("ControlRight", "Right Ctrl")
.replace("AltRight", "Right Alt")
.replace("ShiftRight", "Right Shift"))
return actions
}
}

View File

@@ -5,14 +5,7 @@ import { Shortcut } from "./shortcut";
export class ShortcutManager {
#shortcuts: { [key: string]: Shortcut } = {};
constructor() {
// Stop ctrl+digits from sending the browser to another tab
document.addEventListener("keydown", (ev: KeyboardEvent) => {
if (ev.code.indexOf("Digit") >= 0 && ev.ctrlKey === true && ev.altKey === false && ev.shiftKey === false) {
ev.preventDefault();
}
});
}
constructor() {}
addShortcut(id: string, shortcutOptions: ShortcutOptions) {
this.#shortcuts[id] = new Shortcut(id, shortcutOptions);
@@ -20,6 +13,14 @@ export class ShortcutManager {
return this;
}
getShortcut(id) {
return this.#shortcuts[id];
}
getShortcuts() {
return this.#shortcuts;
}
getShortcutsOptions() {
let shortcutsOptions = {};
for (let id in this.#shortcuts) {
@@ -48,11 +49,18 @@ export class ShortcutManager {
const otherShortcut = this.#shortcuts[otherid];
if (shortcut.getOptions().code === otherShortcut.getOptions().code) {
if (
(shortcut.getOptions().altKey ?? false) === (otherShortcut.getOptions().altKey ?? false) &&
(shortcut.getOptions().ctrlKey ?? false) === (otherShortcut.getOptions().ctrlKey ?? false) &&
(shortcut.getOptions().shiftKey ?? false) === (otherShortcut.getOptions().shiftKey ?? false)
shortcut.getOptions().code === otherShortcut.getOptions().code &&
((shortcut.getOptions().shiftKey === undefined && otherShortcut.getOptions().shiftKey !== undefined) ||
(shortcut.getOptions().shiftKey !== undefined && otherShortcut.getOptions().shiftKey === undefined) ||
shortcut.getOptions().shiftKey === otherShortcut.getOptions().shiftKey) &&
((shortcut.getOptions().altKey === undefined && otherShortcut.getOptions().altKey !== undefined) ||
(shortcut.getOptions().altKey !== undefined && otherShortcut.getOptions().altKey === undefined) ||
shortcut.getOptions().altKey === otherShortcut.getOptions().altKey) &&
((shortcut.getOptions().ctrlKey === undefined && otherShortcut.getOptions().ctrlKey !== undefined) ||
(shortcut.getOptions().ctrlKey !== undefined && otherShortcut.getOptions().ctrlKey === undefined) ||
shortcut.getOptions().ctrlKey === otherShortcut.getOptions().ctrlKey)
) {
console.error("Duplicate shortcut: " + shortcut.getOptions().label + " and " + otherShortcut.getOptions().label)
console.error("Duplicate shortcut: " + shortcut.getOptions().label + " and " + otherShortcut.getOptions().label);
}
}
}