From af82d729ca1fbf5c67148f5e57197ea1f511e82e Mon Sep 17 00:00:00 2001 From: srgooglo Date: Tue, 11 Oct 2022 15:37:05 +0200 Subject: [PATCH] reimplement Shortcuts register --- packages/app/src/cores/shortcuts/index.js | 113 +++++++++++++--------- 1 file changed, 68 insertions(+), 45 deletions(-) diff --git a/packages/app/src/cores/shortcuts/index.js b/packages/app/src/cores/shortcuts/index.js index 66790b27..e4902163 100644 --- a/packages/app/src/cores/shortcuts/index.js +++ b/packages/app/src/cores/shortcuts/index.js @@ -1,71 +1,94 @@ import Core from "evite/src/core" export default class ShortcutsCore extends Core { - shortcuts = {} + shortcutsRegister = [] publicMethods = { shortcuts: this } - initialize() { - document.addEventListener("keydown", this.handleEvent) - } + handleEvent = (event, shortcut, fn) => { + if (typeof shortcut !== "object") { + throw new Error("Shortcut must be an object") + } - handleEvent = (event) => { - // FIXME: event.key sometimes is not defined - //event.key = event.key.toLowerCase() + // check the event key pressed + if (event.key !== shortcut.key) { + return + } - const shortcut = this.shortcuts[event.key] + if (typeof shortcut.ctrl === "boolean" && event.ctrlKey !== shortcut.ctrl) { + return + } - if (shortcut) { - if (typeof shortcut.ctrl === "boolean" && event.ctrlKey !== shortcut.ctrl) { - return - } + if (typeof shortcut.shift === "boolean" && event.shiftKey !== shortcut.shift) { + return + } - if (typeof shortcut.shift === "boolean" && event.shiftKey !== shortcut.shift) { - return - } + if (typeof shortcut.alt === "boolean" && event.altKey !== shortcut.alt) { + return + } - if (typeof shortcut.alt === "boolean" && event.altKey !== shortcut.alt) { - return - } + if (typeof shortcut.meta === "boolean" && event.metaKey !== shortcut.meta) { + return + } - if (typeof shortcut.meta === "boolean" && event.metaKey !== shortcut.meta) { - return - } + if (shortcut.preventDefault) { + event.preventDefault() + } - if (shortcut.preventDefault) { - event.preventDefault() - } - - if (typeof shortcut.fn === "function") { - shortcut.fn() - } + if (typeof fn === "function") { + fn() } } - register = (keybind = {}, fn) => { - if (typeof keybind === "string") { - keybind = { - key: keybind, - } + register = (shortcut, fn) => { + if (!shortcut) { + throw new Error("`shortcut` is required") } - - this.shortcuts[keybind.key] = { - ...keybind, - fn, - } - } - - remove = (array) => { - if (typeof array === "string") { - array = [array] + if (typeof shortcut !== "object") { + throw new Error("Shortcut must be an object") } - array.forEach(key => { - delete this.shortcuts[key] + if (!fn) { + throw new Error("`fn` is required") + } + + const handler = (event) => this.handleEvent(event, shortcut, fn) + + this.shortcutsRegister.push({ + id: shortcut.id, + handler: handler }) + + return document.addEventListener("keydown", handler) + } + + remove = (id) => { + if (!id) { + throw new Error("`id` is required") + } + + if (typeof id !== "string") { + throw new Error("`id` must be a string") + } + + // search the event handler + const register = this.shortcutsRegister.find((handler) => handler.id === id) + + if (!register) { + console.warn(`Shortcut with id "${id}" not found`) + return false + } + + console.log(register) + + // remove the event handler + document.removeEventListener("keydown", register.handler) + + // remove the event handler from the list + this.shortcutsRegister = this.shortcutsRegister.filter((handler) => handler.id !== id) } window = {