11import { RowDataType , NormalizedPath , TableColumn } from 'cdm/FolderModel' ;
22import { Notice , TFile } from 'obsidian' ;
3- import { VaultManagerDB } from 'services/FileManagerService' ;
43import { LOGGER } from "services/Logger" ;
54import NoteInfo from 'services/NoteInfo' ;
6- import { DatabaseCore , InputType , SourceDataTypes , UpdateRowOptions } from "helpers/Constants" ;
7- import { generateDataviewTableQuery , inlineRegexInFunctionOf } from 'helpers/QueryHelper' ;
8- import obtainRowDatabaseFields from 'parsers/FileToRowDatabaseFields' ;
9- import { parseFrontmatterFieldsToString } from 'parsers/RowDatabaseFieldsToFile' ;
5+ import { DatabaseCore , SourceDataTypes , UpdateRowOptions } from "helpers/Constants" ;
6+ import { generateDataviewTableQuery } from 'helpers/QueryHelper' ;
107import { DataviewService } from 'services/DataviewService' ;
118import { Literal } from 'obsidian-dataview/lib/data-model/value' ;
129import { DataArray } from 'obsidian-dataview/lib/api/data-array' ;
13- import { EditionError } from 'errors/ErrorTypes' ;
1410import { FilterSettings , LocalSettings } from 'cdm/SettingsModel' ;
1511import { NoteInfoPage } from 'cdm/DatabaseModel' ;
16- import { inline_regex_target_in_function_of } from './FileManagement ' ;
12+ import { EditEngineService } from 'services/EditEngineService ' ;
1713
1814const noBreakSpace = / \u00A0 / g;
1915
@@ -164,178 +160,6 @@ async function obtainQueryResult(query: string, folderPath: string): Promise<Dat
164160 }
165161}
166162
167- export async function updateRowFileProxy ( file : TFile , columnId : string , newValue : Literal , columns : TableColumn [ ] , ddbbConfig : LocalSettings , option : string ) : Promise < void > {
168- await updateRowFile ( file , columnId , newValue , columns , ddbbConfig , option ) . catch ( ( err ) => {
169- throw err ;
170- } ) ;
171- }
172-
173- /**
174- * Modify the file asociated to the row in function of input options
175- * @param asociatedCFilePathToCell
176- * @param columnId
177- * @param newColumnValue
178- * @param option
179- */
180- export async function updateRowFile ( file : TFile , columnId : string , newValue : Literal , columns : TableColumn [ ] , ddbbConfig : LocalSettings , option : string ) : Promise < void > {
181- LOGGER . info ( `=>updateRowFile. file: ${ file . path } | columnId: ${ columnId } | newValue: ${ newValue } | option: ${ option } ` ) ;
182- const content = await VaultManagerDB . obtainContentFromTfile ( file ) ;
183- const contentHasFrontmatter = hasFrontmatter ( content ) ;
184- const frontmatterKeys = VaultManagerDB . obtainFrontmatterKeys ( content ) ;
185- const rowFields = obtainRowDatabaseFields ( file , columns , frontmatterKeys ) ;
186- const column = columns . find (
187- c => c . key === ( UpdateRowOptions . COLUMN_KEY === option ? newValue : columnId )
188- ) ;
189- /*******************************************************************************************
190- * FRONTMATTER GROUP FUNCTIONS
191- *******************************************************************************************/
192- // Modify value of a column
193- async function columnValue ( ) : Promise < void > {
194- if ( column . config . isInline ) {
195- await inlineColumnEdit ( ) ;
196- return ;
197- }
198- rowFields . frontmatter [ columnId ] = DataviewService . parseLiteral ( newValue , InputType . MARKDOWN , ddbbConfig ) ;
199- await persistFrontmatter ( ) ;
200- await inlineRemoveColumn ( ) ;
201- }
202-
203- // Modify key of a column
204- async function columnKey ( ) : Promise < void > {
205- if ( column . config . isInline ) {
206- // Go to inline mode
207- await inlineColumnKey ( ) ;
208- return ;
209- }
210- // If field does not exist yet, ignore it
211- if ( ! Object . prototype . hasOwnProperty . call ( rowFields . frontmatter , columnId )
212- && ! Object . prototype . hasOwnProperty . call ( rowFields . inline , columnId ) ) {
213- return ;
214- }
215-
216- // Check if the column is already in the frontmatter
217- // assign an empty value to the new key
218- rowFields . frontmatter [ DataviewService . parseLiteral ( newValue , InputType . TEXT , ddbbConfig ) as string ] = rowFields . frontmatter [ columnId ] ?? "" ;
219- delete rowFields . frontmatter [ columnId ] ;
220- await persistFrontmatter ( columnId ) ;
221- }
222-
223- // Remove a column
224- async function removeColumn ( ) : Promise < void > {
225- if ( column . config . isInline ) {
226- await inlineRemoveColumn ( ) ;
227- return ;
228- }
229- delete rowFields . frontmatter [ columnId ] ;
230- await persistFrontmatter ( columnId ) ;
231- }
232-
233- async function persistFrontmatter ( deletedColumn ?: string ) : Promise < void > {
234- const frontmatterGroupRegex = contentHasFrontmatter ? / ^ - - - [ \s \S ] + ?- - - \n / g : / ( ^ [ \s \S ] * $ ) / g;
235- const frontmatterFieldsText = parseFrontmatterFieldsToString ( rowFields , ddbbConfig , deletedColumn ) ;
236- const noteObject = {
237- action : 'replace' ,
238- file : file ,
239- regexp : frontmatterGroupRegex ,
240- newValue : contentHasFrontmatter ? `${ frontmatterFieldsText } ` : `${ frontmatterFieldsText } $1` ,
241- } ;
242- await VaultManagerDB . editNoteContent ( noteObject ) ;
243- }
244-
245- /*******************************************************************************************
246- * INLINE GROUP FUNCTIONS
247- *******************************************************************************************/
248- async function inlineColumnEdit ( ) : Promise < void > {
249- const inlineFieldRegex = inlineRegexInFunctionOf ( columnId ) ;
250- if ( ! inlineFieldRegex . test ( content ) ) {
251- await inlineAddColumn ( ) ;
252- return ;
253- }
254- /* Regex explanation
255- * group 1 is inline field checking that starts in new line
256- * group 2 is the current value of inline field
257- */
258- const noteObject = {
259- action : 'replace' ,
260- file : file ,
261- regexp : inlineFieldRegex ,
262- newValue : `$3$6$7$8 ${ DataviewService . parseLiteral ( newValue , InputType . MARKDOWN , ddbbConfig , true ) } $10$11`
263- } ;
264- await VaultManagerDB . editNoteContent ( noteObject ) ;
265- await persistFrontmatter ( columnId ) ;
266- }
267-
268- async function inlineColumnKey ( ) : Promise < void > {
269- if ( ! Object . keys ( rowFields . inline ) . contains ( columnId ) ) {
270- return ;
271- }
272- /* Regex explanation
273- * group 1 is inline field checking that starts in new line
274- * group 2 is the current value of inline field
275- */
276- const inlineFieldRegex = inlineRegexInFunctionOf ( columnId ) ;
277- const noteObject = {
278- action : 'replace' ,
279- file : file ,
280- regexp : inlineFieldRegex ,
281- newValue : `$6$7${ newValue } :: $4$9$10$11`
282- } ;
283- await VaultManagerDB . editNoteContent ( noteObject ) ;
284- await persistFrontmatter ( ) ;
285- }
286-
287- async function inlineAddColumn ( ) : Promise < void > {
288- const inlineAddRegex = contentHasFrontmatter ? new RegExp ( `(^---[\\s\\S]+?---\n)+([\\s\\S]*$)` , 'g' ) : new RegExp ( `(^[\\s\\S]*$)` , 'g' ) ;
289- const noteObject = {
290- action : 'replace' ,
291- file : file ,
292- regexp : inlineAddRegex ,
293- newValue : inline_regex_target_in_function_of (
294- ddbbConfig . inline_new_position ,
295- columnId ,
296- DataviewService . parseLiteral ( newValue , InputType . MARKDOWN , ddbbConfig ) . toString ( ) ,
297- contentHasFrontmatter
298- )
299- } ;
300- await persistFrontmatter ( columnId ) ;
301- await VaultManagerDB . editNoteContent ( noteObject ) ;
302- }
303-
304- async function inlineRemoveColumn ( ) : Promise < void > {
305- /* Regex explanation
306- * group 1 is inline field checking that starts in new line
307- * group 2 is the current value of inline field
308- */
309- const inlineFieldRegex = inlineRegexInFunctionOf ( columnId ) ;
310- const noteObject = {
311- action : 'replace' ,
312- file : file ,
313- regexp : inlineFieldRegex ,
314- newValue : `$6$11`
315- } ;
316- await VaultManagerDB . editNoteContent ( noteObject ) ;
317- }
318- try {
319- // Record of options
320- const updateOptions : Record < string , any > = { } ;
321- updateOptions [ UpdateRowOptions . COLUMN_VALUE ] = columnValue ;
322- updateOptions [ UpdateRowOptions . COLUMN_KEY ] = columnKey ;
323- updateOptions [ UpdateRowOptions . REMOVE_COLUMN ] = removeColumn ;
324- updateOptions [ UpdateRowOptions . INLINE_VALUE ] = inlineColumnEdit ;
325- // Execute action
326- if ( updateOptions [ option ] ) {
327- // Then execute the action
328- await updateOptions [ option ] ( ) ;
329- } else {
330- throw `Error: option ${ option } not supported yet` ;
331- }
332- } catch ( e ) {
333- LOGGER . error ( `${ EditionError . YamlRead } ` , e ) ;
334- new Notice ( `${ EditionError . YamlRead } : ${ e . message } ` , 6000 ) ;
335- }
336- LOGGER . info ( `<= updateRowFile.asociatedFilePathToCell: ${ file . path } | columnId: ${ columnId } | newValue: ${ newValue } | option: ${ option } ` ) ;
337- }
338-
339163/**
340164 * After update a row value, move the file to the new folder path
341165 * @param folderPath
@@ -348,7 +172,7 @@ export async function moveFile(folderPath: string, info: {
348172 columns : TableColumn [ ] ,
349173 ddbbConfig : LocalSettings
350174} ) : Promise < void > {
351- await updateRowFile (
175+ await EditEngineService . updateRowFileProxy (
352176 info . file ,
353177 info . id ,
354178 info . value ,
0 commit comments