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/refactor: make the template selection card reusable
Adorian Doran3e2e7dad0a31
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 1e1bd27a9e..cfd9a98385 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3444,6 +3444,7 @@"properties-title": "Board properties","card-templates": "Card templates","card-templates-hint": "Choose what note types and note templates you would like to use when creating new board cards.",+ "new-template-name": "New card template","more-templates": "More templates…","card-template": "Select a card template","collapse-hint": "Double click to collapse",diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex 0c7dc19828..701899d197 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -2508,6 +2508,8 @@ describe("Board column rename", () => {more.click();await flush();});+ // The card reads what a note can be made from as it is drawn, which takes a moment.+ await act(async () => { await flush(); });// The last of them: a dialog portalled to the page outlives the board that drew it here.const dialog = [ ...document.querySelectorAll<HTMLElement>(".board-properties-dialog") ]@@ -2515,7 +2517,7 @@ describe("Board column rename", () => {expect(dialog).toBeTruthy();// What the board offers, in the order it stores them, each carried by a grip of its own.- const named = [ ...(dialog?.querySelectorAll(".board-template-name") ?? []) ]+ const named = [ ...(dialog?.querySelectorAll(".template-selection-name") ?? []) ].map(element => element.textContent);expect(named).toEqual([ "Text", "Markdown", "Canvas", "Spreadsheet" ]);expect(dialog?.querySelectorAll(".tn-sortable-grip").length).toBe(4);diff --git a/apps/client/src/widgets/collections/board/properties.spec.tsx b/apps/client/src/widgets/collections/board/properties.spec.tsxindex 8ab1e3d64b..707e63f569 100644--- a/apps/client/src/widgets/collections/board/properties.spec.tsx+++ b/apps/client/src/widgets/collections/board/properties.spec.tsx@@ -15,15 +15,16 @@ vi.mock("../../../services/i18n", () => ({// The card is the app's and is tested where it lives; what the board answers for is the words it is// given and where what it holds is written.vi.mock("../../react/TemplateSelectionCard", () => ({- default: ({ heading, instruction, note, templates, onChange }: {- heading: string, instruction: string, note: FNote, templates: string[],- onChange: (templates: string[]) => void+ default: ({ heading, instruction, note, newTemplateName, templates, onChange }: {+ heading: string, instruction: string, note: FNote, newTemplateName?: string,+ templates: string[], onChange: (templates: string[]) => void}) => (<divclassName="templates-stub"data-heading={heading}data-instruction={instruction}data-note={note.noteId}+ data-new-name={newTemplateName}data-templates={templates.join(",")}onClick={() => onChange([ "type:canvas:application/json" ])}/>@@ -82,6 +83,7 @@ describe("Board properties", () => {expect(card?.dataset.instruction).toBe("board_view.card-templates-hint");// Filed under the board, and drawn from what the board offers now.expect(card?.dataset.note).toBe("board1");+ expect(card?.dataset.newName).toBe("board_view.new-template-name");expect(card?.dataset.templates).toBe("type:text:text/html");});diff --git a/apps/client/src/widgets/collections/board/properties.tsx b/apps/client/src/widgets/collections/board/properties.tsxindex a55062e993..ce99747ad8 100644--- a/apps/client/src/widgets/collections/board/properties.tsx+++ b/apps/client/src/widgets/collections/board/properties.tsx@@ -36,6 +36,7 @@ export default function BoardProperties({ api, note, shown, onClose }: {heading={t("board_view.card-templates")}instruction={t("board_view.card-templates-hint")}note={note}+ newTemplateName={t("board_view.new-template-name")}templates={api.getCardTemplateIds()}onChange={store}/>diff --git a/apps/client/src/widgets/react/TemplateSelectionCard.spec.tsx b/apps/client/src/widgets/react/TemplateSelectionCard.spec.tsxindex dfa6416e3b..268f7bd75f 100644--- a/apps/client/src/widgets/react/TemplateSelectionCard.spec.tsx+++ b/apps/client/src/widgets/react/TemplateSelectionCard.spec.tsx@@ -162,6 +162,23 @@ describe("TemplateSelectionCard", () => {expect(captions()).toEqual([ "Text", "Markdown", "template_selection.new-name" ]);});+ /** A caller whose templates are all of one kind says what one is called. */+ it("calls a template what the caller calls one", async () => {+ const made = vi.spyOn(note_create, "createTemplateNote").mockResolvedValue({+ noteId: "fresh",+ title: "New card template",+ type: "text",+ mime: "text/html",+ getIcon: () => "bx bx-note"+ } as unknown as FNote);+ await draw({ newTemplateName: "New card template" });++ await add("template_selection.create");++ expect(made).toHaveBeenCalledWith("owner1", "New card template");+ expect(captions().at(-1)).toBe("New card template");+ });+it("adds nothing where the template could not be made", async () => {vi.spyOn(note_create, "createTemplateNote").mockResolvedValue(undefined);await draw();@@ -176,13 +193,14 @@ describe("TemplateSelectionCard", () => {// #region The dialog, and what the test does to it/** Drawn, and given the moment it takes to read what a note can be made from. */- async function draw() {+ async function draw({ newTemplateName }: { newTemplateName?: string } = {}) {await act(async () => {render(<TemplateSelectionCardheading="What to use"instruction="Pick what a new one is made from."note={OWNER}+ newTemplateName={newTemplateName}templates={offered}onChange={(ids) => stored.push(ids)}/>,diff --git a/apps/client/src/widgets/react/TemplateSelectionCard.tsx b/apps/client/src/widgets/react/TemplateSelectionCard.tsxindex deacc6bf57..0f63229c67 100644--- a/apps/client/src/widgets/react/TemplateSelectionCard.tsx+++ b/apps/client/src/widgets/react/TemplateSelectionCard.tsx@@ -21,6 +21,8 @@ export interface TemplateSelectionCardProps {instruction: string;/** The note the templates belong to, which is what one made here is filed under. */note: FNote;+ /** What a template made here is called, for a caller whose templates are of one kind. */+ newTemplateName?: string;/** What is offered now, as note type ids, in the order they are offered in. */templates: string[];/** Said with the whole list whenever the reader changes it. */@@ -38,7 +40,7 @@ export interface TemplateSelectionCardProps {* it holds now and where a new one belongs.*/export default function TemplateSelectionCard({- heading, instruction, note, templates, onChange+ heading, instruction, note, newTemplateName, templates, onChange}: TemplateSelectionCardProps) {/*** The list as it stands here, which is what the card is drawn from.@@ -104,7 +106,7 @@ export default function TemplateSelectionCard({/** Makes one and opens it, the reader writing what a note made from it holds. */const create = useCallback(async () => {const template = await note_create.createTemplateNote(- note.noteId, t("template_selection.new-name"));+ note.noteId, newTemplateName ?? t("template_selection.new-name"));if (!template) {return undefined;}@@ -119,7 +121,7 @@ export default function TemplateSelectionCard({setMade((was) => [ ...was, option ]);return { key: option.id, caption: option.title, icon: option.icon };- }, [ note ]);+ }, [ newTemplateName, note ]);return (<SortableCard