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

376 lines
14 KiB
TypeScript
Raw Normal View History

2023-06-28 21:01:13 +00:00
import FolderNotesPlugin from '../main';
2023-08-12 16:01:16 +00:00
import ExistingFolderNoteModal from '../modals/ExistingNote';
2023-06-28 21:01:13 +00:00
import { applyTemplate } from '../template';
2023-05-21 12:16:22 +00:00
import { TFolder, TFile, TAbstractFile, Keymap } from 'obsidian';
2023-08-12 16:01:16 +00:00
import DeleteConfirmationModal from '../modals/DeleteConfirmation';
2024-06-15 15:56:14 +00:00
import { addExcludedFolder, deleteExcludedFolder, getDetachedFolder, getExcludedFolder, updateExcludedFolder } from '../ExcludeFolders/functions/folderFunctions';
import { ExcludedFolder } from '../ExcludeFolders/ExcludeFolder';
import { openExcalidrawView } from './excalidraw';
import { AskForExtensionModal } from 'src/modals/AskForExtension';
import { getEl, addCSSClassToTitleEL, removeCSSClassFromEL } from 'src/functions/styleFunctions';
2024-06-15 15:56:14 +00:00
import { getFolderNameFromPathString, getFolderPathFromString, removeExtension } from 'src/functions/utils';
2023-05-21 12:16:22 +00:00
const defaultExcalidrawTemplate = `---
excalidraw-plugin: parsed
tags: [excalidraw]
---
== Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ==
%%
# Drawing
\`\`\`json
2025-01-07 10:54:21 +00:00
{'type":"excalidraw","version":2,"source":"https://github.com/zsviczian/obsidian-excalidraw-plugin/releases/tag/1.9.20","elements":[],"appState":{"gridSize":null,"viewBackgroundColor":"#ffffff'}}
\`\`\`
%%`;
2024-09-07 11:44:11 +00:00
export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: string, openFile: boolean, extension?: string, displayModal?: boolean, preexistingNote?: TFile) {
2023-05-21 12:16:22 +00:00
const leaf = plugin.app.workspace.getLeaf(false);
const folderName = getFolderNameFromPathString(folderPath);
2023-05-21 12:16:22 +00:00
const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folderName);
2024-09-07 11:44:11 +00:00
let folderNote = getFolderNote(plugin, folderPath)
if (preexistingNote) {
folderNote = preexistingNote;
}
let folderNoteType = extension ?? plugin.settings.folderNoteType;
2024-06-15 15:56:14 +00:00
const detachedFolder = getDetachedFolder(plugin, folderPath)
2024-09-07 11:44:11 +00:00
let path = '';
if (folderNoteType === '.excalidraw') {
folderNoteType = '.md';
2023-10-07 20:36:03 +00:00
extension = '.excalidraw';
} else if (folderNoteType === '.ask') {
2024-09-07 11:44:11 +00:00
return new AskForExtensionModal(plugin, folderPath, openFile, folderNoteType, displayModal, preexistingNote).open();
}
if (plugin.settings.storageLocation === 'parentFolder') {
const parentFolderPath = getFolderPathFromString(folderPath);
if (parentFolderPath.trim() === '') {
path = `${fileName}${folderNoteType}`;
} else {
path = `${parentFolderPath}/${fileName}${folderNoteType}`;
}
} else if (plugin.settings.storageLocation === 'vaultFolder') {
path = `${fileName}${folderNoteType}`;
} else {
path = `${folderPath}/${fileName}${folderNoteType}`;
}
2024-09-07 11:44:11 +00:00
if (detachedFolder && folderNote?.extension !== extension && folderNote) {
2024-06-15 15:56:14 +00:00
deleteExcludedFolder(plugin, detachedFolder);
2024-11-12 21:10:25 +00:00
removeCSSClassFromEL(folderNote?.path, 'is-folder-note', plugin);
2024-06-15 15:56:14 +00:00
const folder = plugin.app.vault.getAbstractFileByPath(folderPath) as TFolder;
if (!folderNote || folderNote.basename !== fileName) return;
let count = 1;
let newName = removeExtension(folderNote.path) + ` (${count}).${folderNote.path.split('.').pop()}`;
while (count < 100 && plugin.app.vault.getAbstractFileByPath(newName)) {
count++;
newName = removeExtension(folderNote.path) + ` (${count}).${folderNote.path.split('.').pop()}`;
}
const [excludedFolder, excludedFolderExisted, disabledSync] = await tempDisableSync(plugin, folder)
await plugin.app.fileManager.renameFile(folderNote, newName).then(() => {
if (!excludedFolder) return;
if (!excludedFolderExisted) {
deleteExcludedFolder(plugin, excludedFolder);
} else if (!disabledSync) {
excludedFolder.disableSync = false;
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
}
});
}
2024-09-07 11:44:11 +00:00
if (!folderNote) {
let content = '';
if (extension !== '.md') {
if (plugin.settings.templatePath && folderNoteType.split('.').pop() == plugin.settings.templatePath.split('.').pop()) {
const templateFile = plugin.app.vault.getAbstractFileByPath(plugin.settings.templatePath);
if (templateFile instanceof TFile) {
if (['md', 'canvas', 'txt'].includes(templateFile.extension)) {
content = await plugin.app.vault.read(templateFile);
if (extension === '.excalidraw' && !content.includes('==⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠==')) {
content = defaultExcalidrawTemplate;
}
} else {
return plugin.app.vault.readBinary(templateFile).then(async (data) => {
2024-09-07 11:44:11 +00:00
folderNote = await plugin.app.vault.createBinary(path, data);
if (openFile) {
2024-09-07 11:44:11 +00:00
await leaf.openFile(folderNote);
}
});
}
}
} else if (plugin.settings.folderNoteType === '.excalidraw' || extension === '.excalidraw') {
content = defaultExcalidrawTemplate;
} else if (plugin.settings.folderNoteType === '.canvas') {
content = '{}'
}
}
2024-09-05 15:05:58 +00:00
2024-09-07 11:44:11 +00:00
folderNote = await plugin.app.vault.create(path, content);
} else {
2024-09-07 11:44:11 +00:00
await plugin.app.fileManager.renameFile(folderNote, path)
}
2023-05-21 12:16:22 +00:00
if (openFile) {
if (plugin.app.workspace.getActiveFile()?.path === path) {
2024-01-21 17:12:15 +00:00
if (plugin.activeFolderDom) {
plugin.activeFolderDom.removeClass('fn-is-active');
plugin.activeFolderDom = null;
}
2024-09-07 11:44:11 +00:00
const folder = getFolder(plugin, folderNote);
2024-01-21 17:12:15 +00:00
if (!folder) { return; }
2024-11-12 21:10:25 +00:00
plugin.activeFolderDom = getEl(folder.path, plugin);
2024-01-21 17:12:15 +00:00
if (plugin.activeFolderDom) plugin.activeFolderDom.addClass('fn-is-active');
}
2024-09-07 11:44:11 +00:00
await leaf.openFile(folderNote);
if (plugin.settings.folderNoteType === '.excalidraw' || extension === '.excalidraw') {
openExcalidrawView(leaf);
}
2023-05-21 12:16:22 +00:00
}
2024-01-20 17:31:50 +00:00
const matchingExtension = extension?.split('.').pop() == plugin.settings.templatePath.split('.').pop();
2024-09-07 11:44:11 +00:00
if (folderNote && matchingExtension && plugin.settings.folderNoteType !== '.excalidraw') {
applyTemplate(plugin, folderNote, leaf, plugin.settings.templatePath);
2023-05-21 12:16:22 +00:00
}
const folder = plugin.app.vault.getAbstractFileByPath(folderPath);
if (!(folder instanceof TFolder)) return;
2024-11-12 21:10:25 +00:00
addCSSClassToTitleEL(path, 'is-folder-note', plugin, true);
addCSSClassToTitleEL(folder.path, 'has-folder-note', plugin);
2023-05-21 12:16:22 +00:00
}
export async function turnIntoFolderNote(plugin: FolderNotesPlugin, file: TFile, folder: TFolder, folderNote?: TFile | null | TAbstractFile, skipConfirmation?: boolean) {
2023-08-15 18:39:06 +00:00
const extension = file.extension
2024-06-15 15:56:14 +00:00
const detachedExcludedFolder = getDetachedFolder(plugin, folder.path);
if (folderNote) {
2024-06-15 15:56:14 +00:00
if (plugin.settings.showRenameConfirmation && !skipConfirmation && !detachedExcludedFolder) {
return new ExistingFolderNoteModal(plugin.app, plugin, file, folder, folderNote).open();
}
2024-11-12 21:10:25 +00:00
removeCSSClassFromEL(folderNote.path, 'is-folder-note', plugin);
2024-06-15 15:56:14 +00:00
const [excludedFolder, excludedFolderExisted, disabledSync] = await tempDisableSync(plugin, folder)
2023-08-15 18:39:06 +00:00
const newPath = `${folder.path}/${folder.name} (${file.stat.ctime.toString().slice(10) + Math.floor(Math.random() * 1000)}).${extension}`;
plugin.app.fileManager.renameFile(folderNote, newPath).then(() => {
2024-06-15 15:56:14 +00:00
if (!excludedFolder) return;
if (!excludedFolderExisted) {
deleteExcludedFolder(plugin, excludedFolder);
} else if (!disabledSync) {
excludedFolder.disableSync = false;
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
}
});
}
const folderName = folder.name;
const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folderName);
2023-08-15 18:39:06 +00:00
let path = `${folder.path}/${fileName}.${extension}`;
if (plugin.settings.storageLocation === 'parentFolder') {
2023-06-26 11:14:31 +00:00
const parentFolderPath = folder.parent?.path;
if (!parentFolderPath) return;
if (parentFolderPath.trim() === '') {
2023-08-15 18:39:06 +00:00
path = `${fileName}.${extension}`;
} else {
2023-08-15 18:39:06 +00:00
path = `${parentFolderPath}/${fileName}.${extension}`;
}
}
2023-08-15 18:39:06 +00:00
2024-06-15 15:56:14 +00:00
if (detachedExcludedFolder) {
deleteExcludedFolder(plugin, detachedExcludedFolder)
}
await plugin.app.fileManager.renameFile(file, path);
2024-11-12 21:10:25 +00:00
addCSSClassToTitleEL(path, 'is-folder-note', plugin, true);
addCSSClassToTitleEL(folder.path, 'has-folder-note', plugin);
2024-06-15 15:56:14 +00:00
if (plugin.activeFolderDom) {
plugin.activeFolderDom.removeClass('fn-is-active');
plugin.activeFolderDom = null;
}
2024-11-12 21:10:25 +00:00
plugin.activeFolderDom = getEl(folder.path, plugin);
2024-06-15 15:56:14 +00:00
if (plugin.activeFolderDom) plugin.activeFolderDom.addClass('fn-is-active');
}
2024-11-12 21:10:25 +00:00
export async function tempDisableSync(plugin: FolderNotesPlugin, folder: TFolder): Promise<[excludedFolder: ExcludedFolder | undefined, excludedFolderExisted: boolean, disabledSync: boolean]> {
2024-09-05 15:05:58 +00:00
let excludedFolder = await getExcludedFolder(plugin, folder.path, false);
2024-06-15 15:56:14 +00:00
let excludedFolderExisted = true;
let disabledSync = false;
if (!excludedFolder) {
excludedFolderExisted = false;
excludedFolder = new ExcludedFolder(folder.path, plugin.settings.excludeFolders.length, undefined, plugin);
excludedFolder.disableSync = true;
addExcludedFolder(plugin, excludedFolder);
} else if (!excludedFolder.disableSync) {
disabledSync = false;
excludedFolder.disableSync = true;
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
}
return [excludedFolder, excludedFolderExisted, disabledSync]
}
export async function openFolderNote(plugin: FolderNotesPlugin, file: TAbstractFile, evt?: MouseEvent) {
2023-05-21 12:16:22 +00:00
const path = file.path;
if (plugin.app.workspace.getActiveFile()?.path === path && !(Keymap.isModEvent(evt) == 'tab')) { return; }
2023-05-21 12:16:22 +00:00
const leaf = plugin.app.workspace.getLeaf(Keymap.isModEvent(evt) || plugin.settings.openInNewTab);
if (file instanceof TFile) {
await leaf.openFile(file);
}
}
export async function deleteFolderNote(plugin: FolderNotesPlugin, file: TFile, displayModal: boolean) {
if (plugin.settings.showDeleteConfirmation && displayModal) {
2023-06-09 15:25:57 +00:00
return new DeleteConfirmationModal(plugin.app, plugin, file).open();
2023-05-21 12:16:22 +00:00
}
2023-06-09 15:25:57 +00:00
const folder = getFolder(plugin, file);
if (!folder) return;
2024-11-12 21:10:25 +00:00
removeCSSClassFromEL(folder.path, 'has-folder-note', plugin);
switch (plugin.settings.deleteFilesAction) {
case 'trash':
await plugin.app.vault.trash(file, true);
break;
case 'obsidianTrash':
await plugin.app.vault.trash(file, false);
break;
case 'delete':
await plugin.app.vault.delete(file);
break;
}
2023-05-21 12:16:22 +00:00
}
export function extractFolderName(template: string, changedFileName: string) {
const [prefix, suffix] = template.split('{{folder_name}}');
if (prefix.trim() === '' && suffix.trim() === '') {
return changedFileName;
}
if (!changedFileName.startsWith(prefix) || !changedFileName.endsWith(suffix)) {
return null;
}
if (changedFileName.startsWith(prefix) && prefix.trim() !== '') {
return changedFileName.slice(prefix.length).replace(suffix, '');
} else if (changedFileName.endsWith(suffix) && suffix.trim() !== '') {
return changedFileName.slice(0, -suffix.length);
}
return null;
}
export function getFolderNote(plugin: FolderNotesPlugin, folderPath: string, storageLocation?: string, file?: TFile) {
2023-05-21 12:16:22 +00:00
if (!folderPath) return null;
const folder = {
path: folderPath,
name: getFolderNameFromPathString(folderPath),
2023-05-21 12:16:22 +00:00
};
let fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name);
if (file) {
fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', file.basename);
}
if (!fileName) return null;
if ((plugin.settings.storageLocation === 'parentFolder' || storageLocation === 'parentFolder') && storageLocation !== 'insideFolder') {
folder.path = getFolderPathFromString(folderPath);
}
let path = `${folder.path}/${fileName}`;
if (folder.path.trim() === '') {
folder.path = fileName;
path = `${fileName}`;
}
let folderNoteType = plugin.settings.folderNoteType;
if (folderNoteType === '.excalidraw') {
folderNoteType = '.md';
}
let folderNote = plugin.app.vault.getAbstractFileByPath(path + folderNoteType);
2023-05-21 12:16:22 +00:00
if (folderNote instanceof TFile) {
return folderNote;
} else {
const supportedFileTypes = plugin.settings.supportedFileTypes.filter((type) => type !== plugin.settings.folderNoteType.replace('.', ''));
for (let type of supportedFileTypes) {
if (type === 'excalidraw' || type === '.excalidraw') {
type = '.md';
}
if (!type.startsWith('.')) {
type = '.' + type;
}
folderNote = plugin.app.vault.getAbstractFileByPath(path + type);
if (folderNote instanceof TFile) {
return folderNote;
}
2023-05-21 12:16:22 +00:00
}
}
}
2024-06-15 15:56:14 +00:00
export function detachFolderNote(plugin: FolderNotesPlugin, file: TFile) {
const folder = getFolder(plugin, file);
if (!folder) return;
const excludedFolder = new ExcludedFolder(folder.path, plugin.settings.excludeFolders.length, undefined, plugin);
excludedFolder.hideInSettings = true;
excludedFolder.disableFolderNote = true;
excludedFolder.disableSync = true;
excludedFolder.subFolders = false;
excludedFolder.excludeFromFolderOverview = false;
excludedFolder.detached = true;
excludedFolder.detachedFilePath = file.path;
addExcludedFolder(plugin, excludedFolder);
}
export function getFolder(plugin: FolderNotesPlugin, file: TFile, storageLocation?: string) {
if (!file) return null;
let folderName = extractFolderName(plugin.settings.folderNoteName, file.basename);
if (plugin.settings.folderNoteName === file.basename && plugin.settings.storageLocation === 'insideFolder') {
folderName = file.parent?.name ?? '';
}
if (!folderName) return null;
let folderPath = getFolderPathFromString(file.path);
let folder: TFolder | TAbstractFile | null = null;
if ((plugin.settings.storageLocation === 'parentFolder' || storageLocation === 'parentFolder') && storageLocation !== 'insideFolder') {
if (folderPath.trim() === '') {
folderPath = folderName;
} else {
folderPath = `${folderPath}/${folderName}`;
}
folder = plugin.app.vault.getAbstractFileByPath(folderPath);
} else {
folder = plugin.app.vault.getAbstractFileByPath(folderPath);
}
if (!folder) { return null; }
return folder;
}
export function getFolderNoteFolder(plugin: FolderNotesPlugin, folderNote: TFile | string, fileName: string) {
if (!folderNote) return null;
let filePath = '';
if (typeof folderNote === 'string') {
filePath = folderNote;
} else {
fileName = folderNote.basename;
filePath = folderNote.path;
}
const folderName = extractFolderName(plugin.settings.folderNoteName, fileName);
if (!folderName) return null;
let folderPath = getFolderPathFromString(filePath);
if (plugin.settings.storageLocation === 'parentFolder') {
if (folderPath.trim() === '') {
folderPath = folderName;
} else {
folderPath = `${folderPath}/${folderName}`;
}
} else {
folderPath = getFolderPathFromString(filePath);
}
const folder = plugin.app.vault.getAbstractFileByPath(folderPath);
if (!folder) { return null; }
return folder;
}