Cleanup and light changes

This commit is contained in:
mProjectsCode 2022-10-09 15:40:37 +02:00
parent 3a4b69717d
commit 918c5b6fd7
8 changed files with 190 additions and 113 deletions

View file

@ -4,7 +4,8 @@ import {MediaTypeModel} from '../models/MediaTypeModel';
import {MediaDbSearchResultModal} from '../modals/MediaDbSearchResultModal';
import {Notice} from 'obsidian';
import MediaDbPlugin from '../main';
import { MediaDbPreviewModal } from 'src/modals/MediaDbPreviewModal';
import {MediaDbPreviewModal} from 'src/modals/MediaDbPreviewModal';
import {CreateNoteOptions} from './Utils';
export enum ModalResultCode {
@ -47,10 +48,21 @@ export interface SelectModalResult {
error?: Error,
}
/**
* Object containing the data {@link ModalHelper.createPreviewModal} returns.
* On {@link ModalResultCode.SUCCESS} this contains {@link PreviewModalData}.
* On {@link ModalResultCode.ERROR} this contains a reference to that error.
*/
export interface PreviewModalResult {
code: ModalResultCode.SUCCESS | ModalResultCode.CLOSE | ModalResultCode.ERROR,
data?: PreviewModalData,
error?: Error,
}
/**
* The data the advanced search modal returns.
* query: the query string
* apis: the selected APIs
* - query: the query string
* - apis: the selected APIs
*/
export interface AdvancedSearchModalData {
query: string,
@ -59,8 +71,8 @@ export interface AdvancedSearchModalData {
/**
* The data the id search modal returns.
* query: the query string
* apis: the selected APIs
* - query: the query string
* - apis: the selected APIs
*/
export interface IdSearchModalData {
query: string,
@ -69,17 +81,25 @@ export interface IdSearchModalData {
/**
* The data the select modal returns.
* selected: the selected items
* - selected: the selected items
*/
export interface SelectModalData {
selected: MediaTypeModel[],
}
/**
* The data the preview modal returns.
* - confirmed: whether the selected element has been confirmed
*/
export interface PreviewModalData {
confirmed: boolean,
}
/**
* Options for the advanced search modal.
* modalTitle: the title of the modal
* preselectedAPIs: a list of preselected APIs
* prefilledSearchString: prefilled query
* - modalTitle: the title of the modal
* - preselectedAPIs: a list of preselected APIs
* - prefilledSearchString: prefilled query
*/
export interface AdvancedSearchModalOptions {
modalTitle?: string,
@ -89,9 +109,9 @@ export interface AdvancedSearchModalOptions {
/**
* Options for the id search modal.
* modalTitle: the title of the modal
* preselectedAPIs: a list of preselected APIs
* prefilledSearchString: prefilled query
* - modalTitle: the title of the modal
* - preselectedAPIs: a list of preselected APIs
* - prefilledSearchString: prefilled query
*/
export interface IdSearchModalOptions {
modalTitle?: string,
@ -101,10 +121,10 @@ export interface IdSearchModalOptions {
/**
* Options for the select modal.
* modalTitle: the title of the modal
* elements: the elements the user can select from
* multiSelect: whether to allow multiselect
* skipButton: whether to add a skip button to the modal
* - modalTitle: the title of the modal
* - elements: the elements the user can select from
* - multiSelect: whether to allow multiselect
* - skipButton: whether to add a skip button to the modal
*/
export interface SelectModalOptions {
modalTitle?: string,
@ -113,6 +133,17 @@ export interface SelectModalOptions {
skipButton?: boolean,
}
/**
* Options for the preview modal.
* - modalTitle: the title of the modal
* - elements: the elements to preview
*/
export interface PreviewModalOptions {
modalTitle?: string,
elements?: MediaTypeModel[],
createNoteOptions?: CreateNoteOptions,
}
export const ADVANCED_SEARCH_MODAL_DEFAULT_OPTIONS: AdvancedSearchModalOptions = {
modalTitle: 'Media DB Advanced Search',
preselectedAPIs: [],
@ -132,6 +163,12 @@ export const SELECT_MODAL_OPTIONS_DEFAULT: SelectModalOptions = {
skipButton: false,
};
export const PREVIEW_MODAL_DEFAULT_OPTIONS: PreviewModalOptions = {
modalTitle: 'Media DB Preview',
elements: [],
createNoteOptions: {attachTemplate: true},
};
/**
* A class providing multiple usefull functions for dealing with the plugins modals.
*/
@ -332,33 +369,42 @@ export class ModalHelper {
}
}
async createPreviewModal(mediaTypeModel: MediaTypeModel[]): Promise<{ result: boolean, previewModal: MediaDbPreviewModal }> {
async createPreviewModal(previewModalOptions: PreviewModalOptions): Promise<{ previewModalResult: PreviewModalResult, previewModal: MediaDbPreviewModal }> {
//todo: handle attachFile for existing files
const modal = new MediaDbPreviewModal(this.plugin, mediaTypeModel, { attachTemplate: true, attachFile: false });
const booleanResult: boolean = await new Promise((resolve, reject) => {
modal.setSubmitCallback(res => resolve(res));
const modal = new MediaDbPreviewModal(this.plugin, previewModalOptions);
const res: PreviewModalResult = await new Promise((resolve, reject) => {
modal.setSubmitCallback(res => resolve({code: ModalResultCode.SUCCESS, data: res}));
modal.setCloseCallback(err => {
if (err) {
reject(err);
resolve({code: ModalResultCode.ERROR, error: err});
}
resolve(undefined);
resolve({code: ModalResultCode.CLOSE});
});
modal.open();
});
return { result: booleanResult, previewModal: modal };
return {previewModalResult: res, previewModal: modal};
}
async openPreviewModal(mediaModels: MediaTypeModel[], submitCallback: (result: boolean) => Promise<boolean>): Promise<boolean> {
const { result, previewModal } = await this.createPreviewModal(mediaModels);
if (!result) {
async openPreviewModal(previewModalOptions: PreviewModalOptions, submitCallback: (previewModalData: PreviewModalData) => Promise<boolean>): Promise<boolean> {
const {previewModalResult, previewModal} = await this.createPreviewModal(previewModalOptions);
if (previewModalResult.code === ModalResultCode.ERROR) {
// there was an error in the modal itself
console.warn(previewModalResult.error);
new Notice(previewModalResult.error.toString());
previewModal.close();
return;
return undefined;
}
if (previewModalResult.code === ModalResultCode.CLOSE) {
// modal is already being closed
return undefined;
}
try {
let callbackRes: boolean;
callbackRes = await submitCallback(result);
callbackRes = await submitCallback(previewModalResult.data);
previewModal.close();
return callbackRes;
} catch (e) {