diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index e0e09c24..bf7cad54 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -2,7 +2,8 @@ import { Plugin, WorkspaceLeaf } from 'obsidian'; import { KhojSetting, KhojSettingTab, DEFAULT_SETTINGS } from 'src/settings' import { KhojSearchModal } from 'src/search_modal' import { KhojChatView } from 'src/chat_view' -import { updateContentIndex, canConnectToBackend, KhojView } from './utils'; +import { updateContentIndex, canConnectToBackend, KhojView, jumpToPreviousView } from './utils'; +import { KhojPaneView } from './pane_view'; export default class Khoj extends Plugin { @@ -89,15 +90,20 @@ export default class Khoj extends Plugin { } 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(); - } + const activeKhojLeaf = workspace.getActiveViewOfType(KhojPaneView)?.leaf; + // Jump to the previous view if the current view is Khoj Side Pane + if (activeKhojLeaf === leaf) jumpToPreviousView(); + // Else Reveal the leaf in case it is in a collapsed sidebar + else { + workspace.revealLeaf(leaf); - // "Reveal" the leaf in case it is in a collapsed sidebar - workspace.revealLeaf(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(); + } + } } } } diff --git a/src/interface/obsidian/src/utils.ts b/src/interface/obsidian/src/utils.ts index 5f3e5acb..4a969793 100644 --- a/src/interface/obsidian/src/utils.ts +++ b/src/interface/obsidian/src/utils.ts @@ -333,6 +333,12 @@ export function createCopyParentText(message: string, originalButton: string = ' } } +export function jumpToPreviousView() { + const editor: Editor = this.app.workspace.getActiveFileView()?.editor + if (!editor) return; + editor.focus(); +} + export function pasteTextAtCursor(text: string | undefined) { // Get the current active file's editor const editor: Editor = this.app.workspace.getActiveFileView()?.editor