Make hotkeys activate only when not using inputs

This commit is contained in:
Stanko K.R.
2025-08-18 20:23:00 +02:00
parent a580a0ddbf
commit dcffe706a3
2 changed files with 26 additions and 9 deletions
+24 -7
View File
@@ -20,15 +20,32 @@ export default class extends Controller {
this.#clearTurboFrame()
}
search() {
this.#openInTurboFrame(this.searchTurboFrameNameValue, this.searchUrlValue)
this.dialogOutlet.open()
search(event) {
if (this.#shouldExecuteHotkey) {
event.preventDefault()
this.#openInTurboFrame(this.searchTurboFrameNameValue, this.searchUrlValue)
this.dialogOutlet.open()
}
}
async ask() {
this.#initializeConversation()
this.#openInTurboFrame(this.askTurboFrameNameValue, this.askUrlValue)
this.dialogOutlet.open()
ask(event) {
if (this.#shouldExecuteHotkey) {
event.preventDefault()
this.#initializeConversation()
this.#openInTurboFrame(this.askTurboFrameNameValue, this.askUrlValue)
this.dialogOutlet.open()
}
}
get #shouldExecuteHotkey() {
const activeElement = document.activeElement
if (activeElement) {
const usingInput = activeElement.closest("input, textarea, lexical-editor")
return !usingInput
} else {
return true
}
}
#clearTurboFrame() {
+2 -2
View File
@@ -8,8 +8,8 @@
bar_ask_url_value: conversation_path,
bar_ask_turbo_frame_name_value: "conversation",
action: "
keydown.meta+k@document->bar#search:prevent keydown.ctrl+k@document->bar#search:prevent
keydown.meta+a@document->bar#ask:prevent keydown.ctrl+a@document->bar#ask:prevent"
keydown.meta+k@document->bar#search keydown.ctrl+k@document->bar#search
keydown.meta+a@document->bar#ask keydown.ctrl+a@document->bar#ask"
} do %>
<div>
Search [<%= hotkey_label(["ctrl", "K"]) %>] or Ask [<%= hotkey_label(["ctrl", "A"]) %>]