From fbb95ca342803352c7db0f98b7934df86f99092d Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 26 Jun 2024 18:10:43 +0530 Subject: [PATCH] Put cursor on chat input when focus on chat view in Obsidian This should improve fluidity of keyboard interactions with Khoj on Obsidian. Open Khoj chat view via keybinding or command pallete and ask question using only the keyboard, with no mouse clicks required --- src/interface/obsidian/src/main.ts | 13 +++++++++++-- src/interface/obsidian/src/pane_view.ts | 12 ++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index d7178f78..e0e09c24 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -88,7 +88,16 @@ export default class Khoj extends Plugin { await leaf?.setViewState({ type: viewType, active: true }); } - // "Reveal" the leaf in case it is in a collapsed sidebar - if (leaf) workspace.revealLeaf(leaf); + if (leaf) { + if (viewType === KhojView.CHAT) { + // focus on the chat input when the chat view is opened + let chatView = leaf.view as KhojChatView; + let chatInput = chatView.contentEl.getElementsByClassName("khoj-chat-input")[0]; + if (chatInput) chatInput.focus(); + } + + // "Reveal" the leaf in case it is in a collapsed sidebar + workspace.revealLeaf(leaf); + } } } diff --git a/src/interface/obsidian/src/pane_view.ts b/src/interface/obsidian/src/pane_view.ts index 74afacab..64a167dd 100644 --- a/src/interface/obsidian/src/pane_view.ts +++ b/src/interface/obsidian/src/pane_view.ts @@ -47,7 +47,15 @@ export abstract class KhojPaneView extends ItemView { await leaf?.setViewState({ type: viewType, active: true }); } - // "Reveal" the leaf in case it is in a collapsed sidebar - if (leaf) workspace.revealLeaf(leaf); + if (leaf) { + if (viewType === KhojView.CHAT) { + // focus on the chat input when the chat view is opened + let chatInput = this.contentEl.getElementsByClassName("khoj-chat-input")[0]; + if (chatInput) chatInput.focus(); + } + + // "Reveal" the leaf in case it is in a collapsed sidebar + workspace.revealLeaf(leaf); + } } }