Modal for existing folder note when using turn into folder note

This commit is contained in:
Lost Paul 2023-06-16 21:51:29 +02:00
parent ed0421bcd5
commit 7d803748fe
2 changed files with 84 additions and 4 deletions

View file

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

View file

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