From 39a9f6dffe5e462565aa019c82656bf6fb78a3bf Mon Sep 17 00:00:00 2001 From: Lost Paul <70213368+LostPaul@users.noreply.github.com> Date: Mon, 29 May 2023 22:17:30 +0200 Subject: [PATCH] Store folder note in parent folder option --- src/events/handleRename.ts | 21 ++++-------- src/folderNoteFunctions.ts | 67 +++++++++++++++++++++++++------------- src/main.ts | 48 ++++++++++++++++++++++++--- src/settings.ts | 33 +++++++++++++++++++ 4 files changed, 128 insertions(+), 41 deletions(-) diff --git a/src/events/handleRename.ts b/src/events/handleRename.ts index 7e47218..d7f29ab 100644 --- a/src/events/handleRename.ts +++ b/src/events/handleRename.ts @@ -1,6 +1,6 @@ import { TFile, TFolder, TAbstractFile, Notice } from 'obsidian'; import FolderNotesPlugin from 'src/main'; -import { extractFolderName } from '../folderNoteFunctions'; +import { extractFolderName, getFolderNote } from '../folderNoteFunctions'; export function handleFolderRename(file: TFolder, oldPath: string, plugin: FolderNotesPlugin) { const oldFileName = plugin.getFileNameFromPathString(oldPath); const folder = plugin.app.vault.getAbstractFileByPath(file.path); @@ -24,23 +24,14 @@ export function handleFolderRename(file: TFolder, oldPath: string, plugin: Folde }); plugin.saveSettings(); const excludedFolder = plugin.getExcludedFolderByPath(file.path); - - const folderNotePath = oldPath + '/' + plugin.settings.folderNoteName.replace('{{folder_name}}', oldFileName) + '.md'; - let note = plugin.app.vault.getAbstractFileByPath(folderNotePath) || plugin.app.vault.getAbstractFileByPath(folderNotePath.slice(0, -3) + '.canvas'); - if (!(note instanceof TFile)) { - note = plugin.app.vault.getAbstractFileByPath(`${oldPath}/${oldFileName}.md`) || plugin.app.vault.getAbstractFileByPath(`${oldPath}/${oldFileName}.canvas`); - if (!(note instanceof TFile)) { return; } - note.path = folder.path + '/' + oldFileName + plugin.getExtensionFromPathString(note.path); - } else { - note.path = folder.path + '/' + plugin.settings.folderNoteName.replace('{{folder_name}}', oldFileName) + plugin.getExtensionFromPathString(note.path); - } - - const newPath = folder.path + '/' + plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name) + plugin.getExtensionFromPathString(note.path); - if (excludedFolder?.disableSync && !plugin.app.vault.getAbstractFileByPath(newPath)) { + const folderNote = getFolderNote(plugin, oldPath); + if (excludedFolder?.disableSync && !folderNote) { return plugin.removeCSSClassFromEL(file.path, 'has-folder-note'); } - plugin.app.vault.rename(note, newPath); + if (!(folderNote instanceof TFile)) return; + const newPath = folder.path + '/' + plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name) + '.' + folderNote.extension; + plugin.app.vault.rename(folderNote, newPath); } export function handleFileRename(file: TFile, oldPath: string, plugin: FolderNotesPlugin) { diff --git a/src/folderNoteFunctions.ts b/src/folderNoteFunctions.ts index fb2c0b5..b2d0072 100644 --- a/src/folderNoteFunctions.ts +++ b/src/folderNoteFunctions.ts @@ -8,7 +8,17 @@ export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: st const leaf = plugin.app.workspace.getLeaf(false); const folderName = plugin.getFolderNameFromPathString(folderPath); const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folderName); - const path = `${folderPath}/${fileName}${plugin.settings.folderNoteType}`; + let path = `${folderPath}/${fileName}${plugin.settings.folderNoteType}`; + if (plugin.settings.storageLocation === 'parentFolder') { + const parentFolderPath = plugin.getParentFolderPathFromPathString(folderPath); + 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}`; + } const file = await plugin.app.vault.create(path, ''); if (openFile) { await leaf.openFile(file); @@ -16,12 +26,14 @@ export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: st if (file) { applyTemplate(this, file, plugin.settings.templatePath); } - plugin.addCSSClassToTitleEL(path, 'is-folder-note', true); - plugin.addCSSClassToTitleEL(file.parent.path, 'has-folder-note'); - if (!plugin.settings.autoCreate) return; - if (!useModal) return; + const folder = plugin.app.vault.getAbstractFileByPath(folderPath); if (!(folder instanceof TFolder)) return; + plugin.addCSSClassToTitleEL(path, 'is-folder-note', true); + plugin.addCSSClassToTitleEL(folder.path, 'has-folder-note'); + + if (!plugin.settings.autoCreate) return; + if (!useModal) return; const modal = new FolderNameModal(plugin.app, plugin, folder); modal.open(); } @@ -66,30 +78,41 @@ export function getFolderNote(plugin: FolderNotesPlugin, folderPath: string) { name: plugin.getFolderNameFromPathString(folderPath), }; const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folder.name); - let path = `${folder.path}/${fileName}${plugin.settings.folderNoteType}`; - let folderNote = plugin.app.vault.getAbstractFileByPath(path); - + if (plugin.settings.storageLocation === 'parentFolder') { + folder.path = plugin.getParentFolderPathFromPathString(folderPath); + } + let path = `${folder.path}/${fileName}`; + if (folder.path.trim() === '') { + path = fileName; + } + let folderNote = plugin.app.vault.getAbstractFileByPath(path + plugin.settings.folderNoteType); if (folderNote instanceof TFile) { return folderNote; } else { if (plugin.settings.folderNoteType === '.canvas') { - folderNote = plugin.app.vault.getAbstractFileByPath(`${folder.path}/${fileName}.md`); + folderNote = plugin.app.vault.getAbstractFileByPath(path + '.md'); } else { - folderNote = plugin.app.vault.getAbstractFileByPath(`${folder.path}/${fileName}.canvas`); + folderNote = plugin.app.vault.getAbstractFileByPath(path + '.canvas'); } - if (!(folderNote instanceof TFile)) { - path = `${folder.path}/${folder.name}${plugin.settings.folderNoteType}`; - folderNote = plugin.app.vault.getAbstractFileByPath(path); - if (!(folderNote instanceof TFile)) { - if (plugin.settings.folderNoteType === '.canvas') { - folderNote = plugin.app.vault.getAbstractFileByPath(`${folder.path}/${folder.name}.md`); - } else { - folderNote = plugin.app.vault.getAbstractFileByPath(`${folder.path}/${folder.name}.canvas`); - } - return folderNote; + if (!folderNote && plugin.settings.storageLocation === 'parentFolder') { + folderNote = plugin.app.vault.getAbstractFileByPath(`${folderPath}/${fileName}.md`); + if (!folderNote && plugin.settings.folderNoteType === '.canvas') { + folderNote = plugin.app.vault.getAbstractFileByPath(`${folderPath}/${fileName}.canvas`); + } + } else if (!folderNote) { + folder.path = plugin.getParentFolderPathFromPathString(folderPath); + path = `${folder.path}/${fileName}`; + if (folder.path.trim() === '') { + path = fileName; + } + folderNote = plugin.app.vault.getAbstractFileByPath(path + plugin.settings.folderNoteType); + if (!folderNote && plugin.settings.folderNoteType === '.canvas') { + folderNote = plugin.app.vault.getAbstractFileByPath(path + '.md'); + } else if (!folderNote) { + folderNote = plugin.app.vault.getAbstractFileByPath(path + '.canvas'); } - } else { - return folderNote; } + + return folderNote; } } diff --git a/src/main.ts b/src/main.ts index 403c245..5951e26 100644 --- a/src/main.ts +++ b/src/main.ts @@ -115,6 +115,15 @@ export default class FolderNotesPlugin extends Plugin { } })); + this.registerEvent(this.app.vault.on('delete', (file: TAbstractFile) => { + if (!(file instanceof TFolder)) { return; } + const folderNote = getFolderNote(this, file.path); + if (!folderNote) { return; } + this.removeCSSClassFromEL(folderNote.path, 'is-folder-note'); + if (!this.settings.syncDelete) { return; } + this.app.vault.delete(folderNote); + })); + if (this.app.workspace.layoutReady) { this.loadFileClasses(); } else { @@ -140,12 +149,16 @@ export default class FolderNotesPlugin extends Plugin { } } + getParentFolderPathFromPathString(path: string): string { + return path.substring(0, path.lastIndexOf('/' || '\\') >= 0 ? path.lastIndexOf('/' || '\\') : 0); + } + getExtensionFromPathString(path: string): string { return path.slice(path.lastIndexOf('.') >= 0 ? path.lastIndexOf('.') : 0); } getFolderPathFromString(path: string): string { - const subString = path.lastIndexOf('/' || '\\') >= 0 ? path.lastIndexOf('/') : path.length; + const subString = path.lastIndexOf('/' || '\\') >= 0 ? path.lastIndexOf('/') : 0; return path.substring(0, subString); } @@ -209,16 +222,43 @@ export default class FolderNotesPlugin extends Plugin { if (this.activeFileExplorer === this.getFileExplorer() && !forceReload) { return; } this.activeFileExplorer = this.getFileExplorer(); this.app.vault.getFiles().forEach((file) => { - if (extractFolderName(this.settings.folderNoteName, file.basename) !== file.parent.name) { return; } + let folder: TFolder | TAbstractFile | null; + const folderName = extractFolderName(this.settings.folderNoteName, file.basename); + if (!folderName) { return; } + if (this.settings.storageLocation === 'parentFolder') { + let folderPath = this.getFolderPathFromString(file.path); + if (folderPath.trim() === '') { + folderPath = folderName; + } else { + folderPath = `${folderPath}/${folderName}`; + } + folder = this.app.vault.getAbstractFileByPath(folderPath); + if (!folder) { + folder = this.app.vault.getAbstractFileByPath(file.parent.path); + } + } else { + folder = this.app.vault.getAbstractFileByPath(file.parent.path); + if (!(folder instanceof TFolder) || extractFolderName(this.settings.folderNoteName, file.basename) !== folder.name) { + let folderPath = this.getFolderPathFromString(file.path); + if (folderPath.trim() === '') { + folderPath = folderName; + } else { + folderPath = `${folderPath}/${folderName}`; + } + folder = this.app.vault.getAbstractFileByPath(folderPath); + } + } + if (!(folder instanceof TFolder)) { return; } + const excludedFolder = this.getExcludedFolderByPath(file.parent.path); // cleanup after ourselves // Incase settings have changed if (excludedFolder?.disableFolderNote) { this.removeCSSClassFromEL(file.path, 'is-folder-note'); - this.removeCSSClassFromEL(file.parent.path, 'has-folder-note'); + this.removeCSSClassFromEL(folder.path, 'has-folder-note'); return; } - this.addCSSClassToTitleEL(file.parent.path, 'has-folder-note'); + this.addCSSClassToTitleEL(folder.path, 'has-folder-note'); this.addCSSClassToTitleEL(file.path, 'is-folder-note'); }); } diff --git a/src/settings.ts b/src/settings.ts index 8f95a0d..af18cce 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -24,6 +24,8 @@ export interface FolderNotesSettings { newFolderNoteName: string; folderNoteType: '.md' | '.canvas'; disableFolderHighlighting: boolean; + storageLocation: 'insideFolder' | 'parentFolder' | 'vaultFolder'; + syncDelete: boolean; } export const DEFAULT_SETTINGS: FolderNotesSettings = { @@ -45,6 +47,8 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = { folderNoteType: '.md', disableFolderHighlighting: false, newFolderNoteName: '{{folder_name}}', + storageLocation: 'insideFolder', + syncDelete: false, }; export class SettingsTab extends PluginSettingTab { plugin: FolderNotesPlugin; @@ -95,6 +99,35 @@ export class SettingsTab extends PluginSettingTab { }) ); + new Setting(containerEl) + .setName('Storage location') + .setDesc('Choose where to store the folder notes') + .addDropdown((dropdown) => + dropdown + .addOption('insideFolder', 'Inside the folder') + .addOption('parentFolder', 'In the parent folder') + .setValue(this.plugin.settings.storageLocation) + .onChange(async (value: 'insideFolder' | 'parentFolder' | 'vaultFolder') => { + this.plugin.settings.storageLocation = value; + await this.plugin.saveSettings(); + this.display(); + }) + ); + + if (this.plugin.settings.storageLocation === 'parentFolder') { + new Setting(containerEl) + .setName('Delete folder notes when deleting the folder') + .setDesc('Delete the folder note when deleting the folder') + .addToggle((toggle) => + toggle + .setValue(this.plugin.settings.syncDelete) + .onChange(async (value) => { + this.plugin.settings.syncDelete = value; + await this.plugin.saveSettings(); + } + ) + ); + } const disableSetting = new Setting(containerEl); disableSetting.setName('Disable folder collapsing');