Skip to content

Build your personal knowledge base with Trilium Notes

feat(content_renderer): render any collection interactively and make embeds reactive

Elian Dorandbe04cdecbff
diff --git a/apps/client/src/services/content_renderer.css b/apps/client/src/services/content_renderer.css
index a4ae38080c..2ebb969c6a 100644
--- a/apps/client/src/services/content_renderer.css
+++ b/apps/client/src/services/content_renderer.css
@@ -18,3 +18,10 @@
.rendered-content:has(> .note-detail-web-view) {
height: 100%;
}
+
+/* A full-height embedded collection view (geo map, calendar, board, table) fills the height its host
+ provides; list/grid collections keep their natural, content-driven height. */
+.rendered-content:has(> .rendered-collection > .note-list-widget.full-height),
+.rendered-collection:has(> .note-list-widget.full-height) {
+ height: 100%;
+}
diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.ts
index e10bf49a5a..5138625b61 100644
--- a/apps/client/src/services/content_renderer.spec.ts
+++ b/apps/client/src/services/content_renderer.spec.ts
@@ -71,8 +71,8 @@ vi.mock("../widgets/type_widgets/file/PdfViewer", () => ({ default: pdfViewerCom
const webViewComponent = vi.fn(() => null);
vi.mock("../widgets/type_widgets/WebView", () => ({ default: webViewComponent }));
-const searchNoteListComponent = vi.fn(() => null);
-vi.mock("../widgets/collections/NoteList", () => ({ SearchNoteList: searchNoteListComponent }));
+const embeddedNoteListComponent = vi.fn(() => null);
+vi.mock("../widgets/collections/NoteList", () => ({ EmbeddedNoteList: embeddedNoteListComponent }));
// `addHook` is a no-op here: sanitize_content.ts registers a DOMPurify hook at
// module load (pulled in transitively), which would otherwise throw against this mock.
@@ -456,7 +456,7 @@ describe("generic FNote fallback / webView", () => {
expect($renderedContent.hasClass("no-preview")).toBe(false);
});
- it("executes the search and mounts the live results list for an interactive search note", async () => {
+ it("executes the search and mounts the live collection list for an interactive search note", async () => {
const note = buildNote({ title: "Saved", type: "search" });
vi.spyOn(note, "getBestNotePathString").mockReturnValue("root/saved");
const loadSpy = vi.spyOn(froca, "loadSearchNote").mockResolvedValue(undefined);
@@ -465,24 +465,51 @@ describe("generic FNote fallback / webView", () => {
expect(type).toBe("search");
expect(loadSpy).toHaveBeenCalledWith(note.noteId);
- expect(searchNoteListComponent).toHaveBeenCalledOnce();
- expect(searchNoteListComponent.mock.calls[0]?.[0]).toMatchObject({ note, media: "screen" });
- expect($renderedContent.find(".rendered-search").length).toBe(1);
- expect($renderedContent.hasClass("no-preview")).toBe(false);
+ expect(embeddedNoteListComponent).toHaveBeenCalledOnce();
+ expect(embeddedNoteListComponent.mock.calls[0]?.[0]).toMatchObject({ note, media: "screen", showTextRepresentation: true });
+ expect($renderedContent.find(".rendered-collection").length).toBe(1);
loadSpy.mockRestore();
});
- it("keeps the static fallback for a search note when interactive is off", async () => {
- const note = buildNote({ title: "Saved2", type: "search" });
+ it("mounts the collection view for an interactive book note without executing a search", async () => {
+ const note = buildNote({ title: "Coll", type: "book" });
+ vi.spyOn(note, "getBestNotePathString").mockReturnValue("root/coll");
const loadSpy = vi.spyOn(froca, "loadSearchNote").mockResolvedValue(undefined);
- const { $renderedContent } = await getRenderedContent(note);
+ const { type, $renderedContent } = await getRenderedContent(note, { interactive: true });
+ expect(type).toBe("book");
expect(loadSpy).not.toHaveBeenCalled();
- expect(searchNoteListComponent).not.toHaveBeenCalled();
- expect($renderedContent.find(".rendered-search").length).toBe(0);
- expect($renderedContent.hasClass("no-preview")).toBe(true);
+ expect(embeddedNoteListComponent).toHaveBeenCalledOnce();
+ expect(embeddedNoteListComponent.mock.calls[0]?.[0]).toMatchObject({ note, media: "screen", showTextRepresentation: false });
+ expect($renderedContent.find(".rendered-collection").length).toBe(1);
+
+ loadSpy.mockRestore();
+ });
+
+ it("does not embed a dashboard-view collection, to avoid recursion", async () => {
+ const note = buildNote({ title: "Dash", type: "book", "#viewType": "dashboard" });
+ const { $renderedContent } = await getRenderedContent(note, { interactive: true });
+ // Falls back to renderText (the basic children list) instead of the live collection.
+ expect(embeddedNoteListComponent).not.toHaveBeenCalled();
+ expect($renderedContent.find(".rendered-collection").length).toBe(0);
+ expect($renderedContent.find(".from-render-text").length).toBe(1);
+ });
+
+ it("keeps the static fallback for book/search notes when interactive is off", async () => {
+ const loadSpy = vi.spyOn(froca, "loadSearchNote").mockResolvedValue(undefined);
+
+ // Book falls back to renderText (basic children list).
+ const book = await getRenderedContent(buildNote({ title: "Coll2", type: "book" }));
+ // Search has no static renderer, so it lands in the no-preview block.
+ const search = await getRenderedContent(buildNote({ title: "Saved2", type: "search" }));
+
+ expect(loadSpy).not.toHaveBeenCalled();
+ expect(embeddedNoteListComponent).not.toHaveBeenCalled();
+ expect(book.$renderedContent.find(".from-render-text").length).toBe(1);
+ expect(search.$renderedContent.hasClass("no-preview")).toBe(true);
+ expect(search.$renderedContent.find(".rendered-collection").length).toBe(0);
loadSpy.mockRestore();
});
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.ts
index 02b077e426..2b625b67be 100644
--- a/apps/client/src/services/content_renderer.ts
+++ b/apps/client/src/services/content_renderer.ts
@@ -2,7 +2,7 @@ import "./content_renderer.css";
import { normalizeMimeTypeForCKEditor, type TextRepresentationResponse } from "@triliumnext/commons";
import DOMPurify from "dompurify";
-import { h, render } from "preact";
+import { h, type JSX, render } from "preact";
import FAttachment from "../entities/fattachment.js";
import FNote from "../entities/fnote.js";
@@ -57,7 +57,13 @@ export async function getRenderedContent(this: {} | { ctx: string }, entity: FNo
const $renderedContent = $('<div class="rendered-content">');
- if (type === "text" || type === "book") {
+ if ((type === "book" || type === "search") && options.interactive && !options.tooltip
+ && entity instanceof FNote && entity.getLabelValue("viewType") !== "dashboard") {
+ // Render the live collection view (grid/table/board/calendar/map/presentation). The dashboard
+ // view type is excluded: it's the only view that re-propagates `interactive` to its tiles, so
+ // skipping it here is what keeps an embedded collection from recursing into itself.
+ await renderCollection(entity, $renderedContent);
+ } else if (type === "text" || type === "book") {
await renderText(entity, $renderedContent, options);
} else if (type === "markdown") {
await renderMarkdown(entity, $renderedContent, options);
@@ -87,8 +93,6 @@ export async function getRenderedContent(this: {} | { ctx: string }, entity: FNo
$renderedContent.append($("<div>").append("<div>This note is protected and to access it you need to enter password.</div>").append("<br/>").append($button));
} else if (type === "webView" && options.interactive && !options.tooltip && entity instanceof FNote && entity.hasLabel("webViewSrc")) {
await renderWebView(entity, $renderedContent);
- } else if (type === "search" && options.interactive && !options.tooltip && entity instanceof FNote) {
- await renderSearch(entity, $renderedContent);
} else if (entity instanceof FNote) {
$renderedContent.addClass("no-preview");
$renderedContent.append(
@@ -349,7 +353,7 @@ async function renderWebView(note: FNote, $renderedContent: JQuery<HTMLElement>)
const $container = $('<div class="note-detail-web-view">');
const container = $container.get(0);
if (container) {
- render(h(WebView, {
+ await mountInteractiveWidget(h(WebView, {
note,
ntxId: undefined,
viewScope: undefined,
@@ -361,30 +365,49 @@ async function renderWebView(note: FNote, $renderedContent: JQuery<HTMLElement>)
}
/**
- * Executes a saved search and mounts the live {@link SearchNoteList} — the same results widget used
- * in the note detail — into the rendered content. Used by interactive contexts (e.g. the dashboard
- * and included notes) that opt in via {@link RenderOptions.interactive}; every other context keeps
- * the static no-preview fallback. Loaded lazily so the collection views (and their dependencies) are
- * only pulled in when a saved search is actually embedded.
+ * Mounts an interactive embedded widget (web view, collection) through the Trilium event bridge.
+ * Wrapping it in a {@link ParentComponent} provider connected to {@link appContext} is what lets its
+ * `useTriliumEvent` subscriptions actually receive events — a bare standalone Preact root has no
+ * parent component in context, so otherwise e.g. an embedded collection never reacts to new notes.
*/
-async function renderSearch(note: FNote, $renderedContent: JQuery<HTMLElement>) {
- const [ { SearchNoteList }, { default: froca } ] = await Promise.all([
+async function mountInteractiveWidget(vnode: JSX.Element, container: HTMLElement) {
+ const [ { renderReactWidgetAtElement }, { default: appContext } ] = await Promise.all([
+ import("../widgets/react/react_utils"),
+ import("../components/app_context")
+ ]);
+ renderReactWidgetAtElement(appContext, vnode, container);
+}
+
+/**
+ * Mounts a collection — a book or a saved search — as the live {@link EmbeddedNoteList}, the same
+ * results widget used in the note detail (grid/list/table/board/calendar/map/presentation). Used by
+ * interactive contexts (e.g. the dashboard and included notes) that opt in via
+ * {@link RenderOptions.interactive}; every other context keeps the static fallback. Loaded lazily so
+ * the collection views (and their dependencies) are only pulled in when a collection is embedded.
+ */
+async function renderCollection(note: FNote, $renderedContent: JQuery<HTMLElement>) {
+ const [ { EmbeddedNoteList }, { default: froca } ] = await Promise.all([
import("../widgets/collections/NoteList"),
import("./froca.js")
]);
- // A saved search must run server-side before its (virtual) result children exist.
- await froca.loadSearchNote(note.noteId);
+ // A saved search must run server-side before its (virtual) result children exist; a book already
+ // has real children, so it skips execution.
+ if (note.type === "search") {
+ await froca.loadSearchNote(note.noteId);
+ }
- const $container = $('<div class="rendered-search">');
+ const $container = $('<div class="rendered-collection">');
const container = $container.get(0);
if (container) {
- render(h(SearchNoteList, {
+ await mountInteractiveWidget(h(EmbeddedNoteList, {
note,
notePath: note.getBestNotePathString(),
ntxId: undefined,
media: "screen",
- highlightedTokens: note.highlightedTokens
+ highlightedTokens: note.highlightedTokens,
+ // Search results get text-representation (highlighted snippets); books don't.
+ showTextRepresentation: note.type === "search"
}), container);
}
$renderedContent.append($container);
diff --git a/apps/client/src/widgets/collections/NoteList.tsx b/apps/client/src/widgets/collections/NoteList.tsx
index a0bd213472..e111831731 100644
--- a/apps/client/src/widgets/collections/NoteList.tsx
+++ b/apps/client/src/widgets/collections/NoteList.tsx
@@ -70,8 +70,17 @@ export default function NoteList(props: Pick<NoteListProps, "displayOnlyCollecti
}
export function SearchNoteList(props: Omit<NoteListProps, "isEnabled" | "viewType">) {
+ return <EmbeddedNoteList {...props} showTextRepresentation />;
+}
+
+/**
+ * Mounts a collection — a book or an (already-executed) saved search — as a live, prop-driven note
+ * list, picking the view type from the note's `viewType` label. Unlike {@link NoteList} it reads no
+ * note context, so it can be embedded outside the note detail (e.g. dashboard tiles, included notes).
+ */
+export function EmbeddedNoteList(props: Omit<NoteListProps, "isEnabled" | "viewType">) {
const viewType = useNoteViewType(props.note);
- return <CustomNoteList {...props} isEnabled={true} viewType={viewType} showTextRepresentation />;
+ return <CustomNoteList {...props} isEnabled={true} viewType={viewType} />;
}
export function CustomNoteList({ note, viewType, isEnabled: shouldEnable, notePath, highlightedTokens, displayOnlyCollections, ntxId, onReady, onProgressChanged, ...restProps }: NoteListProps) {