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';
|
2024-03-19 21:33:29 +00:00
|
|
|
import { ExcludedFolder } from '../ExcludeFolders/ExcludeFolder';
|
2023-10-07 12:05:19 +00:00
|
|
|
import { openExcalidrawView } from './excalidraw';
|
|
|
|
|
import { AskForExtensionModal } from 'src/modals/AskForExtension';
|
2024-02-03 15:38:15 +00:00
|
|
|
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
|
|
|
|
2023-12-28 17:33:58 +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'}}
|
2023-12-28 17:33:58 +00:00
|
|
|
\`\`\`
|
|
|
|
|
%%`;
|
|
|
|
|
|
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);
|
2024-02-03 15:38:15 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2023-10-21 19:41:09 +00:00
|
|
|
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 = '';
|
2023-12-28 17:33:58 +00:00
|
|
|
|
2023-10-07 12:05:19 +00:00
|
|
|
if (folderNoteType === '.excalidraw') {
|
|
|
|
|
folderNoteType = '.md';
|
2023-10-07 20:36:03 +00:00
|
|
|
extension = '.excalidraw';
|
2023-10-07 12:05:19 +00:00
|
|
|
} else if (folderNoteType === '.ask') {
|
2024-09-07 11:44:11 +00:00
|
|
|
return new AskForExtensionModal(plugin, folderPath, openFile, folderNoteType, displayModal, preexistingNote).open();
|
2023-10-07 12:05:19 +00:00
|
|
|
}
|
2023-12-28 17:33:58 +00:00
|
|
|
|
2023-05-29 20:17:30 +00:00
|
|
|
if (plugin.settings.storageLocation === 'parentFolder') {
|
2024-02-03 15:38:15 +00:00
|
|
|
const parentFolderPath = getFolderPathFromString(folderPath);
|
2023-05-29 20:17:30 +00:00
|
|
|
if (parentFolderPath.trim() === '') {
|
2023-10-07 12:05:19 +00:00
|
|
|
path = `${fileName}${folderNoteType}`;
|
2023-05-29 20:17:30 +00:00
|
|
|
} else {
|
2023-10-07 12:05:19 +00:00
|
|
|
path = `${parentFolderPath}/${fileName}${folderNoteType}`;
|
2023-05-29 20:17:30 +00:00
|
|
|
}
|
|
|
|
|
} else if (plugin.settings.storageLocation === 'vaultFolder') {
|
2023-10-07 12:05:19 +00:00
|
|
|
path = `${fileName}${folderNoteType}`;
|
2023-07-05 16:29:55 +00:00
|
|
|
} else {
|
2023-10-07 12:05:19 +00:00
|
|
|
path = `${folderPath}/${fileName}${folderNoteType}`;
|
2023-05-29 20:17:30 +00:00
|
|
|
}
|
2023-12-28 17:33:58 +00:00
|
|
|
|
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) {
|
2023-10-07 12:05:19 +00:00
|
|
|
let content = '';
|
|
|
|
|
if (extension !== '.md') {
|
2023-12-28 17:33:58 +00:00
|
|
|
if (plugin.settings.templatePath && folderNoteType.split('.').pop() == plugin.settings.templatePath.split('.').pop()) {
|
2023-10-07 12:05:19 +00:00
|
|
|
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);
|
2023-12-28 17:33:58 +00:00
|
|
|
if (extension === '.excalidraw' && !content.includes('==⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠==')) {
|
|
|
|
|
content = defaultExcalidrawTemplate;
|
|
|
|
|
}
|
2023-10-07 12:05:19 +00:00
|
|
|
} 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);
|
2023-10-07 12:05:19 +00:00
|
|
|
if (openFile) {
|
2024-09-07 11:44:11 +00:00
|
|
|
await leaf.openFile(folderNote);
|
2023-10-07 12:05:19 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (plugin.settings.folderNoteType === '.excalidraw' || extension === '.excalidraw') {
|
2023-12-28 17:33:58 +00:00
|
|
|
content = defaultExcalidrawTemplate;
|
2023-10-07 12:05:19 +00:00
|
|
|
} 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);
|
2023-06-16 19:51:29 +00:00
|
|
|
} else {
|
2024-09-07 11:44:11 +00:00
|
|
|
await plugin.app.fileManager.renameFile(folderNote, path)
|
2023-06-16 19:51:29 +00:00
|
|
|
}
|
2023-12-28 17:33:58 +00:00
|
|
|
|
2023-05-21 12:16:22 +00:00
|
|
|
if (openFile) {
|
2024-02-03 15:38:15 +00:00
|
|
|
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-02-03 15:38:15 +00:00
|
|
|
|
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);
|
2023-10-07 12:05:19 +00:00
|
|
|
if (plugin.settings.folderNoteType === '.excalidraw' || extension === '.excalidraw') {
|
|
|
|
|
openExcalidrawView(leaf);
|
|
|
|
|
}
|
2023-05-21 12:16:22 +00:00
|
|
|
}
|
2023-12-28 17:33:58 +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
|
|
|
}
|
2023-05-29 20:17:30 +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
|
|
|
}
|
|
|
|
|
|
2023-06-16 19:51:29 +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);
|
|
|
|
|
|
2023-06-09 11:34:28 +00:00
|
|
|
if (folderNote) {
|
2024-06-15 15:56:14 +00:00
|
|
|
if (plugin.settings.showRenameConfirmation && !skipConfirmation && !detachedExcludedFolder) {
|
2023-06-16 19:51:29 +00:00
|
|
|
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}`;
|
2023-11-09 20:51:24 +00:00
|
|
|
plugin.app.fileManager.renameFile(folderNote, newPath).then(() => {
|
2024-06-15 15:56:14 +00:00
|
|
|
if (!excludedFolder) return;
|
2023-06-09 11:34:28 +00:00
|
|
|
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}`;
|
2023-06-09 11:34:28 +00:00
|
|
|
if (plugin.settings.storageLocation === 'parentFolder') {
|
2023-06-26 11:14:31 +00:00
|
|
|
const parentFolderPath = folder.parent?.path;
|
|
|
|
|
if (!parentFolderPath) return;
|
2023-06-09 11:34:28 +00:00
|
|
|
if (parentFolderPath.trim() === '') {
|
2023-08-15 18:39:06 +00:00
|
|
|
path = `${fileName}.${extension}`;
|
2023-06-09 11:34:28 +00:00
|
|
|
} else {
|
2023-08-15 18:39:06 +00:00
|
|
|
path = `${parentFolderPath}/${fileName}.${extension}`;
|
2023-06-09 11:34:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-15 18:39:06 +00:00
|
|
|
|
2024-06-15 15:56:14 +00:00
|
|
|
if (detachedExcludedFolder) {
|
|
|
|
|
deleteExcludedFolder(plugin, detachedExcludedFolder)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-09 20:51:24 +00:00
|
|
|
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]
|
2023-06-09 11:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function openFolderNote(plugin: FolderNotesPlugin, file: TAbstractFile, evt?: MouseEvent) {
|
2023-05-21 12:16:22 +00:00
|
|
|
const path = file.path;
|
2024-01-06 17:08:35 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-06 11:37:26 +00:00
|
|
|
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);
|
2024-08-06 11:37:26 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-05 16:29:55 +00:00
|
|
|
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,
|
2024-02-03 15:38:15 +00:00
|
|
|
name: getFolderNameFromPathString(folderPath),
|
2023-05-21 12:16:22 +00:00
|
|
|
};
|
2024-02-03 15:38:15 +00:00
|
|
|
|
2023-07-05 16:29:55 +00:00
|
|
|
let fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name);
|
|
|
|
|
if (file) {
|
|
|
|
|
fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', file.basename);
|
|
|
|
|
}
|
2024-02-03 15:38:15 +00:00
|
|
|
if (!fileName) return null;
|
2023-06-04 13:31:08 +00:00
|
|
|
|
|
|
|
|
if ((plugin.settings.storageLocation === 'parentFolder' || storageLocation === 'parentFolder') && storageLocation !== 'insideFolder') {
|
2024-02-03 15:38:15 +00:00
|
|
|
folder.path = getFolderPathFromString(folderPath);
|
2023-05-29 20:17:30 +00:00
|
|
|
}
|
2024-02-03 15:38:15 +00:00
|
|
|
|
2023-05-29 20:17:30 +00:00
|
|
|
let path = `${folder.path}/${fileName}`;
|
|
|
|
|
if (folder.path.trim() === '') {
|
2023-06-04 13:31:08 +00:00
|
|
|
folder.path = fileName;
|
|
|
|
|
path = `${fileName}`;
|
2023-05-29 20:17:30 +00:00
|
|
|
}
|
2024-02-03 15:38:15 +00:00
|
|
|
|
2023-10-07 12:05:19 +00:00
|
|
|
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 {
|
2023-10-07 12:05:19 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-30 10:36:41 +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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-06-04 13:31:08 +00:00
|
|
|
export function getFolder(plugin: FolderNotesPlugin, file: TFile, storageLocation?: string) {
|
2023-05-30 10:36:41 +00:00
|
|
|
if (!file) return null;
|
2023-11-04 20:53:30 +00:00
|
|
|
let folderName = extractFolderName(plugin.settings.folderNoteName, file.basename);
|
|
|
|
|
if (plugin.settings.folderNoteName === file.basename && plugin.settings.storageLocation === 'insideFolder') {
|
|
|
|
|
folderName = file.parent?.name ?? '';
|
|
|
|
|
}
|
2023-05-30 10:36:41 +00:00
|
|
|
if (!folderName) return null;
|
2024-02-03 15:38:15 +00:00
|
|
|
let folderPath = getFolderPathFromString(file.path);
|
2023-05-30 10:36:41 +00:00
|
|
|
let folder: TFolder | TAbstractFile | null = null;
|
2023-06-04 13:31:08 +00:00
|
|
|
if ((plugin.settings.storageLocation === 'parentFolder' || storageLocation === 'parentFolder') && storageLocation !== 'insideFolder') {
|
2023-05-30 10:36:41 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2023-07-05 16:29:55 +00:00
|
|
|
|
|
|
|
|
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;
|
2024-02-03 15:38:15 +00:00
|
|
|
let folderPath = getFolderPathFromString(filePath);
|
2023-07-05 16:29:55 +00:00
|
|
|
if (plugin.settings.storageLocation === 'parentFolder') {
|
|
|
|
|
if (folderPath.trim() === '') {
|
|
|
|
|
folderPath = folderName;
|
|
|
|
|
} else {
|
|
|
|
|
folderPath = `${folderPath}/${folderName}`;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-02-03 15:38:15 +00:00
|
|
|
folderPath = getFolderPathFromString(filePath);
|
2023-07-05 16:29:55 +00:00
|
|
|
}
|
|
|
|
|
const folder = plugin.app.vault.getAbstractFileByPath(folderPath);
|
|
|
|
|
if (!folder) { return null; }
|
|
|
|
|
return folder;
|
|
|
|
|
}
|