1- import { DB_ICONS , SourceDataTypes } from "helpers/Constants" ;
1+ import { DatabaseCore , DB_ICONS , DEFAULT_COLUMN_CONFIG , InputType , SourceDataTypes } from "helpers/Constants" ;
22import DBFolderPlugin from "main" ;
33
44import {
55 DatabaseView ,
66} from 'DatabaseView' ;
77import { LOGGER } from "services/Logger" ;
8- import { DataQueryResult , ProjectView , ProjectViewProps } from "obsidian-projects-types" ;
8+ import { DataFieldType , DataQueryResult , ProjectView , ProjectViewProps } from "obsidian-projects-types" ;
99import { resolve_tfile , resolve_tfolder } from "helpers/FileManagement" ;
1010import { generateDbConfiguration , generateNewDatabase } from "helpers/CommandsHelper" ;
1111import { LocalSettings } from "cdm/SettingsModel" ;
12+ import { DatabaseColumn } from "cdm/DatabaseModel" ;
13+ import { dbTrim } from "helpers/StylesHelper" ;
1214
1315class ProjectAPI extends ProjectView {
1416 private plugin : DBFolderPlugin ;
17+ private view : DatabaseView ;
1518
1619 constructor ( plugin : DBFolderPlugin ) {
1720 super ( ) ;
@@ -33,15 +36,42 @@ class ProjectAPI extends ProjectView {
3336
3437 async onData ( { data } : DataQueryResult ) {
3538 const { fields } = data ;
36- fields . forEach ( ( field ) => {
37- const { name, type, identifier, derived } = field ;
38- /**
39- * I can not use the view object here without make it a class variable
40- * and I don't want to do that because I can not support multiple views
41- *
42- * Could we add the config to the data object? I can manage a map of views with that
43- */
44- } ) ;
39+ const currentColumnsLength = Object
40+ . values ( this . view . diskConfig . yaml . columns )
41+ . filter ( ( column ) => ! column . isMetadata ) . length ;
42+ const actualFields = fields
43+ . filter ( ( field ) => ! [ DatabaseCore . FRONTMATTER_KEY , "File" ] . contains ( field . name ) ) ;
44+ if ( currentColumnsLength !== actualFields . length ) {
45+ const newColumns : Record < string , DatabaseColumn > = { } ;
46+ fields . forEach ( ( field , index ) => {
47+ const { name, type } = field ;
48+ /**
49+ * I can not use the view object here without make it a class variable
50+ * and I don't want to do that because I can not support multiple views
51+ *
52+ * Could we add the config to the data object? I can manage a map of views with that
53+ */
54+ const inputType = this . mapperTypeToInputType ( type ) ;
55+ const key = dbTrim ( name ) ;
56+ const newColumn : DatabaseColumn = {
57+ input : inputType ,
58+ accessorKey : key ,
59+ key : key ,
60+ id : key ,
61+ label : name ,
62+ position : index ,
63+ config : {
64+ ...DEFAULT_COLUMN_CONFIG ,
65+ isInline : false
66+ } ,
67+ } ;
68+ newColumns [ name ] = newColumn ;
69+
70+ } ) ;
71+
72+ this . view . diskConfig . yaml . columns = newColumns ;
73+ await this . view . diskConfig . saveOnDisk ( ) ;
74+ }
4575 }
4676
4777 // onOpens is called whenever the user activates your view.
@@ -63,11 +93,11 @@ class ProjectAPI extends ProjectView {
6393 }
6494 const leaf = app . workspace . getLeaf ( ) ;
6595 const file = resolve_tfile ( filePath ) ;
66- const view = new DatabaseView ( leaf , this . plugin , file ) ;
67- view . initRootContainer ( file ) ;
68- await view . initDatabase ( ) ;
96+ this . view = new DatabaseView ( leaf , this . plugin , file ) ;
97+ this . view . initRootContainer ( file ) ;
98+ await this . view . initDatabase ( ) ;
6999 LOGGER . debug ( "Database initialized successfully from project view" ) ;
70- this . dataEl = contentEl . createDiv ( ) . appendChild ( view . containerEl ) ;
100+ this . dataEl = contentEl . createDiv ( ) . appendChild ( this . view . containerEl ) ;
71101 }
72102
73103 async onClose ( ) {
@@ -80,26 +110,54 @@ class ProjectAPI extends ProjectView {
80110 ...this . plugin . settings . local_settings ,
81111 }
82112 if ( project . dataview ) {
113+ localSettings . source_destination_path = project . path ;
83114 localSettings . source_data = SourceDataTypes . QUERY ;
84115 /*
85116 * Check if the query contains FROM or from
86117 * then Split query to only get subtring from "from" to the end
87118 */
88119 let query = "" ;
89- if ( project . query ?. contains ( "FROM" ) ) {
90- query = project . query ?. split ( "FROM" ) [ 1 ] ;
91- } else if ( project . query ?. contains ( "from" ) ) {
92- query = project . query ?. split ( "from" ) [ 1 ] ;
120+ const SOURCE_FLAG = "FROM" ;
121+ if ( project . query ?. contains ( SOURCE_FLAG ) ) {
122+ query = project . query ?. split ( SOURCE_FLAG ) [ 1 ] ;
123+ } else if ( project . query ?. contains ( SOURCE_FLAG . toLowerCase ( ) ) ) {
124+ query = project . query ?. split ( SOURCE_FLAG . toLowerCase ( ) ) [ 1 ] ;
93125 } else {
94126 // Handle error with default configuation
95127 localSettings . source_data = SourceDataTypes . CURRENT_FOLDER ;
96- LOGGER . error ( " The query does not contain a FROM clause. Using current folder as source data" ) ;
128+ LOGGER . error ( ` The query does not contain a ${ SOURCE_FLAG } clause. Using current folder as source data` ) ;
97129 }
98- localSettings . source_destination_path = query
130+ localSettings . source_form_result = ` ${ SOURCE_FLAG } ${ query } ` ;
99131 }
100132
101133 return localSettings ;
102134 }
135+
136+ private mapperTypeToInputType ( type : string ) : string {
137+ let inputType = "" ;
138+ switch ( type ) {
139+ case DataFieldType . String :
140+ case DataFieldType . Link :
141+ case DataFieldType . Unknown :
142+ inputType = InputType . TEXT ;
143+ break ;
144+ case DataFieldType . Number :
145+ inputType = InputType . NUMBER ;
146+ break ;
147+ case DataFieldType . Boolean :
148+ inputType = InputType . CHECKBOX ;
149+ break ;
150+ case DataFieldType . Date :
151+ inputType = InputType . CALENDAR ;
152+ break ;
153+ case DataFieldType . List :
154+ inputType = InputType . TAGS ;
155+ break ;
156+ default :
157+ inputType = InputType . TEXT ;
158+ }
159+ return inputType ;
160+ }
103161}
104162
105163export default ProjectAPI ;
0 commit comments