This commit is contained in:
Simon Cambier
2022-05-31 18:09:08 +02:00
parent 17df16d66c
commit 53a0ec8670
2 changed files with 33 additions and 3 deletions

View File

@@ -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
// 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) {

8
src/types.d.ts vendored
View File

@@ -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
}
}
}