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