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

Commit bc07000

Browse files
authored
Merge pull request #562 from trey-wallis/dev
6.18.6
2 parents c38af18 + 5c375bc commit bc07000

File tree

7 files changed

+93
-16
lines changed

7 files changed

+93
-16
lines changed

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"authorUrl": "https://github.com/trey-wallis",
88
"isDesktopOnly": false,
99
"fundingUrl": "https://www.buymeacoffee.com/treywallis",
10-
"version": "6.18.5"
10+
"version": "6.18.6"
1111
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-notion-like-tables",
3-
"version": "6.18.5",
3+
"version": "6.18.6",
44
"description": "Notion-like tables for Obsidian.md",
55
"main": "main.js",
66
"scripts": {

src/data/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const CURRENT_PLUGIN_VERSION = "6.18.5";
1+
export const CURRENT_PLUGIN_VERSION = "6.18.6";
22

33
export const DEFAULT_TABLE_NAME = "Untitled";
44

src/data/serialize-table-state.ts

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
BodyCell,
55
CellType,
66
Column,
7+
SortDir,
78
TableState,
89
Tag,
910
} from "../shared/types";
@@ -337,6 +338,30 @@ export const deserializeTableState = (data: string): TableState => {
337338
});
338339
}
339340

341+
if (isVersionLessThan(pluginVersion, "6.18.6")) {
342+
const tableState = currentState as TableState6160;
343+
const { columns, bodyRows } = tableState.model;
344+
345+
//Fix: resolve empty rows being inserted but appearing higher up in the table
346+
//This was due to the index being set to the row's position in the array, which
347+
//was sometimes less than the highest index value. This is because the index wasn't being
348+
//decreased.
349+
//This is a reset to force the index to be set to the correct value on all tables.
350+
columns.forEach((column: unknown) => {
351+
const typedColumn = column as Record<string, unknown>;
352+
typedColumn.sortDir = SortDir.NONE;
353+
});
354+
355+
//Sort by index
356+
bodyRows.sort((a, b) => a.index - b.index);
357+
358+
//Set the index to the correct value
359+
bodyRows.forEach((row: unknown, i) => {
360+
const typedRow = row as Record<string, unknown>;
361+
typedRow.index = i;
362+
});
363+
}
364+
340365
const tableState = currentState as TableState;
341366
tableState.pluginVersion = CURRENT_PLUGIN_VERSION;
342367
return tableState;

src/react/table-app/tag-cell-edit/selectable-tag.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ export default function SelectableTag({
5959
`}
6060
className="NLT__focusable NLT__selectable"
6161
onClick={(e) => {
62-
//Only trigger onClick if the click is on the tag and not the menu button
63-
if (e.target === triggerRef.current) {
64-
//Stop propagation so the the menu doesn't remove the focus class
65-
e.stopPropagation();
66-
onClick(id);
67-
}
62+
const target = e.target as HTMLElement;
63+
if (target.classList.contains("NLT__menu-trigger")) return;
64+
65+
//Stop propagation so the the menu doesn't remove the focus class
66+
e.stopPropagation();
67+
onClick(id);
6868
}}
6969
>
7070
<Tag markdown={markdown} color={color} maxWidth="150px" />

src/shared/commands/row-delete-command.ts

+57-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export default class RowDeleteCommand extends TableStateCommand {
1616
cell: BodyCell;
1717
}[];
1818

19+
private previousBodyRows: {
20+
id: string;
21+
index: number;
22+
}[];
23+
1924
constructor(options: { id?: string; last?: boolean }) {
2025
super();
2126
const { id, last } = options;
@@ -30,33 +35,64 @@ export default class RowDeleteCommand extends TableStateCommand {
3035
super.onExecute();
3136

3237
const { bodyRows, bodyCells } = prevState.model;
38+
39+
//If there are no rows to delete, return the previous state
3340
if (bodyRows.length === 0) return prevState;
3441

3542
let id = this.rowId;
36-
if (this.last) {
37-
id = bodyRows[bodyRows.length - 1].id;
38-
}
43+
if (this.last) id = bodyRows[bodyRows.length - 1].id;
3944

45+
//Find the row to delete
4046
const rowToDelete = bodyRows.find((row) => row.id === id);
4147
if (!rowToDelete) throw new RowNotFoundError(id);
4248

49+
//Cache the row to delete
4350
this.deletedRow = {
4451
arrIndex: bodyRows.indexOf(rowToDelete),
4552
row: structuredClone(rowToDelete),
4653
};
4754

55+
//Get a new array of body rows, filtering the row we want to delete
56+
let newBodyRows = bodyRows.filter((row) => row.id !== id);
57+
58+
//Cache the indexes of the rows that were updated
59+
this.previousBodyRows = newBodyRows
60+
.filter((row) => row.index > this.deletedRow.row.index)
61+
.map((row) => {
62+
return {
63+
id: row.id,
64+
index: row.index,
65+
};
66+
});
67+
68+
//Decrement the index of rows that were in a higher position than the deleted row
69+
//This is to avoid having duplicate indexes
70+
newBodyRows = newBodyRows.map((row) => {
71+
if (row.index > this.deletedRow.row.index) {
72+
return {
73+
...row,
74+
index: row.index - 1,
75+
};
76+
}
77+
return row;
78+
});
79+
80+
//Cache the cells to delete
4881
const cellsToDelete = bodyCells.filter((cell) => cell.rowId === id);
4982
this.deletedCells = cellsToDelete.map((cell) => ({
5083
arrIndex: bodyCells.indexOf(cell),
5184
cell: structuredClone(cell),
5285
}));
5386

87+
//Get a new array of body cells, filtering the cells we want to delete
88+
const newBodyCells = bodyCells.filter((cell) => cell.rowId !== id);
89+
5490
return {
5591
...prevState,
5692
model: {
5793
...prevState.model,
58-
bodyRows: bodyRows.filter((row) => row.id !== id),
59-
bodyCells: bodyCells.filter((cell) => cell.rowId !== id),
94+
bodyRows: newBodyRows,
95+
bodyCells: newBodyCells,
6096
},
6197
};
6298
}
@@ -71,13 +107,28 @@ export default class RowDeleteCommand extends TableStateCommand {
71107

72108
const { bodyRows, bodyCells } = prevState.model;
73109

74-
const updatedBodyRows = [...bodyRows];
110+
//Restore the row indexes
111+
const updatedBodyRows = [...bodyRows].map((row) => {
112+
const wasUpdated = this.previousBodyRows.find(
113+
(r) => r.id === row.id
114+
);
115+
if (wasUpdated) {
116+
return {
117+
...row,
118+
index: wasUpdated.index,
119+
};
120+
}
121+
return row;
122+
});
123+
124+
//Restore the deleted row
75125
updatedBodyRows.splice(
76126
this.deletedRow.arrIndex,
77127
0,
78128
this.deletedRow.row
79129
);
80130

131+
//Restore the deleted row cells
81132
const updatedBodyCells = [...bodyCells];
82133
this.deletedCells.forEach((cell) => {
83134
updatedBodyCells.splice(cell.arrIndex, 0, cell.cell);

versions.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,6 @@
107107
"6.18.2": "0.15.0",
108108
"6.18.3": "0.15.0",
109109
"6.18.4": "0.15.0",
110-
"6.18.5": "0.15.0"
110+
"6.18.5": "0.15.0",
111+
"6.18.6": "0.15.0"
111112
}

0 commit comments

Comments
 (0)