lostpaul_obsidian-folder-notes/src/modals/ExistingNote.ts

89 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-08-11 12:39:33 +00:00
import {
Modal,
Setting,
Platform,
type App,
type TFile,
type TFolder,
type TAbstractFile,
} from 'obsidian';
2025-07-27 09:33:21 +00:00
import type FolderNotesPlugin from '../main';
2023-06-28 21:01:13 +00:00
import { turnIntoFolderNote } from 'src/functions/folderNoteFunctions';
export default class ExistingFolderNoteModal extends Modal {
plugin: FolderNotesPlugin;
app: App;
file: TFile;
folder: TFolder;
folderNote: TAbstractFile;
2025-08-11 12:39:33 +00:00
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;
}
2025-08-11 12:39:33 +00:00
onOpen(): void {
const { contentEl } = this;
contentEl.createEl('h2', { text: 'A folder note for this folder already exists' });
const setting = new Setting(contentEl);
2025-08-11 12:39:33 +00:00
// eslint-disable-next-line max-len
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
2025-08-11 12:39:33 +00:00
// eslint-disable-next-line max-len
const buttonContainer = setting.infoEl.createEl('div', { cls: 'fn-delete-confirmation-modal-buttons' });
if (Platform.isMobileApp) {
2025-08-11 12:39:33 +00:00
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();
});
}
2025-08-11 12:39:33 +00:00
onClose(): void {
const { contentEl } = this;
contentEl.empty();
}
}