From cbcb566c1311d9afdc4fe60cefa5ef456cb563bb Mon Sep 17 00:00:00 2001 From: wz <44930227+wenlzhang@users.noreply.github.com> Date: Tue, 25 Feb 2025 16:43:01 +0100 Subject: [PATCH] Debug folder navigation --- src/folderSuggestModal.ts | 47 +++++++++++++++++++++++++++++++-------- src/main.ts | 6 ++++- styles.css | 5 +++++ 3 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 styles.css diff --git a/src/folderSuggestModal.ts b/src/folderSuggestModal.ts index 9c6bd73..dfe81d2 100644 --- a/src/folderSuggestModal.ts +++ b/src/folderSuggestModal.ts @@ -1,4 +1,4 @@ -import { App, FuzzySuggestModal, TFolder } from "obsidian"; +import { App, FuzzySuggestModal, TFolder, View } from "obsidian"; export class FolderSuggestModal extends FuzzySuggestModal { folders: TFolder[]; @@ -35,16 +35,45 @@ export class FolderSuggestModal extends FuzzySuggestModal { } onChooseItem(folder: TFolder): void { - // Reveal the folder in the navigation - const leaf = this.app.workspace.getLeaf(); - this.app.workspace.revealLeaf(leaf); - - // Focus the folder in the file explorer + // Get the file explorer leaf const fileExplorer = this.app.workspace.getLeavesOfType("file-explorer")[0]; - if (fileExplorer) { - const fileExplorerView = fileExplorer.view as any; - fileExplorerView.revealFolder(folder); + if (!fileExplorer) return; + + const view = fileExplorer.view as any; + + // Ensure the file explorer is visible + this.app.workspace.revealLeaf(fileExplorer); + + // Expand parent folders if needed + if (folder.parent && folder.parent.path) { + const pathParts = folder.parent.path.split("/"); + let currentPath = ""; + + // Expand each parent folder + for (const part of pathParts) { + currentPath += (currentPath ? "/" : "") + part; + const parentFolder = + this.app.vault.getAbstractFileByPath(currentPath); + if (parentFolder && parentFolder instanceof TFolder) { + view.expandFolder(parentFolder, true); + } + } + } + + // Expand the target folder itself + view.expandFolder(folder, true); + + // Scroll the folder into view + const fileItem = view.fileItems[folder.path]; + if (fileItem) { + fileItem.el.scrollIntoView({ behavior: "smooth", block: "center" }); + // Highlight the folder temporarily + fileItem.el.addClass("nav-folder-title-highlighted"); + setTimeout( + () => fileItem.el.removeClass("nav-folder-title-highlighted"), + 2000, + ); } } } diff --git a/src/main.ts b/src/main.ts index 4bef3a0..950bf22 100644 --- a/src/main.ts +++ b/src/main.ts @@ -27,7 +27,11 @@ export default class FolderNavigatorPlugin extends Plugin { } async loadSettings() { - this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); + this.settings = Object.assign( + {}, + DEFAULT_SETTINGS, + await this.loadData(), + ); } async saveSettings() { diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..12ad990 --- /dev/null +++ b/styles.css @@ -0,0 +1,5 @@ +.nav-folder-title-highlighted { + background-color: var(--text-selection); + border-radius: 4px; + transition: background-color 0.3s ease; +}