Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix an issue

Adorian Doranebf0e142f45f
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index 5c4feae5d9..789523ec0a 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -432,7 +432,8 @@ describe("BoardApi column mutations", () => {
api.reorderColumn(0, 2);
- expect(saved.at(-1)?.columns?.map(col => col.value)).toEqual([ "Done", "To Do", "In Progress" ]);
+ // "In Progress" followed "Done" before the move and still does.
+ expect(saved.at(-1)?.columns?.map(col => col.value)).toEqual([ "Done", "In Progress", "To Do" ]);
});
});
@@ -1062,11 +1063,10 @@ describe("renaming a column to nothing", () => {
describe("reordering columns the board is not showing all of", () => {
/**
- * A column the board keeps but does not show, an inbox switched off among them, is in the
- * config and not in the list the reorder counts in. It keeps the place it holds rather than
- * being herded to the end.
+ * A column the config keeps but the board does not show, such as a disabled inbox, is missing
+ * from the list the reorder counts in. It is kept rather than dropped or moved to the end.
*/
- it("leaves a stored column the render list does not carry where it stands", () => {
+ it("keeps a stored column the render list does not carry", () => {
const { api, saved } = createApi(
{ columns: [
{ value: "", icon: "bx bx-inbox" }, { value: "To Do" }, { value: "Done" }
@@ -1083,6 +1083,46 @@ describe("reordering columns the board is not showing all of", () => {
{ value: "To Do" }
]);
});
+
+ /**
+ * The hidden column follows the same column it followed before. Reinserting it at the index it
+ * held in the config would place it between whichever columns the move left there.
+ */
+ it("keeps a hidden column beside the column it followed", () => {
+ const { api, saved } = createApi(
+ { columns: [
+ { value: "To Do" }, { value: "" }, { value: "Done" }
+ ] },
+ [ "To Do", "Done" ]
+ );
+
+ // "Done" before "To Do", counted among the two the board shows.
+ api.reorderColumn(1, 0);
+
+ expect(saved.at(-1)?.columns).toEqual([
+ { value: "Done" },
+ { value: "To Do" },
+ { value: "" }
+ ]);
+ });
+
+ it("keeps a run of hidden columns in order behind the same column", () => {
+ const { api, saved } = createApi(
+ { columns: [
+ { value: "To Do" }, { value: "" }, { value: "Archived" }, { value: "Done" }
+ ] },
+ [ "To Do", "Done" ]
+ );
+
+ api.reorderColumn(1, 0);
+
+ expect(saved.at(-1)?.columns).toEqual([
+ { value: "Done" },
+ { value: "To Do" },
+ { value: "" },
+ { value: "Archived" }
+ ]);
+ });
});
describe("the icon the inbox column wears", () => {
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 7cf58bc8fb..c6f216da27 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -457,8 +457,8 @@ export default class BoardApi {
newColumns.splice(adjustedToIndex, 0, movedColumn);
// `columns` is render state: it omits entries the view has yet to catch up with, and any
- // the board is hiding, such as a disabled inbox. Each keeps its index in the config
- // instead of being appended at the end or dropped.
+ // the board is hiding, such as a disabled inbox. Those are neither dropped nor appended at
+ // the end.
const stored = this.viewConfig?.columns ?? [];
const storedColumns = new Map(stored.map(col => [ col.value, col ]));
// Reordering only moves entries, so each keeps its stored icon instead of being rebuilt
@@ -466,10 +466,21 @@ export default class BoardApi {
const reordered: BoardColumnData[] =
newColumns.map(value => storedColumns.get(value) ?? { value });
- for (const [ index, column ] of stored.entries()) {
- if (!newColumns.includes(column.value)) {
- reordered.splice(index, 0, column);
+ // A hidden column goes back after the column it followed in the config, not at the index it
+ // held there: the move has shifted the visible columns, so that index points elsewhere now.
+ // Each one becomes the anchor for the next, which keeps a run of them in order.
+ let anchor: string | undefined;
+ for (const column of stored) {
+ if (newColumns.includes(column.value)) {
+ anchor = column.value;
+ continue;
}
+
+ const at = anchor === undefined
+ ? 0
+ : reordered.findIndex(entry => entry.value === anchor) + 1;
+ reordered.splice(at, 0, column);
+ anchor = column.value;
}
this.storeColumns(reordered);