Skip to content

Build your personal knowledge base with Trilium Notes

client: fix some issues

Adorian Doraneb3cc77fca4a
diff --git a/apps/client/src/widgets/collections/attribute_menu.spec.ts b/apps/client/src/widgets/collections/attribute_menu.spec.ts
index f4b215a19c..25643a7806 100644
--- a/apps/client/src/widgets/collections/attribute_menu.spec.ts
+++ b/apps/client/src/widgets/collections/attribute_menu.spec.ts
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import type FNote from "../../entities/fnote";
import type { MenuCommandItem, MenuItem } from "../../menus/context_menu";
+import froca from "../../services/froca";
import { buildNote } from "../../test/easy-froca";
import { buildAttributeMenuItems } from "./attribute_menu";
import type { PromotedAttribute } from "./promoted_attributes";
@@ -142,6 +143,22 @@ describe("buildAttributeMenuItems", () => {
pick(items[0]);
expect(writes.setLabelValues).toHaveBeenCalledWith(note, "state", []);
});
+
+ /**
+ * Only the labels a note owns are removed, so a note with nothing of its own would keep the
+ * value it inherits. An empty label of its own overrides it.
+ */
+ it("overrides an inherited value rather than removing nothing", () => {
+ const parent = buildNote({
+ title: "Parent",
+ "#state(inheritable)": "Doing",
+ children: [ { title: "Card" } ]
+ });
+ const note = froca.getNoteFromCache(parent.getChildNoteIds()[0]);
+
+ pick(options(note)[0]);
+ expect(writes.setLabelValues).toHaveBeenCalledWith(note, "state", [ "" ]);
+ });
});
it("escapes what a crafted name would plant in the menu", () => {
diff --git a/apps/client/src/widgets/collections/attribute_menu.ts b/apps/client/src/widgets/collections/attribute_menu.ts
index e0ea8a3d11..38bbdea0ad 100644
--- a/apps/client/src/widgets/collections/attribute_menu.ts
+++ b/apps/client/src/widgets/collections/attribute_menu.ts
@@ -94,11 +94,11 @@ function buildSelectItem<T>(
// across every option.
items: [
{
- // Boxed like the options: the menu lays a row out as a flex line, so a bare title
- // keeps the space that separates it from the icon and reads as indented.
+ // Boxed like the options: a menu row is a flex line, so a bare title keeps the
+ // space that separates it from the icon and renders as indented.
title: menuName(t("attribute_menu.not-set")),
trailingIcon: current ? undefined : CHECK,
- handler: () => { void setLabelValues(note, attribute.name, []); }
+ handler: () => { void clearValue(note, attribute.name); }
},
...options.map<MenuItem<T>>((option) => ({
title: menuName(option),
@@ -109,6 +109,20 @@ function buildSelectItem<T>(
};
}
+/**
+ * Takes the value off the note.
+ *
+ * `setLabelValues` removes only the labels the note owns, so an inherited value would still apply.
+ * The note is given an empty label of its own instead, which is what an unset promoted field holds
+ * and what `renderLabelValue` draws as nothing.
+ */
+function clearValue(note: FNote, name: string) {
+ const isInherited = note.getAttributes("label", name)
+ .some((attribute) => attribute.noteId !== note.noteId);
+
+ return setLabelValues(note, name, isInherited ? [ "" ] : []);
+}
+
/** What every entry carries: the attribute's display name, under the icon of its type. */
function attributeEntry<T>(attribute: PromotedAttribute): MenuCommandItem<T> {
return {
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 3249c0d659..83b0294d12 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -620,11 +620,11 @@ export default class BoardApi {
}
/**
- * 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.
+ * Reads a column's stored `orderBy` and `descendingOrder`.
*
- * @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.
+ * @returns {@link DEFAULT_SORT} when the column stores nothing, undefined for the manual
+ * order, and otherwise the stored key. Use {@link getEffectiveColumnSort} to resolve
+ * {@link DEFAULT_SORT} against the board's own order.
*/
getColumnSort(column: string) {
const stored = this.viewConfig?.columns?.find(col => col.value === column);
@@ -635,8 +635,9 @@ export default class BoardApi {
}
/**
- * The order a column is drawn in: its own, or the board's where it takes that. A column taking
- * an order the board does not hold keeps the manual one.
+ * Resolves {@link getColumnSort} against {@link getDefaultSort}: a column storing
+ * {@link DEFAULT_SORT} sorts by the board's key and direction, and by nothing when the board
+ * holds no key.
*/
getEffectiveColumnSort(column: string) {
const stored = this.getColumnSort(column);
@@ -649,8 +650,8 @@ export default class BoardApi {
}
/**
- * 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.
+ * Sets what a column sorts by. `undefined` stores {@link MANUAL_SORT}, since a column storing
+ * nothing sorts by the board's order instead.
*/
async setColumnSort(column: string, orderBy: StoredSortKey | undefined) {
await this.updateColumn(column, { orderBy: orderBy ?? MANUAL_SORT });
@@ -662,8 +663,8 @@ export default class BoardApi {
}
/**
- * The order the board holds for its columns, kept on the board note rather than in the view
- * config: it is a preference to apply, not something a column is drawn by.
+ * Reads `#sortColumns` and `#sortColumnsDescending` off the board note, which is where the
+ * order the columns default to is stored rather than in `board.json`.
*/
getDefaultSort() {
return {
@@ -686,11 +687,13 @@ export default class BoardApi {
}
/**
- * Puts every column back to the board's order, dropping the one each column picked for itself.
+ * Removes `orderBy` and `descendingOrder` from every stored column, so all of them sort by the
+ * board's order again.
*
- * Only the columns the config holds an entry for are written: one with no entry already takes
- * the board's order. Written in one go, since `updateColumn` rewrites the whole config and a
- * run of them would each start from it as it stood before the first.
+ * Only columns `board.json` holds an entry for are written; a column with no entry already
+ * sorts by the board's order. Written through {@link updateColumns} in one go, since
+ * `updateColumn` rewrites the whole config and a run of them would each start from the config
+ * as it stood before the first.
*/
async resetColumnSortsToDefault() {
const stored = this.viewConfig?.columns ?? [];
diff --git a/apps/client/src/widgets/collections/board/properties.tsx b/apps/client/src/widgets/collections/board/properties.tsx
index 7576718c39..e14de735d8 100644
--- a/apps/client/src/widgets/collections/board/properties.tsx
+++ b/apps/client/src/widgets/collections/board/properties.tsx
@@ -67,7 +67,6 @@ export default function BoardProperties({ api, note, shown, onClose }: {
function General({ api, note }: { api: BoardApi, note: FNote }) {
const [ inboxShown ] = useNoteLabelBoolean(note, "enableInboxColumn");
const [ archivedShown ] = useNoteLabelBoolean(note, "includeArchived");
- // Read off the board's own labels, so the dropdown follows what is picked in it.
const defaultSort = useBoardSort(note);
return (
@@ -104,8 +103,8 @@ function General({ api, note }: { api: BoardApi, note: FNote }) {
attributes={api.getPromotedAttributes()}
noneTitle={t("board_view.sort-manually")}
hideDefault
- // The board's own order is a key of its own: `hideDefault` leaves it no
- // entry for taking one from above, which `parseSortKey` also rules out.
+ // `hideDefault` leaves the menu no DEFAULT_SORT entry, and `parseSortKey`
+ // rejects that key as well: `setDefaultSort` takes a plain sort key.
onSelect={(orderBy) => api.setDefaultSort(parseSortKey(orderBy))}
onDirectionChange={(descending) => api.setDefaultSortDirection(descending)}
/>
diff --git a/apps/client/src/widgets/collections/board/sort.ts b/apps/client/src/widgets/collections/board/sort.ts
index 02fa5cbecc..c87d87b84c 100644
--- a/apps/client/src/widgets/collections/board/sort.ts
+++ b/apps/client/src/widgets/collections/board/sort.ts
@@ -10,10 +10,10 @@ export const SORT_LABEL = "sortColumns";
export const SORT_DESCENDING_LABEL = "sortColumnsDescending";
/**
- * The order the board holds, absent while it arranges its cards by hand.
+ * Reads the order the board stores in its labels, absent when it stores no key.
*
- * Kept in step with the labels rather than read per column: every column taking the board's order
- * asks for it, and the labels change only when the reader picks another one.
+ * Memoised on the label values, so the columns that sort by it read one object rather than parsing
+ * the labels each.
*/
export function useBoardSort(note: FNote): ColumnSort | undefined {
const [ stored ] = useNoteLabel(note, SORT_LABEL);
diff --git a/apps/client/src/widgets/collections/sort_menu.ts b/apps/client/src/widgets/collections/sort_menu.ts
index c235138c02..6bdc92735b 100644
--- a/apps/client/src/widgets/collections/sort_menu.ts
+++ b/apps/client/src/widgets/collections/sort_menu.ts
@@ -16,14 +16,11 @@ export interface SortMenuOptions {
attributes: PromotedAttribute[];
/** What the entry for no sorting is called. "None" unless the caller names it. */
noneTitle?: string;
- /**
- * What the entry taking the collection's own order is called. "Default" unless the caller names
- * it.
- */
+ /** What the {@link DEFAULT_SORT} entry is called. "Default" unless the caller names it. */
defaultTitle?: string;
/**
- * Leaves that entry out, for the menu that sets the collection's own order: there is nothing
- * above it for it to take one from.
+ * Drops the {@link DEFAULT_SORT} entry. Set it on the menu that edits the collection's own
+ * order, which has no order above it to fall back to.
*/
hideDefault?: boolean;
/** Called with the key picked, or `undefined` for the order the user arranges by hand. */
@@ -47,8 +44,9 @@ export interface SortEntry {
}
/**
- * What a collection offers to order its items by, and the two directions, for a menu or a dropdown
- * to draw. Both surfaces read the same entries, so an order offered in one is offered in the other.
+ * Builds the orders a collection offers and the two directions, for {@link buildSortMenuItems} and
+ * `SortDropdown` to draw. Both read these entries, so an order offered in one is offered in the
+ * other.
*/
export function sortEntries({
orderBy, isDescending, attributes, noneTitle, defaultTitle, hideDefault, onSelect,
@@ -66,8 +64,7 @@ export function sortEntries({
pick: () => onSelect(key)
});
- // Off while the collection keeps the manual order, and while it takes the order above it,
- // which brings a direction of its own.
+ // Off for the manual order, and for DEFAULT_SORT, which brings the direction with it.
const canPickDirection = !!orderBy && orderBy !== DEFAULT_SORT;
const direction = (descending: boolean, title: string, icon: string): SortEntry => ({
key: descending ? "descending" : "ascending",