Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix some issues

Adorian Doran3f01549b6bf5
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index bc155a4e4f..ffe8f24470 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
+import appContext from "../../../components/app_context";
import FAttribute from "../../../entities/fattribute";
import type FBranch from "../../../entities/fbranch";
import branches from "../../../services/branches";
@@ -550,6 +551,53 @@ describe("BoardApi card operations", () => {
return { ...createApi({}, [ "To Do", "Done" ], board, "status", byColumn), items };
}
+ describe("opening a card", () => {
+ /**
+ * The board can be one split of several, and the focused pane is often the one the reader
+ * came from, so a redirect navigates the pane the board itself is drawn in.
+ */
+ it("navigates the board's own pane rather than the focused one", () => {
+ const { api, items } = createBoardWithCards();
+ const own = vi.fn();
+ const focused = vi.fn();
+ api.noteContext = { setNote: own } as never;
+ const previousTabManager = appContext.tabManager;
+ appContext.tabManager = { getActiveContext: () => ({ setNote: focused }) } as never;
+
+ try {
+ addRedirect(items[0].note, "targetNote");
+ api.openCard(items[0].note);
+ } finally {
+ appContext.tabManager = previousTabManager;
+ }
+
+ expect(own).toHaveBeenCalledWith("targetNote");
+ expect(focused).not.toHaveBeenCalled();
+ });
+
+ it("opens the card itself where it redirects nowhere", () => {
+ const { api, items } = createBoardWithCards();
+ const open = vi.spyOn(appContext, "triggerCommand").mockReturnValue(undefined);
+
+ api.openCard(items[0].note);
+
+ expect(open).toHaveBeenCalledWith(
+ "openInPopup", { noteIdOrPath: items[0].note.noteId });
+ });
+
+ /** Files the relation straight into froca, which is all `openCard` reads. */
+ function addRedirect(note: FNote, target: string) {
+ const attributeId = `redirect-${note.noteId}`;
+ froca.attributes[attributeId] = new FAttribute(froca, {
+ noteId: note.noteId, attributeId, type: "relation",
+ name: "boardCardRedirectTo", value: target, position: 0, isInheritable: false
+ });
+ note.attributes.push(attributeId);
+ // Cleared rather than emptied: the cache is rebuilt only for a note it has no entry for.
+ delete noteAttributeCache.attributes[note.noteId];
+ }
+ });
+
it("files a card moved to another column before the one it was dropped on", async () => {
const { api, items } = createBoardWithCards();
const [ first ] = items;
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 92cfd7c241..fe62cb00ff 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -1,6 +1,7 @@
import { BulkAction, type DefinitionObject, promotedAttributeDefinitionParser } from "@triliumnext/commons";
import appContext from "../../../components/app_context";
+import type NoteContext from "../../../components/note_context";
import FNote from "../../../entities/fnote";
import attributes from "../../../services/attributes";
import branches from "../../../services/branches";
@@ -117,6 +118,14 @@ export default class BoardApi {
*/
groupBy: string;
+ /**
+ * The pane the board is drawn in, set by the board on every render.
+ *
+ * A redirecting card navigates this rather than whichever pane holds the focus: the board can be
+ * one split of several, and the focused one is often the pane the reader came from.
+ */
+ noteContext: NoteContext | null | undefined;
+
/** The config as the board last handed it over, against which a fresh one is recognised. */
private viewConfigSource: BoardViewData | undefined;
private viewConfig: BoardViewData;
@@ -1226,7 +1235,8 @@ export default class BoardApi {
openCard(note: FNote) {
const target = note.getRelationValue(CARD_REDIRECT_RELATION);
if (target) {
- void appContext.tabManager.getActiveContext()?.setNote(target);
+ const context = this.noteContext ?? appContext.tabManager?.getActiveContext();
+ void context?.setNote(target);
return;
}
@@ -1272,9 +1282,8 @@ export default class BoardApi {
/**
* Whether the board draws the inbox column, which holds the cards with no grouping value.
*
- * {@link removeFromBoard} takes that value away, so with the inbox drawn a card has nowhere to
- * go: it lands in the inbox instead of leaving, and one already there does not move at all.
- * The menu and the Delete key leave the action out where this is true.
+ * {@link removeFromBoard} clears that value, so a card lands in the inbox instead of leaving
+ * the board, and one already there does not move at all.
*/
get isInboxEnabled() {
return !!this.parentNote?.isLabelTruthy("enableInboxColumn");
@@ -1426,12 +1435,9 @@ export default class BoardApi {
const whole = this.allByColumn?.get(targetColumn) ?? targetItems;
const held = whole.map((item) => item.branch.branchId);
- // Nothing at all is written where the cards already stand where this would put them, which
- // is what a card dropped back where it was picked up amounts to.
- //
- // Compared over the cards on screen, which is the order the drop was aimed at. Under a
- // filter `moveBeforeBranch` can only place a card against a visible neighbour, so writing
- // a move that leaves the screen unchanged would reorder the hidden cards instead.
+ // A move that leaves `targetItems` in the order it already holds writes nothing:
+ // `moveBeforeBranch` places a card against a drawn neighbour, so under a filter it would
+ // reorder the hidden cards instead.
const standing = targetItems.map((item) => item.branch.branchId);
const landing = [
...staying.slice(0, at).map((item) => item.branch.branchId),
diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.ts
index e0ffad2cf3..04e53b1434 100644
--- a/apps/client/src/widgets/collections/board/context_menu.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.ts
@@ -544,8 +544,8 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, target: N
}),
{ kind: "separator" },
...getArchiveMenuItems<CommandNames>(notes),
- // Left out where the inbox is drawn: taking the grouping value away would leave the
- // card in the inbox rather than off the board. Deleting the note is what removes it.
+ // The inbox holds the cards with no grouping value, so clearing that value keeps a
+ // card on the board. Deleting the note is what takes it off.
...(api.isInboxEnabled ? [] : [ {
title: t("board_view.remove-from-board"),
uiIcon: "bx bx-task-x",
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 67c88240bf..3e5dc68f1d 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -507,6 +507,9 @@ export default function BoardView({
saveConfig, setBranchIdToEdit, statusDefinition, allByColumn, filter.keepNote);
}
const api = apiRef.current.api;
+ // Set here rather than passed in: the api outlives a refresh, and the board can be drawn in a
+ // pane other than the focused one.
+ api.noteContext = noteContext;
// Every member is one of useState's own setters, so this value is built once and never changes
// identity -- a drag cannot reach anything that reads only this.
const openBoardMenu = useCallback((event: ContextMenuEvent) => {
diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.ts
index b1ad339b6b..ff6789a7a0 100644
--- a/apps/client/src/widgets/collections/board/keyboard.ts
+++ b/apps/client/src/widgets/collections/board/keyboard.ts
@@ -315,8 +315,8 @@ export function useBoardKeyboard({
if (e.key === "Delete" && spot.kind === "item") {
const item = itemAt(columns, byColumn, spot);
if (!item) return;
- // Plain Delete takes the card off the board, which the inbox leaves nowhere to do: the
- // card would land there instead. The menu leaves the entry out for the same reason.
+ // Plain Delete clears the grouping value, which with the inbox drawn moves the card
+ // there rather than off the board. Shift+Delete still deletes the note.
if (!e.shiftKey && api.isInboxEnabled) return;
take(e);