1- import { MarkdownRenderer , Modal , Setting , TFile } from "obsidian" ;
2- import MediaDbPlugin from "src/main" ;
3- import { MediaTypeModel } from "src/models/MediaTypeModel" ;
1+ import { ButtonComponent , MarkdownRenderer , Modal , Setting } from 'obsidian' ;
2+ import MediaDbPlugin from 'src/main' ;
3+ import { MediaTypeModel } from 'src/models/MediaTypeModel' ;
4+ import { PREVIEW_MODAL_DEFAULT_OPTIONS , PreviewModalData , PreviewModalOptions } from '../utils/ModalHelper' ;
5+ import { CreateNoteOptions } from '../utils/Utils' ;
46
57export class MediaDbPreviewModal extends Modal {
6- selectedSearchResults : MediaTypeModel [ ] ;
7- options : { attachTemplate : boolean , attachFile : TFile } ;
8- submitCallback : ( res : boolean ) => void ;
9- closeCallback : ( err ?: Error ) => void ;
10- plugin : MediaDbPlugin ;
11- searchBtn : any ;
12- cancelButton : any ;
13- submitButton : any ;
14- busy : any ;
15-
16- constructor ( plugin : MediaDbPlugin , mediaTypeModel : MediaTypeModel [ ] , options : any ) {
17- super ( plugin . app ) ;
18- this . plugin = plugin ;
19- this . selectedSearchResults = mediaTypeModel ;
20- this . options = options ;
21- }
22-
23- setSubmitCallback ( submitCallback : ( res : boolean ) => void ) : void {
24- this . submitCallback = submitCallback ;
25- }
26-
27- setCloseCallback ( closeCallback : ( err ?: Error ) => void ) : void {
28- this . closeCallback = closeCallback ;
29- }
30-
31- async preview ( ) : Promise < void > {
32- let { contentEl } = this ;
33- for ( let result of this . selectedSearchResults ) {
34- let fileContent = await this . plugin . generateMediaDbNoteContents ( result , { attachTemplate : this . options . attachTemplate , attachFile : this . options . attachFile } ) ;
35- this . contentEl . createEl ( "h3" , { text : result . englishTitle } ) ;
36- const fileDiv = this . contentEl . createDiv ( ) ;
37- fileContent = `\n${ fileContent } \n` ;
38- MarkdownRenderer . renderMarkdown ( fileContent , fileDiv , null , null ) ;
39- }
40-
41- contentEl . createDiv ( { cls : 'media-db-plugin-spacer' } ) ;
42-
43- const bottomSettingRow = new Setting ( contentEl ) ;
44- bottomSettingRow . addButton ( btn => {
45- btn . setButtonText ( 'Cancel' ) ;
46- btn . onClick ( ( ) => this . closeCallback ( ) ) ;
47- btn . buttonEl . addClass ( 'media-db-plugin-button' ) ;
48- this . cancelButton = btn ;
49- } ) ;
50- bottomSettingRow . addButton ( btn => {
51- btn . setButtonText ( 'Ok' ) ;
52- btn . setCta ( ) ;
53- btn . onClick ( ( ) => this . submitCallback ( true ) ) ;
54- btn . buttonEl . addClass ( 'media-db-plugin-button' ) ;
55- this . submitButton = btn ;
56- } )
57- }
58-
59- submit ( ) {
60- if ( ! this . busy ) {
61- this . busy = true ;
62- this . submitButton . setButtonText ( 'Creating entry...' ) ;
63- this . submitCallback ( true ) ;
64- }
65- }
66-
67- onOpen ( ) : void {
68- this . preview ( )
69- }
70- }
8+ plugin : MediaDbPlugin ;
9+
10+ createNoteOptions : CreateNoteOptions ;
11+ elements : MediaTypeModel [ ] ;
12+ isBusy : boolean ;
13+ title : string ;
14+ cancelButton : ButtonComponent ;
15+ submitButton : ButtonComponent ;
16+
17+ submitCallback : ( previewModalData : PreviewModalData ) => void ;
18+ closeCallback : ( err ?: Error ) => void ;
19+
20+ constructor ( plugin : MediaDbPlugin , previewModalOptions : PreviewModalOptions ) {
21+ previewModalOptions = Object . assign ( { } , PREVIEW_MODAL_DEFAULT_OPTIONS , previewModalOptions ) ;
22+
23+ super ( plugin . app ) ;
24+
25+ this . plugin = plugin ;
26+ this . title = previewModalOptions . modalTitle ;
27+ this . elements = previewModalOptions . elements ;
28+ this . createNoteOptions = previewModalOptions . createNoteOptions ;
29+ }
30+
31+ setSubmitCallback ( submitCallback : ( previewModalData : PreviewModalData ) => void ) : void {
32+ this . submitCallback = submitCallback ;
33+ }
34+
35+ setCloseCallback ( closeCallback : ( err ?: Error ) => void ) : void {
36+ this . closeCallback = closeCallback ;
37+ }
38+
39+ async preview ( ) : Promise < void > {
40+ let { contentEl} = this ;
41+ contentEl . addClass ( 'media-db-plugin-preview-modal' ) ;
42+
43+ contentEl . createEl ( 'h2' , { text : this . title } ) ;
44+
45+ const previewWrapper = contentEl . createDiv ( { cls : 'media-db-plugin-preview-wrapper' } ) ;
46+
47+ for ( let result of this . elements ) {
48+ previewWrapper . createEl ( 'h3' , { text : result . englishTitle } ) ;
49+ const fileDiv = previewWrapper . createDiv ( ) ;
50+
51+ let fileContent = await this . plugin . generateMediaDbNoteContents ( result , this . createNoteOptions ) ;
52+ fileContent = `\n${ fileContent } \n` ;
53+
54+ MarkdownRenderer . renderMarkdown ( fileContent , fileDiv , null , null ) ;
55+ }
56+
57+ contentEl . createDiv ( { cls : 'media-db-plugin-spacer' } ) ;
58+
59+ const bottomSettingRow = new Setting ( contentEl ) ;
60+ bottomSettingRow . addButton ( btn => {
61+ btn . setButtonText ( 'Cancel' ) ;
62+ btn . onClick ( ( ) => this . closeCallback ( ) ) ;
63+ btn . buttonEl . addClass ( 'media-db-plugin-button' ) ;
64+ this . cancelButton = btn ;
65+ } ) ;
66+ bottomSettingRow . addButton ( btn => {
67+ btn . setButtonText ( 'Ok' ) ;
68+ btn . setCta ( ) ;
69+ btn . onClick ( ( ) => this . submitCallback ( { confirmed : true } ) ) ;
70+ btn . buttonEl . addClass ( 'media-db-plugin-button' ) ;
71+ this . submitButton = btn ;
72+ } ) ;
73+ }
74+
75+ onOpen ( ) : void {
76+ this . preview ( ) ;
77+ }
78+ }
0 commit comments