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(collections): mount a card's JSX render note under the view's component
Elian Doranb80daf7134bc
diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.tsindex 844a6b1ea4..a4218f16d9 100644--- a/apps/client/src/services/content_renderer.spec.ts+++ b/apps/client/src/services/content_renderer.spec.ts@@ -133,6 +133,7 @@ vi.mock("@triliumnext/commons/src/lib/markdown_renderer", async (orig) => ({// --- Imports AFTER the mocks. ---import appContext from "../components/app_context.js";+import Component from "../components/component.js";import type { SaveState } from "../components/note_context.js";import FAttachment from "../entities/fattachment.js";import { buildNote } from "../test/easy-froca.js";@@ -817,6 +818,12 @@ describe("getRenderedContent render / doc / protectedSession / mermaid", () => {expect($renderedContent.find(".render-ok").length).toBe(1);});+ it("mounts a JSX render note under the given parent component", async () => {+ const parentComponent = new Component();+ await getRenderedContent(buildNote({ title: "R", type: "render" }), { parentComponent });+ expect(renderServiceRender.mock.lastCall?.[3]).toBe(parentComponent);+ });+it("render error callback shows an admonition with the error message", async () => {renderServiceRender.mockImplementationOnce(async (_n: any, $content: any, onError: any) => {onError(new Error("kaput"));diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex 0e49c7f92a..063927b838 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -14,6 +14,7 @@ import {import DOMPurify from "dompurify";import { h, type JSX, render } from "preact";+import type Component from "../components/component.js";import type { SaveState } from "../components/note_context.js";import FAttachment from "../entities/fattachment.js";import FNote from "../entities/fnote.js";@@ -108,6 +109,8 @@ export interface RenderOptions {* Without it, that content is read-only.*/noteEditor?: NoteEditor;+ /** The component a JSX render note mounts under, by default the one of the closest `.component` element. */+ parentComponent?: Component;}/** Saves the changes that rendered content makes to attachments of the note that shows it. */@@ -212,7 +215,7 @@ export async function getRenderedContent(this: {} | { ctx: string }, entity: FNoawait renderService.render(entity, $content, (e, noteId) => {showRenderError($content, e, noteId).catch((cardError) => console.error("Failed to render the script error card:", cardError));- });+ }, options.parentComponent);$renderedContent.append($content);} else if (type === "doc" && "noteId" in entity) {diff --git a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsxindex 1658acaee0..b2355ef959 100644--- a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx+++ b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx@@ -2,7 +2,7 @@ import "./ListOrGridView.css";import { Card, CardFrame, CardSection } from "../../react/Card";import type { HighlightedTokenInfo } from "@triliumnext/commons";-import { useCallback, useEffect, useRef, useState } from "preact/hooks";+import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";import FNote from "../../../entities/fnote";import attribute_renderer from "../../../services/attribute_renderer";@@ -13,6 +13,7 @@ import CollectionProperties from "../../note_bars/CollectionProperties";import { useImperativeSearchHighlighlighting, useNoteLabel, useNoteLabelBoolean, useNoteProperty } from "../../react/hooks";import Icon from "../../react/Icon";import NoteLink from "../../react/NoteLink";+import { ParentComponent } from "../../react/react_utils";import { ViewModeProps } from "../interface";import { Pager, usePagination, PaginationContext } from "../Pagination";import { filterChildNotes, useFilteredNoteIds } from "./utils";@@ -297,6 +298,7 @@ export function NoteContent({ note, trim, noChildrenList, highlightedTokens, inc}) {const contentRef = useRef<HTMLDivElement>(null);const highlightSearch = useImperativeSearchHighlighlighting(highlightedTokens);+ const parentComponent = useContext(ParentComponent);const [ready, setReady] = useState(false);const [noteType, setNoteType] = useState<string>("none");@@ -328,7 +330,8 @@ export function NoteContent({ note, trim, noChildrenList, highlightedTokens, incnoContentEmbeds: true,includeArchivedNotes,showTextRepresentation,- interactive+ interactive,+ parentComponent: parentComponent ?? undefined}).then(({ $renderedContent, type }) => {if (cancelled || !contentRef.current) {