From 9a521a174f0b65bcf5e2df94f561698c6ce7b7e0 Mon Sep 17 00:00:00 2001 From: ltctceplrm <14954927+ltctceplrm@users.noreply.github.com> Date: Wed, 5 Mar 2025 15:57:19 +0100 Subject: [PATCH] Added confirmation before overwriting an existing note --- src/main.ts | 13 ++++++-- src/modals/ConfirmOverwriteModal.ts | 46 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/modals/ConfirmOverwriteModal.ts diff --git a/src/main.ts b/src/main.ts index bdb02bb..1846627 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,6 +13,7 @@ import { SteamAPI } from './api/apis/SteamAPI'; import { WikipediaAPI } from './api/apis/WikipediaAPI'; import { ComicVineAPI } from './api/apis/ComicVineAPI'; import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal'; +import { ConfirmOverwriteModal } from './modals/ConfirmOverwriteModal'; import type { MediaTypeModel } from './models/MediaTypeModel'; import { PropertyMapper } from './settings/PropertyMapper'; import { PropertyMapping, PropertyMappingModel } from './settings/PropertyMapping'; @@ -508,9 +509,17 @@ export default class MediaDbPlugin extends Plugin { fileName = replaceIllegalFileNameCharactersInString(fileName); const filePath = `${folder.path}/${fileName}.md`; - // find and delete file with the same name + // look if file already exists and ask if it should be overwritten const file = this.app.vault.getAbstractFileByPath(filePath); if (file) { + const shouldOverwrite = await new Promise(resolve => { + new ConfirmOverwriteModal(this.app, fileName, resolve).open(); + }); + + if (!shouldOverwrite) { + throw new Error('MDB | file creation cancelled by user'); + } + await this.app.vault.delete(file); } @@ -518,7 +527,7 @@ export default class MediaDbPlugin extends Plugin { const targetFile = await this.app.vault.create(filePath, fileContent); console.debug(`MDB | created new file at ${filePath}`); - // open newly crated file + // open newly created file if (options.openNote) { const activeLeaf = this.app.workspace.getUnpinnedLeaf(); if (!activeLeaf) { diff --git a/src/modals/ConfirmOverwriteModal.ts b/src/modals/ConfirmOverwriteModal.ts new file mode 100644 index 0000000..501afea --- /dev/null +++ b/src/modals/ConfirmOverwriteModal.ts @@ -0,0 +1,46 @@ +import type { App } from 'obsidian'; +import { Modal, Setting } from 'obsidian'; + +export class ConfirmOverwriteModal extends Modal { + result: boolean = false; + onSubmit: (result: boolean) => void; + fileName: string; + + constructor(app: App, fileName: string, onSubmit: (result: boolean) => void) { + super(app); + this.fileName = fileName; + this.onSubmit = onSubmit; + } + + onOpen() { + const { contentEl } = this; + contentEl.createEl('h2', { text: 'File already exists' }); + contentEl.createEl('p', { text: `The file "${this.fileName}" already exists. Do you want to overwrite it?` }); + + const buttonContainer = contentEl.createDiv({ cls: 'modal-button-container' }); + + new Setting(buttonContainer) + .addButton(btn => { + btn.setButtonText('Yes'); + btn.onClick(() => { + this.result = true; + this.close(); + }); + btn.buttonEl.addClass('media-db-plugin-button'); + }) + .addButton(btn => { + btn.setButtonText('No'); + btn.onClick(() => { + this.result = false; + this.close(); + }); + btn.buttonEl.addClass('media-db-plugin-button'); + }); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + this.onSubmit(this.result); + } +}