Delete folder note modal

This commit is contained in:
Lost Paul 2023-03-20 12:57:32 +01:00
parent b5f16456db
commit bb5a8ae797
4 changed files with 64 additions and 9 deletions

View file

@ -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);
});
});
}
}));
}
}

View file

@ -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();

View file

@ -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();
}
}

View file

@ -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;