11import {
22 DataTypes ,
3+ DEFAULT_COLUMN_CONFIG ,
34 MaxCapacitiesDatabase ,
45 MetadataColumns ,
56 MetadataDatabaseColumns ,
67} from "helpers/Constants" ;
7- import { TableColumn } from "cdm/FolderModel" ;
8+ import { RowDataType , TableColumn } from "cdm/FolderModel" ;
89import { LOGGER } from "services/Logger" ;
910import { DatabaseColumn } from "cdm/DatabaseModel" ;
1011import { RowSelectOption } from "cdm/ComponentsModel" ;
1112import { LocalSettings } from "cdm/SettingsModel" ;
1213import { dbTrim } from "helpers/StylesHelper" ;
14+ import { TFile } from "obsidian" ;
15+ import { DataviewService } from "services/DataviewService" ;
16+ import { Literal } from "obsidian-dataview/lib/data-model/value" ;
1317
1418/**
15- * Add mandatory columns to the table
19+ * Add mandatory and configured metadata columns of the table
1620 * @param columns
1721 * @returns
1822 */
@@ -63,6 +67,11 @@ export async function obtainMetadataColumns(
6367 return yamlColumns ;
6468}
6569
70+ /**
71+ * Given a record of columns of yaml file, return a record of columns of the table
72+ * @param databaseColumns
73+ * @returns
74+ */
6675export async function obtainColumnsFromFolder (
6776 databaseColumns : Record < string , DatabaseColumn >
6877) : Promise < TableColumn [ ] > {
@@ -82,6 +91,79 @@ export async function obtainColumnsFromFolder(
8291 return sortColumnsByPosition ( columns ) ;
8392}
8493
94+ export async function obtainColumnsFromFile (
95+ file : TFile
96+ ) : Promise < Record < string , DatabaseColumn > > {
97+ const columns : Record < string , DatabaseColumn > = { } ;
98+ const propertiesOfFile = DataviewService . getDataviewAPI ( ) . page ( file . path ) ;
99+ // Check if propertiesOfFile is empty
100+ if ( propertiesOfFile . length === 0 ) {
101+ return columns ;
102+ }
103+
104+ Object . entries ( propertiesOfFile ) . forEach ( ( [ key , value , index ] ) => {
105+ const input = getInputInFuctionOfLiteral ( value ) ;
106+ const newColumn : DatabaseColumn = {
107+ input : input ,
108+ accessor : key ,
109+ label : key ,
110+ key : key ,
111+ position : index ,
112+ config : DEFAULT_COLUMN_CONFIG ,
113+ } ;
114+ columns [ key ] = newColumn ;
115+ } ) ;
116+ // remove metadata fields of dataview
117+ delete columns [ "file" ] ;
118+ return columns ;
119+ }
120+
121+ export function obtainColumnsFromRows (
122+ rows : RowDataType [ ]
123+ ) : Record < string , DatabaseColumn > {
124+ const columns : Record < string , DatabaseColumn > = { } ;
125+ // Obtain unique keys from source
126+ const keys = rows . reduce ( ( acc , row ) => {
127+ const keys = Object . keys ( row ) ;
128+ return [ ...acc , ...keys ] ;
129+ } , [ ] as string [ ] ) ;
130+ // Add keys to columns
131+ keys
132+ // Check metadata columns to not be added
133+ . filter ( ( key ) => ! ( key . startsWith ( "__" ) && key . endsWith ( "__" ) ) )
134+ . forEach ( ( key , index ) => {
135+ columns [ key ] = {
136+ input : DataTypes . TEXT ,
137+ accessor : key ,
138+ label : key ,
139+ key : key ,
140+ position : index ,
141+ config : DEFAULT_COLUMN_CONFIG ,
142+ } ;
143+ } ) ;
144+
145+ return columns ;
146+ }
147+
148+ function getInputInFuctionOfLiteral ( literal : Literal ) {
149+ const wrappedLiteral = DataviewService . wrapLiteral ( literal ) ;
150+ let input = DataTypes . TEXT ;
151+ switch ( wrappedLiteral . type ) {
152+ case DataTypes . NUMBER :
153+ input = DataTypes . NUMBER ;
154+ break ;
155+ case "date" :
156+ input = DataTypes . CALENDAR ;
157+ break ;
158+ case "duration" :
159+ input = DataTypes . CALENDAR_TIME ;
160+ break ;
161+ default :
162+ input = DataTypes . TEXT ;
163+ }
164+ return input ;
165+ }
166+
85167function columnOptions (
86168 columnKey : string ,
87169 index : number ,
0 commit comments