Skip to content

Commit bb4265e

Browse files
Add functionality to retrieve WorkspaceLeaf for a TFile in the navigation tree
1 parent 17809a8 commit bb4265e

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/navtree/naveTree.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TFile, TFolder, Plugin, Menu } from 'obsidian';
1+
import { TFile, TFolder, Plugin, Menu, WorkspaceLeaf, MarkdownView } from 'obsidian';
22
import { ExplorerLeaf, ElementsObj, Pairs } from '../types/obsidian';
33

44
export function newNavTreeStart(plugin: Plugin) {
@@ -46,13 +46,17 @@ export function oldNavTreeChange(plugin: Plugin, pairs: Pairs) {
4646
if (file instanceof TFile) {
4747
// Create a new Menu object
4848
const menu = new Menu();
49-
49+
const leaf = getLeafForFile(plugin, file)
5050
// Trigger the 'file-menu' event
51-
//@ts-ignore
52-
plugin.app.workspace.trigger("file-menu", menu, file, "file-explorer");
51+
if (leaf) {
52+
53+
//@ts-ignore
54+
plugin.app.workspace.trigger("file-menu", menu, file, "file-explorer", leaf);
5355

54-
// Show the menu at the mouse position
55-
menu.showAtMouseEvent(e);
56+
// Show the menu at the mouse position
57+
menu.showAtMouseEvent(e);
58+
return
59+
}
5660
}
5761
});
5862
targetInnerEl.style.display = "none"
@@ -120,3 +124,17 @@ export function getElementsObj(plugin: Plugin): ElementsObj | undefined {
120124

121125
return elementsObj.folders.length > 0 ? elementsObj : undefined;
122126
}
127+
128+
function getLeafForFile(plugin: Plugin, file: TFile): WorkspaceLeaf | null {
129+
const leaves = plugin.app.workspace.getLeavesOfType("markdown");
130+
131+
for (const leaf of leaves) {
132+
const view = leaf.view;
133+
134+
if (view instanceof MarkdownView && view.file?.path === file.path) {
135+
return leaf; // Found the leaf for this file
136+
}
137+
}
138+
139+
return null; // No matching leaf found
140+
}

0 commit comments

Comments
 (0)