Added new command to open folder note of current active folder in file explorer.

This commit is contained in:
jfsicilia 2024-10-21 21:17:32 +01:00
parent 75443255b0
commit 6743340aba
2 changed files with 61 additions and 2 deletions

View file

@ -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',

35
src/globals.d.ts vendored
View file

@ -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<FileOrFolderItem>;
}
interface FileExplorerView extends View {
fileItems: { [path: string]: FileExplorerViewFileItem };
activeDom: FileOrFolderItem;
tree: TreeItem;
}
declare global {