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
feat(collections): embed live web views in dashboard tiles
Elian Dorancecfcc93eb19
diff --git a/apps/client/src/services/content_renderer.css b/apps/client/src/services/content_renderer.cssindex 9c0d45ff13..a4ae38080c 100644--- a/apps/client/src/services/content_renderer.css+++ b/apps/client/src/services/content_renderer.css@@ -13,3 +13,8 @@margin-bottom: 0;}}++/* An interactive embedded web view (RenderOptions.interactive) fills the height its host provides. */+.rendered-content:has(> .note-detail-web-view) {+ height: 100%;+}diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.tsindex d16b491eb2..0b4bd40b96 100644--- a/apps/client/src/services/content_renderer.spec.ts+++ b/apps/client/src/services/content_renderer.spec.ts@@ -68,6 +68,9 @@ vi.mock("mermaid", () => ({const pdfViewerComponent = vi.fn(() => null);vi.mock("../widgets/type_widgets/file/PdfViewer", () => ({ default: pdfViewerComponent }));+const webViewComponent = vi.fn(() => null);+vi.mock("../widgets/type_widgets/WebView", () => ({ default: webViewComponent }));+// `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.vi.mock("dompurify", () => ({ default: { sanitize: (s: string) => s, addHook: () => {} } }));@@ -437,6 +440,37 @@ describe("generic FNote fallback / webView", () => {expect($renderedContent.find(".webview-footer").length).toBe(0);expect($renderedContent.hasClass("no-preview")).toBe(true);});++ it("mounts the live WebView widget when interactive (outside a tooltip)", async () => {+ const note = buildNote({ title: "WI", type: "webView", "#webViewSrc": "https://example.com" });+ const { type, $renderedContent } = await getRenderedContent(note, { interactive: true });+ expect(type).toBe("webView");+ expect(webViewComponent).toHaveBeenCalledOnce();+ expect(webViewComponent.mock.calls[0]?.[0]).toMatchObject({ note });+ expect($renderedContent.find(".note-detail-web-view").length).toBe(1);+ // The live embed replaces the static fallback.+ expect($renderedContent.find(".webview-footer").length).toBe(0);+ expect($renderedContent.hasClass("no-preview")).toBe(false);+ });++ it("keeps the static fallback in a tooltip or without a src, even when interactive", async () => {+ // Tooltips never embed the live widget, even with interactive set.+ const tip = await getRenderedContent(+ buildNote({ title: "WT", type: "webView", "#webViewSrc": "https://example.com" }),+ { interactive: true, tooltip: true }+ );+ // Interactive set, but the note has no configured src.+ const noSrc = await getRenderedContent(+ buildNote({ title: "WN", type: "webView" }),+ { interactive: true }+ );++ expect(webViewComponent).not.toHaveBeenCalled();+ expect(tip.$renderedContent.find(".note-detail-web-view").length).toBe(0);+ expect(tip.$renderedContent.find(".webview-footer").length).toBe(1);+ expect(noSrc.$renderedContent.find(".note-detail-web-view").length).toBe(0);+ expect(noSrc.$renderedContent.hasClass("no-preview")).toBe(true);+ });});describe("defensive guards for entities that are neither FNote nor FAttachment", () => {diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex d4fdf49ace..85441da1c6 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -33,6 +33,12 @@ export interface RenderOptions {/** Set of note IDs that have already been seen during rendering to prevent infinite recursion. */seenNoteIds?: Set<string>;showTextRepresentation?: boolean;+ /**+ * If enabled, note types that have a richer live representation (currently only web views) are+ * mounted as their interactive type widget instead of a static preview/placeholder. Off by+ * default and intentionally left off for lightweight previews such as tooltips and the note list.+ */+ interactive?: boolean;}const CODE_MIME_TYPES = new Set(["application/json"]);@@ -79,6 +85,8 @@ export async function getRenderedContent(this: {} | { ctx: string }, entity: FNoconst $button = $(`<button class="btn btn-sm"><span class="tn-icon bx bx-log-in"></span> Enter protected session</button>`).on("click", protectedSessionService.enterProtectedSession);$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 (entity instanceof FNote) {$renderedContent.addClass("no-preview");$renderedContent.append(@@ -328,6 +336,28 @@ async function renderMermaid(note: FNote | FAttachment, $renderedContent: JQuery}}+/**+ * Mounts the live {@link WebView} type widget — an Electron `<webview>` or a sandboxed `<iframe>` —+ * into the rendered content. Used by interactive contexts (e.g. the dashboard) that opt in via+ * {@link RenderOptions.interactive}; every other context keeps the static "open externally" fallback.+ * Loaded lazily so the widget (and its dependencies) are only pulled in when a web view is embedded.+ */+async function renderWebView(note: FNote, $renderedContent: JQuery<HTMLElement>) {+ const WebView = (await import("../widgets/type_widgets/WebView")).default;+ const $container = $('<div class="note-detail-web-view">');+ const container = $container.get(0);+ if (container) {+ render(h(WebView, {+ note,+ ntxId: undefined,+ viewScope: undefined,+ parentComponent: undefined,+ noteContext: undefined+ }), container);+ }+ $renderedContent.append($container);+}+function getRenderingType(entity: FNote | FAttachment) {let type: string = "";if ("type" in entity) {diff --git a/apps/client/src/widgets/collections/dashboard/index.css b/apps/client/src/widgets/collections/dashboard/index.cssindex 563479e708..b217954bfa 100644--- a/apps/client/src/widgets/collections/dashboard/index.css+++ b/apps/client/src/widgets/collections/dashboard/index.css@@ -103,7 +103,8 @@&.type-canvas,&.type-video,&.type-code,- &.type-mindMap {+ &.type-mindMap,+ &.type-webView {padding: 0;.rendered-content {diff --git a/apps/client/src/widgets/collections/dashboard/index.tsx b/apps/client/src/widgets/collections/dashboard/index.tsxindex f9cb5b0f5d..0a9a1a271a 100644--- a/apps/client/src/widgets/collections/dashboard/index.tsx+++ b/apps/client/src/widgets/collections/dashboard/index.tsx@@ -346,6 +346,7 @@ function DashboardWidget({ note, parentNote, highlightedTokens, includeArchived,<NoteContent key={refreshKey}note={note}trim+ interactivehighlightedTokens={highlightedTokens}includeArchivedNotes={includeArchived}showTextRepresentation={showTextRepresentation} />diff --git a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsxindex ed4ff377a4..d77cc4a1ca 100644--- a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx+++ b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx@@ -207,13 +207,15 @@ function NoteAttributes({ note }: { note: FNote }) {return <span className="note-list-attributes" ref={ref} />;}-export function NoteContent({ note, trim, noChildrenList, highlightedTokens, includeArchivedNotes, showTextRepresentation, onReady }: {+export function NoteContent({ note, trim, noChildrenList, highlightedTokens, includeArchivedNotes, showTextRepresentation, interactive, onReady }: {note: FNote;trim?: boolean;noChildrenList?: boolean;highlightedTokens: string[] | null | undefined;includeArchivedNotes: boolean;showTextRepresentation?: boolean;+ /** Render live interactive type widgets (e.g. web views) instead of static previews. */+ interactive?: boolean;onReady?: () => void;}) {const contentRef = useRef<HTMLDivElement>(null);@@ -242,7 +244,8 @@ export function NoteContent({ note, trim, noChildrenList, highlightedTokens, incnoChildrenList,noIncludedNotes: true,includeArchivedNotes,- showTextRepresentation+ showTextRepresentation,+ interactive}).then(({ $renderedContent, type }) => {if (!contentRef.current) return;