Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 2534e33

Browse files
committed
Merge branch 'testing-new-editing'
2 parents 28a787f + cbb35db commit 2534e33

File tree

11 files changed

+238
-196
lines changed

11 files changed

+238
-196
lines changed

src/components/NavBar.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ export function NavBar(navBarProps: NavBarProps) {
9393
PaperProps={{
9494
style: {
9595
maxHeight: NavBarConfig.ITEM_HEIGHT * 4.5,
96-
width: "20ch",
9796
},
9897
}}
9998
MenuListProps={{

src/components/headerActions/handlers/buttons/RemoveColumnHandlerAction.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import TrashIcon from "components/img/Trash";
44
import React from "react";
55
import { UpdateRowOptions } from "helpers/Constants";
66
import { RowDataType, TableColumn } from "cdm/FolderModel";
7-
import { updateRowFileProxy } from "helpers/VaultManagement";
87
import headerButtonComponent from "components/headerActions/HeaderButtonComponent";
8+
import { EditEngineService } from "services/EditEngineService";
99

1010
export default class RemoveColumnHandlerAction extends AbstractHeaderAction {
1111
globalHeaderActionResponse: HeaderActionResponse;
@@ -49,7 +49,7 @@ function removeButton(headerActionResponse: HeaderActionResponse) {
4949
if (ddbbConfig.remove_field_when_delete_column) {
5050
Promise.all(
5151
rows.map(async (row: RowDataType) => {
52-
updateRowFileProxy(
52+
EditEngineService.updateRowFileProxy(
5353
row.__note__.getFile(),
5454
hooks.keyState,
5555
undefined, // delete does not need this field

src/errors/ErrorTypes.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1+
import { Notice } from "obsidian";
2+
import { LOGGER } from "services/Logger";
3+
14
/**
25
* REPOSITORY OF POSSIBLE ERRORS IN FUNCTION OF THE AMBIT
36
*/
4-
export enum EditionError {
5-
YamlRead = `[Error reading yaml file](https://rafaelgb.github.io/obsidian-db-folder/faq/#possible-edition-issues-while-you-are-saving-a-cell-change)`,
6-
}
7+
type DBError = {
8+
error: string;
9+
solution: string;
10+
}
11+
12+
type DBErrorTypeEnum = {
13+
[key: string]: DBError;
14+
}
15+
export function showDBError(err: DBError, exception: Error) {
16+
LOGGER.error(`${err.error}. See ${err.solution}`, exception);
17+
new Notice(`${err.error}. See ${err.solution}`, 6000);
18+
}
19+
20+
21+
export const EditionError: DBErrorTypeEnum = Object.freeze({
22+
YamlRead: {
23+
error: `Error reading yaml file`,
24+
solution: `https://rafaelgb.github.io/obsidian-db-folder/faq/#possible-edition-issues-while-you-are-saving-a-cell-change`,
25+
}
26+
});

src/errors/HelperException.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export default class HelperException extends DbFolderException {
77
constructor(message: string) {
88
super(message, {});
99
}
10-
}
10+
}

src/helpers/FileManagement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ export function inline_regex_target_in_function_of(position: string, columnId: s
7474
let regex_target = "";
7575
switch (position) {
7676
case INLINE_POSITION.BOTTOM:
77-
regex_target = contentHasFrontmatter ? `$1$2${columnId}:: ${newValue}` : `$1${columnId}:: ${newValue}`;
77+
regex_target = contentHasFrontmatter ? `$1$2\n${columnId}:: ${newValue}` : `$1\n${columnId}:: ${newValue}`;
7878
break;
7979
default:
80-
regex_target = contentHasFrontmatter ? `$1${columnId}:: ${newValue}\n$2` : `${columnId}:: ${newValue}\n$1`;
80+
regex_target = contentHasFrontmatter ? `$1\n${columnId}:: ${newValue}$2` : `${columnId}:: ${newValue}\n$1`;
8181
}
8282
return regex_target;
8383
}

src/helpers/VaultManagement.ts

Lines changed: 4 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import { RowDataType, NormalizedPath, TableColumn } from 'cdm/FolderModel';
22
import { Notice, TFile } from 'obsidian';
3-
import { VaultManagerDB } from 'services/FileManagerService';
43
import { LOGGER } from "services/Logger";
54
import 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';
107
import { DataviewService } from 'services/DataviewService';
118
import { Literal } from 'obsidian-dataview/lib/data-model/value';
129
import { DataArray } from 'obsidian-dataview/lib/api/data-array';
13-
import { EditionError } from 'errors/ErrorTypes';
1410
import { FilterSettings, LocalSettings } from 'cdm/SettingsModel';
1511
import { NoteInfoPage } from 'cdm/DatabaseModel';
16-
import { inline_regex_target_in_function_of } from './FileManagement';
12+
import { EditEngineService } from 'services/EditEngineService';
1713

1814
const 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,

src/parsers/RowDatabaseFieldsToFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const parseFrontmatterFieldsToString = (databaseFields: RowDatabaseFields
1212
}
1313
});
1414
if (array.length > 0) {
15-
array = [`---`, ...array, `---\n`];
15+
array = [`---`, ...array, `---`];
1616
}
1717
return array.join('\n');
1818
}

0 commit comments

Comments
 (0)