11import type { App , ButtonComponent } from 'obsidian' ;
22import { DropdownComponent , Modal , Setting , TextComponent , ToggleComponent } from 'obsidian' ;
33import type MediaDbPlugin from '../main' ;
4+ import type { APIModel } from 'src/api/APIModel' ;
5+ import { BulkImportLookupMethod } from 'src/utils/BulkImportLookupMethod' ;
46
57export class MediaDbFolderImportModal extends Modal {
68 plugin : MediaDbPlugin ;
7- onSubmit : ( selectedAPI : string , titleFieldName : string , appendContent : boolean ) => void ;
9+ onSubmit : ( selectedAPI : string , lookupMethod : string , fieldName : string , appendContent : boolean ) => void ;
810 selectedApi : string ;
911 searchBtn ?: ButtonComponent ;
10- titleFieldName : string ;
12+ lookupMethod : string ;
13+ fieldName : string ;
1114 appendContent : boolean ;
1215
13- constructor ( app : App , plugin : MediaDbPlugin , onSubmit : ( selectedAPI : string , titleFieldName : string , appendContent : boolean ) => void ) {
16+ constructor ( app : App , plugin : MediaDbPlugin , onSubmit : ( selectedAPI : string , lookupMethod : string , fieldName : string , appendContent : boolean ) => void ) {
1417 super ( app ) ;
1518 this . plugin = plugin ;
1619 this . onSubmit = onSubmit ;
1720 this . selectedApi = plugin . apiManager . apis [ 0 ] . apiName ;
18- this . titleFieldName = '' ;
21+ this . lookupMethod = BulkImportLookupMethod . TITLE ;
22+ this . fieldName = '' ;
1923 this . appendContent = false ;
2024 }
2125
2226 submit ( ) : void {
23- this . onSubmit ( this . selectedApi , this . titleFieldName , this . appendContent ) ;
27+ this . onSubmit ( this . selectedApi , this . lookupMethod , this . fieldName , this . appendContent ) ;
2428 this . close ( ) ;
2529 }
2630
@@ -29,21 +33,19 @@ export class MediaDbFolderImportModal extends Modal {
2933
3034 contentEl . createEl ( 'h2' , { text : 'Import folder as Media DB entries' } ) ;
3135
32- const apiSelectorWrapper = contentEl . createEl ( 'div' , { cls : 'media-db-plugin-list-wrapper' } ) ;
33- const apiSelectorTextWrapper = apiSelectorWrapper . createEl ( 'div' , { cls : 'media-db-plugin-list-text-wrapper' } ) ;
34- apiSelectorTextWrapper . createEl ( 'span' , { text : 'API to search' , cls : 'media-db-plugin-list-text' } ) ;
35-
36- const apiSelectorComponent = new DropdownComponent ( apiSelectorWrapper ) ;
37- apiSelectorComponent . onChange ( ( value : string ) => {
38- this . selectedApi = value ;
39- } ) ;
40- for ( const api of this . plugin . apiManager . apis ) {
41- apiSelectorComponent . addOption ( api . apiName , api . apiName ) ;
42- }
43- apiSelectorWrapper . appendChild ( apiSelectorComponent . selectEl ) ;
36+ this . createDropdownEl (
37+ contentEl ,
38+ 'API to search' ,
39+ ( value : string ) => {
40+ this . selectedApi = value ;
41+ } ,
42+ this . plugin . apiManager . apis . map ( ( api : APIModel ) => {
43+ return { value : api . apiName , display : api . apiName } ;
44+ } ) ,
45+ ) ;
4446
4547 contentEl . createDiv ( { cls : 'media-db-plugin-spacer' } ) ;
46- contentEl . createEl ( 'h3' , { text : 'Append note content to Media DB entry. ' } ) ;
48+ contentEl . createEl ( 'h3' , { text : 'Append note content to Media DB entry? ' } ) ;
4749
4850 const appendContentToggleElementWrapper = contentEl . createEl ( 'div' , { cls : 'media-db-plugin-list-wrapper' } ) ;
4951 const appendContentToggleTextWrapper = appendContentToggleElementWrapper . createEl ( 'div' , { cls : 'media-db-plugin-list-text-wrapper' } ) ;
@@ -60,19 +62,38 @@ export class MediaDbFolderImportModal extends Modal {
6062 appendContentToggleComponentWrapper . appendChild ( appendContentToggle . toggleEl ) ;
6163
6264 contentEl . createDiv ( { cls : 'media-db-plugin-spacer' } ) ;
63- contentEl . createEl ( 'h3' , { text : 'The name of the metadata field that should be used as the title to query.' } ) ;
64-
65- const placeholder = 'title' ;
66- const titleFieldNameComponent = new TextComponent ( contentEl ) ;
67- titleFieldNameComponent . inputEl . style . width = '100%' ;
68- titleFieldNameComponent . setPlaceholder ( placeholder ) ;
69- titleFieldNameComponent . onChange ( value => ( this . titleFieldName = value ) ) ;
70- titleFieldNameComponent . inputEl . addEventListener ( 'keydown' , ke => {
65+ contentEl . createEl ( 'h3' , { text : 'Media lookup method' } ) ;
66+ contentEl . createEl ( 'p' , {
67+ text : 'Choose whether to search the API by title (can return multiple results) or lookup directly using an ID (returns at most one result), and specify the name of the frontmatter property which contains the title or ID of the media.' ,
68+ } ) ;
69+
70+ this . createDropdownEl (
71+ contentEl ,
72+ 'Lookup media by' ,
73+ ( value : string ) => {
74+ this . lookupMethod = value ;
75+ } ,
76+ [
77+ { value : BulkImportLookupMethod . TITLE , display : 'Title' } ,
78+ { value : BulkImportLookupMethod . ID , display : 'ID' } ,
79+ ] ,
80+ ) ;
81+
82+ contentEl . createDiv ( { cls : 'media-db-plugin-spacer' } ) ;
83+
84+ const fieldNameWrapperEl = contentEl . createEl ( 'div' , { cls : 'media-db-plugin-list-wrapper' } ) ;
85+ const fieldNameLabelWrapperEl = fieldNameWrapperEl . createEl ( 'div' , { cls : 'media-db-plugin-list-text-wrapper' } ) ;
86+ fieldNameLabelWrapperEl . createEl ( 'span' , { text : 'Using the property named' , cls : 'media-db-plugin-list-text' } ) ;
87+
88+ const fieldNameComponent = new TextComponent ( fieldNameWrapperEl ) ;
89+ fieldNameComponent . setPlaceholder ( 'title / id' ) ;
90+ fieldNameComponent . onChange ( value => ( this . fieldName = value ) ) ;
91+ fieldNameComponent . inputEl . addEventListener ( 'keydown' , ke => {
7192 if ( ke . key === 'Enter' ) {
7293 this . submit ( ) ;
7394 }
7495 } ) ;
75- contentEl . appendChild ( titleFieldNameComponent . inputEl ) ;
96+ contentEl . appendChild ( fieldNameWrapperEl ) ;
7697
7798 contentEl . createDiv ( { cls : 'media-db-plugin-spacer' } ) ;
7899
@@ -93,6 +114,19 @@ export class MediaDbFolderImportModal extends Modal {
93114 } ) ;
94115 }
95116
117+ createDropdownEl ( parentEl : HTMLElement , label : string , onChange : ( value : string ) => void , options : { value : string ; display : string } [ ] ) : void {
118+ const wrapperEl = parentEl . createEl ( 'div' , { cls : 'media-db-plugin-list-wrapper' } ) ;
119+ const labelWrapperEl = wrapperEl . createEl ( 'div' , { cls : 'media-db-plugin-list-text-wrapper' } ) ;
120+ labelWrapperEl . createEl ( 'span' , { text : label , cls : 'media-db-plugin-list-text' } ) ;
121+
122+ const dropDownComponent = new DropdownComponent ( wrapperEl ) ;
123+ dropDownComponent . onChange ( onChange ) ;
124+ for ( const option of options ) {
125+ dropDownComponent . addOption ( option . value , option . display ) ;
126+ }
127+ wrapperEl . appendChild ( dropDownComponent . selectEl ) ;
128+ }
129+
96130 onClose ( ) : void {
97131 const { contentEl } = this ;
98132 contentEl . empty ( ) ;
0 commit comments