2025-05-05 09:57:53 +00:00
|
|
|
import { TFolder, TFile, View } from 'obsidian';
|
|
|
|
|
import { FileExplorerWorkspaceLeaf, FileExplorerView } from 'src/globals';
|
|
|
|
|
import { getFolderNote } from './folderNoteFunctions';
|
2025-01-07 10:54:21 +00:00
|
|
|
import FolderNotesPlugin from 'src/main';
|
|
|
|
|
import { FileExplorerLeaf } from 'obsidian-typings';
|
2025-02-24 10:56:38 +00:00
|
|
|
import FolderOverviewPlugin from 'src/obsidian-folder-overview/src/main';
|
2024-02-03 15:38:15 +00:00
|
|
|
|
|
|
|
|
export function getFileNameFromPathString(path: string): string {
|
2025-01-07 10:54:21 +00:00
|
|
|
return path.substring(path.lastIndexOf('/') >= 0 ? path.lastIndexOf('/') + 1 : 0);
|
2024-02-03 15:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getFolderNameFromPathString(path: string): string {
|
2025-01-07 10:54:21 +00:00
|
|
|
if (path.endsWith('.md') || path.endsWith('.canvas')) {
|
|
|
|
|
return path.split('/').slice(-2)[0];
|
|
|
|
|
} else {
|
|
|
|
|
return path.split('/').slice(-1)[0];
|
|
|
|
|
}
|
2024-02-03 15:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function removeExtension(name: string): string {
|
2025-01-07 10:54:21 +00:00
|
|
|
return name.replace(/\.[^/.]+$/, '');
|
2024-02-03 15:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getExtensionFromPathString(path: string): string {
|
2025-01-07 10:54:21 +00:00
|
|
|
return path.slice(path.lastIndexOf('.') >= 0 ? path.lastIndexOf('.') : 0);
|
2024-02-03 15:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getFolderPathFromString(path: string): string {
|
2025-01-07 10:54:21 +00:00
|
|
|
const subString = path.lastIndexOf('/') >= 0 ? path.lastIndexOf('/') : 0;
|
|
|
|
|
const folderPath = path.substring(0, subString);
|
|
|
|
|
if (folderPath === '') {
|
|
|
|
|
return '/';
|
|
|
|
|
} else {
|
|
|
|
|
return folderPath;
|
|
|
|
|
}
|
2024-02-03 15:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getParentFolderPath(path: string): string {
|
2025-01-07 10:54:21 +00:00
|
|
|
return this.getFolderPathFromString(this.getFolderPathFromString(path));
|
2024-02-03 15:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-24 10:56:38 +00:00
|
|
|
export function getFileExplorer(plugin: FolderNotesPlugin | FolderOverviewPlugin) {
|
2025-01-07 10:54:21 +00:00
|
|
|
return plugin.app.workspace.getLeavesOfType('file-explorer')[0] as any as FileExplorerWorkspaceLeaf;
|
2024-02-03 15:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-27 21:10:30 +00:00
|
|
|
export function getFileExplorerView(plugin: FolderNotesPlugin) {
|
2025-01-07 10:54:21 +00:00
|
|
|
return getFileExplorer(plugin).view;
|
2024-03-19 21:33:29 +00:00
|
|
|
}
|
2024-12-27 21:10:30 +00:00
|
|
|
|
|
|
|
|
export function getFocusedItem(plugin: FolderNotesPlugin) {
|
2025-01-07 10:54:21 +00:00
|
|
|
const fileExplorer = getFileExplorer(plugin) as any as FileExplorerLeaf;
|
2025-01-08 11:43:29 +00:00
|
|
|
const focusedItem = fileExplorer.view.tree.focusedItem;
|
2025-01-07 10:54:21 +00:00
|
|
|
return focusedItem;
|
2024-03-19 21:33:29 +00:00
|
|
|
}
|
2024-10-22 10:34:18 +00:00
|
|
|
|
2025-05-05 09:57:53 +00:00
|
|
|
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;
|
2024-10-22 10:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getFileExplorerActiveFolderNote(): TFile | null {
|
2025-05-05 09:57:53 +00:00
|
|
|
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;
|
|
|
|
|
}
|