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(canvas): embed notes via the content renderer in embeddables
Elian Doran0dc23baa6e7c
diff --git a/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx b/apps/client/src/widgets/type_widgets/canvas/Canvas.tsxindex 6ffe65c43f..1294c7efad 100644--- a/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx@@ -6,11 +6,13 @@ import { useCallback, useMemo, useRef } from "preact/hooks";import { type ExcalidrawImperativeAPI, type AppState } from "@excalidraw/excalidraw/types";import options from "../../../services/options";import "./Canvas.css";-import { NonDeletedExcalidrawElement } from "@excalidraw/excalidraw/element/types";+import { NonDeleted, NonDeletedExcalidrawElement, ExcalidrawEmbeddableElement } from "@excalidraw/excalidraw/element/types";import { goToLinkExt } from "../../../services/link";import useCanvasPersistence from "./persistence";import { LANGUAGE_MAPPINGS } from "./i18n";import { DISPLAYABLE_LOCALE_IDS } from "@triliumnext/commons";+import tree from "../../../services/tree";+import NoteEmbeddable from "./NoteEmbeddable";// currently required by excalidraw, in order to allows self-hosting fonts locally.// this avoids making excalidraw load the fonts from an external CDN.@@ -46,6 +48,18 @@ export default function Canvas({ note, noteContext }: TypeWidgetProps) {return goToLinkExt(nativeEvent, link, null);}, []);+ // Allow embeddables that point at a Trilium note (e.g. `root/<noteId>`), which Excalidraw would+ // otherwise reject as an unrecognized provider. Returning `undefined` defers to the default+ // whitelist so normal web embeds keep working.+ const validateEmbeddable = useCallback((link: string) => getNoteIdFromEmbeddableLink(link) ? true : undefined, []);++ // Render the note's content via the shared content renderer instead of the default iframe.+ // Returning `null` falls back to Excalidraw's built-in embeddable rendering.+ const renderEmbeddable = useCallback((element: NonDeleted<ExcalidrawEmbeddableElement>) => {+ const noteId = getNoteIdFromEmbeddableLink(element.link);+ return noteId ? <NoteEmbeddable noteId={noteId} /> : null;+ }, []);+return (<div className="canvas-render" onWheel={onWheel}><div className="excalidraw-wrapper">@@ -66,9 +80,29 @@ export default function Canvas({ note, noteContext }: TypeWidgetProps) {}}}onLinkOpen={onLinkOpen}+ validateEmbeddable={validateEmbeddable}+ renderEmbeddable={renderEmbeddable}{...persistence}/></div></div>)}++/**+ * Extracts a Trilium note ID from an embeddable's link, accepting the canonical note-path form+ * (`root/<noteId>` or `#root/<noteId>`). Returns `null` for anything else so non-note embeds are+ * left to Excalidraw's default handling.+ */+function getNoteIdFromEmbeddableLink(link: string | null) {+ if (!link) {+ return null;+ }++ const cleaned = link.replace(/^#/, "");+ if (!cleaned.startsWith("root/")) {+ return null;+ }++ return tree.getNoteIdFromUrl(cleaned);+}diff --git a/apps/client/src/widgets/type_widgets/canvas/NoteEmbeddable.css b/apps/client/src/widgets/type_widgets/canvas/NoteEmbeddable.cssnew file mode 100644index 0000000000..7963cecbe0--- /dev/null+++ b/apps/client/src/widgets/type_widgets/canvas/NoteEmbeddable.css@@ -0,0 +1,8 @@+.canvas-note-embeddable {+ width: 100%;+ height: 100%;+ overflow: auto;+ padding: 10px;+ background-color: var(--main-background-color);+ color: var(--main-text-color);+}diff --git a/apps/client/src/widgets/type_widgets/canvas/NoteEmbeddable.tsx b/apps/client/src/widgets/type_widgets/canvas/NoteEmbeddable.tsxnew file mode 100644index 0000000000..16249bf465--- /dev/null+++ b/apps/client/src/widgets/type_widgets/canvas/NoteEmbeddable.tsx@@ -0,0 +1,42 @@+import { useEffect, useRef } from "preact/hooks";+import content_renderer from "../../../services/content_renderer";+import froca from "../../../services/froca";+import "./NoteEmbeddable.css";++/**+ * Renders an arbitrary Trilium note inside an Excalidraw embeddable element, reusing the shared+ * {@link content_renderer}. Excalidraw hands us a sized box (via its `renderEmbeddable` prop); we+ * mount the rendered note content into it, opting into `interactive` so live note types+ * (collections, web views) behave the same as they do in an included note.+ */+export default function NoteEmbeddable({ noteId }: { noteId: string }) {+ const ref = useRef<HTMLDivElement>(null);++ useEffect(() => {+ let cancelled = false;+ const container = ref.current;+ if (!container) {+ return;+ }++ (async () => {+ const note = await froca.getNote(noteId);+ if (!note || cancelled) {+ return;+ }++ const { $renderedContent } = await content_renderer.getRenderedContent(note, { interactive: true });+ if (cancelled) {+ return;+ }++ container.replaceChildren(...$renderedContent.toArray());+ })();++ return () => {+ cancelled = true;+ };+ }, [noteId]);++ return <div ref={ref} className="canvas-note-embeddable ck-content" />;+}