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

Commit d61a674

Browse files
committed
Merge branch 'bug-pr-feedback-official-release'
2 parents 7a175c8 + 6beed32 commit d61a674

File tree

8 files changed

+134
-72
lines changed

8 files changed

+134
-72
lines changed

src/cdm/DatabaseModel.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { RowType } from "cdm/RowTypeModel"
2-
import { Literal } from "obsidian-dataview/lib/data-model/value";
32
import { LocalSettings } from "Settings"
43

54
/** database column */
6-
export type DatabaseColumn = {
7-
input: string,
8-
accessor: string,
9-
label: string,
10-
key?: string,
11-
position?: number,
12-
isMetadata?: boolean,
13-
skipPersist?: boolean,
14-
csvCandidate?: boolean,
15-
isInline?: boolean,
16-
[key: string]: RowType
5+
export interface DatabaseColumn {
6+
input: string;
7+
accessor: string;
8+
label: string;
9+
key: string;
10+
position?: number;
11+
isMetadata?: boolean;
12+
skipPersist?: boolean;
13+
csvCandidate?: boolean;
14+
isInline?: boolean;
15+
[key: string]: RowType;
1716
}
1817

1918
/** database yaml */

src/cdm/FolderModel.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,23 @@ export type Models = {
3434
[key: string]: FolderModel
3535
}
3636

37-
export type TableColumn = {
38-
id: string,
39-
Header?: any,
40-
label?: string,
41-
key: string,
42-
position: number,
43-
accessor: any,
44-
minWidth?: number,
45-
width?: number
46-
dataType?: string,
47-
options?: RowSelectOption[]
48-
Cell?: any,
49-
getHeaderProps?: any,
50-
getResizerProps?: any,
51-
isMetadata?: boolean,
52-
skipPersist?: boolean,
53-
isInline?: boolean,
54-
csvCandidate: boolean
37+
export interface TableColumn {
38+
id: string;
39+
label: string;
40+
key: string;
41+
position: number;
42+
accessor: any;
43+
minWidth?: number;
44+
width?: number;
45+
dataType?: string;
46+
options?: RowSelectOption[];
47+
Cell?: any;
48+
getHeaderProps?: any;
49+
getResizerProps?: any;
50+
isMetadata?: boolean;
51+
skipPersist?: boolean;
52+
isInline?: boolean;
53+
csvCandidate: boolean;
5554
}
5655

5756
export type RowDataType = {

src/components/Columns.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export async function obtainMetadataColumns(
2222
...MetadataDatabaseColumns.FILE,
2323
...(yamlColumns[MetadataColumns.FILE] ?? {}),
2424
};
25-
2625
yamlColumns[MetadataColumns.ADD_COLUMN] = MetadataDatabaseColumns.ADD_COLUMN;
2726
return yamlColumns;
2827
}
@@ -46,24 +45,35 @@ export async function obtainColumnsFromFolder(
4645
return sortColumnsByPosition(columns);
4746
}
4847

48+
function parseDatabaseToTableColumn(
49+
databaseColumn: DatabaseColumn,
50+
columnKey: string,
51+
index: number
52+
): TableColumn {
53+
const tableColumn: TableColumn = {
54+
...(databaseColumn as Partial<TableColumn>),
55+
id: columnKey,
56+
position: databaseColumn.position ?? index,
57+
key: databaseColumn.key ?? columnKey,
58+
label: databaseColumn.label,
59+
accessor: databaseColumn.accessor ?? dbTrim(databaseColumn.label),
60+
csvCandidate: databaseColumn.csvCandidate ?? true,
61+
};
62+
return tableColumn;
63+
}
64+
4965
async function columnOptions(
5066
columnKey: string,
5167
index: number,
5268
column: DatabaseColumn
5369
): Promise<TableColumn> {
5470
LOGGER.debug(`=> columnOptions. column: ${JSON.stringify(column)}`);
5571
const options: RowSelectOption[] = [];
56-
const tableRow: TableColumn = {
57-
id: columnKey,
58-
position: column.position ?? index,
59-
label: column.label,
60-
key: column.key ?? columnKey,
61-
accessor: column.accessor ?? dbTrim(column.label),
62-
isMetadata: column.isMetadata ?? false,
63-
skipPersist: column.skipPersist ?? false,
64-
csvCandidate: column.csvCandidate ?? true,
65-
isInline: column.isInline ?? false,
66-
};
72+
const tableRow: TableColumn = parseDatabaseToTableColumn(
73+
column,
74+
columnKey,
75+
index
76+
);
6777
/**
6878
* return plain text
6979
* @returns {TableColumn}

src/components/HeaderMenu.tsx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
7070
const [settingsPopperElement, setSettingsPopperElement] = useState(null);
7171
const [showSettings, setShowSettings] = useState(false);
7272

73+
// Manage errors
74+
const [labelStateInvalid, setLabelStateInvalid] = useState(false);
7375
useEffect(() => {
7476
if (created) {
7577
setExpanded(true);
@@ -208,9 +210,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
208210
}
209211
);
210212

211-
function persistLabelChange() {
212-
// trim label will get a valid yaml key
213-
const newKey = dbTrim(labelState);
213+
function persistLabelChange(newKey: string) {
214214
const futureOrder = headerMenuProps.headerProps.allColumns.map(
215215
(o: Column) => (o.id === column.id ? newKey : o.id)
216216
);
@@ -239,12 +239,26 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
239239
}
240240
function handleKeyDown(e: any) {
241241
if (e.key === "Enter") {
242-
persistLabelChange();
242+
// trim label will get a valid yaml key
243+
const newKey = dbTrim(labelState);
244+
// Check if key already exists. If so, mark it as invalid
245+
if (
246+
headerMenuProps.headerProps.allColumns.find(
247+
(o: Column) => o.id === newKey
248+
)
249+
) {
250+
setLabelStateInvalid(true);
251+
return;
252+
}
253+
persistLabelChange(newKey);
243254
}
244255
}
245256

246257
function handleChange(e: any) {
247258
setLabelState(e.target.value);
259+
if (labelStateInvalid) {
260+
setLabelStateInvalid(false);
261+
}
248262
}
249263

250264
/**
@@ -292,7 +306,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
292306
<div
293307
className={`menu ${c("popper")}`}
294308
style={{
295-
width: 240
309+
width: 240,
296310
}}
297311
>
298312
{/** Edit header label section */}
@@ -305,7 +319,11 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
305319
>
306320
<div className="is-fullwidth" style={{ marginBottom: 12 }}>
307321
<input
308-
className="form-input"
322+
className={
323+
labelStateInvalid
324+
? `${c("invalid-form")}`
325+
: `${c("form-input")}`
326+
}
309327
ref={setInputRef}
310328
type="text"
311329
value={labelState}
@@ -356,7 +374,10 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
356374
>
357375
{types.map((type) => (
358376
<div key={type.label}>
359-
<div className="menu-item sort-button" onClick={type.onClick}>
377+
<div
378+
className="menu-item sort-button"
379+
onClick={type.onClick}
380+
>
360381
<span className="svg-icon svg-text icon-margin">
361382
{type.icon}
362383
</span>
@@ -439,9 +460,8 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
439460
</div>
440461
</div>
441462
</div>
442-
)
443-
}
444-
</div >
463+
)}
464+
</div>
445465
);
446466
};
447467

src/components/Table.tsx

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import { TableDataType, RowDataType, TableColumn } from "cdm/FolderModel";
1414
import { DatabaseView } from "DatabaseView";
1515
import StateManager from "StateManager";
1616
import { getNormalizedPath } from "helpers/VaultManagement";
17-
import { ActionTypes, DatabaseCore } from "helpers/Constants";
17+
import { ActionTypes, DatabaseCore, MetadataColumns } from "helpers/Constants";
1818
import PlusIcon from "components/img/Plus";
1919
import { LOGGER } from "services/Logger";
2020
import DefaultCell from "components/Cell";
2121
import Header from "components/Header";
22-
import { c } from "helpers/StylesHelper";
22+
import { c, getTotalWidth } from "helpers/StylesHelper";
2323
import { HeaderNavBar } from "components/NavBar";
2424
import { getColumnsWidthStyle } from "components/styles/ColumnWidthStyle";
2525
import { HeaderContext } from "components/contexts/HeaderContext";
@@ -196,6 +196,21 @@ export function Table(initialState: TableDataType) {
196196
// Manage input of new row
197197
const [inputNewRow, setInputNewRow] = React.useState("");
198198
const newRowRef = React.useRef(null);
199+
function handleKeyDown(e: any) {
200+
if (e.key === "Enter") {
201+
handleAddNewRow();
202+
}
203+
}
204+
205+
function handleAddNewRow() {
206+
dataDispatch({
207+
type: ActionTypes.ADD_ROW,
208+
filename: inputNewRow,
209+
});
210+
setInputNewRow("");
211+
newRowRef.current.value = "";
212+
}
213+
199214
// Manage NavBar
200215
const csvButtonProps = {
201216
columns: columns,
@@ -215,7 +230,7 @@ export function Table(initialState: TableDataType) {
215230
{...getTableProps({
216231
style: {
217232
...getTableProps().style,
218-
width: totalColumnsWidth,
233+
minWidth: totalColumnsWidth,
219234
},
220235
})}
221236
className={`${c("table noselect")}`}
@@ -234,7 +249,7 @@ export function Table(initialState: TableDataType) {
234249
csvButtonProps={csvButtonProps}
235250
globalFilterRows={globalFilterRows}
236251
headerGroupProps={headerGroups[0].getHeaderGroupProps({
237-
style: { width: totalColumnsWidth },
252+
style: { width: getTotalWidth(columnsWidthState) },
238253
})}
239254
/>
240255
{/** Headers */}
@@ -280,6 +295,11 @@ export function Table(initialState: TableDataType) {
280295
{...headerGroup.getHeaderGroupProps({
281296
style: {
282297
...getDndListStyle(snapshot.isDraggingOver),
298+
width:
299+
getTotalWidth(columnsWidthState) -
300+
columnsWidthState.widthRecord[
301+
MetadataColumns.ADD_COLUMN
302+
],
283303
},
284304
})}
285305
ref={provided.innerRef}
@@ -291,6 +311,9 @@ export function Table(initialState: TableDataType) {
291311
draggableId={`${column.id}`}
292312
index={index}
293313
isDragDisabled={(column as any).skipPersist}
314+
disableInteractiveElementBlocking={
315+
(column as any).skipPersist
316+
}
294317
>
295318
{(provided, snapshot) => {
296319
const tableCellBaseProps = {
@@ -342,7 +365,7 @@ export function Table(initialState: TableDataType) {
342365
<div
343366
{...row.getRowProps({
344367
style: {
345-
maxWidth: `${totalColumnsWidth}px`,
368+
minWidth: `${totalColumnsWidth}px`,
346369
},
347370
})}
348371
className={`${c("tr")}`}
@@ -372,20 +395,11 @@ export function Table(initialState: TableDataType) {
372395
onChange={(e) => {
373396
setInputNewRow(e.target.value);
374397
}}
398+
onKeyDown={handleKeyDown}
375399
placeholder="filename of new row"
376400
/>
377401
</div>
378-
<div
379-
className={`${c("td")}`}
380-
onClick={() => {
381-
dataDispatch({
382-
type: ActionTypes.ADD_ROW,
383-
filename: inputNewRow,
384-
});
385-
setInputNewRow("");
386-
newRowRef.current.value = "";
387-
}}
388-
>
402+
<div className={`${c("td")}`} onClick={handleAddNewRow}>
389403
<span className="svg-icon svg-gray" style={{ marginRight: 4 }}>
390404
<PlusIcon />
391405
</span>

src/helpers/Constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export const MetadataDatabaseColumns = Object.freeze({
4242
{
4343
key: MetadataColumns.FILE,
4444
input: DataTypes.MARKDOWN,
45-
Header: MetadataColumns.FILE,
4645
label: MetadataLabels.FILE,
4746
accessor: MetadataColumns.FILE,
4847
isMetadata: true,
@@ -52,7 +51,6 @@ export const MetadataDatabaseColumns = Object.freeze({
5251
},
5352
ADD_COLUMN: {
5453
key: MetadataColumns.ADD_COLUMN,
55-
Header: MetadataColumns.ADD_COLUMN,
5654
input: DataTypes.NEW_COLUMN,
5755
disableResizing: true,
5856
label: MetadataLabels.ADD_COLUMN,

src/helpers/StylesHelper.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,17 @@ export function shortId() {
2626
*/
2727
export function dbTrim(str: string) {
2828
return str.trim().replaceAll("\n", "").replaceAll("\t", "").replaceAll(" ", "_");
29+
}
30+
31+
/**
32+
* Calculate summatory of column widths
33+
* @param state
34+
* @returns
35+
*/
36+
export function getTotalWidth(state: ColumnWidthState): number {
37+
let totalWidth: number = 0;
38+
Object.keys(state.widthRecord).forEach((key) => {
39+
totalWidth += state.widthRecord[key];
40+
});
41+
return totalWidth;
2942
}

styles.css

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
transform: rotate(180deg);
5555
}
5656

57-
.form-input {
57+
.database-plugin__form-input {
5858
padding: 0.375rem;
5959
background-color: var(--background-primary);
6060
border: none;
@@ -63,11 +63,20 @@
6363
color: var(--text-normal);
6464
}
6565

66-
.form-input:focus {
66+
.database-plugin__form-input:focus {
6767
outline: none;
6868
box-shadow: 0 0 1px 2px var(--background-modifier-border);
6969
}
7070

71+
.database-plugin__invalid-form{
72+
padding: 0.375rem;
73+
border: none;
74+
border-radius: 4px;
75+
font-size: 0.875rem;
76+
color: var(--text-error);
77+
background-color: var(--background-modifier-error);
78+
}
79+
7180
.is-fullwidth {
7281
width: 100%;
7382
}

0 commit comments

Comments
 (0)