Skip to content

Commit

Permalink
Option to delete folder note permanently, move to trash, ...
Browse files Browse the repository at this point in the history
Now you can choose if you want to delete the folder note permanently or just want to move it the system/obsidian trash like it's possible for normal files

This also fixes #142 & #120
  • Loading branch information
LostPaul committed Aug 6, 2024
1 parent 945c25f commit 7e26d0e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Commands {
if (!(folder instanceof TFolder)) return;
const folderNote = getFolderNote(this.plugin, folder.path);
if (!(folderNote instanceof TFile)) return;
deleteFolderNote(this.plugin, folderNote);
deleteFolderNote(this.plugin, folderNote, true);
}
});
this.plugin.addCommand({
Expand Down Expand Up @@ -298,7 +298,7 @@ export class Commands {
item.setTitle('Delete folder note')
.setIcon('trash')
.onClick(() => {
deleteFolderNote(this.plugin, folderNote);
deleteFolderNote(this.plugin, folderNote, true);
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/events/handleDelete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TAbstractFile, TFolder, TFile } from 'obsidian';
import FolderNotesPlugin from 'src/main';
import { getFolderNote, getFolder } from 'src/functions/folderNoteFunctions';
import { getFolderNote, getFolder, deleteFolderNote } from 'src/functions/folderNoteFunctions';
import { removeCSSClassFromEL, addCSSClassToTitleEL } from 'src/functions/styleFunctions';
import { getFolderPathFromString } from 'src/functions/utils';

Expand Down Expand Up @@ -28,5 +28,5 @@ export function handleDelete(file: TAbstractFile, plugin: FolderNotesPlugin) {
if (!folderNote) { return; }
removeCSSClassFromEL(folderNote.path, 'is-folder-note');
if (!plugin.settings.syncDelete) { return; }
plugin.app.vault.delete(folderNote);
deleteFolderNote(plugin, folderNote, false);
}
17 changes: 14 additions & 3 deletions src/functions/folderNoteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,25 @@ export async function openFolderNote(plugin: FolderNotesPlugin, file: TAbstractF
}
}

export async function deleteFolderNote(plugin: FolderNotesPlugin, file: TFile) {
if (plugin.settings.showDeleteConfirmation) {
export async function deleteFolderNote(plugin: FolderNotesPlugin, file: TFile, displayModal: boolean) {
if (plugin.settings.showDeleteConfirmation && displayModal) {
return new DeleteConfirmationModal(plugin.app, plugin, file).open();
}
const folder = getFolder(plugin, file);
if (!folder) return;
removeCSSClassFromEL(folder.path, 'has-folder-note');
await plugin.app.vault.delete(file);
switch (plugin.settings.deleteFilesAction) {
case 'trash':
await plugin.app.vault.trash(file, true);
break;
case 'obsidianTrash':
console.log('obsidianTrash');
await plugin.app.vault.trash(file, false);
break;
case 'delete':
await plugin.app.vault.delete(file);
break;
}
}

export function extractFolderName(template: string, changedFileName: string) {
Expand Down
81 changes: 41 additions & 40 deletions src/modals/DeleteConfirmation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { App, Modal, Setting, TFile, Platform } from 'obsidian';
import FolderNotesPlugin from '../main';
import { getFolder } from 'src/functions/folderNoteFunctions';
import { deleteFolderNote, getFolder } from 'src/functions/folderNoteFunctions';
import { removeCSSClassFromEL } from 'src/functions/styleFunctions';
export default class DeleteConfirmationModal extends Modal {
plugin: FolderNotesPlugin;
Expand All @@ -13,59 +13,60 @@ export default class DeleteConfirmationModal extends Modal {
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: 'It will be moved to your system trash.' });
const { contentEl, plugin } = this;
const modalTitle = contentEl.createDiv({ cls: 'fn-modal-title' });
const modalContent = contentEl.createDiv({ cls: 'fn-modal-content' });
modalTitle.createEl('h2', { text: 'Delete folder note' });
modalContent.createEl('p', { text: `Are you sure you want to delete the folder note "${this.file.name}" ?` });
switch (plugin.settings.deleteFilesAction) {
case 'trash':
modalContent.createEl('p', { text: 'It will be moved to your system trash.' });
break;
case 'obsidianTrash':
modalContent.createEl('p', { text: 'It will be moved to your Obsidian trash, which is located in the ".trash" hidden folder in your vault.' });
break;
case 'delete':
modalContent.createEl('p', { text: 'It will be permanently deleted.' }).setCssStyles({ color: 'red' });
break;
}

setting.infoEl.parentElement?.classList.add('fn-delete-confirmation-modal');
const buttonContainer = contentEl.createEl('div', { cls: 'modal-button-container' });

// Create a container for the buttons and the checkbox
const buttonContainer = setting.infoEl.createEl('div', { cls: 'fn-delete-confirmation-modal-buttons' });
if (Platform.isMobileApp) {
const confirmButton = buttonContainer.createEl('button', { text: 'Delete and don\'t ask again' });
confirmButton.classList.add('mod-warning', 'fn-confirmation-modal-button');
confirmButton.addEventListener('click', async () => {
this.plugin.settings.showDeleteConfirmation = false;
this.plugin.saveSettings();
this.close();
const folder = getFolder(this.plugin, this.file);
if (!folder) return;
removeCSSClassFromEL(folder?.path, 'has-folder-note');
this.app.vault.delete(this.file);
});
} else {
const checkbox = buttonContainer.createEl('input', { type: 'checkbox' });
checkbox.addEventListener('change', (e) => {
if (!Platform.isMobile) {
const checkbox = buttonContainer.createEl('label', { cls: 'mod-checkbox' })
checkbox.tabIndex = -1
const input = checkbox.createEl('input', { type: 'checkbox' });
checkbox.appendText('Don\'t ask again')
input.addEventListener('change', (e) => {
const target = e.target as HTMLInputElement;
if (target.checked) {
this.plugin.settings.showDeleteConfirmation = false;
plugin.settings.showDeleteConfirmation = false;
} else {
this.plugin.settings.showDeleteConfirmation = true;
plugin.settings.showDeleteConfirmation = true;
}
this.plugin.saveSettings();
plugin.saveSettings();
});
const checkBoxText = buttonContainer.createEl('span', { text: 'Don\'t ask again' });
checkBoxText.addEventListener('click', () => {
checkbox.click();
} else {
const confirmButton = buttonContainer.createEl('button', { text: 'Delete and don\'t ask again', cls: 'mod-destructive' });
confirmButton.addEventListener('click', async () => {
plugin.settings.showDeleteConfirmation = false;
plugin.saveSettings();
this.close();
deleteFolderNote(plugin, this.file, false);
});
}
const button = buttonContainer.createEl('button', { text: 'Delete' });
button.classList.add('mod-warning', 'fn-confirmation-modal-button');
button.addEventListener('click', async () => {

const deleteButton = buttonContainer.createEl('button', { text: 'Delete', cls: 'mod-warning' });
deleteButton.addEventListener('click', async () => {
this.close();
const folder = getFolder(this.plugin, this.file);
if (!folder) return;
removeCSSClassFromEL(folder.path, 'has-folder-note');
this.app.vault.delete(this.file);
deleteFolderNote(plugin, this.file, false);
});
button.focus();
const cancelButton = buttonContainer.createEl('button', { text: 'Cancel' });
deleteButton.focus();

const cancelButton = buttonContainer.createEl('button', { text: 'Cancel', cls: 'mod-cancel' });
cancelButton.addEventListener('click', async () => {
this.close();
});

}
onClose() {
const { contentEl } = this;
Expand Down
29 changes: 29 additions & 0 deletions src/settings/GeneralSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,35 @@ export async function renderGeneral(settingsTab: SettingsTab) {
settingsTab.display();
})
);

new Setting(containerEl)
.setName('Confirm folder note deletion')
.setDesc('Ask for confirmation before deleting a folder note')
.addToggle((toggle) =>
toggle
.setValue(settingsTab.plugin.settings.showDeleteConfirmation)
.onChange(async (value) => {
settingsTab.plugin.settings.showDeleteConfirmation = value;
await settingsTab.plugin.saveSettings();
settingsTab.display();
})
);

new Setting(containerEl)
.setName('Deleted folder notes')
.setDesc('What happens to the folder note after you delete it')
.addDropdown((dropdown) => {
dropdown.addOption('trash', 'Move to system trash');
dropdown.addOption('obsidianTrash', 'Move to Obsidian trash (.trash folder)');
dropdown.addOption('delete', 'Delete permanently');
dropdown.setValue(settingsTab.plugin.settings.deleteFilesAction);
dropdown.onChange(async (value: 'trash' | 'delete' | 'obsidianTrash') => {
settingsTab.plugin.settings.deleteFilesAction = value;
await settingsTab.plugin.saveSettings();
settingsTab.display();
});
});

if (Platform.isDesktop) {
const setting3 = new Setting(containerEl);
setting3.setName('Open folder note in a new tab by default');
Expand Down
2 changes: 2 additions & 0 deletions src/settings/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface FolderNotesSettings {
hideCollapsingIcon: boolean;
ignoreAttachmentFolder: boolean;
tabManagerEnabled: boolean;
deleteFilesAction: 'delete' | 'trash' | 'obsidianTrash';
}

export const DEFAULT_SETTINGS: FolderNotesSettings = {
Expand Down Expand Up @@ -147,6 +148,7 @@ export const DEFAULT_SETTINGS: FolderNotesSettings = {
hideCollapsingIcon: false,
tabManagerEnabled: true,
ignoreAttachmentFolder: true,
deleteFilesAction: 'trash',
};

export class SettingsTab extends PluginSettingTab {
Expand Down

0 comments on commit 7e26d0e

Please sign in to comment.