Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/sorting: change the way the attributes of type "select" are sorted (by the definition's own order of options)

Adorian Doranb3dfd4e26f03
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 380ceb77eb..054803f589 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -3567,6 +3567,17 @@ describe("a column that sorts its cards", () => {
expect(cardTitlesIn(board, 0)).toEqual([ "Beta", "Alpha", "Delta" ]);
});
+ it("sorts a select by the order its own definition offers the options in", async () => {
+ const { board } = await renderSortedBoard({ orderBy: "attr:priority" }, {
+ "#label:priority(inheritable)":
+ "promoted,alias=Priority,single,select,options=Low;Medium;High;Urgent",
+ values: { Delta: "Urgent", Beta: "Low", Alpha: "High" }
+ });
+
+ // Alphabetically this would read High, Low, Urgent.
+ expect(cardTitlesIn(board, 0)).toEqual([ "Beta", "Alpha", "Delta" ]);
+ });
+
it("reorders the column as soon as the sort is picked from its menu", async () => {
const { board } = await renderSortedBoard({});
expect(cardTitlesIn(board, 0)).toEqual([ "Delta", "Beta", "Alpha" ]);
diff --git a/apps/client/src/widgets/collections/promoted_attributes.ts b/apps/client/src/widgets/collections/promoted_attributes.ts
index b5960a9699..db5159430e 100644
--- a/apps/client/src/widgets/collections/promoted_attributes.ts
+++ b/apps/client/src/widgets/collections/promoted_attributes.ts
@@ -26,6 +26,8 @@ export interface PromotedAttribute {
definitionValue: string;
/** What the field holds: `text`, `date`, `boolean` and the rest. Absent for a relation. */
labelType?: string;
+ /** The values a `select` field offers, in the order it offers them. */
+ selectOptions?: string[];
/** Whether the note that defines it is the collection itself rather than an ancestor. */
isOwned: boolean;
/**
@@ -71,6 +73,7 @@ export function resolvePromotedAttributes(
type,
title: parsed?.promotedAlias || name,
labelType: parsed?.labelType,
+ selectOptions: parsed?.selectOptions,
hidden: false,
definitionValue: definition.value,
isOwned: definition.noteId === note?.noteId,
diff --git a/apps/client/src/widgets/collections/sorting.spec.ts b/apps/client/src/widgets/collections/sorting.spec.ts
index 5559cdfdaf..ddadf8d4c3 100644
--- a/apps/client/src/widgets/collections/sorting.spec.ts
+++ b/apps/client/src/widgets/collections/sorting.spec.ts
@@ -104,6 +104,46 @@ describe("sortItems", () => {
.toEqual([ "False", "Bare", "True" ]);
});
+ it("sorts a select by the order its options are declared in, not alphabetically", () => {
+ const options = [ "Low", "Medium", "High", "Urgent" ];
+ const items = build([
+ { title: "A", "#field": "Urgent" },
+ { title: "B", "#field": "Low" },
+ { title: "C", "#field": "High" },
+ { title: "D", "#field": "Medium" }
+ ]);
+
+ expect(titles(sort(items, "attr:field", false, "select", options)))
+ .toEqual([ "B", "D", "C", "A" ]);
+ expect(titles(sort(items, "attr:field", true, "select", options)))
+ .toEqual([ "A", "C", "D", "B" ]);
+ });
+
+ it("sorts a value the select no longer offers after every option it does", () => {
+ const items = build([
+ { title: "Dropped", "#field": "Blocked" },
+ { title: "None" },
+ { title: "Urgent", "#field": "Urgent" },
+ { title: "Low", "#field": "Low" }
+ ]);
+
+ // Ahead of the card carrying no value at all, which stays last in both directions.
+ expect(titles(sort(items, "attr:field", false, "select", [ "Low", "Urgent" ])))
+ .toEqual([ "Low", "Urgent", "Dropped", "None" ]);
+ expect(titles(sort(items, "attr:field", true, "select", [ "Low", "Urgent" ])))
+ .toEqual([ "Dropped", "Urgent", "Low", "None" ]);
+ });
+
+ it("compares a select offering no options as text", () => {
+ const items = build([
+ { title: "A", "#field": "beta" },
+ { title: "B", "#field": "alpha" }
+ ]);
+
+ expect(titles(sort(items, "attr:field", false, "select"))).toEqual([ "B", "A" ]);
+ expect(titles(sort(items, "attr:field", false, "select", []))).toEqual([ "B", "A" ]);
+ });
+
it("sorts colours by hue and leaves grey with the items that have no value", () => {
const items = build([
{ title: "Blue", "#field": "#0000ff" },
@@ -198,7 +238,10 @@ function build(drafts: ItemDraft[]) {
return drafts.map(({ createdAt, ...noteDef }) => ({ note: buildNote(noteDef), createdAt }));
}
-function sort(items: Item[], key: SortKey, isDescending = false, fieldType?: FieldType) {
+function sort(
+ items: Item[], key: SortKey, isDescending = false, fieldType?: FieldType,
+ selectOptions?: string[]
+) {
const dates = new Map(items.map(({ note, createdAt }) => [ note.noteId, createdAt ]));
const definitions = new Map<string, PromotedAttribute>();
@@ -212,6 +255,7 @@ function sort(items: Item[], key: SortKey, isDescending = false, fieldType?: Fie
hidden: false,
definitionValue: "",
labelType: isRelation ? undefined : fieldType,
+ selectOptions,
isOwned: true,
isInheritable: true
});
diff --git a/apps/client/src/widgets/collections/sorting.ts b/apps/client/src/widgets/collections/sorting.ts
index 132dddfcdd..d51b8c5b61 100644
--- a/apps/client/src/widgets/collections/sorting.ts
+++ b/apps/client/src/widgets/collections/sorting.ts
@@ -119,11 +119,11 @@ function sortValueOf(note: FNote, key: SortKey, context: SortContext): SortValue
return label.value === "false" ? 0 : 1;
}
- return label.value ? labelValueOf(label.value, definition?.labelType) : undefined;
+ return label.value ? labelValueOf(label.value, definition) : undefined;
}
-function labelValueOf(value: string, labelType: string | undefined): SortValue {
- switch (labelType) {
+function labelValueOf(value: string, definition: PromotedAttribute | undefined): SortValue {
+ switch (definition?.labelType) {
case "number":
return toNumber(parseFloat(value));
case "date":
@@ -133,11 +133,29 @@ function labelValueOf(value: string, labelType: string | undefined): SortValue {
return secondsOfDay(value);
case "color":
return hueOf(value);
+ case "select":
+ return optionIndex(value, definition.selectOptions);
default:
return value;
}
}
+/**
+ * Where a value stands among the options a select offers, so the field is read in the order it was
+ * defined in rather than alphabetically: `options=Low;Medium;High;Urgent` sorts by rank.
+ */
+function optionIndex(value: string, options: string[] | undefined): SortValue {
+ if (!options?.length) {
+ // A select that offers nothing has no order of its own, so the values compare as text.
+ return value;
+ }
+
+ const at = options.indexOf(value);
+ // A value the definition no longer offers sorts after every option it does, rather than with
+ // the cards that carry no value at all.
+ return at >= 0 ? at : options.length;
+}
+
function toNumber(value: number) {
return Number.isNaN(value) ? undefined : value;
}