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
fix(print): give printed collections a root component
Elian Doran3173b34c82d6
diff --git a/apps/client/src/print.spec.tsx b/apps/client/src/print.spec.tsxindex ea9f040aed..80c849ccfb 100644--- a/apps/client/src/print.spec.tsx+++ b/apps/client/src/print.spec.tsx@@ -9,20 +9,26 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";// also how we reach `App`'s inline `onReady`/`onProgressChanged` callbacks.const h = vi.hoisted(() => ({getRenderedContent: vi.fn(),- noteListProps: null as any+ noteListProps: null as any,+ noteListParent: null as unknown}));vi.mock("./services/content_renderer", () => ({default: { getRenderedContent: h.getRenderedContent }}));-vi.mock("./widgets/collections/NoteList", () => ({- useNoteViewType: () => "grid",- CustomNoteList: (props: any) => {- h.noteListProps = props;- return "NOTELIST";- }-}));+vi.mock("./widgets/collections/NoteList", async () => {+ const { useContext } = await import("preact/hooks");+ const { ParentComponent } = await import("./widgets/react/react_utils");+ return {+ useNoteViewType: () => "grid",+ CustomNoteList: (props: any) => {+ h.noteListProps = props;+ h.noteListParent = useContext(ParentComponent);+ return "NOTELIST";+ }+ };+});import Component from "./components/component";import { App, Error404, loadCustomCss, main, SingleNoteRenderer } from "./print";@@ -139,6 +145,15 @@ describe("App", () => {window.removeEventListener("note-ready", readyListener);});+ it("gives a collection a root component, for the hooks of its view", async () => {+ buildNote({ id: "root", title: "root", children: [{ id: "app-book-parent", title: "Book", type: "book" }] });+ const note = await froca.getNote("app-book-parent");+ renderInto(<App note={note} noteId="app-book-parent" />);++ await vi.waitFor(() => expect(h.noteListProps).toBeTruthy());+ expect(h.noteListParent).toBeInstanceOf(Component);+ });+it("renders CollectionRenderer for a search note, whose results print as a list", async () => {const loadSearchNote = vi.spyOn(froca, "loadSearchNote").mockResolvedValue(undefined);buildNote({ id: "root", title: "root", children: [{ id: "app-search", title: "Search", type: "search" }] });diff --git a/apps/client/src/print.tsx b/apps/client/src/print.tsxindex e334fd7a20..2652414ae1 100644--- a/apps/client/src/print.tsx+++ b/apps/client/src/print.tsx@@ -8,6 +8,7 @@ import froca from "./services/froca";import { waitForPendingRenders } from "./services/pending_renders";import { isElectron } from "./services/utils";import { CustomNoteList, useNoteViewType } from "./widgets/collections/NoteList";+import { ParentComponent } from "./widgets/react/react_utils";interface RendererProps {note: FNote;@@ -185,6 +186,8 @@ export function CollectionRenderer({ note, onReady, onProgressChanged }: Rendereconst viewType = useNoteViewType(note);// A saved search holds its results as virtual children only once it has run server-side.const [ ready, setReady ] = useState(note.type !== "search");+ // The print page has no component tree, so the view's hooks get a bare root `Component`.+ const [ rootComponent ] = useState(() => new Component());useEffect(() => {if (note.type !== "search") {@@ -224,21 +227,25 @@ export function CollectionRenderer({ note, onReady, onProgressChanged }: Rendereif (!ready) return null;- return <CustomNoteList- viewType={viewType}- isEnabled- note={note}- notePath={note.getBestNotePath().join("/")}- ntxId="print"- highlightedTokens={null}- media="print"- onReady={async (data: PrintReport) => {- await loadCustomCss(note);- await document.fonts.ready;- onReady(data);- }}- onProgressChanged={onProgressChanged}- />;+ return (+ <ParentComponent.Provider value={rootComponent}>+ <CustomNoteList+ viewType={viewType}+ isEnabled+ note={note}+ notePath={note.getBestNotePath().join("/")}+ ntxId="print"+ highlightedTokens={null}+ media="print"+ onReady={async (data: PrintReport) => {+ await loadCustomCss(note);+ await document.fonts.ready;+ onReady(data);+ }}+ onProgressChanged={onProgressChanged}+ />+ </ParentComponent.Provider>+ );}export function Error404({ noteId }: { noteId: string }) {