From 53a0ec8670d75fadae43301cdd11ecd60ace307a Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Tue, 31 May 2022 18:09:08 +0200 Subject: [PATCH] Fixed #51 --- src/notes.ts | 28 ++++++++++++++++++++++++++-- src/types.d.ts | 8 +++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/notes.ts b/src/notes.ts index a3e12c6..e37466d 100644 --- a/src/notes.ts +++ b/src/notes.ts @@ -1,4 +1,9 @@ -import { MarkdownView, TFile, type CachedMetadata } from 'obsidian' +import { + MarkdownView, + TFile, + WorkspaceLeaf, + type CachedMetadata, +} from 'obsidian' import type { IndexedNote, ResultNote } from './globals' import { stringsToRegex } from './utils' @@ -32,7 +37,26 @@ export async function openNote( const reg = stringsToRegex(item.foundWords) reg.exec(item.content) const offset = reg.lastIndex - await app.workspace.openLinkText(item.path, '', newPane) + + // Check if the note is already open + // const pane = MarkdownView.getPane(item.path) + + // Check if the note is already open, + // to avoid opening it twice if the first one is pinned + let existing = false + app.workspace.iterateAllLeaves(leaf => { + if (leaf.view instanceof MarkdownView) { + if (leaf.getViewState().state?.file === item.path) { + app.workspace.setActiveLeaf(leaf, false, true) + existing = true + } + } + }) + + if (!existing) { + // Open a new note + await app.workspace.openLinkText(item.path, '', newPane) + } const view = app.workspace.getActiveViewOfType(MarkdownView) if (!view) { diff --git a/src/types.d.ts b/src/types.d.ts index cccea5a..ad8686a 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,4 +1,4 @@ -import { type MetadataCache } from 'obsidian' +import type { MetadataCache, ViewState } from 'obsidian' declare module 'obsidian' { interface MetadataCache { @@ -8,4 +8,10 @@ declare module 'obsidian' { interface FrontMatterCache { aliases?: string[] | string } + + interface ViewState { + state?: { + file?: string + } + } }