From 6743340aba909a761a80ea844ca195fa645492a4 Mon Sep 17 00:00:00 2001 From: jfsicilia Date: Mon, 21 Oct 2024 21:17:32 +0100 Subject: [PATCH] Added new command to open folder note of current active folder in file explorer. --- src/Commands.ts | 28 +++++++++++++++++++++++++++- src/globals.d.ts | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/Commands.ts b/src/Commands.ts index 71bc48f..e0243fb 100644 --- a/src/Commands.ts +++ b/src/Commands.ts @@ -1,4 +1,4 @@ -import { App, TFolder, Menu, TAbstractFile, Notice, TFile, Editor, MarkdownView, Platform, stringifyYaml } from 'obsidian'; +import { App, TFolder, Menu, TAbstractFile, Notice, TFile, Editor, MarkdownView, Platform, stringifyYaml, View } from 'obsidian'; import FolderNotesPlugin from './main'; import { getFolderNote, createFolderNote, deleteFolderNote, turnIntoFolderNote, openFolderNote, extractFolderName, detachFolderNote } from './functions/folderNoteFunctions'; import { ExcludedFolder } from './ExcludeFolders/ExcludeFolder'; @@ -9,6 +9,8 @@ import ExcludedFolderSettings from './ExcludeFolders/modals/ExcludeFolderSetting import { ExcludePattern } from './ExcludeFolders/ExcludePattern'; import PatternSettings from './ExcludeFolders/modals/PatternSettings'; import { applyCSSClassesToFolder } from './functions/styleFunctions'; +import { FileExplorerView } from './globals'; + export class Commands { plugin: FolderNotesPlugin; @@ -114,6 +116,30 @@ export class Commands { openFolderNote(this.plugin, folderNote); } }); + this.plugin.addCommand({ + id: 'open-folder-note-for-active-file-explorer-folder', + name: 'Open folder note of current active folder in file explorer', + checkCallback: (checking: boolean) => { + // Check if the active view is a file explorer. + const view = this.app.workspace.getActiveViewOfType(View); + if (view?.getViewType() !== 'file-explorer') return false; + // Check if there is a focused or active item in the file explorer. + const fe = view as FileExplorerView; + const activeFileOrFolder = + fe.tree.focusedItem?.file ?? fe.activeDom?.file; + if (!activeFileOrFolder) return false; + // Only interested in focused or active folder. + if (activeFileOrFolder instanceof TFile) return false; + if (checking) return true; + + // Everything is fine and now not checking, let's do action: + // if folder has a folder note, open it. + const folder = activeFileOrFolder as TFolder; + const folderNote = getFolderNote(this.plugin, folder.path); + if (!(folderNote instanceof TFile)) return; + openFolderNote(this.plugin, folderNote); + }, + }); this.plugin.addCommand({ id: 'insert-folder-overview-fn', name: 'Insert folder overview', diff --git a/src/globals.d.ts b/src/globals.d.ts index 7bc4834..b322eba 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -1,4 +1,4 @@ -import { Plugin, TAbstractFile, View, WorkspaceLeaf } from 'obsidian'; +import { Plugin, TAbstractFile, TFile, TFolder, View, WorkspaceLeaf } from 'obsidian'; declare module 'obsidian' { interface Setting { @@ -34,8 +34,41 @@ interface FileExplorerViewFileItem extends TAbstractFile { selfEl: HTMLElement } +type FileOrFolderItem = FolderItem | FileItem; + +interface FileItem { + el: HTMLDivElement; + file: TFile; + fileExplorer: FileExplorerView; + info: any; + selfEl: HTMLDivElement; + innerEl: HTMLDivElement; +} + +interface FolderItem { + el: HTMLDivElement; + fileExplorer: FileExplorerView; + info: any; + selfEl: HTMLDivElement; + innerEl: HTMLDivElement; + file: TFolder; + children: FileOrFolderItem[]; + childrenEl: HTMLDivElement; + collapseIndicatorEl: HTMLDivElement; + collapsed: boolean; + setCollapsed: (collapsed: boolean) => void; + pusherEl: HTMLDivElement; +} + +interface TreeItem { + focusedItem: FileOrFolderItem; + setFocusedItem: (item: FileOrFolderItem, moveViewport: boolean) => void; + selectedDoms: Set; +} interface FileExplorerView extends View { fileItems: { [path: string]: FileExplorerViewFileItem }; + activeDom: FileOrFolderItem; + tree: TreeItem; } declare global {