From bb5a8ae797bd59880ff8eb3455139ce2edc35c76 Mon Sep 17 00:00:00 2001 From: Lost Paul <70213368+LostPaul@users.noreply.github.com> Date: Mon, 20 Mar 2023 12:57:32 +0100 Subject: [PATCH] Delete folder note modal --- src/commands.ts | 29 +++++++++++++++++-------- src/main.ts | 5 +++++ src/modals/deleteConfirmation.ts | 37 ++++++++++++++++++++++++++++++++ src/settings.ts | 2 ++ 4 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 src/modals/deleteConfirmation.ts diff --git a/src/commands.ts b/src/commands.ts index 0ec03af..acccb98 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,4 +1,4 @@ -import { App, TFolder, Menu, TAbstractFile, Notice, Platform } from 'obsidian'; +import { App, TFolder, Menu, TAbstractFile, Notice, Platform, TFile } from 'obsidian'; import FolderNotesPlugin from './main'; import { ExcludedFolder } from './settings'; export class Commands { @@ -34,14 +34,25 @@ export class Commands { new Notice('Successfully excluded folder from folder notes'); }); }); - - menu.addItem((item) => { - item.setTitle('Create folder note') - .setIcon('plus') - .onClick(() => { - this.plugin.createFolderNote(file.path, true); - }); - }); + if (this.plugin.app.vault.getAbstractFileByPath(file.path + '/' + file.name + '.md')) { + menu.addItem((item) => { + item.setTitle('Delete folder note') + .setIcon('trash') + .onClick(() => { + file = this.plugin.app.vault.getAbstractFileByPath(file?.path + '/' + file?.name + '.md') as TFile; + if (!(file instanceof TFile)) return; + this.plugin.deleteFolderNote(file); + }); + }); + } else { + menu.addItem((item) => { + item.setTitle('Create folder note') + .setIcon('plus') + .onClick(() => { + this.plugin.createFolderNote(file.path, true); + }); + }); + } })); } } diff --git a/src/main.ts b/src/main.ts index 94cae54..4d445b5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { DEFAULT_SETTINGS, FolderNotesSettings, SettingsTab } from './settings'; import FolderNameModal from './modals/folderName'; import { applyTemplate } from './template'; import { Commands } from './commands'; +import DeleteConfirmationModal from './modals/deleteConfirmation'; export default class FolderNotesPlugin extends Plugin { observer: MutationObserver; folders: TFolder[] = []; @@ -209,6 +210,10 @@ export default class FolderNotesPlugin extends Plugin { } } + async deleteFolderNote(file: TFile) { + new DeleteConfirmationModal(this.app, this, file).open(); + } + onunload() { console.log('unloading folder notes plugin'); this.observer.disconnect(); diff --git a/src/modals/deleteConfirmation.ts b/src/modals/deleteConfirmation.ts new file mode 100644 index 0000000..315b35a --- /dev/null +++ b/src/modals/deleteConfirmation.ts @@ -0,0 +1,37 @@ +import { App, Modal, Setting, TFile } from 'obsidian'; +import FolderNotesPlugin from '../main'; +export default class DeleteConfirmationModal extends Modal { + plugin: FolderNotesPlugin; + app: App; + file: TFile; + constructor(app: App, plugin: FolderNotesPlugin, file: TFile) { + super(app); + this.plugin = plugin; + this.app = app; + this.file = file; + } + onOpen() { + const { contentEl } = this; + contentEl.createEl('h2', { text: 'Delete folder note' }); + const setting = new Setting(contentEl); + setting.infoEl.createEl('p', { text: `Are you sure you want to delete the folder note "${this.file.name}" ?` }); + setting.infoEl.createEl('p', { text: 'lt will be moved to your system trash.' }); + + setting.infoEl.parentElement?.classList.add('fn-delete-confirmation-modal'); + const button = setting.infoEl.createEl('button', { text: 'Delete' }); + button.classList.add('mod-warning', 'fn-confirmation-modal-button'); + button.addEventListener('click', async () => { + this.close(); + this.app.vault.delete(this.file); + }); + button.focus(); + const cancelButton = setting.infoEl.createEl('button', { text: 'Cancel' }); + cancelButton.addEventListener('click', async () => { + this.close(); + }); + } + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} diff --git a/src/settings.ts b/src/settings.ts index d24bb1d..c12196c 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -13,6 +13,7 @@ export interface FolderNotesSettings { autoCreate: boolean; enableCollapsing: boolean; excludeFolders: ExcludedFolder[]; + showDeleteConfirmation: boolean; } export const DEFAULT_SETTINGS: FolderNotesSettings = { @@ -24,6 +25,7 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = { autoCreate: false, enableCollapsing: false, excludeFolders: [], + showDeleteConfirmation: true, }; export class SettingsTab extends PluginSettingTab { plugin: FolderNotesPlugin;