1- import { Modal } from "obsidian" ;
1+ import { LocalSettings } from "cdm/SettingsModel" ;
2+ import { generateDbConfiguration , generateNewDatabase } from "helpers/CommandsHelper" ;
3+ import { InputType , SourceDataTypes , StyleClasses } from "helpers/Constants" ;
4+ import { resolve_tfolder } from "helpers/FileManagement" ;
5+ import { generateDataviewTableQuery } from "helpers/QueryHelper" ;
6+ import { Modal , Notice , Setting , TFolder } from "obsidian" ;
7+ import { DataviewService } from "services/DataviewService" ;
8+ import { add_dropdown , add_setting_header } from "settings/SettingsComponents" ;
9+ import { FileSuggest } from "settings/suggesters/FileSuggester" ;
10+ import { FolderSuggest } from "settings/suggesters/FolderSuggester" ;
211
312export class DatabaseHelperCreationModal extends Modal {
413 databaseHelperCreationModalManager : DatabaseHelperCreationModalManager ;
5- constructor ( ) {
14+ local_settings : LocalSettings ;
15+ constructor ( local_settings : LocalSettings ) {
616 super ( app ) ;
17+ this . local_settings = local_settings ;
718 this . databaseHelperCreationModalManager = new DatabaseHelperCreationModalManager ( this ) ;
819 }
920 onOpen ( ) {
@@ -20,11 +31,222 @@ export class DatabaseHelperCreationModal extends Modal {
2031
2132export class DatabaseHelperCreationModalManager {
2233 databaseHelperCreationModal : DatabaseHelperCreationModal ;
34+ destinationFolder = '/' ;
35+ databaseName = '' ;
2336 constructor (
2437 databaseHelperCreationModal : DatabaseHelperCreationModal ,
2538 ) {
2639 this . databaseHelperCreationModal = databaseHelperCreationModal ;
40+
41+
2742 }
2843 constructUI ( containerEl : HTMLElement ) {
44+ add_setting_header ( containerEl , "Database creator helper" , 'h2' ) ;
45+ const helperBody = containerEl . createDiv ( ) ;
46+ helperBody . addClass ( StyleClasses . SETTINGS_MODAL_BODY ) ;
47+ helperBody . setAttribute ( "id" , StyleClasses . SETTINGS_MODAL_BODY ) ;
48+ this . constructSettingBody ( helperBody ) ;
2949 }
30- }
50+
51+ constructSettingBody ( bodyElement : HTMLElement ) {
52+ new Setting ( bodyElement )
53+ . setName ( "Database name" )
54+ . setDesc ( "Name of the database to create" )
55+ . addText ( text => {
56+ text . setPlaceholder ( "Database name" )
57+ . setValue ( this . databaseName )
58+ . onChange ( async ( value : string ) : Promise < void > => {
59+ this . databaseName = this . parseValueToThuthyYaml ( value ) ;
60+ } ) ;
61+ } ) ;
62+
63+ add_dropdown (
64+ bodyElement ,
65+ 'Select source' ,
66+ 'Select from which source you want to create your custom database.' ,
67+ this . databaseHelperCreationModal . local_settings . source_data ,
68+ {
69+ current_folder : SourceDataTypes . CURRENT_FOLDER ,
70+ tag : SourceDataTypes . TAG ,
71+ outgoing_link : SourceDataTypes . OUTGOING_LINK ,
72+ incoming_link : SourceDataTypes . INCOMING_LINK ,
73+ query : SourceDataTypes . QUERY ,
74+ } ,
75+ async ( value : string ) => {
76+ this . databaseHelperCreationModal . local_settings . source_data = value ;
77+ this . reset ( ) ;
78+ }
79+ ) ;
80+ switch ( this . databaseHelperCreationModal . local_settings . source_data ) {
81+ case SourceDataTypes . TAG :
82+ this . tagHandler ( bodyElement ) ;
83+ break ;
84+ case SourceDataTypes . INCOMING_LINK :
85+ case SourceDataTypes . OUTGOING_LINK :
86+ this . outgoingAndIncomingHandler ( bodyElement ) ;
87+ break ;
88+ case SourceDataTypes . QUERY :
89+ this . queryHandler ( bodyElement ) ;
90+ break ;
91+ default :
92+ // do nothing
93+ }
94+
95+ new Setting ( bodyElement )
96+ . setName ( 'Select where to save your database' )
97+ . setDesc ( 'Select the destination of where you want to save your database.' )
98+ . addSearch ( ( cb ) => {
99+ new FolderSuggest (
100+ cb . inputEl
101+ ) ;
102+ cb . setPlaceholder ( "Example: path/to/folder" )
103+ . setValue ( this . destinationFolder )
104+ . onChange ( ( value : string ) => {
105+ this . destinationFolder = value ;
106+ } ) ;
107+ } ) ;
108+
109+ new Setting ( bodyElement )
110+ . setName ( 'Submit' )
111+ . setDesc ( 'Close to cancel or submit to create your database.' )
112+ . addButton ( ( cb ) => {
113+ cb . setButtonText ( 'Close' )
114+ . onClick ( ( ) => {
115+ this . databaseHelperCreationModal . close ( ) ;
116+ } ) ;
117+ } ) . addButton ( ( cb ) => {
118+ cb . setButtonText ( 'Create' )
119+ . onClick ( async ( ) => {
120+ await this . createButtonHandler ( )
121+ } ) ;
122+ } ) ;
123+ }
124+
125+ reset ( ) {
126+ const bodyElement = activeDocument . getElementById ( StyleClasses . SETTINGS_MODAL_BODY ) ;
127+ // remove all sections
128+ bodyElement . empty ( ) ;
129+ this . constructSettingBody ( bodyElement ) ;
130+ }
131+
132+ async createButtonHandler ( ) {
133+ try {
134+ const targetFolder = resolve_tfolder ( this . destinationFolder ) ;
135+ await generateNewDatabase (
136+ generateDbConfiguration ( this . databaseHelperCreationModal . local_settings ) ,
137+ targetFolder ,
138+ this . databaseName
139+ ) ;
140+ new Notice ( `Database "${ this . databaseName } " created in "${ targetFolder . path } "` , 1500 ) ;
141+ } catch ( e ) {
142+ new Notice ( `Error creating database "${ this . databaseName } ": ${ e } ` , 1500 ) ;
143+ }
144+ this . databaseHelperCreationModal . close ( ) ;
145+ }
146+
147+ tagHandler ( containerEl : HTMLElement ) {
148+ const tagArray : Record < string , number > = ( app . metadataCache as unknown as any ) . getTags ( ) ;
149+ if ( tagArray ) {
150+ const tagRecords : Record < string , string > = { } ;
151+ // Order tagRecord by key (tag name)
152+ Object . entries ( tagArray )
153+ . sort ( ( a , b ) => a [ 0 ] . localeCompare ( b [ 0 ] ) )
154+ . forEach ( ( [ key , value ] ) => {
155+ tagRecords [ key ] = `${ key } (${ value } )` ;
156+ } ) ;
157+ const source_form_promise = async ( value : string ) : Promise < void > => {
158+ // update form result
159+ this . databaseHelperCreationModal . local_settings . source_form_result = value . slice ( 1 ) ;
160+ } ;
161+
162+ add_dropdown (
163+ containerEl ,
164+ 'Select a tag' ,
165+ 'Select tag to get data from' ,
166+ `#${ this . databaseHelperCreationModal . local_settings . source_form_result } ` ,
167+ tagRecords ,
168+ source_form_promise
169+ ) ;
170+ this . destinationFolderHandler ( containerEl ) ;
171+ }
172+ }
173+
174+ queryHandler ( containerEl : HTMLElement ) {
175+ const query_promise = async ( value : string ) : Promise < void > => {
176+ // update settings
177+ this . databaseHelperCreationModal . local_settings . source_form_result = this . parseValueToThuthyYaml ( value ) ;
178+ } ;
179+ new Setting ( containerEl )
180+ . setName ( 'Dataview query' )
181+ . setDesc ( 'Enter a dataview query starting with FROM (the plugin autocomplete the query with TABLE & the column fields)' )
182+ . addTextArea ( ( textArea ) => {
183+ textArea . setValue ( this . databaseHelperCreationModal . local_settings . source_form_result ) ;
184+ textArea . setPlaceholder ( 'Write here your dataview query...' ) ;
185+ textArea . onChange ( query_promise ) ;
186+ } ) . addExtraButton ( ( cb ) => {
187+ cb . setIcon ( "check" )
188+ . setTooltip ( "Validate query" )
189+ . onClick ( async ( ) : Promise < void > => {
190+ const query = generateDataviewTableQuery (
191+ [ ] ,
192+ this . databaseHelperCreationModal . local_settings . source_form_result ) ;
193+ if ( query ) {
194+ DataviewService . getDataviewAPI ( ) . tryQuery ( query )
195+ . then ( ( ) => {
196+ new Notice ( `Dataview query "${ query } " is valid!` , 2000 ) ;
197+ } )
198+ . catch ( ( e ) => {
199+ new Notice ( `Dataview query "${ query } " is invalid: ${ e . message } ` , 10000 ) ;
200+ } ) ;
201+ }
202+ } ) ;
203+ } ) ;
204+ this . destinationFolderHandler ( containerEl ) ;
205+ }
206+
207+ outgoingAndIncomingHandler ( containerEl : HTMLElement ) {
208+ const source_form_promise = async ( value : string ) : Promise < void > => {
209+ // update form result
210+ this . databaseHelperCreationModal . local_settings . source_form_result = value ;
211+ } ;
212+ new Setting ( containerEl )
213+ . setName ( 'Select a file' )
214+ . setDesc ( 'Select file from vault to be used as source of data.' )
215+ . addSearch ( ( cb ) => {
216+ new FileSuggest (
217+ cb . inputEl ,
218+ "/"
219+ ) ;
220+ cb . setPlaceholder ( "Example: folder1/template_file" )
221+ . setValue ( this . databaseHelperCreationModal . local_settings . source_form_result )
222+ . onChange ( source_form_promise ) ;
223+ } ) ;
224+ this . destinationFolderHandler ( containerEl ) ;
225+ }
226+
227+ destinationFolderHandler ( containerEl : HTMLElement ) {
228+ const source_form_promise = async ( value : string ) : Promise < void > => {
229+ // update settings
230+ this . databaseHelperCreationModal . local_settings . source_destination_path = value ;
231+ } ;
232+ new Setting ( containerEl )
233+ . setName ( 'Select destination folder' )
234+ . setDesc ( 'Select the destination of new entries for this source' )
235+ . addSearch ( ( cb ) => {
236+ new FolderSuggest (
237+ cb . inputEl
238+ ) ;
239+ cb . setPlaceholder ( "Example: path/to/folder" )
240+ . setValue ( this . databaseHelperCreationModal . local_settings . source_destination_path )
241+ . onChange ( source_form_promise ) ;
242+ } ) ;
243+ }
244+
245+ parseValueToThuthyYaml ( value : string ) : string {
246+ return DataviewService . parseLiteral (
247+ value ,
248+ InputType . MARKDOWN ,
249+ this . databaseHelperCreationModal . local_settings
250+ ) . toString ( ) ;
251+ }
252+ }
0 commit comments