Added confirmation before overwriting an existing note
This commit is contained in:
parent
d54822444a
commit
9a521a174f
2 changed files with 57 additions and 2 deletions
13
src/main.ts
13
src/main.ts
|
|
@ -13,6 +13,7 @@ import { SteamAPI } from './api/apis/SteamAPI';
|
||||||
import { WikipediaAPI } from './api/apis/WikipediaAPI';
|
import { WikipediaAPI } from './api/apis/WikipediaAPI';
|
||||||
import { ComicVineAPI } from './api/apis/ComicVineAPI';
|
import { ComicVineAPI } from './api/apis/ComicVineAPI';
|
||||||
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal';
|
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal';
|
||||||
|
import { ConfirmOverwriteModal } from './modals/ConfirmOverwriteModal';
|
||||||
import type { MediaTypeModel } from './models/MediaTypeModel';
|
import type { MediaTypeModel } from './models/MediaTypeModel';
|
||||||
import { PropertyMapper } from './settings/PropertyMapper';
|
import { PropertyMapper } from './settings/PropertyMapper';
|
||||||
import { PropertyMapping, PropertyMappingModel } from './settings/PropertyMapping';
|
import { PropertyMapping, PropertyMappingModel } from './settings/PropertyMapping';
|
||||||
|
|
@ -508,9 +509,17 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
fileName = replaceIllegalFileNameCharactersInString(fileName);
|
fileName = replaceIllegalFileNameCharactersInString(fileName);
|
||||||
const filePath = `${folder.path}/${fileName}.md`;
|
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);
|
const file = this.app.vault.getAbstractFileByPath(filePath);
|
||||||
if (file) {
|
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);
|
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);
|
const targetFile = await this.app.vault.create(filePath, fileContent);
|
||||||
console.debug(`MDB | created new file at ${filePath}`);
|
console.debug(`MDB | created new file at ${filePath}`);
|
||||||
|
|
||||||
// open newly crated file
|
// open newly created file
|
||||||
if (options.openNote) {
|
if (options.openNote) {
|
||||||
const activeLeaf = this.app.workspace.getUnpinnedLeaf();
|
const activeLeaf = this.app.workspace.getUnpinnedLeaf();
|
||||||
if (!activeLeaf) {
|
if (!activeLeaf) {
|
||||||
|
|
|
||||||
46
src/modals/ConfirmOverwriteModal.ts
Normal file
46
src/modals/ConfirmOverwriteModal.ts
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue