From 7d803748feea741a23099dd693f45e220ef1474c Mon Sep 17 00:00:00 2001 From: Lost Paul <70213368+LostPaul@users.noreply.github.com> Date: Fri, 16 Jun 2023 21:51:29 +0200 Subject: [PATCH] Modal for existing folder note when using turn into folder note --- src/folderNoteFunctions.ts | 18 +++++++--- src/modals/existingNote.ts | 70 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 src/modals/existingNote.ts diff --git a/src/folderNoteFunctions.ts b/src/folderNoteFunctions.ts index 76e1d4b..7b7a070 100644 --- a/src/folderNoteFunctions.ts +++ b/src/folderNoteFunctions.ts @@ -1,11 +1,12 @@ import FolderNotesPlugin from './main'; import FolderNameModal from './modals/folderName'; +import ExistingFolderNoteModal from './modals/existingNote'; import { applyTemplate } from './template'; import { TFolder, TFile, TAbstractFile, Keymap } from 'obsidian'; import DeleteConfirmationModal from './modals/deleteConfirmation'; import { addExcludedFolder, deleteExcludedFolder, getExcludedFolder, ExcludedFolder, updateExcludedFolder } from './excludedFolder'; -export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: string, openFile: boolean, useModal?: boolean) { +export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: string, openFile: boolean, useModal?: boolean, existingNote?: TFile) { const leaf = plugin.app.workspace.getLeaf(false); const folderName = plugin.getFolderNameFromPathString(folderPath); const fileName = plugin.settings.folderNoteName.replace('{{folder_name}}', folderName); @@ -20,11 +21,17 @@ export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: st } else if (plugin.settings.storageLocation === 'vaultFolder') { path = `${fileName}${plugin.settings.folderNoteType}`; } - const file = await plugin.app.vault.create(path, ''); + let file: TFile; + if (!existingNote) { + file = await plugin.app.vault.create(path, ''); + } else { + file = existingNote; + plugin.app.vault.rename(existingNote, path); + } if (openFile) { await leaf.openFile(file); } - if (file) { + if (file && !existingNote) { applyTemplate(this, file, plugin.settings.templatePath); } @@ -39,8 +46,11 @@ export async function createFolderNote(plugin: FolderNotesPlugin, folderPath: st modal.open(); } -export async function turnIntoFolderNote(plugin: FolderNotesPlugin, file: TFile, folder: TFolder, folderNote?: TFile | null | TAbstractFile) { +export async function turnIntoFolderNote(plugin: FolderNotesPlugin, file: TFile, folder: TFolder, folderNote?: TFile | null | TAbstractFile, skipConfirmation?: boolean) { if (folderNote) { + if (plugin.settings.showRenameConfirmation && !skipConfirmation) { + return new ExistingFolderNoteModal(plugin.app, plugin, file, folder, folderNote).open(); + } plugin.removeCSSClassFromEL(folderNote.path, 'is-folder-note'); let excludedFolder = getExcludedFolder(plugin, folder.path); let excludedFolderExisted = true; diff --git a/src/modals/existingNote.ts b/src/modals/existingNote.ts new file mode 100644 index 0000000..a572c9b --- /dev/null +++ b/src/modals/existingNote.ts @@ -0,0 +1,70 @@ +import { App, Modal, Setting, TFile, Platform, TFolder, TAbstractFile } from 'obsidian'; +import FolderNotesPlugin from '../main'; +import { turnIntoFolderNote } from 'src/folderNoteFunctions'; +export default class ExistingFolderNoteModal extends Modal { + plugin: FolderNotesPlugin; + app: App; + file: TFile; + folder: TFolder; + folderNote: TAbstractFile; + constructor(app: App, plugin: FolderNotesPlugin, file: TFile, folder: TFolder, folderNote: TAbstractFile) { + super(app); + this.plugin = plugin; + this.app = app; + this.file = file; + this.folder = folder; + this.folderNote = folderNote; + } + onOpen() { + const { contentEl } = this; + contentEl.createEl('h2', { text: 'A folder note for this folder already exists' }); + const setting = new Setting(contentEl); + setting.infoEl.createEl('p', { text: 'Are you sure you want to turn the note into a folder note and rename the existing folder note?' }); + + setting.infoEl.parentElement?.classList.add('fn-delete-confirmation-modal'); + + // 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: 'Rename and don\'t ask again' }); + confirmButton.classList.add('mod-warning', 'fn-confirmation-modal-button'); + confirmButton.addEventListener('click', async () => { + this.plugin.settings.showRenameConfirmation = false; + this.plugin.saveSettings(); + this.close(); + turnIntoFolderNote(this.plugin, this.file, this.folder, this.folderNote, true); + }); + } else { + const checkbox = buttonContainer.createEl('input', { type: 'checkbox' }); + checkbox.addEventListener('change', (e) => { + const target = e.target as HTMLInputElement; + if (target.checked) { + this.plugin.settings.showRenameConfirmation = false; + } else { + this.plugin.settings.showRenameConfirmation = true; + } + }); + const checkBoxText = buttonContainer.createEl('span', { text: 'Don\'t ask again' }); + checkBoxText.addEventListener('click', () => { + checkbox.click(); + }); + } + const button = buttonContainer.createEl('button', { text: 'Rename' }); + button.classList.add('mod-warning', 'fn-confirmation-modal-button'); + button.addEventListener('click', async () => { + this.plugin.saveSettings(); + this.close(); + turnIntoFolderNote(this.plugin, this.file, this.folder, this.folderNote, true); + }); + button.focus(); + const cancelButton = buttonContainer.createEl('button', { text: 'Cancel' }); + cancelButton.addEventListener('click', async () => { + this.close(); + }); + + } + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +}