Fixed note creation

This commit is contained in:
Simon Cambier
2022-04-22 21:32:13 +02:00
parent d709ce086a
commit cfb74f30b5
2 changed files with 19 additions and 18 deletions

View File

@@ -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<void> {
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<void> {
await createNote(searchQuery)
modal.close()
}

View File

@@ -24,3 +24,19 @@ export async function openNote(
to: { line: pos.line + 10, ch: 0 },
})
}
export async function createNote(name: string): Promise<void> {
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)
}
}