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

Commit 3d2c7ac

Browse files
committed
Merge branch 'hotfix-update-cell-after-change-column-label'
2 parents 7b5de90 + e4c2c36 commit 3d2c7ac

File tree

5 files changed

+42
-22
lines changed

5 files changed

+42
-22
lines changed

src/components/Columns.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {TableColumn} from 'cdm/FolderModel';
33
import { LOGGER } from "services/Logger";
44
import { DatabaseColumn } from 'cdm/DatabaseModel';
55
import { RowSelectOption } from 'cdm/RowSelectModel';
6+
import { dbTrim } from 'parsers/DatabaseParser';
67

78
/**
89
* Add mandatory columns to the table
@@ -58,7 +59,7 @@ async function columnOptions(columnKey:string,index:number, column:DatabaseColum
5859
position: column.position ?? index,
5960
label: column.label,
6061
key: column.key ?? columnKey,
61-
accessor: column.accessor ?? column.label.trim().toLowerCase(),
62+
accessor: column.accessor ?? dbTrim(column.label),
6263
isMetadata: column.isMetadata ?? false
6364
}
6465
/**

src/components/Header.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ActionTypes, DataTypes, MetadataColumns } from 'helpers/Constants';
1414
import { LOGGER } from 'services/Logger';
1515
import { DatabaseHeaderProps } from 'cdm/FolderModel';
1616
import ReactDOM from 'react-dom';
17+
import { dbTrim } from 'parsers/DatabaseParser';
1718

1819
function setOptionsOfSelectDataType(options:any[],rows:any,columnId:string):any[]{
1920
rows.forEach((row:any)=>{
@@ -53,7 +54,7 @@ export default function Header(headerProps:DatabaseHeaderProps) {
5354
strategy: "absolute"
5455
});
5556
const [labelState, setLabelState] = useState(headerProps.column.label);
56-
const [keyState, setkeyState] = useState(key.trim());
57+
const [keyState, setkeyState] = useState(dbTrim(key));
5758
const [typeReferenceElement, setTypeReferenceElement] = useState(null);
5859
const [typePopperElement, setTypePopperElement] = useState(null);
5960
const [showType, setShowType] = useState(false);
@@ -185,7 +186,7 @@ export default function Header(headerProps:DatabaseHeaderProps) {
185186
label: labelState
186187
});
187188
setExpanded(false);
188-
setkeyState(labelState.trim());
189+
setkeyState(dbTrim(labelState));
189190
}
190191
}
191192

src/components/reducers/DatabaseDispatch.tsx

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { adapterRowToDatabaseYaml, updateRowFile } from 'helpers/VaultManagement
99
import { randomColor } from 'helpers/Colors';
1010
import { DatabaseColumn } from 'cdm/DatabaseModel';
1111
import NoteInfo from 'services/NoteInfo';
12+
import { dbTrim } from 'parsers/DatabaseParser';
1213

1314
export function databaseReducer(state:TableDataType, action:ActionType) {
1415
LOGGER.debug(`<=>databaseReducer action: ${action.type}`);
@@ -74,27 +75,20 @@ export function databaseReducer(state:TableDataType, action:ActionType) {
7475
(column:any) => column.id === action.columnId
7576
);
7677
// trim label will get a valid yaml key
77-
const update_col_key:string = action.label.trim();
78-
// Update configuration on disk
78+
const update_col_key:string = dbTrim(action.label);
79+
// Update configuration & row files on disk
7980
state.view.diskConfig.updateColumnProperties(
8081
action.columnId,
81-
{label: action.label, accessor: update_col_key, key: update_col_key}
82+
{label: action.label, accessor: update_col_key, key: update_col_key},
83+
state.data // Update all rows with new key
8284
);
83-
Promise.all(state.data.map(async (row:TableRow) => {
84-
updateRowFile(
85-
row.note.getFile(),
86-
action.accessor,
87-
update_col_key,
88-
UpdateRowOptions.COLUMN_KEY
89-
);
90-
}));
9185
// Update state
9286
return {
9387
...state,
9488
skipReset: true,
9589
columns: [
9690
...state.columns.slice(0, index),
97-
{ ...state.columns[index], label: action.label },
91+
{ ...state.columns[index], label: action.label, id: update_col_key, key: update_col_key, accessor: update_col_key },
9892
...state.columns.slice(index + 1, state.columns.length)
9993
]
10094
};
@@ -238,7 +232,7 @@ export function databaseReducer(state:TableDataType, action:ActionType) {
238232
label: newLeftColumn.label,
239233
key: newLeftColumn.key,
240234
accessor: newLeftColumn.accessor,
241-
dataType: newLeftColumn.input,
235+
dataType: DataTypes.TEXT,
242236
created: action.focus && true,
243237
options: []
244238
},
@@ -282,7 +276,7 @@ export function databaseReducer(state:TableDataType, action:ActionType) {
282276
label: newRIghtColumn.label,
283277
key: newRIghtColumn.key,
284278
accessor: newRIghtColumn.accessor,
285-
dataType: newRIghtColumn.input,
279+
dataType: DataTypes.TEXT,
286280
created: action.focus && true,
287281
options: []
288282
},

src/parsers/DatabaseParser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,13 @@ export function hasFrontmatterKey(data: string) {
4949
databaseConfigString.push(`config:`);
5050
databaseConfigString.push(`${yamlIndent.repeat(1)}enable_show_state: ${databaseConfig.config.enable_show_state}`);
5151
return databaseConfigString;
52+
}
53+
54+
/**
55+
* Given a string, parse it to be key candidate
56+
* @param str
57+
* @returns {string}
58+
*/
59+
export function dbTrim(str: string) {
60+
return str.trim().replaceAll("\n", "").replaceAll("\t", "").replaceAll(" ", "_");
5261
}

src/services/DatabaseInfo.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
import { LOGGER } from 'services/Logger';
77
import { VaultManagerDB } from 'services/FileManagerService';
88
import { convertDatabaseYamlToParsedString, hasFrontmatterKey } from 'parsers/DatabaseParser';
9-
import { NoteContentAction } from 'cdm/FolderModel';
9+
import { NoteContentAction, TableRow } from 'cdm/FolderModel';
1010
import { LocalSettings } from 'Settings';
11+
import { updateRowFile } from 'helpers/VaultManagement';
12+
import { UpdateRowOptions } from 'helpers/Constants';
1113

1214
export default class DatabaseInfo {
1315
private file: TFile;
@@ -78,12 +80,25 @@ export default class DatabaseInfo {
7880
* @param columnId
7981
* @param properties
8082
*/
81-
async updateColumnProperties<P extends keyof DatabaseColumn>(columnId:string, properties:Record<string,P>):Promise<void>{
82-
const currentCol = this.yaml.columns[columnId];
83+
async updateColumnProperties<P extends keyof DatabaseColumn>(columnId:string, properties:Record<string,P>,rows?:TableRow[]):Promise<void>{
84+
const colToUpdate = this.yaml.columns[columnId];
85+
const currentKey = colToUpdate.key;
8386
for (const key in properties) {
84-
currentCol[key] = properties[key];
87+
colToUpdate[key] = properties[key];
88+
}
89+
this.yaml.columns[columnId] = colToUpdate;
90+
if(rows){
91+
await this.updateColumnKey(columnId, colToUpdate.accessor);
92+
// Once the column is updated, update the rows in case the key is changed
93+
await Promise.all(rows.map(async (row:TableRow) => {
94+
updateRowFile(
95+
row.note.getFile(),
96+
currentKey,
97+
colToUpdate.key,
98+
UpdateRowOptions.COLUMN_KEY
99+
);
100+
}));
85101
}
86-
this.yaml.columns[columnId] = currentCol;
87102
await this.saveOnDisk();
88103
}
89104

0 commit comments

Comments
 (0)