Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix some issues

Adorian Doranae64eac716d7
diff --git a/apps/client/src/services/clipboard_ext.ts b/apps/client/src/services/clipboard_ext.ts
index ded51bfcf5..4a75202f87 100644
--- a/apps/client/src/services/clipboard_ext.ts
+++ b/apps/client/src/services/clipboard_ext.ts
@@ -83,13 +83,12 @@ export async function copyHtmlWithToast(html: string, plainText: string = html)
}
/**
- * Copies `href` as a reference link, so that pasting it into a note's content gives the title, icon
- * and colour of what it points at rather than the address itself. Reports the outcome as a toast.
+ * Copies `href` as an `<a class="reference-link">`, which a text note renders as the title, icon
+ * and colour of the note it points at. Reports the outcome as a toast.
*
- * The plain-text alternative is the address, which is what a target taking no HTML — a code note, a
- * Markdown note, a terminal — needs to build a link of its own. The editor keeps only the `href`
- * when it takes the rich flavour in and draws the rest from it, so the markup built here is what a
- * non-Trilium target gets.
+ * The plain-text flavour is `href` itself, for a Markdown or code note. `ReferenceLink` upcasts
+ * only the `href` and resolves the rest through `link.loadReferenceLinkTitle()`, so the markup
+ * written here reaches non-Trilium targets alone.
*/
export async function copyReferenceWithToast(href: string) {
const $link = $("<a>").addClass("reference-link").attr("href", href);
diff --git a/apps/client/src/services/link.ts b/apps/client/src/services/link.ts
index e282b20ab9..57951769ad 100644
--- a/apps/client/src/services/link.ts
+++ b/apps/client/src/services/link.ts
@@ -10,7 +10,7 @@ import { showError } from "./toast.js";
import treeService from "./tree.js";
import utils from "./utils.js";
-/** The icon a column reference falls back to when the link carries none of its own. */
+/** The icon a column reference uses when the link carries no `columnIcon`. */
const DEFAULT_COLUMN_REFERENCE_ICON = "bx bx-columns";
function getNotePathFromUrl(url: string) {
@@ -85,10 +85,9 @@ export interface ViewScope {
*/
column?: string;
/**
- * How a column reference names its column: the title, icon and colour the board showed when the
- * reference was made. Carried so that a link to a column can be drawn without reading the
- * board's configuration; {@link column} is what the board resolves, so a renamed column is
- * still found and only the label goes stale.
+ * The title, icon and colour a column reference renders as, copied from the board when the
+ * reference was made. Carried in the link because reading them needs the board's own
+ * configuration; {@link column} is what the board resolves, so a rename only dates the label.
*/
columnTitle?: string;
columnIcon?: string;
@@ -647,8 +646,8 @@ async function loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string | n
console.warn("Missing note ID.");
}
- // A card reference names the board in its path and the card in `card`, so the card is what the
- // link is drawn from while the board is what it opens.
+ // A card reference holds the board in its path and the card in `card`. The link renders the
+ // card; the path is what it opens.
const subjectId = viewScope?.card || noteId;
const note = subjectId ? await froca.getNote(subjectId, true) : null;
@@ -657,7 +656,7 @@ async function loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string | n
}
const title = await getReferenceLinkTitle(href);
- // A column reference reads "<board>: <column>", the column following in a suffix of its own.
+ // A column reference renders as "<board>: <column>", the column in a `<small>` below.
$el.text(viewScope?.columnTitle ? `${title}:` : title);
if (viewScope?.bookmark) {
@@ -691,7 +690,7 @@ async function getReferenceLinkTitle(href: string) {
return "[missing note]";
}
- // A card reference is named by the card, not by the board its path names.
+ // A card reference is titled by the card, not by the board in its path.
const note = await froca.getNote(viewScope?.card || noteId);
if (!note) {
return "[missing note]";
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index 5b85a03b47..2571f82b41 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -2654,6 +2654,32 @@ describe("references to a board's columns and cards", () => {
expect(api.getCardReference("card00000001")).toBe(`#${path}?card=card00000001`);
});
+ /**
+ * A relation board titles its columns from the notes they point at, and that title is written
+ * into the link permanently. The heading loads the note only after the board is interactive,
+ * so a reference copied before then would carry the note id as the title.
+ */
+ it("loads a relation column's note before naming it", async () => {
+ const target = buildNote({ title: "Alice", "#iconClass": "bx bx-user" });
+ const board = buildNote({ title: "Board" });
+ vi.spyOn(server, "put").mockResolvedValue({ id: "colAlice0001", stored: true } as never);
+ const { api } = createApi({}, [ target.noteId ], board, "~assignee");
+
+ // Not in the cache until it has been asked for, which is the state the board opens in.
+ let isCached = false;
+ vi.spyOn(froca, "getNoteFromCache").mockImplementation(
+ (noteId) => (isCached && noteId === target.noteId ? target : null) as never);
+ const getNote = vi.spyOn(froca, "getNote").mockImplementation(async () => {
+ isCached = true;
+ return target as never;
+ });
+
+ await expect(api.getColumnReference(target.noteId)).resolves.toBe(
+ `#${board.noteId}?column=colAlice0001&columnTitle=Alice`
+ + `&columnIcon=${encodeURIComponent("tn-icon bx bx-user")}`);
+ expect(getNote).toHaveBeenCalledWith(target.noteId, true);
+ });
+
/** A board drawn outside a pane, such as in a note preview, still has itself to name. */
it("falls back to the board's own id where the pane names no path", () => {
const board = buildNote({ title: "Board" });
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index b0469e8336..b77263dcd0 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -620,10 +620,9 @@ export default class BoardApi {
}
/**
- * What names a column wherever it stands for itself away from the board: its title, its icon
- * and the colour it is tinted with.
+ * The title, icon and colour naming a column outside the board itself.
*
- * A relation board keys its columns by note id, so each one is named from the note's title.
+ * A relation board keys its columns by note id, so the title comes from the note.
*/
getColumnLabel(column: string): ColumnReferenceLabel {
return {
@@ -864,6 +863,13 @@ export default class BoardApi {
* copying a reference writes to `board.json` where nothing has been stored for the column yet.
*/
async getColumnReference(column: string) {
+ // `getColumnLabel` reads the note from froca's cache, which the heading fills only after
+ // the board is interactive. Load it first: the label is written into the link for good, so
+ // a cache miss would title the column with its note id.
+ if (this.isRelationMode && column !== INBOX_COLUMN) {
+ await froca.getNote(column, true);
+ }
+
return columnReference(this.boardNotePath, await this.ensureColumnId(column),
this.getColumnLabel(column));
}
diff --git a/apps/client/src/widgets/collections/board/card_templates.ts b/apps/client/src/widgets/collections/board/card_templates.ts
index 0d1981c5a2..fc554bd3c4 100644
--- a/apps/client/src/widgets/collections/board/card_templates.ts
+++ b/apps/client/src/widgets/collections/board/card_templates.ts
@@ -38,10 +38,10 @@ export function currentCardTemplate(offered: NoteTypeOption[], stored: string |
}
/**
- * The icon a card made from a template carries, as an icon class the picker also writes.
+ * The icon a card made from `template` carries, in the form `IconPicker` writes.
*
- * `FNote.getIcon()` prefixes what it returns with `tn-icon`, which an `#iconClass` label never
- * carries, so a template note's icon is trimmed to what a pick would hand over.
+ * `FNote.getIcon()` prefixes its result with `tn-icon` and an `#iconClass` label does not, so a
+ * template note's icon needs that prefix removed to compare with a picked one.
*/
export function cardTemplateIcon(template: NoteTypeOption | undefined) {
return template?.icon.replace(/^tn-icon\s+/, "");
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index 4c327afb66..3172b2c7e4 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -919,15 +919,15 @@ function AddNewItem({
// What the editor opens with: empty to begin with, then whatever was typed into it and left
// unsaved, so that reaching for something else and coming back does not cost the title.
const [ initialTitle, setInitialTitle ] = useState("");
- // What a card made from the template now offered would be drawn with.
+ // The icon a card made from the current template would carry.
const templateIcon = cardTemplateIcon(cardTemplates.current) ?? DEFAULT_CARD_ICON;
// Kept between cards, unlike the title: a run of cards is often a run of the same kind of card.
const [ icon, setIcon ] = useState(templateIcon);
- /** The template icon the field is showing, against which a pick of the reader's is told. */
+ /** The template icon `icon` was last set from, telling a picked icon from a followed one. */
const followedIcon = useRef(templateIcon);
- // The icon follows the template while it is still the template's own. One the reader picked
- // stands, since the template changing under it says nothing about the icon they chose.
+ // Follow the template only while `icon` is still the previous template's. A picked icon is
+ // kept: a template change says nothing about it.
useEffect(() => {
if (templateIcon === followedIcon.current) {
return;
diff --git a/apps/client/src/widgets/collections/board/reference.ts b/apps/client/src/widgets/collections/board/reference.ts
index ea211bb2a7..ce81a61aa6 100644
--- a/apps/client/src/widgets/collections/board/reference.ts
+++ b/apps/client/src/widgets/collections/board/reference.ts
@@ -4,7 +4,7 @@
* A reference is a note path plus a `?column=` or `?card=` parameter. `link.ts` carries both in the
* pane's view scope, as it carries `?bookmark=`, and {@link useBoardReference} reads the parameter
* once and reveals what it names. A column reference also carries the column's title, icon and
- * colour, which is what `link.ts` draws it from when the reference is pasted into a note.
+ * colour, which `link.ts` renders the link from when the reference is pasted into a note.
*
* A card uses its note id. A column uses an id stored in `board.json`, because renaming a column
* rewrites the value its cards carry.
@@ -272,7 +272,7 @@ export function readColumnId(
return columns?.find(column => column.value === value)?.id;
}
-/** How a column reference names its column, which the link carries for display only. */
+/** The column's title, icon and colour, carried in the link for display only. */
export interface ColumnReferenceLabel {
title: string;
icon: string;
@@ -282,9 +282,9 @@ export interface ColumnReferenceLabel {
/**
* The link that opens a board on one of its columns.
*
- * The label rides along so that a link can be drawn without reading `board.json`, which only the
- * board itself loads. The id is what the board resolves, so renaming a column leaves the link
- * working and only its label behind.
+ * `label` is written into the link because `board.json`, which holds it, is loaded by the board
+ * alone. `columnId` is what the board resolves, so a rename leaves the link working and only its
+ * label out of date.
*/
export function columnReference(notePath: string, columnId: string, label: ColumnReferenceLabel) {
const params: [ string, string ][] = [
diff --git a/apps/client/src/widgets/collections/collection_filter.tsx b/apps/client/src/widgets/collections/collection_filter.tsx
index 35dabdd075..b6c826e86c 100644
--- a/apps/client/src/widgets/collections/collection_filter.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.tsx
@@ -199,8 +199,8 @@ export function CollectionFilterInput({ filter, placeholder }: {
// Adopt a query submitted elsewhere, or the stored one arriving on mount.
useEffect(() => setTyped(filter.query), [ filter.query ]);
- // Searching the note lands here: a collection holds no text of its own, so the find bar has
- // nothing to look through, and narrowing to what is searched for is what the reader is after.
+ // A collection has no text of its own for the find bar to search, so `findInText` focuses
+ // this box instead and the query narrows the collection.
useTriliumEvent("findInText", ({ ntxId }) => {
if (ntxId ? ntxId !== noteContext?.ntxId : !noteContext?.isActive()) {
return;