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

Commit 6f1920c

Browse files
committed
add column now do not create in all rows the info
1 parent 58afcd4 commit 6f1920c

File tree

5 files changed

+109
-100
lines changed

5 files changed

+109
-100
lines changed

src/components/reducers/DatabaseDispatch.tsx

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ActionTypes,
55
DataTypes,
66
MetadataColumns,
7+
TableColumnsTemplate,
78
UpdateRowOptions,
89
} from "helpers/Constants";
910
import { TableColumn, TableDataType, RowDataType } from "cdm/FolderModel";
@@ -94,32 +95,47 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
9495
* Update key of column in all rows
9596
*/
9697
case ActionTypes.UPDATE_COLUMN_LABEL:
97-
const index = state.columns.findIndex(
98+
const update_column_label_index = state.columns.findIndex(
9899
(column: any) => column.id === action.columnId
99100
);
100101
// trim label will get a valid yaml key
101102
const update_col_key: string = dbTrim(action.label);
102103
// Update configuration & row files on disk
103-
state.view.diskConfig.updateColumnProperties(
104-
action.columnId,
105-
{ label: action.label, accessor: update_col_key, key: update_col_key },
106-
state // Update all rows with new key
104+
state.view.diskConfig.updateColumnProperties(action.columnId, {
105+
label: action.label,
106+
accessor: update_col_key,
107+
key: update_col_key,
108+
});
109+
// Once the column is updated, update the rows in case the key is changed
110+
Promise.all(
111+
state.data.map(async (row: RowDataType) => {
112+
updateRowFile(
113+
row.note.getFile(),
114+
state.columns[update_column_label_index].key,
115+
update_col_key,
116+
state,
117+
UpdateRowOptions.COLUMN_KEY
118+
);
119+
})
107120
);
108121
// Update state
109122
return {
110123
...state,
111124
skipReset: true,
112125
// Add column visually into the new label
113126
columns: [
114-
...state.columns.slice(0, index),
127+
...state.columns.slice(0, update_column_label_index),
115128
{
116-
...state.columns[index],
129+
...state.columns[update_column_label_index],
117130
label: action.label,
118131
id: update_col_key,
119132
key: update_col_key,
120133
accessor: update_col_key,
121134
},
122-
...state.columns.slice(index + 1, state.columns.length),
135+
...state.columns.slice(
136+
update_column_label_index + 1,
137+
state.columns.length
138+
),
123139
],
124140
// Add data visually into the new label
125141
data: state.data.map((row: RowDataType) => {
@@ -246,36 +262,25 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
246262
};
247263
// Update configuration on disk
248264
state.view.diskConfig.addColumn(action.columnInfo.name, newLeftColumn);
249-
250-
Promise.all(
251-
state.data.map(async (row: RowDataType) => {
252-
updateRowFile(
253-
row.note.getFile(),
254-
newLeftColumn.key,
255-
"",
256-
state,
257-
UpdateRowOptions.COLUMN_VALUE
258-
);
259-
})
260-
);
261265
// Update state
262-
return {
263-
...state,
264-
skipReset: true,
265-
columns: [
266-
...state.columns.slice(0, leftIndex),
267-
{
268-
id: newLeftColumn.accessor,
269-
label: newLeftColumn.label,
270-
key: newLeftColumn.key,
271-
accessor: newLeftColumn.accessor,
272-
dataType: DataTypes.TEXT,
273-
created: action.focus && true,
274-
options: [],
275-
},
276-
...state.columns.slice(leftIndex, state.columns.length),
277-
],
278-
};
266+
return update(state, {
267+
skipReset: { $set: true },
268+
columns: {
269+
$set: [
270+
...state.columns.slice(0, leftIndex),
271+
{
272+
...TableColumnsTemplate,
273+
id: newLeftColumn.accessor,
274+
label: newLeftColumn.label,
275+
key: newLeftColumn.key,
276+
accessor: newLeftColumn.accessor,
277+
position: newLeftColumn.position,
278+
csvCandidate: true,
279+
},
280+
...state.columns.slice(leftIndex, state.columns.length),
281+
],
282+
},
283+
});
279284
/**
280285
* Add new column to the table to the right of the column with the given id
281286
* and save it on disk
@@ -295,36 +300,25 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
295300
// Update configuration on disk
296301
state.view.diskConfig.addColumn(action.columnInfo.name, newRIghtColumn);
297302

298-
Promise.all(
299-
state.data.map(async (row: RowDataType) => {
300-
updateRowFile(
301-
row.note.getFile(),
302-
newRIghtColumn.key,
303-
"",
304-
state,
305-
UpdateRowOptions.COLUMN_VALUE
306-
);
307-
})
308-
);
309-
310-
return {
311-
...state,
312-
skipReset: true,
303+
return update(state, {
304+
skipReset: { $set: true },
313305
// Add column visually to the right of the column with the given id
314-
columns: [
315-
...state.columns.slice(0, rightIndex + 1),
316-
{
317-
id: newRIghtColumn.accessor,
318-
label: newRIghtColumn.label,
319-
key: newRIghtColumn.key,
320-
accessor: newRIghtColumn.accessor,
321-
dataType: DataTypes.TEXT,
322-
created: action.focus && true,
323-
options: [],
324-
},
325-
...state.columns.slice(rightIndex + 1, state.columns.length),
326-
],
327-
};
306+
columns: {
307+
$set: [
308+
...state.columns.slice(0, rightIndex + 1),
309+
{
310+
...TableColumnsTemplate,
311+
id: newRIghtColumn.accessor,
312+
label: newRIghtColumn.label,
313+
key: newRIghtColumn.key,
314+
accessor: newRIghtColumn.accessor,
315+
position: newRIghtColumn.position,
316+
csvCandidate: true,
317+
},
318+
...state.columns.slice(rightIndex + 1, state.columns.length),
319+
],
320+
},
321+
});
328322
case ActionTypes.DELETE_COLUMN:
329323
const deleteIndex = state.columns.findIndex(
330324
(column) => column.id === action.columnId
@@ -343,14 +337,15 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
343337
})
344338
);
345339
// Update state
346-
return {
347-
...state,
348-
skipReset: true,
349-
columns: [
350-
...state.columns.slice(0, deleteIndex),
351-
...state.columns.slice(deleteIndex + 1, state.columns.length),
352-
],
353-
};
340+
return update(state, {
341+
skipReset: { $set: true },
342+
columns: {
343+
$set: [
344+
...state.columns.slice(0, deleteIndex),
345+
...state.columns.slice(deleteIndex + 1, state.columns.length),
346+
],
347+
},
348+
});
354349
case ActionTypes.UPDATE_OPTION_CELL:
355350
// check if this column is configured as a group folder
356351
if (dbconfig.group_folder_column === action.key) {
@@ -421,7 +416,19 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
421416
state.view.diskConfig.updateColumnProperties(action.columnId, {
422417
isInline: action.isInline,
423418
});
424-
return state;
419+
const toggleInlineIndex = state.columns.findIndex(
420+
(column) => column.id === action.columnId
421+
);
422+
423+
return update(state, {
424+
columns: {
425+
[toggleInlineIndex]: {
426+
$merge: {
427+
isInline: action.isInline,
428+
},
429+
},
430+
},
431+
});
425432
default:
426433
LOGGER.warn(`<=> databaseReducer: unknown action ${action.type}`);
427434
}

src/helpers/Constants.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { TableColumn } from "cdm/FolderModel";
2+
13
/** Table Actions */
24
export const ActionTypes = Object.freeze({
35
ADD_OPTION_TO_COLUMN: 'add_option_to_column',
@@ -46,21 +48,32 @@ export const MetadataDatabaseColumns = Object.freeze({
4648
isMetadata: true,
4749
skipPersist: false,
4850
csvCandidate: true,
51+
isInline: false
4952
},
5053
ADD_COLUMN: {
5154
key: MetadataColumns.ADD_COLUMN,
5255
Header: MetadataColumns.ADD_COLUMN,
5356
input: DataTypes.NEW_COLUMN,
54-
width: 20,
5557
disableResizing: true,
5658
label: MetadataLabels.ADD_COLUMN,
5759
accessor: MetadataColumns.ADD_COLUMN,
5860
isMetadata: true,
5961
skipPersist: true,
6062
csvCandidate: false,
63+
isInline: false
6164
}
6265
});
6366

67+
export const TableColumnsTemplate: Partial<TableColumn> =
68+
{
69+
dataType: DataTypes.TEXT,
70+
isMetadata: false,
71+
skipPersist: false,
72+
isInline: false,
73+
options: [],
74+
csvCandidate: true,
75+
}
76+
6477
export const DatabaseCore = Object.freeze({
6578
FRONTMATTER_KEY: 'database-plugin'
6679
});
@@ -107,6 +120,6 @@ export const StyleVariables = Object.freeze({
107120
});
108121

109122
export const WidthVariables = Object.freeze({
110-
ICON_SPACING: 15,
123+
ICON_SPACING: 17,
111124
MAGIC_SPACING: 10
112125
});

src/helpers/VaultManagement.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: str
157157
* group 1 is inline field checking that starts in new line
158158
* group 2 is the current value of inline field
159159
*/
160-
const inlineFieldRegex = new RegExp(`(^${columnId}[:]{2}\\s)+([\\w\\W]+?$)`, 'gm');
160+
const inlineFieldRegex = new RegExp(`(^${columnId}[:]{2})+(.*$)`, 'gm');
161161
if (!inlineFieldRegex.test(content)) {
162162
await inlineAddColumn();
163163
return;
@@ -166,27 +166,29 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: str
166166
action: 'replace',
167167
file: file,
168168
regexp: inlineFieldRegex,
169-
newValue: `$1${newValue}`
169+
newValue: `$1 ${newValue}`
170170
};
171171
await VaultManagerDB.editNoteContent(noteObject);
172+
await persistFrontmatter();
172173
}
173174

174175
async function inlineColumnKey(): Promise<void> {
175176
/* Regex explanation
176177
* group 1 is inline field checking that starts in new line
177178
* group 2 is the current value of inline field
178179
*/
179-
const inlineFieldRegex = new RegExp(`(^${columnId}[:]{2}\\s)+([\\w\\W]+?$)`, 'gm');
180+
const inlineFieldRegex = new RegExp(`(^${columnId}[:]{2})+(.*$)`, 'gm');
180181
if (!inlineFieldRegex.test(content)) {
181182
return;
182183
}
183184
const noteObject = {
184185
action: 'replace',
185186
file: file,
186187
regexp: inlineFieldRegex,
187-
newValue: `${newValue}:: $2`
188+
newValue: `${newValue}::$2`
188189
};
189190
await VaultManagerDB.editNoteContent(noteObject);
191+
await persistFrontmatter();
190192
}
191193

192194
async function inlineAddColumn(): Promise<void> {
@@ -198,6 +200,7 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: str
198200
newValue: `$1${columnId}:: ${newValue}\n$2`
199201
};
200202
await VaultManagerDB.editNoteContent(noteObject);
203+
await persistFrontmatter();
201204
}
202205

203206
async function inlineRemoveColumn(): Promise<void> {
@@ -212,6 +215,7 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: str
212215
regexp: inlineFieldRegex
213216
};
214217
await VaultManagerDB.editNoteContent(noteObject);
218+
await persistFrontmatter(columnId);
215219
}
216220
// Record of options
217221
const updateOptions: Record<string, any> = {};

src/parsers/RowDatabaseFieldsToFile.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ export const parseFrontmatterFieldsToString = (databaseFields: RowDatabaseFields
66
const array: string[] = [];
77
array.push(`---`);
88
Object.keys(frontmatterFields).forEach(key => {
9-
// check if frontmatter field is inside inline fields
10-
if (!inlineFields.hasOwnProperty(key)) {
11-
array.push(`${key}: ${frontmatterFields[key]}`);
12-
}
9+
array.push(`${key}: ${frontmatterFields[key]}`);
1310
});
1411
if (original !== undefined) {
1512
const match = original.match(/^---\s+([\w\W]+?)\s+---/);
@@ -18,7 +15,8 @@ export const parseFrontmatterFieldsToString = (databaseFields: RowDatabaseFields
1815
const yaml = parseYaml(frontmatterRaw);
1916
Object.keys(yaml).forEach(key => {
2017
// add frontmatter fields that are not specified as database fields
21-
if (!frontmatterFields[key] && key !== deletedColumn) {
18+
// check if frontmatter field is inside inline fields
19+
if (!inlineFields.hasOwnProperty(key) && !frontmatterFields[key] && key !== deletedColumn) {
2220
array.push(`${key}: ${yaml[key]}`);
2321
}
2422
});

src/services/DatabaseInfo.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,12 @@ export default class DatabaseInfo {
7272
* @param columnId
7373
* @param properties
7474
*/
75-
async updateColumnProperties<P extends keyof DatabaseColumn>(columnId: string, properties: Record<string, P>, state?: TableDataType): Promise<void> {
75+
async updateColumnProperties<P extends keyof DatabaseColumn>(columnId: string, properties: Record<string, P>): Promise<void> {
7676
const colToUpdate = this.yaml.columns[columnId];
77-
const currentKey = colToUpdate.key;
7877
for (const key in properties) {
7978
colToUpdate[key] = properties[key];
8079
}
8180
this.yaml.columns[columnId] = colToUpdate;
82-
if (state !== undefined) {
83-
// Once the column is updated, update the rows in case the key is changed
84-
await Promise.all(state.data.map(async (row: RowDataType) => {
85-
updateRowFile(
86-
row.note.getFile(),
87-
currentKey,
88-
colToUpdate.key,
89-
state,
90-
UpdateRowOptions.COLUMN_KEY
91-
);
92-
}));
93-
}
9481
await this.saveOnDisk();
9582
}
9683

0 commit comments

Comments
 (0)