mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
added Shortcuts
extension
This commit is contained in:
parent
f045c38cb4
commit
5ef619b68b
@ -7,4 +7,5 @@ export * as Notifications from "./notifications"
|
|||||||
|
|
||||||
export { default as SettingsController } from "./settings"
|
export { default as SettingsController } from "./settings"
|
||||||
export { default as API } from "./api"
|
export { default as API } from "./api"
|
||||||
export { default as Debug } from "./debug"
|
export { default as Debug } from "./debug"
|
||||||
|
export { default as Shortcuts } from "./shortcuts"
|
76
packages/app/src/extensions/shortcuts/index.js
Normal file
76
packages/app/src/extensions/shortcuts/index.js
Normal 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
|
Loading…
x
Reference in New Issue
Block a user