Skip to content

Commit ce04d93

Browse files
Merge pull request #184 from ltctceplrm/overwrite-confim
Add an overwrite confimation popup
2 parents d548224 + 9f37561 commit ce04d93

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/main.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { SteamAPI } from './api/apis/SteamAPI';
1313
import { WikipediaAPI } from './api/apis/WikipediaAPI';
1414
import { ComicVineAPI } from './api/apis/ComicVineAPI';
1515
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal';
16+
import { ConfirmOverwriteModal } from './modals/ConfirmOverwriteModal';
1617
import type { MediaTypeModel } from './models/MediaTypeModel';
1718
import { PropertyMapper } from './settings/PropertyMapper';
1819
import { PropertyMapping, PropertyMappingModel } from './settings/PropertyMapping';
@@ -508,17 +509,25 @@ export default class MediaDbPlugin extends Plugin {
508509
fileName = replaceIllegalFileNameCharactersInString(fileName);
509510
const filePath = `${folder.path}/${fileName}.md`;
510511

511-
// find and delete file with the same name
512+
// look if file already exists and ask if it should be overwritten
512513
const file = this.app.vault.getAbstractFileByPath(filePath);
513514
if (file) {
515+
const shouldOverwrite = await new Promise<boolean>(resolve => {
516+
new ConfirmOverwriteModal(this.app, fileName, resolve).open();
517+
});
518+
519+
if (!shouldOverwrite) {
520+
throw new Error('MDB | file creation cancelled by user');
521+
}
522+
514523
await this.app.vault.delete(file);
515524
}
516525

517526
// create the file
518527
const targetFile = await this.app.vault.create(filePath, fileContent);
519528
console.debug(`MDB | created new file at ${filePath}`);
520529

521-
// open newly crated file
530+
// open newly created file
522531
if (options.openNote) {
523532
const activeLeaf = this.app.workspace.getUnpinnedLeaf();
524533
if (!activeLeaf) {

src/modals/ConfirmOverwriteModal.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import type { App } from 'obsidian';
2+
import { Modal, Setting } from 'obsidian';
3+
4+
export class ConfirmOverwriteModal extends Modal {
5+
result: boolean = false;
6+
onSubmit: (result: boolean) => void;
7+
fileName: string;
8+
9+
constructor(app: App, fileName: string, onSubmit: (result: boolean) => void) {
10+
super(app);
11+
this.fileName = fileName;
12+
this.onSubmit = onSubmit;
13+
}
14+
15+
onOpen() {
16+
const { contentEl } = this;
17+
contentEl.createEl('h2', { text: 'File already exists' });
18+
contentEl.createEl('p', { text: `The file "${this.fileName}" already exists. Do you want to overwrite it?` });
19+
20+
contentEl.createDiv({ cls: 'media-db-plugin-spacer' });
21+
22+
const bottomSettingRow = new Setting(contentEl);
23+
bottomSettingRow.addButton(btn => {
24+
btn.setButtonText('No');
25+
btn.onClick(() => this.close());
26+
btn.buttonEl.addClass('media-db-plugin-button');
27+
});
28+
bottomSettingRow.addButton(btn => {
29+
btn.setButtonText('Yes');
30+
btn.setCta();
31+
btn.onClick(() => {
32+
this.result = true;
33+
this.close();
34+
});
35+
btn.buttonEl.addClass('media-db-plugin-button');
36+
});
37+
}
38+
39+
onClose() {
40+
const { contentEl } = this;
41+
contentEl.empty();
42+
this.onSubmit(this.result);
43+
}
44+
}

0 commit comments

Comments
 (0)