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: make the new card input field follow the icon of the template
Adorian Doran785c51a56387
diff --git a/apps/client/src/widgets/collections/board/card_templates.spec.ts b/apps/client/src/widgets/collections/board/card_templates.spec.tsindex 42814dea7c..7b1e3012b1 100644--- a/apps/client/src/widgets/collections/board/card_templates.spec.ts+++ b/apps/client/src/widgets/collections/board/card_templates.spec.ts@@ -1,7 +1,7 @@import { describe, expect, it } from "vitest";import { type NoteTypeOption } from "../../../services/note_types";-import { currentCardTemplate, DEFAULT_CARD_TEMPLATES } from "./card_templates";+import { cardTemplateIcon, currentCardTemplate, DEFAULT_CARD_TEMPLATES } from "./card_templates";/** An option as `getNoteTypeOptions` builds one, cut down to what the board reads. */function option(id: string, title: string): NoteTypeOption {@@ -30,4 +30,16 @@ describe("what a board makes its cards from", () => {expect(currentCardTemplate(offered, undefined)?.title).toBe("Text");expect(currentCardTemplate([], "type:text:text/html")).toBeUndefined();});++ /**+ * A note type carries a bare icon class, while a template note's comes from `FNote.getIcon()`+ * with `tn-icon` in front. The picker writes neither prefix, so both have to read alike.+ */+ it("gives a template's icon as the picker would write it", () => {+ const template = { ...option("note:mine", "Mine"), icon: "tn-icon bx bx-user" };++ expect(cardTemplateIcon(template)).toBe("bx bx-user");+ expect(cardTemplateIcon(option("type:text:text/html", "Text"))).toBe("bx bx-note");+ expect(cardTemplateIcon(undefined)).toBeUndefined();+ });});diff --git a/apps/client/src/widgets/collections/board/card_templates.ts b/apps/client/src/widgets/collections/board/card_templates.tsindex 87faf7cd37..0d1981c5a2 100644--- a/apps/client/src/widgets/collections/board/card_templates.ts+++ b/apps/client/src/widgets/collections/board/card_templates.ts@@ -36,3 +36,13 @@ export const DEFAULT_CARD_TEMPLATES = [export function currentCardTemplate(offered: NoteTypeOption[], stored: string | undefined) {return offered.find((template) => template.id === stored) ?? offered[0];}++/**+ * The icon a card made from a template carries, as an icon class the picker also 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.+ */+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.tsxindex f5ea28ef0f..4c327afb66 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -42,7 +42,7 @@ import { BoardActionsContext, BoardDragStateContext, TitleEditor } from ".";import BoardApi from "./api";import Card from "./card";import CardTemplatePill from "./card_template_pill";-import { type CardTemplates } from "./card_templates";+import { cardTemplateIcon, type CardTemplates } from "./card_templates";import { DEFAULT_CARD_ICON, DEFAULT_COLUMN_ICON, INBOX_COLUMN } from "./columns";import { openColumnContextMenu, openColumnSortMenu, openCreateCardMenu } from "./context_menu";import type { ColumnSort } from "./data";@@ -919,8 +919,23 @@ 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.+ 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(DEFAULT_CARD_ICON);+ const [ icon, setIcon ] = useState(templateIcon);+ /** The template icon the field is showing, against which a pick of the reader's is told. */+ 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.+ useEffect(() => {+ if (templateIcon === followedIcon.current) {+ return;+ }++ setIcon(shown => shown === followedIcon.current ? templateIcon : shown);+ followedIcon.current = templateIcon;+ }, [ templateIcon ]);const open = useCallback((title: string) => {setInitialTitle(title);@@ -958,7 +973,7 @@ function AddNewItem({/** Makes the card, where the field stands or at the end of the column. */const create = useCallback(async (title: string, atStart?: boolean) => {- const cardIcon = icon !== DEFAULT_CARD_ICON ? icon : undefined;+ const cardIcon = icon !== templateIcon ? icon : undefined;if (insert) {// Left standing until the column has drawn the card, which takes the field's place.@@ -974,7 +989,7 @@ function AddNewItem({onCreated(await api.createNewItem(column, title, atStart ? "top" : "bottom", cardIcon, cardTemplates.current));- }, [ api, cardTemplates, column, icon, insert, onCreated ]);+ }, [ api, cardTemplates, column, icon, insert, onCreated, templateIcon ]);return (<div@@ -1012,8 +1027,8 @@ function AddNewItem({icon={{current: icon,onSelect: setIcon,- onReset: icon !== DEFAULT_CARD_ICON- ? () => setIcon(DEFAULT_CARD_ICON)+ onReset: icon !== templateIcon+ ? () => setIcon(templateIcon): undefined}}/>diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex 3e54e77021..905aeb59cb 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -62,13 +62,14 @@ vi.mock("../../../services/note_types", () => ({let templateReads = 0;const NOTE_TYPE_OPTIONS = [- [ "type:text:text/html", "Text", "text", "text/html" ],- [ "type:code:text/x-markdown", "Markdown", "code", "text/x-markdown" ],- [ "type:canvas:application/json", "Canvas", "canvas", "application/json" ],- [ "type:spreadsheet:application/json", "Spreadsheet", "spreadsheet", "application/json" ],- [ "type:mermaid:text/mermaid", "Mermaid", "mermaid", "text/mermaid" ]-].map(([ id, title, type, mime ]) => ({- id, title, icon: "bx bx-note", group: "type" as const, options: { type, mime }+ [ "type:text:text/html", "Text", "text", "text/html", "bx bx-note" ],+ [ "type:code:text/x-markdown", "Markdown", "code", "text/x-markdown", "bx bx-code" ],+ [ "type:canvas:application/json", "Canvas", "canvas", "application/json", "bx bx-pen" ],+ [ "type:spreadsheet:application/json", "Spreadsheet", "spreadsheet", "application/json",+ "bx bx-table" ],+ [ "type:mermaid:text/mermaid", "Mermaid", "mermaid", "text/mermaid", "bx bx-selection" ]+].map(([ id, title, type, mime, icon ]) => ({+ id, title, icon, group: "type" as const, options: { type, mime }})) as NoteTypeOption[];vi.mock("../../react/IconPicker", () => ({@@ -2726,6 +2727,54 @@ describe("Board column rename", () => {.toContain(PICKED_ICON);});+ /**+ * The icon follows the template while it is still the template's own. One the reader picked+ * stands: the template changing under it says nothing about the icon they chose.+ */+ it("moves the card's icon to the template picked, unless one was picked by hand", async () => {+ const { container } = await setup();+ const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]+ .querySelector<HTMLElement>(".board-new-item");++ await act(async () => {+ slot?.click();+ await flush();+ });++ const shownIcon = () => slot?.querySelector(".title-editor-icon button")?.className ?? "";++ // The icon of the template the board opens on, rather than a stock one.+ expect(shownIcon()).toContain("bx bx-note");++ await pickTemplate(slot, 2);+ expect(shownIcon()).toContain("bx bx-pen");++ await pickIcon(slot);+ await pickTemplate(slot, 1);+ expect(shownIcon()).toContain(PICKED_ICON);+ });++ /** Picks one of the templates the pill offers, by where it stands in its menu. */+ async function pickTemplate(slot: HTMLElement | null | undefined, index: number) {+ const pill = slot?.querySelector<HTMLElement>(".card-template-pill button");+ if (!pill) throw new Error("expected the template pill");++ await act(async () => {+ pill.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));+ $(pill.closest(".dropdown") as HTMLElement).trigger("show.bs.dropdown");+ await flush();+ });++ // The menu is portalled to the page, and the boards rendered before left theirs behind, so+ // the last one is the board under test.+ const menu = [ ...document.querySelectorAll<HTMLElement>(".card-template-pill") ].at(-1);+ const items = [ ...(menu?.querySelectorAll<HTMLElement>(".dropdown-item") ?? []) ];+ await act(async () => {+ items[index].click();+ await flush();+ });+ }+/*** The picker takes focus with it, and losing focus is what closes the editor: it would take the* picker down with itself the moment it was opened.