@@ -4,6 +4,7 @@ import { LocalSettings } from "cdm/SettingsModel";
44import { DataState } from "cdm/TableStateInterface" ;
55import { DatabaseView } from "DatabaseView" ;
66import { MetadataColumns , UpdateRowOptions } from "helpers/Constants" ;
7+ import { dbTrim } from "helpers/StylesHelper" ;
78import { moveFile , updateRowFileProxy } from "helpers/VaultManagement" ;
89import { DateTime } from "luxon" ;
910import { Literal } from "obsidian-dataview" ;
@@ -102,6 +103,32 @@ const useDataStore = (view: DatabaseView) => {
102103 return { rows : [ ...state . rows . slice ( 0 , rowIndex ) , row , ...state . rows . slice ( rowIndex + 1 ) ] } ;
103104 }
104105 ) ,
106+ updateDataAfterLabelChange : ( column : TableColumn , label : string , columns : TableColumn [ ] , ddbbConfig : LocalSettings ) => set ( ( state ) => {
107+ const newKey = dbTrim ( label ) ;
108+ // Save on disk
109+ Promise . all (
110+ state . rows . map ( async ( row : RowDataType ) => {
111+ updateRowFileProxy (
112+ row . __note__ . getFile ( ) ,
113+ column . id ,
114+ newKey ,
115+ columns ,
116+ ddbbConfig ,
117+ UpdateRowOptions . COLUMN_KEY
118+ ) ;
119+ } ) ) ;
120+
121+ // Save on memory
122+ const alterRows = state . rows . map ( ( row ) => {
123+ row [ newKey ] = row [ column . id ] ;
124+ delete row [ column . id ] ;
125+ return row ;
126+ } ) ;
127+
128+ return { rows : alterRows } ;
129+ }
130+ ) ,
131+
105132 parseDataOfColumn : ( column : TableColumn , input : string , ddbbConfig : LocalSettings ) => {
106133 set ( ( updater ) => {
107134 const parsedRows = updater . rows . map ( ( row ) => ( {
@@ -126,62 +153,67 @@ const useDataStore = (view: DatabaseView) => {
126153 return { rows : newRows } ;
127154 } ) ,
128155 } ) ,
156+
129157 ) ;
130158}
131159export default useDataStore ;
132-
133160/**
134- *
135- * dispatch({
136- type: ActionTypes.UPDATE_OPTION_CELL,
137- file: note.getFile(),
138- key: tableColumn.key,
139- value: option.label,
140- row: row,
141- columnId: column.id,
142- state: table.options.meta,
143- });
144-
145-
146-
147- * case ActionTypes.UPDATE_OPTION_CELL:
148- // check if this column is configured as a group folder
149- if (dbconfig.group_folder_column === action.key) {
150- moveFile(`${state.view.file.parent.path}/${action.value}`, action);
151- action.row[
152- MetadataColumns.FILE
153- ] = `[[${state.view.file.parent.path}/${action.value}/${action.file.name}|${action.file.basename}]]`;
154- // Check if action.value is a valid folder name
155- const auxPath =
156- action.value !== ""
157- ? `${state.view.file.parent.path}/${action.value}/${action.file.name}`
158- : `${state.view.file.parent.path}/${action.file.name}`;
161+ * case ActionTypes.UPDATE_COLUMN_LABEL:
162+ const update_column_label_index = state.view.columns.findIndex(
163+ (column: any) => column.id === action.columnId
164+ );
159165
160- action.row.original.__note__ = new NoteInfo({
161- ...action.row,
162- file: {
163- path: auxPath,
164- },
165- });
166- // Update original cell value
167- const update_option_cell_index = state.view.columns.findIndex(
168- (column) => column.id === action.columnId
169- );
170- const update_option_cell_column_key =
171- state.view.columns[update_option_cell_index].key;
172- return update(state, {
173- view: {
174- rows: {
175- [action.row.index]: {
176- $merge: {
177- [MetadataColumns.FILE]: action.row[MetadataColumns.FILE],
178- note: action.row.original.__note__,
179- [update_option_cell_column_key]: action.value,
180- },
166+ // Update configuration & row files on disk
167+ state.view.diskConfig.updateColumnKey(
168+ action.columnId,
169+ action.newKey,
170+ action.label
171+ );
172+ // Promise.all(
173+ // state.view.rows.map(async (row: RowDataType) => {
174+ // await updateRowFileProxy(
175+ // row.__note__.getFile(),
176+ // action.columnId,
177+ // action.newKey,
178+ // action.state,
179+ // UpdateRowOptions.COLUMN_KEY
180+ // );
181+ // })
182+ // );
183+ return update(state, {
184+ skipReset: { $set: true },
185+ // Modify column visually with the new label
186+ view: {
187+ columns: {
188+ $set: [
189+ ...state.view.columns.slice(0, update_column_label_index),
190+ {
191+ ...state.view.columns[update_column_label_index],
192+ label: action.label,
193+ id: action.newKey,
194+ key: action.newKey,
195+ accessorKey: action.newKey,
181196 },
197+ ...state.view.columns.slice(
198+ update_column_label_index + 1,
199+ state.view.columns.length
200+ ),
201+ ],
202+ },
203+ // Modify data visually with the new key
204+ rows: {
205+ $set: state.view.rows.map((row: RowDataType) => {
206+ row[action.newKey] = row[action.columnId];
207+ delete row[action.columnId];
208+ return row;
209+ }),
210+ },
211+ // Update view yaml state
212+ diskConfig: {
213+ yaml: {
214+ $set: state.view.diskConfig.yaml,
182215 },
183216 },
184- });
185- }
186- break;
217+ },
218+ });
187219 */
0 commit comments