Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/collections/board: allow board items to redirect to any note outside the subtree
Adorian Dorancff57d315bbe
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 9e0a8ba7b8..5a3ec68ac3 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -740,6 +740,7 @@"max_nesting_depth": "How many levels of children a table shows. Notes cannot be reordered while it is set.","include_archived": "Includes archived notes in this collection, which are otherwise left out.","enable_inbox_column": "For board collections, shows the inbox column, which lists all child notes that are not assigned to a board column.",+ "board_card_redirect_to": "On a board card, opening the card jumps to the note this points at instead of opening the card itself.","sort_columns": "For board collections, the order the columns sort their cards in, which a column follows unless it is given an order of its own. Holds <code>title</code>, <code>creationDate</code> or <code>attr:</code> followed by a promoted attribute's name.","sort_columns_descending": "For board collections, runs the order in <code>#sortColumns</code> backwards.","calendar_view": "Which view a calendar opens in: <code>timeGridDay</code>, <code>timeGridWeek</code>, <code>dayGridMonth</code>, <code>multiMonthYear</code> or <code>listMonth</code>.",diff --git a/apps/client/src/widgets/attribute_widgets/attr_help.ts b/apps/client/src/widgets/attribute_widgets/attr_help.tsindex da6a3e71d3..75bbacbdd0 100644--- a/apps/client/src/widgets/attribute_widgets/attr_help.ts+++ b/apps/client/src/widgets/attribute_widgets/attr_help.ts@@ -192,6 +192,7 @@ export const ATTR_HELP: AttrHelpMap = {internalBookmark: t("attribute_detail.internal_bookmark")},relation: {+ boardCardRedirectTo: t("attribute_detail.board_card_redirect_to"),runOnNoteCreation: t("attribute_detail.run_on_note_creation"),runOnChildNoteCreation: t("attribute_detail.run_on_child_note_creation"),runOnNoteTitleChange: t("attribute_detail.run_on_note_title_change"),diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.tsindex 37a8647e4f..92cfd7c241 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -34,6 +34,9 @@ import { SORT_DESCENDING_LABEL, SORT_LABEL } from "./sort";/** Which end of a column a new card is made at. */export type CardPlacement = "top" | "bottom";+/** The relation a card carries to stand in for another note rather than open an editor of its own. */+export const CARD_REDIRECT_RELATION = "boardCardRedirectTo";+/** One write's claim on a column, held until that write lands or is taken back. */interface ColumnClaim {/**@@ -1213,6 +1216,23 @@ export default class BoardApi {appContext.triggerCommand("openInPopup", { noteIdOrPath: noteId });}+ /**+ * Answers the card's own open gesture, a click or Space.+ *+ * A card carrying `boardCardRedirectTo` stands in for the note that relation points at, so it+ * navigates there instead of opening an editor of its own. Quick edit calls `openNote` and+ * still opens the card's own editor.+ */+ openCard(note: FNote) {+ const target = note.getRelationValue(CARD_REDIRECT_RELATION);+ if (target) {+ void appContext.tabManager.getActiveContext()?.setNote(target);+ return;+ }++ this.openNote(note.noteId);+ }+startEditing(branchId: string) {this.setBranchIdToEdit(branchId);}diff --git a/apps/client/src/widgets/collections/board/card.spec.tsx b/apps/client/src/widgets/collections/board/card.spec.tsxindex 6ac5ecac8a..37caf69dcc 100644--- a/apps/client/src/widgets/collections/board/card.spec.tsx+++ b/apps/client/src/widgets/collections/board/card.spec.tsx@@ -82,7 +82,7 @@ describe("Board card", () => {expect(cardClasses(first)).not.toContain("archived");- await addLabel(component, first, "archived");+ await addAttribute(component, first, "archived");expect(cardClasses(first)).toContain("archived");expect(cardClasses(second)).not.toContain("archived");@@ -117,7 +117,7 @@ describe("Board card", () => {expect(cardClasses(first)).not.toContain(coloured);- await addLabel(component, first, "color", "#ff0000");+ await addAttribute(component, first, "color", "#ff0000");expect(cardClasses(first)).toContain(coloured);expect(cardClasses(second)).not.toContain(coloured);@@ -128,7 +128,7 @@ describe("Board card", () => {expect(cardIcon(first)).not.toContain("bx-bug");- await addLabel(component, first, "iconClass", "bx bx-bug");+ await addAttribute(component, first, "iconClass", "bx bx-bug");expect(cardIcon(first)).toContain("bx-bug");});@@ -143,6 +143,51 @@ describe("Board card", () => {.toHaveBeenCalledWith("openInPopup", { noteIdOrPath: first });});+ /**+ * A card carrying `boardCardRedirectTo` stands in for the note it points at, so the open gesture+ * navigates there rather than opening an editor for the card itself.+ */+ it("jumps to the note it redirects to instead of opening itself", async () => {+ const { first, component } = await renderBoard();+ await addAttribute(component, first, "boardCardRedirectTo", "targetNote", "relation");++ const openInPopup = vi.spyOn(appContext, "triggerCommand").mockReturnValue(undefined);+ const setNote = vi.fn();+ const previousTabManager = appContext.tabManager;+ appContext.tabManager = { getActiveContext: () => ({ setNote }) } as never;++ try {+ await act(async () => { card(first).click(); });+ } finally {+ // Put back before the assertions: a stub left standing reaches every later test.+ appContext.tabManager = previousTabManager;+ }++ expect(setNote).toHaveBeenCalledWith("targetNote");+ expect(openInPopup).not.toHaveBeenCalled();+ // `shortcut` is what underlines the title on hover, drawing it as a link.+ expect(cardClasses(first)).toContain("shortcut");+ });++ it("marks no card a shortcut without the relation", async () => {+ const { first } = await renderBoard();++ expect(cardClasses(first)).not.toContain("shortcut");+ });++ /**+ * The underline is drawn on the text alone. A decoration set on `.title` would propagate to the+ * icon standing beside the text, and a descendant cannot turn a propagated one off.+ */+ it("keeps the title text in an element of its own, apart from the icon", async () => {+ const { first } = await renderBoard();+ const title = card(first).querySelector(".title");++ expect(title?.querySelector(".text")).toBeTruthy();+ expect(title?.querySelector(".text .icon")).toBeNull();+ expect(title?.querySelector(":scope > .icon")).toBeTruthy();+ });+/*** A relation on a card is drawn as a link to the note it points at. The page's own link handler* would open that in a tab, and the card underneath would open itself over the top of it.@@ -554,18 +599,24 @@ describe("Board card", () => {.map(column => column.getAttribute("data-column"));}- /** Adds a label to a note already in froca and announces it the way a websocket message would. */- async function addLabel(component: Component, noteId: string, name: string, value = "") {+ /**+ * Adds an attribute to a note already in froca and announces it the way a websocket message+ * would.+ */+ async function addAttribute(+ component: Component, noteId: string, name: string, value = "",+ type: "label" | "relation" = "label"+ ) {const attributeId = utils.randomString(12);const attribute = new FAttribute(froca, {- noteId, attributeId, type: "label", name, value, position: 0, isInheritable: false+ noteId, attributeId, type, name, value, position: 0, isInheritable: false});froca.attributes[attributeId] = attribute;froca.notes[noteId].attributes.push(attributeId);noteAttributeCache.attributes[noteId] = [ ...(noteAttributeCache.attributes[noteId] ?? []), attribute ];- const entity = { attributeId, noteId, type: "label", name, value, isDeleted: false };+ const entity = { attributeId, noteId, type, name, value, isDeleted: false };const loadResults = new LoadResults([ {entityName: "attributes", entityId: attributeId, entity, hash: "", isSynced: true, isErased: false} ]);diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsxindex a46a6c8dbd..0eec3be29e 100644--- a/apps/client/src/widgets/collections/board/card.tsx+++ b/apps/client/src/widgets/collections/board/card.tsx@@ -6,7 +6,7 @@ import {import FBranch from "../../../entities/fbranch";import FNote from "../../../entities/fnote";-import BoardApi from "./api";+import BoardApi, { CARD_REDIRECT_RELATION } from "./api";import {BoardActionsContext, BoardHighlightTokensContext, BoardKeptCardsContext,BoardPromotedAttributesContext, TitleEditor@@ -19,7 +19,8 @@ import UserAttributesDisplay from "../../attribute_widgets/UserAttributesList";import { parseNavigationStateFromUrl } from "../../../services/link";import { FLIP_SETTLE_MS } from "../../react/flip";import {- useNoteColorClass, useNoteIcon, useNoteLabel, useNoteLabelBoolean, useTriliumEvent+ useNoteColorClass, useNoteIcon, useNoteLabel, useNoteLabelBoolean, useNoteRelation,+ useTriliumEvent} from "../../react/hooks";import { TooltipIcon } from "../../react/Icon";import { HighlightedText } from "../../react/RawHtml";@@ -81,6 +82,9 @@ function Card({const cardRef = useRef<HTMLDivElement>(null);const [ isArchived ] = useNoteLabelBoolean(note, "archived");const [ iconClass, setIconClass ] = useNoteLabel(note, "iconClass");+ // Only whether the card redirects, which is what draws its title as a link. Where it goes is+ // read when the card is opened.+ const [ redirectTo ] = useNoteRelation(note, CARD_REDIRECT_RELATION);// The card stays the one just made until another is, so what has already been shown is// remembered here rather than played again by every redraw of the column.const [ isRevealed, setIsRevealed ] = useState(false);@@ -161,7 +165,7 @@ function Card({}selection.clear();- api.openNote(note.noteId);+ api.openCard(note);}, [ api, note, column, selection ]);/**@@ -253,6 +257,7 @@ function Card({<divref={cardRef}className={clsx("board-note", colorClass, {+ shortcut: !!redirectTo,dragging: isDragging,editing: isEditing,archived: isArchived,@@ -275,7 +280,8 @@ function Card({<><span className="title"><span class={`icon ${icon}`} />- <HighlightedText text={title} highlightedTokens={highlightedTokens} />+ <HighlightedText+ className="text" text={title} highlightedTokens={highlightedTokens} /></span><spanclassName="edit-icon icon bx bx-edit"diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.cssindex ced0c03204..7ee40c7acc 100644--- a/apps/client/src/widgets/collections/board/index.css+++ b/apps/client/src/widgets/collections/board/index.css@@ -434,6 +434,13 @@ body.mobile .board-view-container .board-add-column {}}+/* A card carrying `boardCardRedirectTo` opens the note it points at, so its title reads as a link.+ Drawn on `.text` rather than on `.title`: a decoration set on the title propagates to the icon+ beside it, and a descendant cannot turn a propagated one off. */+.board-view-container .board-note.shortcut:hover > .title > .text {+ text-decoration: underline;+}+.board-view-container .board-note > .title > .icon {flex: none;display: flex;diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsxindex 35fc5e5abf..17276d0c16 100644--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx@@ -1058,6 +1058,26 @@ describe("Board keyboard", () => {expect(open).toHaveBeenCalledWith("openInPopup", { noteIdOrPath: noteOf(board, "First") });});+ /** Space is the card's open gesture, so it redirects where a click on the card would. */+ it("jumps to the redirect target with Space", async () => {+ const board = await renderBoard(undefined, undefined, [], false, true);+ const open = vi.spyOn(appContext, "triggerCommand").mockReturnValue(undefined);+ const setNote = vi.fn();+ const previousTabManager = appContext.tabManager;+ appContext.tabManager = { getActiveContext: () => ({ setNote }) } as never;++ try {+ focusCard(board, 0, 0);+ press(board, " ");+ } finally {+ // Put back before the assertions: a stub left standing reaches every later test.+ appContext.tabManager = previousTabManager;+ }++ expect(setNote).toHaveBeenCalledWith("targetNote");+ expect(open).not.toHaveBeenCalled();+ });+/** Which column a card is drawn in, counted as the reader sees them. */function columnOf(board: HTMLElement, title: string) {return [ ...board.querySelectorAll(".board-column") ]@@ -1196,7 +1216,8 @@ describe("Board keyboard", () => {* the tests about moving a run of them need a column deep enough to hold one.*/async function renderBoard(- collapsed?: string, orderBy?: string, extra: string[] = [], inbox = false+ collapsed?: string, orderBy?: string, extra: string[] = [], inbox = false,+ redirect = false) {boardNote = buildNote({title: "Board",@@ -1204,7 +1225,11 @@ describe("Board keyboard", () => {"#viewType": "board",...(inbox ? { "#enableInboxColumn": "true" } : {}),children: [- { title: "First", "#status": "To Do" },+ {+ title: "First",+ "#status": "To Do",+ ...(redirect ? { "~boardCardRedirectTo": "targetNote" } : {})+ },{ title: "Second", "#status": "To Do" },{ title: "Third", "#status": "Doing" },...extra.map((status, at) => (diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex 4830b58476..b1ad339b6b 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -283,7 +283,7 @@ export function useBoardKeyboard({const item = itemAt(columns, byColumn, spot);if (item) {take(e);- api.openNote(item.note.noteId);+ api.openCard(item.note);}return;}diff --git a/packages/commons/src/lib/builtin_attributes.ts b/packages/commons/src/lib/builtin_attributes.tsindex 8bddc6c36c..270b7f3c09 100644--- a/packages/commons/src/lib/builtin_attributes.ts+++ b/packages/commons/src/lib/builtin_attributes.ts@@ -269,6 +269,8 @@ const BUILTIN_ATTRIBUTES = [{ type: "label", name: "maxNestingDepth", valueType: "number", hasUserValue: true },{ type: "label", name: "includeArchived", valueType: "boolean", hasUserValue: true },{ type: "label", name: "enableInboxColumn", valueType: "boolean", hasUserValue: true },+ // Carried by a card that stands in for another note: opening it navigates there instead.+ { type: "relation", name: "boardCardRedirectTo" },// The order a board offers for its columns, which its properties apply to every column at once.{ type: "label", name: "sortColumns", valueType: "text", hasUserValue: true },{ type: "label", name: "sortColumnsDescending", valueType: "boolean", hasUserValue: true },