diff --git a/src/functions/folderNoteFunctions.ts b/src/functions/folderNoteFunctions.ts index 251a60e..89f73fe 100644 --- a/src/functions/folderNoteFunctions.ts +++ b/src/functions/folderNoteFunctions.ts @@ -223,10 +223,34 @@ export async function tempDisableSync(plugin: FolderNotesPlugin, folder: TFolder export async function openFolderNote(plugin: FolderNotesPlugin, file: TAbstractFile, evt?: MouseEvent) { const path = file.path; - if (plugin.app.workspace.getActiveFile()?.path === path && !(Keymap.isModEvent(evt) === 'tab')) { return; } - const leaf = plugin.app.workspace.getLeaf(Keymap.isModEvent(evt) || plugin.settings.openInNewTab); - if (file instanceof TFile) { - await leaf.openFile(file); + const focusExistingTab = plugin.settings.focusExistingTab; + const activeFilePath = plugin.app.workspace.getActiveFile()?.path; + + // If already active and not opening in new tab, do nothing + if (activeFilePath === path && !(Keymap.isModEvent(evt) === 'tab')) { + return; + } + + // Try to find an existing tab with this file open + let foundLeaf = null; + if (focusExistingTab && file instanceof TFile) { + plugin.app.workspace.iterateAllLeaves((leaf) => { + if ( + leaf.getViewState().type === 'markdown' && + (leaf.view as import('obsidian').MarkdownView).file?.path === path + ) { + foundLeaf = leaf; + } + }); + } + + if (foundLeaf) { + plugin.app.workspace.setActiveLeaf(foundLeaf, { focus: true }); + } else { + const leaf = plugin.app.workspace.getLeaf(Keymap.isModEvent(evt) || plugin.settings.openInNewTab); + if (file instanceof TFile) { + await leaf.openFile(file); + } } } diff --git a/src/settings/GeneralSettings.ts b/src/settings/GeneralSettings.ts index 5371df2..a6a76fb 100644 --- a/src/settings/GeneralSettings.ts +++ b/src/settings/GeneralSettings.ts @@ -334,6 +334,21 @@ export async function renderGeneral(settingsTab: SettingsTab) { setting3.infoEl.style.color = settingsTab.app.vault.getConfig('accentColor') as string || '#7d5bed'; } + if (settingsTab.plugin.settings.openInNewTab) { + new Setting(containerEl) + .setName('Focus existing tab instead of creating a new one') + .setDesc('If a folder note is already open in a tab, focus that tab instead of creating a new one.') + .addToggle((toggle) => + toggle + .setValue(settingsTab.plugin.settings.focusExistingTab) + .onChange(async (value) => { + settingsTab.plugin.settings.focusExistingTab = value; + await settingsTab.plugin.saveSettings(); + settingsTab.display(); + }) + ); + } + new Setting(containerEl) .setName('Sync folder name') .setDesc('Automatically rename the folder note when the folder name is changed') diff --git a/src/settings/SettingsTab.ts b/src/settings/SettingsTab.ts index 8e25684..4cea682 100644 --- a/src/settings/SettingsTab.ts +++ b/src/settings/SettingsTab.ts @@ -33,6 +33,7 @@ export interface FolderNotesSettings { underlineFolderInPath: boolean; openFolderNoteOnClickInPath: boolean; openInNewTab: boolean; + focusExistingTab: boolean; oldFolderNoteName: string | undefined; folderNoteName: string; newFolderNoteName: string; @@ -95,6 +96,7 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = { underlineFolderInPath: true, openFolderNoteOnClickInPath: true, openInNewTab: false, + focusExistingTab: false, oldFolderNoteName: undefined, folderNoteName: '{{folder_name}}', folderNoteType: '.md',