2023-05-21 12:16:22 +00:00
|
|
|
import FolderNotesPlugin from './main';
|
|
|
|
|
import FolderNameModal from './modals/folderName';
|
2023-06-16 19:51:29 +00:00
|
|
|
import ExistingFolderNoteModal from './modals/existingNote';
|
2023-05-21 12:16:22 +00:00
|
|
|
import { applyTemplate } from './template';
|
|
|
|
|
import { TFolder, TFile, TAbstractFile, Keymap } from 'obsidian';
|
|
|
|
|
import DeleteConfirmationModal from './modals/deleteConfirmation';
|
2023-06-09 11:34:28 +00:00
|
|
|
import { addExcludedFolder, deleteExcludedFolder, getExcludedFolder, ExcludedFolder, updateExcludedFolder } from './excludedFolder';
|
2023-05-21 12:16:22 +00:00
|
|
|
|
2023-06-16 19:51:29 +00:00
|
|
|
export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: string, openFile: boolean, useModal?: boolean, existingNote?: TFile) {
|
2023-05-21 12:16:22 +00:00
|
|
|
const leaf = plugin.app.workspace.getLeaf(false);
|
|
|
|
|
const folderName = plugin.getFolderNameFromPathString(folderPath);
|
|
|
|
|
const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folderName);
|
2023-05-29 20:17:30 +00:00
|
|
|
let path = `${folderPath}/${fileName}${plugin.settings.folderNoteType}`;
|
|
|
|
|
if (plugin.settings.storageLocation === 'parentFolder') {
|
2023-06-04 13:31:08 +00:00
|
|
|
const parentFolderPath = plugin.getFolderPathFromString(folderPath);
|
2023-05-29 20:17:30 +00:00
|
|
|
if (parentFolderPath.trim() === '') {
|
|
|
|
|
path = `${fileName}${plugin.settings.folderNoteType}`;
|
|
|
|
|
} else {
|
|
|
|
|
path = `${parentFolderPath}/${fileName}${plugin.settings.folderNoteType}`;
|
|
|
|
|
}
|
|
|
|
|
} else if (plugin.settings.storageLocation === 'vaultFolder') {
|
|
|
|
|
path = `${fileName}${plugin.settings.folderNoteType}`;
|
|
|
|
|
}
|
2023-06-16 19:51:29 +00:00
|
|
|
let file: TFile;
|
|
|
|
|
if (!existingNote) {
|
|
|
|
|
file = await plugin.app.vault.create(path, '');
|
|
|
|
|
} else {
|
|
|
|
|
file = existingNote;
|
|
|
|
|
plugin.app.vault.rename(existingNote, path);
|
|
|
|
|
}
|
2023-05-21 12:16:22 +00:00
|
|
|
if (openFile) {
|
|
|
|
|
await leaf.openFile(file);
|
|
|
|
|
}
|
2023-06-16 19:51:29 +00:00
|
|
|
if (file && !existingNote) {
|
2023-05-21 12:16:22 +00:00
|
|
|
applyTemplate(this, file, plugin.settings.templatePath);
|
|
|
|
|
}
|
2023-05-29 20:17:30 +00:00
|
|
|
|
|
|
|
|
const folder = plugin.app.vault.getAbstractFileByPath(folderPath);
|
|
|
|
|
if (!(folder instanceof TFolder)) return;
|
2023-05-21 12:16:22 +00:00
|
|
|
plugin.addCSSClassToTitleEL(path, 'is-folder-note', true);
|
2023-05-29 20:17:30 +00:00
|
|
|
plugin.addCSSClassToTitleEL(folder.path, 'has-folder-note');
|
|
|
|
|
|
2023-05-21 12:16:22 +00:00
|
|
|
if (!plugin.settings.autoCreate) return;
|
|
|
|
|
if (!useModal) return;
|
|
|
|
|
const modal = new FolderNameModal(plugin.app, plugin, folder);
|
|
|
|
|
modal.open();
|
|
|
|
|
}
|
|
|
|
|
|
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-06-09 11:34:28 +00:00
|
|
|
if (folderNote) {
|
2023-06-16 19:51:29 +00:00
|
|
|
if (plugin.settings.showRenameConfirmation && !skipConfirmation) {
|
|
|
|
|
return new ExistingFolderNoteModal(plugin.app, plugin, file, folder, folderNote).open();
|
|
|
|
|
}
|
2023-06-11 12:39:09 +00:00
|
|
|
plugin.removeCSSClassFromEL(folderNote.path, 'is-folder-note');
|
2023-06-09 11:34:28 +00:00
|
|
|
let excludedFolder = getExcludedFolder(plugin, folder.path);
|
|
|
|
|
let excludedFolderExisted = true;
|
|
|
|
|
let disabledSync = false;
|
|
|
|
|
|
|
|
|
|
if (!excludedFolder) {
|
|
|
|
|
excludedFolderExisted = false;
|
|
|
|
|
excludedFolder = new ExcludedFolder(folder.path, plugin.settings.excludeFolders.length);
|
|
|
|
|
addExcludedFolder(plugin, excludedFolder);
|
|
|
|
|
} else if (!excludedFolder.disableSync) {
|
|
|
|
|
disabledSync = false;
|
|
|
|
|
excludedFolder.disableSync = true;
|
|
|
|
|
updateExcludedFolder(plugin, excludedFolder, excludedFolder);
|
|
|
|
|
}
|
2023-06-10 10:05:48 +00:00
|
|
|
const newPath = `${folder.path}/${folder.name} (${file.stat.ctime.toString().slice(10) + Math.floor(Math.random() * 1000)})${plugin.settings.folderNoteType}`;
|
2023-06-09 11:34:28 +00:00
|
|
|
plugin.app.vault.rename(folderNote, newPath).then(() => {
|
|
|
|
|
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);
|
|
|
|
|
let path = `${folder.path}/${fileName}${plugin.settings.folderNoteType}`;
|
|
|
|
|
if (plugin.settings.storageLocation === 'parentFolder') {
|
|
|
|
|
const parentFolderPath = folder.parent.path;
|
|
|
|
|
if (parentFolderPath.trim() === '') {
|
|
|
|
|
path = `${fileName}${plugin.settings.folderNoteType}`;
|
|
|
|
|
} else {
|
|
|
|
|
path = `${parentFolderPath}/${fileName}${plugin.settings.folderNoteType}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-11 12:39:09 +00:00
|
|
|
await plugin.app.vault.rename(file, path);
|
|
|
|
|
plugin.addCSSClassToTitleEL(path, 'is-folder-note', true);
|
|
|
|
|
plugin.addCSSClassToTitleEL(folder.path, 'has-folder-note');
|
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;
|
|
|
|
|
if (plugin.app.workspace.getActiveFile()?.path === path) { return; }
|
|
|
|
|
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) {
|
|
|
|
|
if (plugin.settings.showDeleteConfirmation) {
|
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;
|
|
|
|
|
plugin.removeCSSClassFromEL(folder.path, 'has-folder-note');
|
2023-05-21 12:16:22 +00:00
|
|
|
await plugin.app.vault.delete(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-06-04 13:31:08 +00:00
|
|
|
export function getFolderNote(plugin: FolderNotesPlugin, folderPath: string, storageLocation?: string) {
|
2023-05-21 12:16:22 +00:00
|
|
|
if (!folderPath) return null;
|
|
|
|
|
const folder = {
|
|
|
|
|
path: folderPath,
|
|
|
|
|
name: plugin.getFolderNameFromPathString(folderPath),
|
|
|
|
|
};
|
|
|
|
|
const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name);
|
2023-06-04 13:31:08 +00:00
|
|
|
|
|
|
|
|
if ((plugin.settings.storageLocation === 'parentFolder' || storageLocation === 'parentFolder') && storageLocation !== 'insideFolder') {
|
|
|
|
|
folder.path = plugin.getFolderPathFromString(folderPath);
|
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
|
|
|
}
|
2023-06-04 13:31:08 +00:00
|
|
|
|
2023-05-29 20:17:30 +00:00
|
|
|
let folderNote = plugin.app.vault.getAbstractFileByPath(path + plugin.settings.folderNoteType);
|
2023-05-21 12:16:22 +00:00
|
|
|
if (folderNote instanceof TFile) {
|
|
|
|
|
return folderNote;
|
|
|
|
|
} else {
|
|
|
|
|
if (plugin.settings.folderNoteType === '.canvas') {
|
2023-05-29 20:17:30 +00:00
|
|
|
folderNote = plugin.app.vault.getAbstractFileByPath(path + '.md');
|
2023-05-21 12:16:22 +00:00
|
|
|
} else {
|
2023-05-29 20:17:30 +00:00
|
|
|
folderNote = plugin.app.vault.getAbstractFileByPath(path + '.canvas');
|
2023-05-21 12:16:22 +00:00
|
|
|
}
|
2023-05-29 20:17:30 +00:00
|
|
|
return folderNote;
|
2023-05-21 12:16:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-30 10:36:41 +00:00
|
|
|
|
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;
|
|
|
|
|
const folderName = extractFolderName(plugin.settings.folderNoteName, file.basename);
|
|
|
|
|
if (!folderName) return null;
|
|
|
|
|
let folderPath = plugin.getFolderPathFromString(file.path);
|
|
|
|
|
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;
|
|
|
|
|
}
|