diff --git a/packages/app/src/extensions/index.js b/packages/app/src/extensions/index.js index 0db8f5d1..93efe2eb 100644 --- a/packages/app/src/extensions/index.js +++ b/packages/app/src/extensions/index.js @@ -7,4 +7,5 @@ export * as Notifications from "./notifications" export { default as SettingsController } from "./settings" export { default as API } from "./api" -export { default as Debug } from "./debug" \ No newline at end of file +export { default as Debug } from "./debug" +export { default as Shortcuts } from "./shortcuts" \ No newline at end of file diff --git a/packages/app/src/extensions/shortcuts/index.js b/packages/app/src/extensions/shortcuts/index.js new file mode 100644 index 00000000..f3814e9f --- /dev/null +++ b/packages/app/src/extensions/shortcuts/index.js @@ -0,0 +1,76 @@ +export class ShortcutsController { + constructor() { + this.shortcuts = {} + + document.addEventListener("keydown", (event) => { + const key = event.key.toLowerCase() + + const shortcut = this.shortcuts[key] + + if (shortcut) { + event.preventDefault() + + if (typeof shortcut.ctrl === "boolean" && event.ctrlKey !== shortcut.ctrl) { + return + } + + if (typeof shortcut.shift === "boolean" && event.shiftKey !== shortcut.shift) { + return + } + + if (typeof shortcut.alt === "boolean" && event.altKey !== shortcut.alt) { + return + } + + if (typeof shortcut.meta === "boolean" && event.metaKey !== shortcut.meta) { + return + } + + if (typeof shortcut.fn === "function") { + shortcut.fn() + } + } + }) + } + + register = (keybind = {}, fn) => { + if (typeof keybind === "string") { + keybind = { + key: keybind, + } + } + + + this.shortcuts[keybind.key] = { + ...keybind, + fn, + } + } + + remove = (array) => { + if (typeof array === "string") { + array = [array] + } + + array.forEach(key => { + delete this.shortcuts[key] + }) + } +} + +export const extension = { + key: "shortcuts", + expose: [ + { + initialization: [ + (app, main) => { + app.ShortcutsController = new ShortcutsController() + + main.setToWindowContext("ShortcutsController", app.ShortcutsController) + } + ], + }, + ] +} + +export default extension \ No newline at end of file