added Shortcuts extension

This commit is contained in:
srgooglo 2022-03-14 22:05:32 +01:00
parent f045c38cb4
commit 5ef619b68b
2 changed files with 78 additions and 1 deletions

View File

@ -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"
export { default as Debug } from "./debug"
export { default as Shortcuts } from "./shortcuts"

View File

@ -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