diff --git a/src/commands.ts b/src/commands.ts index e2cb6ce..8af7370 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -34,6 +34,7 @@ export class Commands { new Notice('Successfully excluded folder from folder notes'); }); }); + if (!(file instanceof TFolder)) return; const path = file.path + '/' + this.plugin.settings.folderNoteName.replace('{{folder_name}}', file.name) + this.plugin.settings.folderNoteType; let folderNote = this.plugin.app.vault.getAbstractFileByPath(path.slice(0, -this.plugin.settings.folderNoteType.length) + '.md') || this.plugin.app.vault.getAbstractFileByPath(path); if (!folderNote) { diff --git a/src/main.ts b/src/main.ts index f9f16f9..2da9212 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,6 +18,8 @@ export default class FolderNotesPlugin extends Plugin { await this.loadSettings(); this.settingsTab = new SettingsTab(this.app, this); this.addSettingTab(this.settingsTab); + this.settings.oldFolderNoteName = this.settings.folderNoteName; + this.saveSettings(); // Add CSS Classes document.body.classList.add('folder-notes-plugin'); @@ -69,7 +71,7 @@ export default class FolderNotesPlugin extends Plugin { if (!(file instanceof TFile)) { return; } // parent is null here even if the parent exists // not entirely sure why - const parentPath = this.getPathFromString(file.path); + const parentPath = this.getFolderPathFromString(file.path); const parentName = this.getNameFromPathString(parentPath); if (parentName !== file.basename) { return; } this.removeCSSClassFromEL(parentPath, 'has-folder-note'); @@ -236,10 +238,10 @@ export default class FolderNotesPlugin extends Plugin { handleFileRename(file: TFile, oldPath: string) { const oldFileName = this.getNameFromPathString(oldPath); - const oldFilePath = this.getPathFromString(oldPath); + const oldFilePath = this.getFolderPathFromString(oldPath); const fileExtension = this.getExtensionFromPathString(oldPath); const oldFolder = this.app.vault.getAbstractFileByPath(oldFilePath); - const newFilePath = this.getPathFromString(file.path); + const newFilePath = this.getFolderPathFromString(file.path); const newFolder = this.app.vault.getAbstractFileByPath(newFilePath); const excludedFolder = this.getExcludedFolderByPath(newFolder?.path || ''); @@ -299,6 +301,9 @@ export default class FolderNotesPlugin extends Plugin { 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; } @@ -325,13 +330,14 @@ export default class FolderNotesPlugin extends Plugin { if (file) { applyTemplate(this, file, this.settings.templatePath); } + this.addCSSClassToTitleEL(path, 'is-folder-note', true); + this.addCSSClassToTitleEL(file.parent.path, 'has-folder-note'); if (!this.settings.autoCreate) return; if (!useModal) return; - const folder = this.app.vault.getAbstractFileByPath(this.getPathFromString(path)); + const folder = this.app.vault.getAbstractFileByPath(this.getFolderPathFromString(path)); if (!(folder instanceof TFolder)) return; const modal = new FolderNameModal(this.app, this, folder); modal.open(); - this.addCSSClassToTitleEL(path, 'is-folder-note', true); } async openFolderNote(file: TAbstractFile, evt: MouseEvent) { @@ -347,6 +353,7 @@ export default class FolderNotesPlugin extends Plugin { if (this.settings.showDeleteConfirmation) { return new DeleteConfirmationModal(this.app, this, file).open(); } + this.removeCSSClassFromEL(file.parent.path, 'has-folder-note'); await this.app.vault.delete(file); } @@ -362,7 +369,7 @@ export default class FolderNotesPlugin extends Plugin { return path.slice(path.lastIndexOf('.') >= 0 ? path.lastIndexOf('.') : 0); } - getPathFromString(path: string): string { + getFolderPathFromString(path: string): string { const subString = path.lastIndexOf('/' || '\\') >= 0 ? path.lastIndexOf('/') : path.length; return path.substring(0, subString); } @@ -371,7 +378,7 @@ export default class FolderNotesPlugin extends Plugin { return this.settings.excludeFolders.find((excludedFolder) => { if (excludedFolder.path === path) { return true; } if (!excludedFolder.subFolders) { return false; } - return this.getPathFromString(path).startsWith(excludedFolder.path); + return this.getFolderPathFromString(path).startsWith(excludedFolder.path); }); } diff --git a/src/modals/deleteConfirmation.ts b/src/modals/deleteConfirmation.ts index 704c9bc..f2ee93b 100644 --- a/src/modals/deleteConfirmation.ts +++ b/src/modals/deleteConfirmation.ts @@ -28,6 +28,7 @@ export default class DeleteConfirmationModal extends Modal { this.plugin.settings.showDeleteConfirmation = false; this.plugin.saveSettings(); this.close(); + this.plugin.removeCSSClassFromEL(this.file.parent.path, 'has-folder-note'); this.app.vault.delete(this.file); }); } else { @@ -50,6 +51,7 @@ export default class DeleteConfirmationModal extends Modal { button.classList.add('mod-warning', 'fn-confirmation-modal-button'); button.addEventListener('click', async () => { this.close(); + this.plugin.removeCSSClassFromEL(this.file.parent.path, 'has-folder-note'); this.app.vault.delete(this.file); }); button.focus(); diff --git a/src/settings.ts b/src/settings.ts index 7e3c792..1780b75 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,4 +1,4 @@ -import { App, Platform, PluginSettingTab, Setting } from 'obsidian'; +import { App, Notice, Platform, PluginSettingTab, Setting, TFile, TFolder } from 'obsidian'; import FolderNotesPlugin from './main'; import { FolderSuggest } from './suggesters/FolderSuggester'; import ExcludedFolderSettings from './modals/exludeFolderSettings'; @@ -20,6 +20,7 @@ export interface FolderNotesSettings { openFolderNoteOnClickInPath: boolean; openInNewTab: boolean; folderNoteName: string; + oldFolderNoteName: string; folderNoteType: '.md' | '.canvas'; disableFolderHighlighting: boolean; } @@ -42,6 +43,7 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = { folderNoteName: '{{folder_name}}', folderNoteType: '.md', disableFolderHighlighting: false, + oldFolderNoteName: '{{folder_name}}', }; export class SettingsTab extends PluginSettingTab { plugin: FolderNotesPlugin; @@ -59,7 +61,7 @@ export class SettingsTab extends PluginSettingTab { new Setting(containerEl) .setName('Folder note name') - .setDesc('The name of the folder note') + .setDesc('{{folder_name}} will be replaced with the name of the folder') .addText((text) => text .setValue(this.plugin.settings.folderNoteName) @@ -74,6 +76,7 @@ export class SettingsTab extends PluginSettingTab { .setButtonText('Rename existing folder notes') .setCta() .onClick(async () => { + this.updateFolderNotes(this.plugin.settings.oldFolderNoteName, this.plugin.settings.folderNoteName); }) ); @@ -388,19 +391,44 @@ export class SettingsTab extends PluginSettingTab { }); }); } + addExcludedFolder(excludedFolder: ExcludedFolder) { this.plugin.settings.excludeFolders.push(excludedFolder); this.plugin.saveSettings(); } + deleteExcludedFolder(excludedFolder: ExcludedFolder) { this.plugin.settings.excludeFolders = this.plugin.settings.excludeFolders.filter((folder) => folder.path !== excludedFolder.path); this.plugin.saveSettings(); } + updateExcludedFolder(excludedFolder: ExcludedFolder, newExcludeFolder: ExcludedFolder) { this.plugin.settings.excludeFolders = this.plugin.settings.excludeFolders.filter((folder) => folder.path !== excludedFolder.path); this.addExcludedFolder(newExcludeFolder); } + updateFolderNotes(oldTemplate: string, newTemplate: string) { + this.plugin.settings.oldFolderNoteName = newTemplate; + this.plugin.saveSettings(); + new Notice('Starting to update folder notes...'); + this.app.vault.getFiles().forEach((file) => { + if (file instanceof TFile) { + const folder = this.app.vault.getAbstractFileByPath(this.plugin.getFolderPathFromString(file.path)); + if (!(folder instanceof TFolder)) return; + let fileName = file.name.slice(0, -file.extension.length - 1); + fileName = this.plugin.extractFolderName(oldTemplate, fileName) || ''; + if (fileName === folder?.name) { + const newPath = `${folder?.path}/${this.plugin.settings.folderNoteName.replace('{{folder_name}}', fileName)}.${file.extension}`; + this.app.vault.rename(file, newPath); + } else if (folder?.name === file.name.slice(0, -file.extension.length - 1) || '') { + const newPath = `${folder?.path}/${this.plugin.settings.folderNoteName.replace('{{folder_name}}', file.name.slice(0, -file.extension.length - 1) || '')}.${file.extension}`; + this.app.vault.rename(file, newPath); + } + } + }); + new Notice('Finished updating folder notes'); + } + } export class ExcludedFolder { path: string;