Merge pull request #184 from ltctceplrm/overwrite-confim

Add an overwrite confimation popup
This commit is contained in:
Moritz Jung 2025-03-07 15:51:35 +01:00 committed by GitHub
commit ce04d93472
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 2 deletions

View file

@ -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<boolean>(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) {

View file

@ -0,0 +1,44 @@
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?` });
contentEl.createDiv({ cls: 'media-db-plugin-spacer' });
const bottomSettingRow = new Setting(contentEl);
bottomSettingRow.addButton(btn => {
btn.setButtonText('No');
btn.onClick(() => this.close());
btn.buttonEl.addClass('media-db-plugin-button');
});
bottomSettingRow.addButton(btn => {
btn.setButtonText('Yes');
btn.setCta();
btn.onClick(() => {
this.result = true;
this.close();
});
btn.buttonEl.addClass('media-db-plugin-button');
});
}
onClose() {
const { contentEl } = this;
contentEl.empty();
this.onSubmit(this.result);
}
}