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

Commit cbb35db

Browse files
committed
improving editEngine
1 parent 45a19ce commit cbb35db

File tree

5 files changed

+49
-35
lines changed

5 files changed

+49
-35
lines changed

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/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
}

src/services/EditEngineService.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { RowDatabaseFields } from "cdm/DatabaseModel";
2-
import { NoteContentAction, TableColumn } from "cdm/FolderModel";
1+
import { TableColumn } from "cdm/FolderModel";
32
import { LocalSettings } from "cdm/SettingsModel";
4-
import { FileContent } from "helpers/FileContent";
5-
import { inline_regex_target_in_function_of, resolve_tfile } from "helpers/FileManagement";
6-
import { Notice, parseYaml, TFile, TFolder } from "obsidian";
7-
import { parseFrontmatterFieldsToString, parseInlineFieldsToString } from "parsers/RowDatabaseFieldsToFile";
3+
import { inline_regex_target_in_function_of } from "helpers/FileManagement";
4+
import { TFile } from "obsidian";
5+
import { parseFrontmatterFieldsToString } from "parsers/RowDatabaseFieldsToFile";
86
import { LOGGER } from "services/Logger";
97
import { DataviewService } from "services/DataviewService";
10-
import { InputType, SourceDataTypes, UpdateRowOptions } from "helpers/Constants";
8+
import { InputType, UpdateRowOptions } from "helpers/Constants";
119
import { Literal } from "obsidian-dataview";
1210
import { VaultManagerDB } from "services/FileManagerService";
1311
import { inlineRegexInFunctionOf } from "helpers/QueryHelper";
14-
import { EditionError } from "errors/ErrorTypes";
12+
import { EditionError, showDBError } from "errors/ErrorTypes";
1513
import { hasFrontmatter } from "helpers/VaultManagement";
1614
import obtainRowDatabaseFields from "parsers/FileToRowDatabaseFields";
1715
class EditEngine {
@@ -26,7 +24,7 @@ class EditEngine {
2624
*/
2725
public async updateRowFileProxy(file: TFile, columnId: string, newValue: Literal, columns: TableColumn[], ddbbConfig: LocalSettings, option: string): Promise<void> {
2826
await this.updateRowFile(file, columnId, newValue, columns, ddbbConfig, option).catch((err) => {
29-
throw err;
27+
showDBError(EditionError.YamlRead, err);
3028
});
3129
}
3230

@@ -84,7 +82,7 @@ class EditEngine {
8482
}
8583

8684
async function persistFrontmatter(deletedColumn?: string): Promise<void> {
87-
const frontmatterGroupRegex = contentHasFrontmatter ? /^---[\s\S]+?---\n/g : /(^[\s\S]*$)/g;
85+
const frontmatterGroupRegex = contentHasFrontmatter ? /^---[\s\S]+?---/g : /(^[\s\S]*$)/g;
8886
const frontmatterFieldsText = parseFrontmatterFieldsToString(rowFields, ddbbConfig, deletedColumn);
8987
const noteObject = {
9088
action: 'replace',
@@ -138,7 +136,7 @@ class EditEngine {
138136
}
139137

140138
async function inlineAddColumn(): Promise<void> {
141-
const inlineAddRegex = contentHasFrontmatter ? new RegExp(`(^---[\\s\\S]+?---\n)+([\\s\\S]*$)`, 'g') : new RegExp(`(^[\\s\\S]*$)`, 'g');
139+
const inlineAddRegex = contentHasFrontmatter ? new RegExp(`(^---[\\s\\S]+?---)+([\\s\\S]*$)`, 'g') : new RegExp(`(^[\\s\\S]*$)`, 'g');
142140
const noteObject = {
143141
action: 'replace',
144142
file: file,
@@ -168,23 +166,19 @@ class EditEngine {
168166
};
169167
await VaultManagerDB.editNoteContent(noteObject);
170168
}
171-
try {
172-
// Record of options
173-
const updateOptions: Record<string, any> = {};
174-
updateOptions[UpdateRowOptions.COLUMN_VALUE] = columnValue;
175-
updateOptions[UpdateRowOptions.COLUMN_KEY] = columnKey;
176-
updateOptions[UpdateRowOptions.REMOVE_COLUMN] = removeColumn;
177-
updateOptions[UpdateRowOptions.INLINE_VALUE] = inlineColumnEdit;
178-
// Execute action
179-
if (updateOptions[option]) {
180-
// Then execute the action
181-
await updateOptions[option]();
182-
} else {
183-
throw `Error: option ${option} not supported yet`;
184-
}
185-
} catch (e) {
186-
LOGGER.error(`${EditionError.YamlRead}`, e);
187-
new Notice(`${EditionError.YamlRead} : ${e.message}`, 6000);
169+
170+
// Record of options
171+
const updateOptions: Record<string, any> = {};
172+
updateOptions[UpdateRowOptions.COLUMN_VALUE] = columnValue;
173+
updateOptions[UpdateRowOptions.COLUMN_KEY] = columnKey;
174+
updateOptions[UpdateRowOptions.REMOVE_COLUMN] = removeColumn;
175+
updateOptions[UpdateRowOptions.INLINE_VALUE] = inlineColumnEdit;
176+
// Execute action
177+
if (updateOptions[option]) {
178+
// Then execute the action
179+
await updateOptions[option]();
180+
} else {
181+
throw `Error: option ${option} not supported yet`;
188182
}
189183
LOGGER.info(`<= updateRowFile.asociatedFilePathToCell: ${file.path} | columnId: ${columnId} | newValue: ${newValue} | option: ${option} `);
190184
}

0 commit comments

Comments
 (0)