fix: remove unused focus listeners in keybindings
This commit is contained in:
@@ -10,13 +10,6 @@ import { isDOMElement, isTypableElement } from "./utils/dom"
|
|||||||
*/
|
*/
|
||||||
let keybindingsEnabled = true
|
let keybindingsEnabled = true
|
||||||
|
|
||||||
/**
|
|
||||||
* This variable keeps track whether the currently focused element on the document
|
|
||||||
* is something that accepts a keyboard input
|
|
||||||
* (this is to prevent single character shortcuts from firing while typing)
|
|
||||||
*/
|
|
||||||
let focusNotTypable = true
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alt is also regarded as macOS OPTION (⌥) key
|
* Alt is also regarded as macOS OPTION (⌥) key
|
||||||
* Ctrl is also regarded as macOS COMMAND (⌘) key (NOTE: this differs from HTML Keyboard spec where COMMAND is Meta key!)
|
* Ctrl is also regarded as macOS COMMAND (⌘) key (NOTE: this differs from HTML Keyboard spec where COMMAND is Meta key!)
|
||||||
@@ -62,35 +55,13 @@ export const bindings: {
|
|||||||
export function hookKeybindingsListener() {
|
export function hookKeybindingsListener() {
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
document.addEventListener("keydown", handleKeyDown)
|
document.addEventListener("keydown", handleKeyDown)
|
||||||
document.addEventListener("focusin", handleFocusUpdate)
|
|
||||||
document.addEventListener("focusout", handleFocusUpdate)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
document.removeEventListener("keydown", handleKeyDown)
|
document.removeEventListener("keydown", handleKeyDown)
|
||||||
document.removeEventListener("focusin", handleFocusUpdate)
|
|
||||||
document.removeEventListener("focusout", handleFocusUpdate)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleFocusUpdate(ev: FocusEvent) {
|
|
||||||
const target = ev.target
|
|
||||||
|
|
||||||
if (isDOMElement(target) && isTypableElement(target)) {
|
|
||||||
if (focusNotTypable) {
|
|
||||||
console.log(
|
|
||||||
"Single Char keybindings are disabled because typable element is having focus"
|
|
||||||
)
|
|
||||||
focusNotTypable = false
|
|
||||||
}
|
|
||||||
} else if (!focusNotTypable) {
|
|
||||||
console.log(
|
|
||||||
"Single Char keybindings are restored because typable element is no longer focused"
|
|
||||||
)
|
|
||||||
focusNotTypable = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleKeyDown(ev: KeyboardEvent) {
|
function handleKeyDown(ev: KeyboardEvent) {
|
||||||
// Do not check keybinds if the mode is disabled
|
// Do not check keybinds if the mode is disabled
|
||||||
if (!keybindingsEnabled) return
|
if (!keybindingsEnabled) return
|
||||||
|
|||||||
Reference in New Issue
Block a user