diff --git a/src/Commands.ts b/src/Commands.ts index 08d981f..f0654a1 100644 --- a/src/Commands.ts +++ b/src/Commands.ts @@ -1,15 +1,10 @@ -import { App, TFolder, Menu, TAbstractFile, Notice, TFile, Editor, MarkdownView, Platform, stringifyYaml, View } from 'obsidian'; +import { App, TFolder, Menu, TAbstractFile, Notice, TFile, Editor, MarkdownView, Platform, stringifyYaml } from 'obsidian'; import FolderNotesPlugin from './main'; import { getFolderNote, createFolderNote, deleteFolderNote, turnIntoFolderNote, openFolderNote, extractFolderName, detachFolderNote } from './functions/folderNoteFunctions'; import { ExcludedFolder } from './ExcludeFolders/ExcludeFolder'; import { getFolderPathFromString, getFileExplorerActiveFolder } from './functions/utils'; -import { getExcludedFolderByPattern } from './ExcludeFolders/functions/patternFunctions'; -import { addExcludedFolder, deleteExcludedFolder, getDetachedFolder, getExcludedFolder, getExcludedFoldersByPath, updateExcludedFolder } from './ExcludeFolders/functions/folderFunctions'; -import ExcludedFolderSettings from './ExcludeFolders/modals/ExcludeFolderSettings'; -import { ExcludePattern } from './ExcludeFolders/ExcludePattern'; -import PatternSettings from './ExcludeFolders/modals/PatternSettings'; +import { addExcludedFolder, deleteExcludedFolder, getDetachedFolder, getExcludedFolder } from './ExcludeFolders/functions/folderFunctions'; import { applyCSSClassesToFolder } from './functions/styleFunctions'; -import { FileExplorerView } from './globals'; @@ -91,23 +86,23 @@ export class Commands { }); }); this.plugin.settings.supportedFileTypes.forEach((fileType) => { - const type = fileType === 'md' ? 'markdown' : fileType; + const type = fileType === 'md' ? 'markdown' : fileType; this.plugin.addCommand({ id: `create-${type}-folder-note-for-active-file-explorer-folder`, name: `Create ${type} folder note for current active folder in file explorer`, checkCallback: (checking: boolean) => { - const folder = getFileExplorerActiveFolder(); - if (!folder) return false; - // Is there already a folder note for the active folder? - const folderNote = getFolderNote(this.plugin, folder.path); - if (folderNote instanceof TFile) return false; - if (checking) return true; + const folder = getFileExplorerActiveFolder(); + if (!folder) return false; + // Is there already a folder note for the active folder? + const folderNote = getFolderNote(this.plugin, folder.path); + if (folderNote instanceof TFile) return false; + if (checking) return true; - // Everything is fine and not checking, let's create the folder note. - const ext = '.' + fileType; - const path = folder.path; - createFolderNote(this.plugin, path, true, ext, false); - } + // Everything is fine and not checking, let's create the folder note. + const ext = '.' + fileType; + const path = folder.path; + createFolderNote(this.plugin, path, true, ext, false); + }, }); }); @@ -129,16 +124,16 @@ export class Commands { id: 'delete-folder-note-of-active-file-explorer-folder', name: 'Delete folder note of current active folder in file explorer', checkCallback: (checking: boolean) => { - const folder = getFileExplorerActiveFolder(); - if (!folder) return false; - // Is there any folder note for the active folder? + const folder = getFileExplorerActiveFolder(); + if (!folder) return false; + // Is there any folder note for the active folder? const folderNote = getFolderNote(this.plugin, folder.path); if (!(folderNote instanceof TFile)) return false; - if (checking) return true; + if (checking) return true; - // Everything is fine and not checking, let's delete the folder note. + // Everything is fine and not checking, let's delete the folder note. deleteFolderNote(this.plugin, folderNote, true); - } + }, }); this.plugin.addCommand({ id: 'open-folder-note-for-current-folder', @@ -151,22 +146,22 @@ export class Commands { const folderNote = getFolderNote(this.plugin, folder.path); if (!(folderNote instanceof TFile)) return; openFolderNote(this.plugin, folderNote); - } + }, }); this.plugin.addCommand({ id: 'open-folder-note-of-active-file-explorer-folder', name: 'Open folder note of current active folder in file explorer', checkCallback: (checking: boolean) => { - const folder = getFileExplorerActiveFolder(); - if (!folder) return false; - // Is there any folder note for the active folder? + const folder = getFileExplorerActiveFolder(); + if (!folder) return false; + // Is there any folder note for the active folder? const folderNote = getFolderNote(this.plugin, folder.path); if (!(folderNote instanceof TFile)) return false; - if (checking) return true; + if (checking) return true; - // Everything is fine and not checking, let's open the folder note. + // Everything is fine and not checking, let's open the folder note. openFolderNote(this.plugin, folderNote); - } + }, }); this.plugin.addCommand({ id: 'insert-folder-overview-fn', @@ -176,9 +171,9 @@ export class Commands { const lineText = editor.getLine(line); if (lineText.trim() === '' || lineText.trim() === '>') { if (!checking) { - let json = Object.assign({}, this.plugin.settings.defaultOverview); + const json = Object.assign({}, this.plugin.settings.defaultOverview); json.id = crypto.randomUUID(); - const yaml = stringifyYaml(json) + const yaml = stringifyYaml(json); if (lineText.trim() === '') { editor.replaceSelection(`\`\`\`folder-overview\n${yaml}\`\`\`\n`); } else if (lineText.trim() === '>') { diff --git a/src/functions/utils.ts b/src/functions/utils.ts index 7df32f9..c50a5e7 100644 --- a/src/functions/utils.ts +++ b/src/functions/utils.ts @@ -1,7 +1,6 @@ -import { TAbstractFile, TFolder, TFile, View} from "obsidian"; -import { FileExplorerWorkspaceLeaf, FileExplorerView } from "src/globals"; -import { getFolderNote } from "./folderNoteFunctions"; -import { FileExplorerWorkspaceLeaf } from 'src/globals'; +import { TFolder, TFile, View } from 'obsidian'; +import { FileExplorerWorkspaceLeaf, FileExplorerView } from 'src/globals'; +import { getFolderNote } from './folderNoteFunctions'; import FolderNotesPlugin from 'src/main'; import { FileExplorerLeaf } from 'obsidian-typings'; import FolderOverviewPlugin from 'src/obsidian-folder-overview/src/main'; @@ -54,23 +53,23 @@ export function getFocusedItem(plugin: FolderNotesPlugin) { return focusedItem; } -export function getFileExplorerActiveFolder() : TFolder | null { - // Check if the active view is a file explorer. - const view = this.app.workspace.getActiveViewOfType(View); - if (view?.getViewType() !== 'file-explorer') return null; - // 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 instanceof TFolder)) return null; - return activeFileOrFolder as TFolder; +export function getFileExplorerActiveFolder(): TFolder | null { + // Check if the active view is a file explorer. + const view = this.app.workspace.getActiveViewOfType(View); + if (view?.getViewType() !== 'file-explorer') return null; + // 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 instanceof TFolder)) return null; + return activeFileOrFolder as TFolder; } export function getFileExplorerActiveFolderNote(): TFile | null { - const folder = getFileExplorerActiveFolder(); - if (!folder) return null; - // Is there any folder note for the active folder? - const folderNote = getFolderNote(this.plugin, folder.path); - if (!(folderNote instanceof TFile)) return null; - return folderNote; -} \ No newline at end of file + const folder = getFileExplorerActiveFolder(); + if (!folder) return null; + // Is there any folder note for the active folder? + const folderNote = getFolderNote(this.plugin, folder.path); + if (!(folderNote instanceof TFile)) return null; + return folderNote; +} diff --git a/src/globals.d.ts b/src/globals.d.ts index 261cc0f..ffe085a 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -1,4 +1,4 @@ -import { Plugin, TAbstractFile, TFile, TFolder, View, WorkspaceLeaf } from 'obsidian'; +import { TAbstractFile, TFile, TFolder, View, WorkspaceLeaf } from 'obsidian'; declare module 'obsidian' { interface Setting { @@ -59,7 +59,7 @@ interface FolderItem { setCollapsed: (collapsed: boolean) => void; pusherEl: HTMLDivElement; } - + interface TreeItem { focusedItem: FileOrFolderItem; setFocusedItem: (item: FileOrFolderItem, moveViewport: boolean) => void;