diff --git a/src/main.ts b/src/main.ts index 5a175fa..5f87f9f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import {MarkdownView, Notice, parseYaml, Plugin, stringifyYaml, TFile, TFolder} import {getDefaultSettings, MediaDbPluginSettings, MediaDbSettingTab} from './settings/Settings'; import {APIManager} from './api/APIManager'; import {MediaTypeModel} from './models/MediaTypeModel'; -import {dateTimeToString, markdownTable, replaceIllegalFileNameCharactersInString} from './utils/Utils'; +import {CreateNoteOptions, dateTimeToString, markdownTable, replaceIllegalFileNameCharactersInString} from './utils/Utils'; import {OMDbAPI} from './api/apis/OMDbAPI'; import {MALAPI} from './api/apis/MALAPI'; import {WikipediaAPI} from './api/apis/WikipediaAPI'; @@ -179,9 +179,9 @@ export default class MediaDbPlugin extends Plugin { return; } - proceed = await this.modalHelper.openPreviewModal(selectResults, async () => { - return true; - }) + proceed = await this.modalHelper.openPreviewModal({elements: selectResults}, async (previewModalData) => { + return previewModalData.confirmed; + }); } await this.createMediaDbNotes(selectResults); @@ -194,14 +194,14 @@ export default class MediaDbPlugin extends Plugin { while (!proceed) { idSearchResult = await this.modalHelper.openIdSearchModal({}, async (idSearchModalData) => { return await this.apiManager.queryDetailedInfoById(idSearchModalData.query, idSearchModalData.api); - }) + }); if (!idSearchResult) { return; } - proceed = await this.modalHelper.openPreviewModal([idSearchResult], async () => { - return true; - }) + proceed = await this.modalHelper.openPreviewModal({elements: [idSearchResult]}, async (previewModalData) => { + return previewModalData.confirmed; + }); } await this.createMediaDbNoteFromModel(idSearchResult, {attachTemplate: true, openNote: true}); @@ -226,11 +226,11 @@ export default class MediaDbPlugin extends Plugin { return detailModels; } - async createMediaDbNoteFromModel(mediaTypeModel: MediaTypeModel, options: { attachTemplate?: boolean, attachFile?: TFile, openNote?: boolean }): Promise { + async createMediaDbNoteFromModel(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions): Promise { try { console.debug('MDB | creating new note'); - let fileContent = await this.generateMediaDbNoteContents(mediaTypeModel, {attachTemplate: options.attachTemplate, attachFile: options.attachFile}); + let fileContent = await this.generateMediaDbNoteContents(mediaTypeModel, options); await this.createNote(this.mediaTypeManager.getFileName(mediaTypeModel), fileContent, options.openNote); } catch (e) { @@ -239,15 +239,13 @@ export default class MediaDbPlugin extends Plugin { } } - async generateMediaDbNoteContents(mediaTypeModel: MediaTypeModel, options: {attachTemplate?: boolean, attachFile?: TFile}) { + async generateMediaDbNoteContents(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions) { let fileMetadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject()); let fileContent = ''; + const template = options.attachTemplate ? await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app) : ''; ({fileMetadata, fileContent} = await this.attachFile(fileMetadata, fileContent, options.attachFile)); - ({ - fileMetadata, - fileContent, - } = await this.attachTemplate(fileMetadata, fileContent, options.attachTemplate ? await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app) : '')); + ({fileMetadata, fileContent} = await this.attachTemplate(fileMetadata, fileContent, template)); fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(fileMetadata) : stringifyYaml(fileMetadata)}---\n` + fileContent; return fileContent; diff --git a/src/modals/MediaDbPreviewModal.ts b/src/modals/MediaDbPreviewModal.ts index 51268ca..6e4934d 100644 --- a/src/modals/MediaDbPreviewModal.ts +++ b/src/modals/MediaDbPreviewModal.ts @@ -1,70 +1,78 @@ -import { MarkdownRenderer, Modal, Setting, TFile } from "obsidian"; -import MediaDbPlugin from "src/main"; -import { MediaTypeModel } from "src/models/MediaTypeModel"; +import {ButtonComponent, MarkdownRenderer, Modal, Setting} from 'obsidian'; +import MediaDbPlugin from 'src/main'; +import {MediaTypeModel} from 'src/models/MediaTypeModel'; +import {PREVIEW_MODAL_DEFAULT_OPTIONS, PreviewModalData, PreviewModalOptions} from '../utils/ModalHelper'; +import {CreateNoteOptions} from '../utils/Utils'; export class MediaDbPreviewModal extends Modal { - selectedSearchResults: MediaTypeModel[]; - options: { attachTemplate: boolean, attachFile: TFile}; - submitCallback: (res: boolean) => void; - closeCallback: (err?: Error) => void; - plugin: MediaDbPlugin; - searchBtn: any; - cancelButton: any; - submitButton: any; - busy: any; + plugin: MediaDbPlugin; - constructor(plugin: MediaDbPlugin, mediaTypeModel: MediaTypeModel[], options: any) { - super(plugin.app); - this.plugin = plugin; - this.selectedSearchResults = mediaTypeModel; - this.options = options; - } + createNoteOptions: CreateNoteOptions; + elements: MediaTypeModel[]; + isBusy: boolean; + title: string; + cancelButton: ButtonComponent; + submitButton: ButtonComponent; - setSubmitCallback(submitCallback: (res: boolean) => void): void { - this.submitCallback = submitCallback; - } + submitCallback: (previewModalData: PreviewModalData) => void; + closeCallback: (err?: Error) => void; - setCloseCallback(closeCallback: (err?: Error) => void): void { - this.closeCallback = closeCallback; - } + constructor(plugin: MediaDbPlugin, previewModalOptions: PreviewModalOptions) { + previewModalOptions = Object.assign({}, PREVIEW_MODAL_DEFAULT_OPTIONS, previewModalOptions); - async preview(): Promise { - let { contentEl } = this; - for (let result of this.selectedSearchResults) { - let fileContent = await this.plugin.generateMediaDbNoteContents(result, { attachTemplate: this.options.attachTemplate, attachFile: this.options.attachFile }); - this.contentEl.createEl("h3", {text: result.englishTitle}); - const fileDiv = this.contentEl.createDiv(); - fileContent = `\n${fileContent}\n`; - MarkdownRenderer.renderMarkdown(fileContent, fileDiv, null, null); - } + super(plugin.app); - contentEl.createDiv({ cls: 'media-db-plugin-spacer' }); + this.plugin = plugin; + this.title = previewModalOptions.modalTitle; + this.elements = previewModalOptions.elements; + this.createNoteOptions = previewModalOptions.createNoteOptions; + } - const bottomSettingRow = new Setting(contentEl); - bottomSettingRow.addButton(btn => { - btn.setButtonText('Cancel'); - btn.onClick(() => this.closeCallback()); - btn.buttonEl.addClass('media-db-plugin-button'); - this.cancelButton = btn; - }); - bottomSettingRow.addButton(btn => { - btn.setButtonText('Ok'); - btn.setCta(); - btn.onClick(() => this.submitCallback(true)); - btn.buttonEl.addClass('media-db-plugin-button'); - this.submitButton = btn; - }) - } + setSubmitCallback(submitCallback: (previewModalData: PreviewModalData) => void): void { + this.submitCallback = submitCallback; + } - submit() { - if (!this.busy) { - this.busy = true; - this.submitButton.setButtonText('Creating entry...'); - this.submitCallback(true); - } - } + setCloseCallback(closeCallback: (err?: Error) => void): void { + this.closeCallback = closeCallback; + } - onOpen(): void { - this.preview() - } -} \ No newline at end of file + async preview(): Promise { + let {contentEl} = this; + contentEl.addClass('media-db-plugin-preview-modal'); + + contentEl.createEl('h2', {text: this.title}); + + const previewWrapper = contentEl.createDiv({cls: 'media-db-plugin-preview-wrapper'}); + + for (let result of this.elements) { + previewWrapper.createEl('h3', {text: result.englishTitle}); + const fileDiv = previewWrapper.createDiv(); + + let fileContent = await this.plugin.generateMediaDbNoteContents(result, this.createNoteOptions); + fileContent = `\n${fileContent}\n`; + + MarkdownRenderer.renderMarkdown(fileContent, fileDiv, null, null); + } + + contentEl.createDiv({cls: 'media-db-plugin-spacer'}); + + const bottomSettingRow = new Setting(contentEl); + bottomSettingRow.addButton(btn => { + btn.setButtonText('Cancel'); + btn.onClick(() => this.closeCallback()); + btn.buttonEl.addClass('media-db-plugin-button'); + this.cancelButton = btn; + }); + bottomSettingRow.addButton(btn => { + btn.setButtonText('Ok'); + btn.setCta(); + btn.onClick(() => this.submitCallback({confirmed: true})); + btn.buttonEl.addClass('media-db-plugin-button'); + this.submitButton = btn; + }); + } + + onOpen(): void { + this.preview(); + } +} diff --git a/src/modals/SelectModal.ts b/src/modals/SelectModal.ts index 5f726ee..b3a3a87 100644 --- a/src/modals/SelectModal.ts +++ b/src/modals/SelectModal.ts @@ -79,11 +79,11 @@ export abstract class SelectModal extends Modal { async onOpen() { const {contentEl} = this; + contentEl.addClass('media-db-plugin-select-modal'); + contentEl.createEl('h2', {text: this.title}); contentEl.createEl('p', {text: this.description}); - contentEl.addClass('media-db-plugin-select-modal'); - this.elementWrapper = contentEl.createDiv({cls: 'media-db-plugin-select-wrapper'}); this.elementWrapper.tabIndex = 0; diff --git a/src/settings/PropertyMappingModelComponent.svelte b/src/settings/PropertyMappingModelComponent.svelte index a0a3b68..6fe5227 100644 --- a/src/settings/PropertyMappingModelComponent.svelte +++ b/src/settings/PropertyMappingModelComponent.svelte @@ -46,16 +46,16 @@
- { /if } - { /if } + { /if } + { /if } - { /each } + { /each } { #if !validationResult?.res }
{validationResult?.err?.message}
- { /if } + { /if }