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

Commit 1640b75

Browse files
committed
cleaning
1 parent a5779b8 commit 1640b75

File tree

7 files changed

+150
-64
lines changed

7 files changed

+150
-64
lines changed

src/cdm/ComponentsModel.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ export type RowSelectOption = {
99
}
1010

1111
export type PopperProps = {
12-
dispatch: (action: any) => void;
1312
defaultCell: CellContext<RowDataType, Literal>;
14-
note: NoteInfo;
1513
}
1614

1715
export type TagsProps = {

src/cdm/TableStateInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface InitialState {
1616
export interface DataState {
1717
rows: RowDataType[];
1818
addRow: (filename: string, columns: TableColumn[], ddbbConfig: LocalSettings) => void;
19-
updateCell: (rowIndex: number, column: TableColumn, value: Literal, columns: TableColumn[], ddbbConfig: LocalSettings) => void;
19+
updateCell: (rowIndex: number, column: TableColumn, value: Literal, columns: TableColumn[], ddbbConfig: LocalSettings, isMovingFile?: boolean) => void;
2020
parseDataOfColumn: (column: TableColumn, input: string, ddbbConfig: LocalSettings) => void;
2121
removeRow: (row: RowDataType) => void;
2222
removeDataOfColumn: (column: TableColumn) => void;

src/components/DefaultCell.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useRef, useState } from "react";
2-
import { ActionTypes, InputType } from "helpers/Constants";
2+
import { InputType } from "helpers/Constants";
33
import { c } from "helpers/StylesHelper";
44

55
import { LOGGER } from "services/Logger";
@@ -37,8 +37,6 @@ export default function DefaultCell(
3737
);
3838
/** Type of cell */
3939
const input = columns.find((col) => col.id === column.id).input;
40-
/** Note info of current Cell */
41-
const note: NoteInfo = row.original.__note__;
4240
/** Ref to cell container */
4341
const containerCellRef = useRef<HTMLDivElement>();
4442
const editableMdRef = useRef<HTMLInputElement>();
@@ -233,15 +231,7 @@ export default function DefaultCell(
233231

234232
/** Selector option */
235233
case InputType.SELECT:
236-
return (
237-
<TableCellContext.Provider value={{ contextValue, setContextValue }}>
238-
<PopperSelectPortal
239-
dispatch={dataDispatch}
240-
note={note}
241-
defaultCell={defaultCell}
242-
/>
243-
</TableCellContext.Provider>
244-
);
234+
return <PopperSelectPortal defaultCell={defaultCell} />;
245235
/** Tags option */
246236
case InputType.TAGS:
247237
return (
@@ -258,7 +248,7 @@ export default function DefaultCell(
258248
case InputType.CHECKBOX:
259249
return (
260250
<TableCellContext.Provider value={{ contextValue, setContextValue }}>
261-
<CheckboxCell column={column} defaultCell={defaultCell} />
251+
<CheckboxCell defaultCell={defaultCell} />
262252
</TableCellContext.Provider>
263253
);
264254
case InputType.NEW_COLUMN:

src/components/portals/CalendarPortal.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ const CalendarPortal = (calendarProps: CalendarProps) => {
2424

2525
/** state of cell value */
2626
const [showDatePicker, setShowDatePicker] = useState(false);
27-
/** Note info of current Cell */
28-
const note: NoteInfo = row.original.__note__;
2927
const calendarValue = rows[row.index][tableColumn.key];
3028

3129
function handleSpanOnClick(event: any) {

src/components/portals/PopperSelectPortal.tsx

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
import PlusIcon from "components/img/Plus";
22
import Relationship from "components/RelationShip";
33
import { grey, randomColor } from "helpers/Colors";
4-
import { ActionTypes, StyleVariables } from "helpers/Constants";
5-
import React, { useContext, useState } from "react";
4+
import { StyleVariables } from "helpers/Constants";
5+
import React, { useState } from "react";
66
import ReactDOM from "react-dom";
77
import { usePopper } from "react-popper";
88
import { PopperProps } from "cdm/ComponentsModel";
9-
import { TableCellContext } from "components/contexts/CellContext";
109
import CrossIcon from "components/img/CrossIcon";
1110
import { TableColumn } from "cdm/FolderModel";
1211

1312
const PopperSelectPortal = (popperProps: PopperProps) => {
14-
const { dispatch, note, defaultCell } = popperProps;
13+
const { defaultCell } = popperProps;
1514
const { row, column, table } = defaultCell;
15+
const [rows, updateCell] = table.options.meta.tableState.data((state) => [
16+
state.rows,
17+
state.updateCell,
18+
]);
19+
const columns = table.options.meta.tableState.columns(
20+
(state) => state.columns
21+
);
22+
const ddbbConfig = table.options.meta.tableState.configState(
23+
(state) => state.ddbbConfig
24+
);
25+
const tableColumn = column.columnDef as TableColumn;
1626
/** state of cell value */
17-
const { contextValue, setContextValue } = useContext(TableCellContext);
27+
const selectState = rows[row.index][tableColumn.key];
1828
// Selector reference state
1929
const [selectRef, setSelectRef] = useState(null);
2030
const [showSelect, setShowSelect] = useState(false);
@@ -29,22 +39,21 @@ const PopperSelectPortal = (popperProps: PopperProps) => {
2939
const addOptionToColumn = table.options.meta.tableState.columns(
3040
(state) => state.addOptionToColumn
3141
);
32-
const tableColumn = column.columnDef as TableColumn;
3342
React.useEffect(() => {
34-
setDomReady(true);
43+
if (!domReady) {
44+
setDomReady(true);
45+
}
3546
});
3647

3748
function handleRemoveOption(e: any) {
38-
dispatch({
39-
type: ActionTypes.UPDATE_OPTION_CELL,
40-
file: note.getFile(),
41-
key: tableColumn.key,
42-
value: "",
43-
row: row,
44-
columnId: column.id,
45-
state: table.options.meta,
46-
});
47-
setContextValue({ value: "", update: true });
49+
updateCell(
50+
row.index,
51+
column.columnDef as TableColumn,
52+
"",
53+
columns,
54+
ddbbConfig,
55+
true
56+
);
4857
setShowSelect(false);
4958
}
5059

@@ -53,16 +62,14 @@ const PopperSelectPortal = (popperProps: PopperProps) => {
5362
backgroundColor?: string;
5463
}) {
5564
// save on disk & move file if its configured on the column
56-
dispatch({
57-
type: ActionTypes.UPDATE_OPTION_CELL,
58-
file: note.getFile(),
59-
key: tableColumn.key,
60-
value: option.label,
61-
row: row,
62-
columnId: column.id,
63-
state: table.options.meta,
64-
});
65-
setContextValue({ value: option.label, update: true });
65+
updateCell(
66+
row.index,
67+
column.columnDef as TableColumn,
68+
option.label,
69+
columns,
70+
ddbbConfig,
71+
true
72+
);
6673
setShowSelect(false);
6774
}
6875

@@ -97,7 +104,7 @@ const PopperSelectPortal = (popperProps: PopperProps) => {
97104

98105
function getColor() {
99106
const match = tableColumn.options.find(
100-
(option: { label: string }) => option.label === contextValue.value
107+
(option: { label: string }) => option.label === selectState
101108
);
102109
return (match && match.backgroundColor) || grey(200);
103110
}
@@ -203,9 +210,9 @@ const PopperSelectPortal = (popperProps: PopperProps) => {
203210
onClick={() => setShowSelect(true)}
204211
style={{ width: column.getSize() }}
205212
>
206-
{contextValue.value && (
213+
{selectState && (
207214
<Relationship
208-
value={contextValue.value.toString()}
215+
value={selectState.toString()}
209216
backgroundColor={getColor()}
210217
/>
211218
)}

src/helpers/VaultManagement.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,19 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: Lit
355355
* @param folderPath
356356
* @param action
357357
*/
358-
export async function moveFile(folderPath: string, action: any): Promise<void> {
358+
export async function moveFile(folderPath: string, info: {
359+
file: TFile,
360+
id: string,
361+
value: Literal,
362+
columns: TableColumn[],
363+
ddbbConfig: LocalSettings
364+
}): Promise<void> {
359365
await updateRowFile(
360-
action.file,
361-
action.key,
362-
action.value,
363-
action.columns,
364-
action.ddbbConfig,
366+
info.file,
367+
info.id,
368+
info.value,
369+
info.columns,
370+
info.ddbbConfig,
365371
UpdateRowOptions.COLUMN_VALUE
366372
);
367373
try {
@@ -371,8 +377,8 @@ export async function moveFile(folderPath: string, action: any): Promise<void> {
371377
// Handle error
372378
throw error;
373379
}
374-
const filePath = `${folderPath} / ${action.file.name}`;
375-
await app.fileManager.renameFile(action.file, filePath);
380+
const filePath = `${folderPath} / ${info.file.name}`;
381+
await app.fileManager.renameFile(info.file, filePath);
376382
}
377383

378384
export async function createFolder(folderPath: string): Promise<void> {

src/stateManagement/useDataStore.ts

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { LocalSettings } from "cdm/SettingsModel";
44
import { DataState } from "cdm/TableStateInterface";
55
import { DatabaseView } from "DatabaseView";
66
import { MetadataColumns, UpdateRowOptions } from "helpers/Constants";
7-
import { updateRowFileProxy } from "helpers/VaultManagement";
7+
import { moveFile, updateRowFileProxy } from "helpers/VaultManagement";
88
import { DateTime } from "luxon";
99
import { Literal } from "obsidian-dataview";
1010
import { DataviewService } from "services/DataviewService";
@@ -52,8 +52,43 @@ const useDataStore = (view: DatabaseView) => {
5252
};
5353
return { rows: [...state.rows, row] }
5454
}),
55-
updateCell: (rowIndex: number, column: TableColumn, value: Literal, columns: TableColumn[], ddbbConfig: LocalSettings) => set((state) => {
56-
const rowTFile = state.rows[rowIndex].__note__.getFile();
55+
updateCell: (rowIndex: number, column: TableColumn, value: Literal, columns: TableColumn[], ddbbConfig: LocalSettings, isMovingFile?: boolean) => set((state) => {
56+
const row = { ...state.rows[rowIndex] };
57+
row[column.key] = value;
58+
console.log(ddbbConfig.show_metadata_modified);
59+
if (ddbbConfig.show_metadata_modified) {
60+
row[MetadataColumns.MODIFIED] = DateTime.now();
61+
}
62+
let rowTFile = state.rows[rowIndex].__note__.getFile();
63+
if (isMovingFile && ddbbConfig.group_folder_column === column.id) {
64+
const moveInfo = {
65+
file: rowTFile,
66+
id: column.id,
67+
value: value,
68+
columns: columns,
69+
ddbbConfig: ddbbConfig,
70+
}
71+
moveFile(`${view.file.parent.path}/${value}`, moveInfo);
72+
// Update row file
73+
row[
74+
MetadataColumns.FILE
75+
] = `[[${view.file.parent.path}/${value}/${rowTFile.name}|${rowTFile.basename}]]`;
76+
// Check if action.value is a valid folder name
77+
const auxPath =
78+
value !== ""
79+
? `${view.file.parent.path}/${value}/${rowTFile.name}`
80+
: `${view.file.parent.path}/${rowTFile.name}`;
81+
82+
row.__note__ = new NoteInfo({
83+
...row,
84+
file: {
85+
path: auxPath,
86+
},
87+
});
88+
// Update rows
89+
return { rows: [...state.rows.slice(0, rowIndex), row, ...state.rows.slice(rowIndex + 1)] };
90+
}
91+
5792
// Save on disk
5893
updateRowFileProxy(
5994
rowTFile,
@@ -64,10 +99,6 @@ const useDataStore = (view: DatabaseView) => {
6499
UpdateRowOptions.COLUMN_VALUE
65100
);
66101

67-
// Update row in memory
68-
const row = { ...state.rows[rowIndex] };
69-
row[column.key] = value;
70-
row[MetadataColumns.MODIFIED] = DateTime.now();
71102
return { rows: [...state.rows.slice(0, rowIndex), row, ...state.rows.slice(rowIndex + 1)] };
72103
}
73104
),
@@ -97,4 +128,60 @@ const useDataStore = (view: DatabaseView) => {
97128
}),
98129
);
99130
}
100-
export default useDataStore;
131+
export default useDataStore;
132+
133+
/**
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}`;
159+
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+
},
181+
},
182+
},
183+
},
184+
});
185+
}
186+
break;
187+
*/

0 commit comments

Comments
 (0)