lostpaul_obsidian-folder-notes/src/functions/utils.ts

91 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-05-05 09:57:53 +00:00
import { TFolder, TFile, View } from 'obsidian';
2025-07-27 09:33:21 +00:00
import type { FileExplorerWorkspaceLeaf, FileExplorerView } from 'src/globals';
2025-05-05 09:57:53 +00:00
import { getFolderNote } from './folderNoteFunctions';
2025-07-27 09:33:21 +00:00
import type FolderNotesPlugin from 'src/main';
2025-08-11 12:39:33 +00:00
import type { FileExplorerLeaf, FileTreeItem, TreeNode } from 'obsidian-typings';
2025-07-27 09:33:21 +00:00
import type FolderOverviewPlugin from 'src/obsidian-folder-overview/src/main';
export function getFileNameFromPathString(path: string): string {
2025-01-07 10:54:21 +00:00
return path.substring(path.lastIndexOf('/') >= 0 ? path.lastIndexOf('/') + 1 : 0);
}
export function getFolderNameFromPathString(path: string): string {
2025-08-11 12:39:33 +00:00
const PARENT_FOLDER_INDEX = -2;
const LAST_FOLDER_INDEX = -1;
2025-01-07 10:54:21 +00:00
if (path.endsWith('.md') || path.endsWith('.canvas')) {
2025-08-11 12:39:33 +00:00
return path.split('/').slice(PARENT_FOLDER_INDEX)[0];
2025-01-07 10:54:21 +00:00
}
2025-08-11 12:39:33 +00:00
return path.split('/').slice(LAST_FOLDER_INDEX)[0];
}
export function removeExtension(name: string): string {
2025-01-07 10:54:21 +00:00
return name.replace(/\.[^/.]+$/, '');
}
export function getExtensionFromPathString(path: string): string {
2025-01-07 10:54:21 +00:00
return path.slice(path.lastIndexOf('.') >= 0 ? path.lastIndexOf('.') : 0);
}
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 '/';
}
2025-07-27 09:33:21 +00:00
return folderPath;
}
export function getParentFolderPath(path: string): string {
2025-01-07 10:54:21 +00:00
return this.getFolderPathFromString(this.getFolderPathFromString(path));
}
2025-08-11 12:39:33 +00:00
export function getFileExplorer(
plugin: FolderNotesPlugin | FolderOverviewPlugin,
): FileExplorerWorkspaceLeaf {
// eslint-disable-next-line max-len
2025-12-18 17:58:14 +00:00
let leaf = plugin.app.workspace.getLeavesOfType('file-explorer')[0] as unknown as FileExplorerWorkspaceLeaf;
/* make.md plugin integration */
if (leaf.containerEl.lastChild.dataset.type == 'mk-path-view') {
plugin.app.workspace.iterateAllLeaves((x) => {
if (x.tabHeaderEl.dataset.type == 'file-explorer') {
leaf = x;
}
});
}
2025-08-11 12:39:33 +00:00
return leaf;
}
2025-08-11 12:39:33 +00:00
export function getFileExplorerView(plugin: FolderNotesPlugin): FileExplorerView {
2025-01-07 10:54:21 +00:00
return getFileExplorer(plugin).view;
}
2024-12-27 21:10:30 +00:00
2025-08-11 12:39:33 +00:00
export function getFocusedItem(plugin: FolderNotesPlugin): TreeNode<FileTreeItem> | null {
const fileExplorer = getFileExplorer(plugin) as unknown as FileExplorerLeaf;
2025-07-27 09:33:21 +00:00
const { focusedItem } = fileExplorer.view.tree;
2025-01-07 10:54:21 +00:00
return focusedItem;
}
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;
}
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;
}