From cfb74f30b58c097566223b4d611a876b8d8084b0 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Fri, 22 Apr 2022 21:32:13 +0200 Subject: [PATCH] Fixed note creation --- src/CmpModalVault.svelte | 21 +++------------------ src/notes.ts | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/CmpModalVault.svelte b/src/CmpModalVault.svelte index 6f1a9cc..1cb0d37 100644 --- a/src/CmpModalVault.svelte +++ b/src/CmpModalVault.svelte @@ -9,7 +9,7 @@ import CmpInput from "./CmpInput.svelte" import CmpResultNote from "./CmpResultNote.svelte" import type { ResultNote } from "./globals" import { ModalInFile, type ModalVault } from "./modal" -import { openNote } from "./notes" +import { createNote, openNote } from "./notes" import { getSuggestions } from "./search" import { loopIndex } from "./utils" @@ -32,20 +32,6 @@ onMount(() => { searchQuery = lastSearch }) -async function createOrOpenNote(item: ResultNote): Promise { - try { - const file = await app.vault.create(searchQuery + ".md", "# " + searchQuery) - await app.workspace.openLinkText(file.path, "") - } catch (e) { - if (e instanceof Error && e.message === "File already exists.") { - // Open the existing file instead of creating it - await openNote(item) - } else { - console.error(e) - } - } -} - function onClick() { if (!selectedNote) return openNote(selectedNote) @@ -65,9 +51,8 @@ function onInputCtrlEnter(): void { modal.close() } -function onInputShiftEnter(): void { - if (!selectedNote) return - createOrOpenNote(selectedNote) +async function onInputShiftEnter(): Promise { + await createNote(searchQuery) modal.close() } diff --git a/src/notes.ts b/src/notes.ts index e2bda8c..2020b4f 100644 --- a/src/notes.ts +++ b/src/notes.ts @@ -24,3 +24,19 @@ export async function openNote( to: { line: pos.line + 10, ch: 0 }, }) } + +export async function createNote(name: string): Promise { + try { + const file = await app.vault.create(name + '.md', '# ' + name + '\n') + await app.workspace.openLinkText(file.path, '') + const view = app.workspace.getActiveViewOfType(MarkdownView) + if (!view) { + throw new Error('OmniSearch - No active MarkdownView') + } + const pos = view.editor.offsetToPos(name.length + 5) + pos.ch = 0 + } + catch (e) { + console.error(e) + } +}