mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
reimplement Shortcuts register
This commit is contained in:
parent
1e5da1d6d7
commit
af82d729ca
@ -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 = {
|
||||
|
Loading…
x
Reference in New Issue
Block a user