Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: make the "Board's default" the default sorting option for new columns

Adorian Doranda5bb8859491
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index a0de5446bd..8e85922ea7 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -1782,14 +1782,26 @@ describe("the promoted attributes a card shows", () => {
});
describe("how a column orders its cards", () => {
- it("reads a column nobody has sorted as the order the user arranged", () => {
+ /** Storing nothing is what takes the board's order, so a fresh column reads as taking it. */
+ it("reads a column nobody has sorted as taking the board's order", () => {
const { api } = createApi({ columns: [ { value: "To Do" } ] }, [ "To Do" ]);
- expect(api.getColumnSort("To Do")).toEqual({ orderBy: undefined, isDescending: false });
- expect(api.getColumnSort("Unwritten")).toEqual({ orderBy: undefined, isDescending: false });
+ expect(api.getColumnSort("To Do")).toEqual({ orderBy: "default", isDescending: false });
+ expect(api.getColumnSort("Unwritten")).toEqual({ orderBy: "default", isDescending: false });
});
- it("stores what a column sorts by, and clears it back to the manual order", async () => {
+ it("reads a column stored as manual as the order the user arranged", () => {
+ const { api } = createApi(
+ { columns: [ { value: "To Do", orderBy: "manual" } ] },
+ [ "To Do" ],
+ buildNote({ title: "Board", "#sortColumns": "title" }));
+
+ expect(api.getColumnSort("To Do").orderBy).toBeUndefined();
+ expect(api.getEffectiveColumnSort("To Do").orderBy).toBeUndefined();
+ expect(api.isColumnSorted("To Do")).toBe(false);
+ });
+
+ it("stores what a column sorts by, and writes the manual order out", async () => {
const { api, saved } = createApi(
{ columns: [ { value: "To Do", icon: "bx bx-list-ul" }, { value: "Done" } ] },
[ "To Do", "Done" ]);
@@ -1802,10 +1814,13 @@ describe("how a column orders its cards", () => {
expect(api.getColumnSort("To Do"))
.toEqual({ orderBy: "attr:dueDate", isDescending: false });
- // Strict, so the assertion catches the key being stored as undefined rather than dropped.
+ // Written rather than dropped: a column storing nothing takes the board's order.
await api.setColumnSort("To Do", undefined);
- expect(saved.at(-1)?.columns)
- .toStrictEqual([ { value: "To Do", icon: "bx bx-list-ul" }, { value: "Done" } ]);
+ expect(saved.at(-1)?.columns).toStrictEqual([
+ { value: "To Do", icon: "bx bx-list-ul", orderBy: "manual" },
+ { value: "Done" }
+ ]);
+ expect(api.getColumnSort("To Do").orderBy).toBeUndefined();
});
it("stores the direction and clears it rather than storing it false", async () => {
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index e40468149a..28b6bee7d5 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -19,7 +19,7 @@ import {
visiblePromotedAttributeNames
} from "../promoted_attributes";
import {
- DEFAULT_SORT, parseSortKey, parseStoredSortKey, type SortKey, type StoredSortKey
+ DEFAULT_SORT, MANUAL_SORT, parseSortKey, parseStoredSortKey, type SortKey, type StoredSortKey
} from "../sorting";
import { BoardColumnData, BoardViewData } from ".";
import { currentCardTemplate, DEFAULT_CARD_TEMPLATES } from "./card_templates";
@@ -623,7 +623,8 @@ export default class BoardApi {
}
/**
- * What a column is set to order its cards by, which can be the board's own order.
+ * What a column is set to order its cards by, which is the board's own order until the reader
+ * picks something for the column.
*
* @returns the stored key, absent for the manual order, and the direction stored with it. See
* {@link getEffectiveColumnSort} for what the column is actually drawn in.
@@ -650,9 +651,12 @@ export default class BoardApi {
return { orderBy: board.orderBy, isDescending: board.isDescending };
}
- /** Sets what a column sorts by. Pass `undefined` for the manual order. */
+ /**
+ * Sets what a column sorts by. Pass `undefined` for the manual order, which is written out:
+ * a column storing nothing takes the board's order.
+ */
async setColumnSort(column: string, orderBy: StoredSortKey | undefined) {
- await this.updateColumn(column, { orderBy });
+ await this.updateColumn(column, { orderBy: orderBy ?? MANUAL_SORT });
}
/** Sets whether a column's order runs backwards. */
diff --git a/apps/client/src/widgets/collections/board/data.ts b/apps/client/src/widgets/collections/board/data.ts
index 517295661c..bb9bb87d77 100644
--- a/apps/client/src/widgets/collections/board/data.ts
+++ b/apps/client/src/widgets/collections/board/data.ts
@@ -3,7 +3,8 @@ import FNote from "../../../entities/fnote";
import type LoadResults from "../../../services/load_results";
import type { PromotedAttribute } from "../promoted_attributes";
import {
- DEFAULT_SORT, parseSortKey, sortedAttributeName, sortItems, type SortContext, type SortKey
+ DEFAULT_SORT, parseStoredSortKey, sortedAttributeName, sortItems, type SortContext,
+ type SortKey
} from "../sorting";
import { INBOX_COLUMN, resolveBoardColumns } from "./columns";
import { BoardColumnData, BoardViewData } from "./index";
@@ -84,25 +85,27 @@ export interface SortWatch {
/**
* What each column sorts by, leaving out every column that keeps the manual order.
*
- * @param defaultSort the order the board holds, which a column stored as `default` takes. Such a
- * column keeps the manual order while the board holds none.
+ * @param defaultSort the order the board holds, which a column takes until it is given one of its
+ * own. Such a column keeps the manual order while the board holds none.
+ * @param drawnColumns every column the board shows, since one the config says nothing about takes
+ * the board's order like the rest.
*/
export function resolveColumnSorts(
- columns: BoardColumnData[] | undefined, defaultSort?: ColumnSort
+ columns: BoardColumnData[] | undefined, defaultSort?: ColumnSort, drawnColumns?: string[]
) {
const sorts = new Map<string, ColumnSort>();
+ const stored = new Map((columns ?? []).map(column => [ column.value, column ]));
- for (const { value, orderBy, descendingOrder } of columns ?? []) {
- if (orderBy === DEFAULT_SORT) {
+ for (const value of new Set([ ...drawnColumns ?? [], ...stored.keys() ])) {
+ const entry = stored.get(value);
+ const key = parseStoredSortKey(entry?.orderBy);
+
+ if (key === DEFAULT_SORT) {
if (defaultSort) {
sorts.set(value, defaultSort);
}
- continue;
- }
-
- const key = parseSortKey(orderBy);
- if (key) {
- sorts.set(value, { orderBy: key, isDescending: !!descendingOrder });
+ } else if (key) {
+ sorts.set(value, { orderBy: key, isDescending: !!entry?.descendingOrder });
}
}
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index bd029b790e..8747d5e1df 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -3589,7 +3589,8 @@ describe("a column that sorts its cards", () => {
expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
await pickSort(board, "bx bx-move-vertical");
- expect(saved.at(-1)?.columns?.[0]).toStrictEqual({ value: "To Do", descendingOrder: true });
+ expect(saved.at(-1)?.columns?.[0])
+ .toStrictEqual({ value: "To Do", orderBy: "manual", descendingOrder: true });
expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
});
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index bf933d1380..7ba6e73951 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -404,7 +404,8 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
return orderBy ? { orderBy, isDescending: isDefaultDescending } : undefined;
}, [ storedDefaultSort, isDefaultDescending ]);
const columnSorts = useMemo(
- () => resolveColumnSorts(viewConfig?.columns, defaultSort), [ viewConfig, defaultSort ]);
+ () => resolveColumnSorts(viewConfig?.columns, defaultSort, usableColumns),
+ [ viewConfig, defaultSort, usableColumns ]);
const sortContext = useMemo<SortContext>(() => ({
definitions: new Map(promotedAttributes.map(attribute => [ attribute.name, attribute ])),
creationDate: getCreationDate,
diff --git a/apps/client/src/widgets/collections/sorting.ts b/apps/client/src/widgets/collections/sorting.ts
index 105858d066..2314c2a136 100644
--- a/apps/client/src/widgets/collections/sorting.ts
+++ b/apps/client/src/widgets/collections/sorting.ts
@@ -9,6 +9,14 @@ export type SortKey = "title" | "creationDate" | `attr:${string}`;
/** What an item stores to take the collection's own order rather than one of its own. */
export const DEFAULT_SORT = "default";
+/**
+ * What an item stores to keep the order the reader arranged.
+ *
+ * Written out because storing nothing means taking the collection's own order: a column is drawn in
+ * that until the reader picks something for it, this included.
+ */
+export const MANUAL_SORT = "manual";
+
/** What one column of a collection stores: an order of its own, or the collection's. */
export type StoredSortKey = SortKey | typeof DEFAULT_SORT;
@@ -44,11 +52,26 @@ export function parseSortKey(orderBy: string | null | undefined): SortKey | unde
return undefined;
}
-/** Reads a stored `orderBy` setting, the collection's own order included. */
+/**
+ * Reads what one item of a collection stores.
+ *
+ * @returns the key it sorts by, {@link DEFAULT_SORT} where it takes the collection's own order —
+ * which storing nothing does — and undefined for the manual order.
+ */
export function parseStoredSortKey(
orderBy: string | null | undefined
): StoredSortKey | undefined {
- return orderBy === DEFAULT_SORT ? DEFAULT_SORT : parseSortKey(orderBy);
+ if (orderBy === MANUAL_SORT) {
+ return undefined;
+ }
+
+ if (!orderBy || orderBy === DEFAULT_SORT) {
+ return DEFAULT_SORT;
+ }
+
+ // Anything else the collection cannot order by, such as a key written by a newer version,
+ // leaves the items as the reader arranged them.
+ return parseSortKey(orderBy);
}
/** The attribute a key sorts by, or undefined when the key names something else. */