diff --git a/src/interface/obsidian/src/search_modal.ts b/src/interface/obsidian/src/search_modal.ts index bd4d6271..5f88ff9a 100644 --- a/src/interface/obsidian/src/search_modal.ts +++ b/src/interface/obsidian/src/search_modal.ts @@ -1,5 +1,6 @@ import { App, SuggestModal, request, MarkdownRenderer, Instruction, Platform } from 'obsidian'; import { KhojSetting } from 'src/settings'; +import { createNoteAndCloseModal } from 'src/utils'; export interface SearchResult { entry: string; @@ -10,6 +11,7 @@ export class KhojSearchModal extends SuggestModal { setting: KhojSetting; rerank: boolean = false; find_similar_notes: boolean; + query: string = ""; app: App; constructor(app: App, setting: KhojSetting, find_similar_notes: boolean = false) { @@ -31,6 +33,14 @@ export class KhojSearchModal extends SuggestModal { this.rerank = false }); + // Register Modal Keybindings to Create New Note with Query as Title + this.scope.register(['Shift'], 'Enter', async () => { + if (this.query != "") createNoteAndCloseModal(this.query, this); + }); + this.scope.register(['Ctrl', 'Shift'], 'Enter', async () => { + if (this.query != "") createNoteAndCloseModal(this.query, this, { newLeaf: true }); + }); + // Add Hints to Modal for available Keybindings const modalInstructions: Instruction[] = [ { @@ -86,6 +96,7 @@ export class KhojSearchModal extends SuggestModal { .filter((result: any) => !this.find_similar_notes || !result.additional.file.endsWith(this.app.workspace.getActiveFile()?.path)) .map((result: any) => { return { entry: result.entry, file: result.additional.file } as SearchResult; }); + this.query = query; return results; } diff --git a/src/interface/obsidian/src/utils.ts b/src/interface/obsidian/src/utils.ts index 5a84a191..c7cc2ef0 100644 --- a/src/interface/obsidian/src/utils.ts +++ b/src/interface/obsidian/src/utils.ts @@ -1,4 +1,4 @@ -import { FileSystemAdapter, Notice, RequestUrlParam, request, Vault } from 'obsidian'; +import { FileSystemAdapter, Notice, RequestUrlParam, request, Vault, Modal } from 'obsidian'; import { KhojSetting } from 'src/settings' export function getVaultAbsolutePath(vault: Vault): string { @@ -139,3 +139,35 @@ export async function updateKhojBackend(khojUrl: string, khojConfig: Object) { function getIndexDirectoryFromBackendConfig(filepath: string) { return filepath.split("/").slice(0, -1).join("/"); } + +export async function createNote(name: string, newLeaf = false): Promise { + try { + let pathPrefix: string + switch (app.vault.getConfig('newFileLocation')) { + case 'current': + pathPrefix = (app.workspace.getActiveFile()?.parent.path ?? '') + '/' + break + case 'folder': + pathPrefix = this.app.vault.getConfig('newFileFolderPath') + '/' + break + default: // 'root' + pathPrefix = '' + break + } + await app.workspace.openLinkText(`${pathPrefix}${name}.md`, '', newLeaf) + } catch (e) { + console.error('Khoj: Could not create note.\n' + (e as any).message); + throw e + } + } + +export async function createNoteAndCloseModal(query: string, modal: Modal, opt?: { newLeaf: boolean }): Promise { + try { + await createNote(query, opt?.newLeaf); + } + catch (e) { + new Notice((e as Error).message) + return + } + modal.close(); +}