From 1728671abbd3b3e542780562a6add7ab388bcfb3 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Tue, 12 Apr 2022 23:08:06 +0200 Subject: [PATCH] Fixed #5 - jump to search match Jumps to the first match --- src/globals.ts | 2 ++ src/main.ts | 9 +++++++-- src/modal.ts | 28 ++++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/globals.ts b/src/globals.ts index b5e9e8b..6560d3c 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -19,4 +19,6 @@ export type ResultNote = { path: string basename: string content: string + keyword: string + occurence: number } diff --git a/src/main.ts b/src/main.ts index 4179747..3f53cc0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,6 @@ import { Notice, Plugin, TAbstractFile, TFile } from 'obsidian' import MiniSearch from 'minisearch' -import { clearContent, getTitleLine, removeTitleLine, wait } from './utils' +import { clearContent, getTitleLine, wait } from './utils' import { IndexedNote } from './globals' import { OmnisearchModal } from './modal' @@ -66,7 +66,9 @@ export default class OmnisearchPlugin extends Plugin { // This is basically the same behavior as MiniSearch's `addAllAsync()`. // We index files by batches of 10 - if (files.length) { console.log('Omnisearch - indexing ' + files.length + ' files') } + if (files.length) { + console.log('Omnisearch - indexing ' + files.length + ' files') + } for (let i = 0; i < files.length; ++i) { if (i % 10 === 0) await wait(0) const file = files[i] @@ -86,6 +88,8 @@ export default class OmnisearchPlugin extends Plugin { async addToIndex(file: TAbstractFile): Promise { if (!(file instanceof TFile) || file.extension !== 'md') return try { + // console.log(`Omnisearch - adding ${file.path} to index`) + if (this.indexedNotes[file.path]) { throw new Error(`${file.basename} is already indexed`) } @@ -114,6 +118,7 @@ export default class OmnisearchPlugin extends Plugin { removeFromIndex(file: TAbstractFile): void { if (file instanceof TFile && file.path.endsWith('.md')) { + // console.log(`Omnisearch - removing ${file.path} from index`) return this.removeFromIndexByPath(file.path) } } diff --git a/src/modal.ts b/src/modal.ts index 1b0f1ee..46f0907 100644 --- a/src/modal.ts +++ b/src/modal.ts @@ -1,4 +1,4 @@ -import { SuggestModal } from 'obsidian' +import { MarkdownView, SuggestModal, TFile } from 'obsidian' import { ResultNote } from './globals' import OmnisearchPlugin from './main' import { escapeRegex, highlighter } from './utils' @@ -133,7 +133,14 @@ export class OmnisearchModal extends SuggestModal { content = content.replace(reg, highlighter) basename = basename.replace(reg, highlighter) - return { content, basename, path: note.path } + const resultNote: ResultNote = { + content, + basename, + path: note.path, + keyword: result.terms[0], + occurence: 0, + } + return resultNote }) } @@ -150,7 +157,20 @@ export class OmnisearchModal extends SuggestModal { body.innerHTML = value.content } - onChooseSuggestion(item: ResultNote): void { - this.app.workspace.openLinkText(item.path, '') + async onChooseSuggestion(item: ResultNote): Promise { + const file = this.app.vault.getAbstractFileByPath(item.path) as TFile + const content = (await this.app.vault.cachedRead(file)).toLowerCase() + const offset = content.indexOf(item.keyword.toLowerCase()) + await this.app.workspace.openLinkText(item.path, '') + + const view = this.app.workspace.getActiveViewOfType(MarkdownView) + const pos = view.editor.offsetToPos(offset) + pos.ch = 0 + + view.editor.setCursor(pos) + view.editor.scrollIntoView({ + from: { line: pos.line - 10, ch: 0 }, + to: { line: pos.line + 10, ch: 0 }, + }) } }