diff --git a/src/components/ModalVault.svelte b/src/components/ModalVault.svelte index dd6f4b9..c0cab8a 100644 --- a/src/components/ModalVault.svelte +++ b/src/components/ModalVault.svelte @@ -34,9 +34,9 @@ searchQuery = searchHistory[historySearchIndex] eventBus.enable('vault') eventBus.on('vault', 'enter', openNoteAndCloseModal) - eventBus.on('vault', 'shift-enter', createNoteAndCloseModal) - eventBus.on('vault', 'ctrl-enter', openNoteInNewPane) - eventBus.on('vault', 'alt-enter', insertLink) + eventBus.on('vault', 'create-note', createNoteAndCloseModal) + eventBus.on('vault', 'open-in-new-pane', openNoteInNewPane) + eventBus.on('vault', 'insert-link', insertLink) eventBus.on('vault', 'tab', switchToInFileModal) eventBus.on('vault', 'arrow-up', () => moveIndex(-1)) eventBus.on('vault', 'arrow-down', () => moveIndex(1)) @@ -105,9 +105,12 @@ openNote(note, newPane) } - async function createNoteAndCloseModal(): Promise { + async function createNoteAndCloseModal(opt?: { + newLeaf: boolean + }): Promise { + console.log(opt) try { - await createNote(searchQuery) + await createNote(searchQuery, opt?.newLeaf) } catch (e) { new Notice((e as Error).message) return @@ -208,6 +211,7 @@ to switch to In-File Search +
@@ -218,17 +222,22 @@ shift ↵ to create
+
+ ctrl shift ↵ + to create in a new pane +
+ +
+
alt ↵ to insert a link
-
- escto close -
-
-
ctrl+hto toggle context
+
+ escto close +
diff --git a/src/main.ts b/src/main.ts index bcc56e2..2371b92 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,6 +11,7 @@ import { eventBus } from './globals' import { registerAPI } from '@vanakat/plugin-api' import api from './api' import { loadSearchHistory } from './search-history' +import { get } from 'svelte/store' // let mainWindow: { on: any; off: any } | null = null // try { @@ -35,7 +36,7 @@ export default class OmnisearchPlugin extends Plugin { _registerAPI(this) - if (settings.ribbonIcon) { + if (get(settings).ribbonIcon) { this.addRibbonButton() } diff --git a/src/modals.ts b/src/modals.ts index a393d4e..91ee5e7 100644 --- a/src/modals.ts +++ b/src/modals.ts @@ -62,26 +62,35 @@ abstract class OmnisearchModal extends Modal { } // #endregion Up/Down navigation + + // Open in new pane this.scope.register(['Mod'], 'Enter', e => { e.preventDefault() - eventBus.emit('ctrl-enter') // Open in new pane + eventBus.emit('open-in-new-pane') }) + // Insert link this.scope.register(['Alt'], 'Enter', e => { e.preventDefault() - eventBus.emit('alt-enter') // Insert link + eventBus.emit('insert-link') }) + // Create a new note this.scope.register(['Shift'], 'Enter', e => { e.preventDefault() - eventBus.emit('shift-enter') // Create a new note + eventBus.emit('create-note') + }) + this.scope.register(['Ctrl', 'Shift'], 'Enter', e => { + e.preventDefault() + eventBus.emit('create-note', { newLeaf: true }) }) + // Open in current pane this.scope.register([], 'Enter', e => { if (!isInputComposition()) { // Check if the user is still typing e.preventDefault() - eventBus.emit('enter') // Open in current pane + eventBus.emit('enter') } }) diff --git a/src/notes.ts b/src/notes.ts index 77d168b..e194bee 100644 --- a/src/notes.ts +++ b/src/notes.ts @@ -1,11 +1,17 @@ -import { MarkdownView, TFile, type CachedMetadata } from 'obsidian' +import { + MarkdownView, + TFile, + WorkspaceLeaf, + type CachedMetadata, +} from 'obsidian' import { notesCacheFilePath, type IndexedNote, type ResultNote, } from './globals' -import { stringsToRegex } from './utils' +import { stringsToRegex, wait } from './utils' import { settings } from './settings' +import { get } from 'svelte/store' /** * This is an in-memory cache of the notes, with all their computed fields @@ -20,7 +26,7 @@ export function resetNotesCache(): void { export async function loadNotesCache(): Promise { if ( - settings.storeIndexInFile && + get(settings).storeIndexInFile && (await app.vault.adapter.exists(notesCacheFilePath)) ) { try { @@ -93,7 +99,7 @@ export async function openNote( }) } -export async function createNote(name: string): Promise { +export async function createNote(name: string, newLeaf = false): Promise { try { let pathPrefix = '' switch (app.vault.getConfig('newFileLocation')) { @@ -107,14 +113,7 @@ export async function createNote(name: string): Promise { pathPrefix = '' break } - const file = await app.vault.create(`${pathPrefix}${name}.md`, '') - 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 + app.workspace.openLinkText(`${pathPrefix}${name}.md`, '', newLeaf) } catch (e) { ;(e as any).message = 'OmniSearch - Could not create note: ' + (e as any).message diff --git a/src/query.ts b/src/query.ts index 1bb8d2a..a88c6e2 100644 --- a/src/query.ts +++ b/src/query.ts @@ -1,3 +1,4 @@ +import { get } from 'svelte/store' import { settings } from './settings' import { removeDiacritics, stripSurroundingQuotes } from './utils' import { parseQuery } from './vendor/parse-query' @@ -22,7 +23,7 @@ export class Query { public exclusions: QueryToken[] = [] constructor(text = '') { - if (settings.ignoreDiacritics) text = removeDiacritics(text) + if (get(settings).ignoreDiacritics) text = removeDiacritics(text) const tokens = parseQuery(text.toLowerCase(), { tokenize: true }) this.exclusions = tokens.exclude.text .map(this.formatToken) diff --git a/src/search.ts b/src/search.ts index 02d1fdb..fb6ff39 100644 --- a/src/search.ts +++ b/src/search.ts @@ -18,7 +18,7 @@ import { wait, } from './utils' import type { Query } from './query' -import { settings } from './settings' +import { settings as storeSettings } from './settings' import { removeNoteFromCache, getNoteFromCache, @@ -31,10 +31,13 @@ import { saveNotesCacheToFile, isCacheOutdated, } from './notes' +import { get } from 'svelte/store' let minisearchInstance: MiniSearch let isIndexChanged: boolean +const settings = get(storeSettings) + const tokenize = (text: string): string[] => { const tokens = text.split(SPACE_OR_PUNCTUATION) const chsSegmenter = (app as any).plugins.plugins['cm-chs-patch'] @@ -140,6 +143,7 @@ export async function initGlobalSearchIndex(): Promise { */ async function search(query: Query): Promise { if (!query.segmentsToStr()) return [] + let results = minisearchInstance.search(query.segmentsToStr(), { prefix: true, fuzzy: term => (term.length > 4 ? 0.2 : false),