Cleanup and light changes
This commit is contained in:
parent
3a4b69717d
commit
918c5b6fd7
8 changed files with 190 additions and 113 deletions
|
|
@ -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<void> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
async preview(): Promise<void> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,11 +79,11 @@ export abstract class SelectModal<T> 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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue