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
refactor(client): share the source pane, box heights and corners of embeds
Elian Doran65128a31cd00
diff --git a/.claude/skills/developing-content-embeds/SKILL.md b/.claude/skills/developing-content-embeds/SKILL.mdindex 6579470dd4..23024099f0 100644--- a/.claude/skills/developing-content-embeds/SKILL.md+++ b/.claude/skills/developing-content-embeds/SKILL.md@@ -124,8 +124,12 @@ embeds receive `SourceEmbedProps` (`content`, `editor`) whichever storage the so2. Map the MIME to the content type in `getFileContentType()` and give it an icon in theattachment icon map next to it; add the content type to the list in `content_renderer.ts`.3. Write the component on the note type's parts (see the rule above). For a split source/preview- view, `SplitLayout` + `useMountedPanes()` + `CodeEmbedEditor` give the layout, the divider, the- error card and the stale badge for free.+ view, `SplitLayout` + `useMountedPanes()` + `EmbedSourcePane` (in `code/CodeEmbedEditor.tsx`)+ give the layout, the divider, the error card, the stale badge and a source pane that keeps its+ undo history behind the preview. Delay re-rendering with `useDebouncedValue()`. Give the root+ the `include-note-box-height` class for the 10/20/50em heights per box size and full height in+ fullscreen, and round it with `--_include-note-content-*-radius` (edge to edge) or+ `--_include-note-framed-*-radius` (inside the 2px frame), all in `style.css`.4. An insert command: `INSERT_INLINE_EMBED_COMMAND` with `{ language, source }` (see`mermaid_ui.ts` and `slash_commands.ts`). New embeds are editable, untitled and Medium.5. Read-only rendering needs nothing more: `applySourceEmbeds()` in `ReadOnlyText.tsx` picks up anydiff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex 1fec7b21ce..0ebfa13421 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -638,7 +638,7 @@ async function renderCanvasDrawing(await CONTENT_RENDERERS.file.canvasDrawing();if (options.interactive && entity instanceof FAttachment) {- const $container = $('<div class="canvas-drawing">');+ const $container = $('<div class="canvas-drawing include-note-box-height">');const container = $container.get(0);if (container) {await mountInteractiveWidget(h(CanvasDrawing, {diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex 948c53a65d..32bb720d64 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -1144,6 +1144,22 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-tmax-height: none;}+.include-note-box-height {+ height: var(--include-note-height, 20em);+}++.include-note[data-box-size="small"] .include-note-box-height {+ height: var(--include-note-height, 10em);+}++.include-note[data-box-size="full"] .include-note-box-height {+ height: 50em;+}++.include-note-content:fullscreen .include-note-box-height {+ height: 100%;+}+/* Tiny and Full fit their content: only these box sizes take a resized size. */.include-note:is([data-box-size="small"], [data-box-size="medium"], [data-box-size="expandable"]) {margin-inline: auto;diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.cssindex c7db7fb2c8..5316b74fb5 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css@@ -14,7 +14,6 @@}.canvas-drawing {- height: var(--include-note-height, 20em);background-color: var(--canvas-drawing-background-color);/* Resets the typography inherited from the text note. */@@ -131,16 +130,6 @@}}-.include-note {- &[data-box-size="small"] .canvas-drawing {- height: var(--include-note-height, 10em);- }-- &[data-box-size="full"] .canvas-drawing {- height: 50em;- }-}-/* Excalidraw hides its overflow, so its rounded container clips what it draws. */.include-note-content:not(:fullscreen) .canvas-drawing {&, .excalidraw-container, .canvas-drawing-view {@@ -152,8 +141,6 @@}.include-note-content:fullscreen .canvas-drawing {- height: 100%;-/* Below the toolbar, clear of the "Exit fullscreen" button of the embed. */.Island.App-menu__left {top: calc(var(--lg-button-size) + 1rem);diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbedEditor.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbedEditor.tsxindex da0da680d3..2d106f31a3 100644--- a/apps/client/src/widgets/type_widgets/code/CodeEmbedEditor.tsx+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbedEditor.tsx@@ -7,7 +7,7 @@ import type { ContentEditor } from "../../../services/content_renderer";import { CodeMirrorFindTarget } from "../../find_in_code";import { useNoteLabelOptionalBool, useTriliumOptionBool } from "../../react/hooks";import { registerEmbedFindTarget } from "../text/content_embed_tools";-import { useEmbedCaretNavigation } from "../text/editable_embed";+import { CaretNavigationContext, useEmbedCaretNavigation } from "../text/editable_embed";import { CodeEditor } from "./Code";export interface CodeEmbedEditorProps {@@ -110,3 +110,44 @@ export default function CodeEmbedEditor({/>);}++export interface EmbedSourcePaneProps+ extends Omit<CodeEmbedEditorProps, "isShown" | "onClose" | "onChange"> {+ /** The root of the embed content, which the arrow keys enter the source from. */+ rootRef: RefObject<HTMLElement | null>;+ /** Whether the Editable toggle of the embed is on. */+ isEditing: boolean;+ /** Whether the display mode of the embed shows the source. */+ isSourceShown: boolean;+ /** Receives the content as it is edited, and when the editor goes away. */+ onContentChange: (content: string) => void;+}++/**+ * The source pane of an embed laid out by `SplitLayout`: a {@link CodeEmbedEditor} that edits the+ * source while `isEditing`, and shows it read-only otherwise.+ */+export function EmbedSourcePane({+ rootRef, editor, isEditing, isSourceShown, onContentChange, ...editorProps+}: EmbedSourcePaneProps) {+ return (+ <div className="note-detail-split-editor">+ {/* An edited source stays mounted while the rendering shows alone, which keeps its+ undo history. "Search in note" and the arrow keys skip it then. */}+ {(isEditing || isSourceShown) && (+ <CaretNavigationContext.Provider+ value={isEditing && isSourceShown ? rootRef : null}+ >+ <CodeEmbedEditor+ key={isEditing ? "editing" : "read-only"}+ editor={isEditing ? editor : undefined}+ isShown={isSourceShown}+ onClose={onContentChange}+ onChange={onContentChange}+ {...editorProps}+ />+ </CaretNavigationContext.Provider>+ )}+ </div>+ );+}diff --git a/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.css b/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.cssindex d42c7dbe0e..858ddf4b77 100644--- a/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.css+++ b/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.css@@ -10,10 +10,6 @@.markdown-embed-content {text-align: start;- &.with-source {- height: var(--include-note-height, 20em);- }-.editable-embed-preview {height: 100%;}@@ -26,17 +22,7 @@}}-.include-note {- &[data-box-size="small"] .markdown-embed-content.with-source {- height: var(--include-note-height, 10em);- }-- &[data-box-size="full"] .markdown-embed-content.with-source {- height: 50em;- }-}--.include-note-content:fullscreen :is(.markdown-embed, .markdown-embed-content.with-source) {+.include-note-content:fullscreen .markdown-embed {height: 100%;}diff --git a/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.tsx b/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.tsxindex 483992261a..b8562347d9 100644--- a/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/markdown/MarkdownEmbed.tsx@@ -12,13 +12,11 @@ import { isDesktop } from "../../../services/utils";import { useNoteCodeSettings } from "../../layout/code_menus";import { useDebouncedValue } from "../../react/hooks";import { SaveStatus, useSavedContent } from "../code/CodeEmbed";-import CodeEmbedEditor from "../code/CodeEmbedEditor";+import { EmbedSourcePane } from "../code/CodeEmbedEditor";import { resolveDisplayMode } from "../helpers/split_editor_mode";import { SplitLayout, useMountedPanes } from "../helpers/SplitEditor";import { registerContentEmbedTools, useContentEmbedDisplayMode } from "../text/content_embed_tools";-import {- CaretNavigationContext, EmbedPreview, useEditableEmbed, useEmbedPreview-} from "../text/editable_embed";+import { EmbedPreview, useEditableEmbed, useEmbedPreview } from "../text/editable_embed";import {renderWithSourceLines, useMarkdownKeymap, useNoteLinkChips, useSyncedHighlight,useSyncedScrolling@@ -70,40 +68,32 @@ export default function MarkdownEmbed({useSyncedHighlight(shownView, previewElement, renderedContent);useNoteLinkChips(shownView);useMarkdownKeymap(isEditing ? shownView : null);- const caretNavigationRoot = isEditing && isSourceShown ? rootRef : null;return (<divref={rootRef}- className={clsx("markdown-embed-content", isSourceShown && "with-source")}+ className={clsx(+ "markdown-embed-content", isSourceShown && "with-source include-note-box-height"+ )}><SplitLayoutmode={mode}orientation={isDesktop() ? "horizontal" : "vertical"}editor={(- <div className="note-detail-split-editor">- {/* An edited source stays mounted while the preview shows alone, which- keeps its undo history. "Search in note" and the arrow keys skip it- then. */}- {(isEditing || isSourceShown) && (- <CaretNavigationContext.Provider value={caretNavigationRoot}>- <CodeEmbedEditor- key={isEditing ? "editing" : "read-only"}- note={note}- editor={isEditing ? editor : undefined}- content={content}- mime={MARKDOWN_MIME}- tabWidth={tabWidth}- useTabs={useTabs}- viewRef={viewRef}- isShown={isSourceShown}- onClose={setContent}- onChange={setContent}- onReady={setView}- />- </CaretNavigationContext.Provider>- )}- </div>+ <EmbedSourcePane+ rootRef={rootRef}+ isEditing={isEditing}+ isSourceShown={isSourceShown}+ note={note}+ editor={editor}+ content={content}+ mime={MARKDOWN_MIME}+ tabWidth={tabWidth}+ useTabs={useTabs}+ viewRef={viewRef}+ onContentChange={setContent}+ onReady={setView}+ />)}isEditorMounted={editorMounted.current}isPreviewMounted={previewMounted.current}diff --git a/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.css b/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.cssindex b0a7f330a5..cc67679ab4 100644--- a/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.css+++ b/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.css@@ -1,5 +1,4 @@.mermaid-diagram {- height: var(--include-note-height, 20em);text-align: start;pre.code-embed-editor {@@ -8,36 +7,9 @@}}-.include-note {- &[data-box-size="small"] .mermaid-diagram {- height: var(--include-note-height, 10em);- }-- &[data-box-size="full"] .mermaid-diagram {- height: 50em;- }-}--.include-note-content:fullscreen .mermaid-diagram {- height: 100%;-}--.include-note {- /* The corners of the embed box that the diagram touches. */- --_mermaid-diagram-top-radius: 0;- --_mermaid-diagram-bottom-radius: var(--_include-note-bottom-border-radius, 8px);-- &[data-hide-title="true"]:not([data-box-size="tiny"], [data-box-size="expandable"]) {- --_mermaid-diagram-top-radius: var(--_include-note-top-border-radius, 8px);- }-- &:has(> figcaption) {- --_mermaid-diagram-bottom-radius: 0;- }-}-.include-note-content:not(:fullscreen) .mermaid-diagram {overflow: hidden;- border-radius: var(--_mermaid-diagram-top-radius) var(--_mermaid-diagram-top-radius)- var(--_mermaid-diagram-bottom-radius) var(--_mermaid-diagram-bottom-radius);+ border-radius: var(--_include-note-content-top-radius) var(--_include-note-content-top-radius)+ var(--_include-note-content-bottom-radius)+ var(--_include-note-content-bottom-radius);}diff --git a/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.tsx b/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.tsxindex 79b309992e..8cd0b86743 100644--- a/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.tsx+++ b/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.tsx@@ -14,6 +14,7 @@ import { t } from "../../../services/i18n";import { renderMermaidSvg } from "../../../services/mermaid";import type { SourceEmbedProps } from "../../../services/source_embeds";import { isMobile, openInAppHelpFromUrl } from "../../../services/utils";+import { useDebouncedValue } from "../../react/hooks";import { OverlayFullscreenButton } from "../../react/OverlayControlGroup";import { useZoomPanWheel } from "../../react/zoom_pan";import { useZoomPanKeyboard } from "../../react/zoom_pan_keyboard";@@ -33,9 +34,10 @@ import {useContentEmbedEvent,useNeedsFullscreenButton} from "../text/content_embed_tools";-import { CaretNavigationContext, useEditableEmbed } from "../text/editable_embed";+import { useEditableEmbed } from "../text/editable_embed";-const CodeEmbedEditor = lazy(() => import("../code/CodeEmbedEditor"));+const EmbedSourcePane = lazy(async () =>+ (await import("../code/CodeEmbedEditor")).EmbedSourcePane);/*** A Mermaid diagram in an embed, laid out by the `SplitLayout` and shown in the `SvgPreview` of@@ -81,39 +83,28 @@ export default function MermaidDiagram({ content, editor }: SourceEmbedProps) {}, []);return (- <div ref={rootRef} className="mermaid-diagram">+ <div ref={rootRef} className="mermaid-diagram include-note-box-height"><SplitLayoutclassName="svg-editor"mode={mode}orientation={isMobile() ? "vertical" : "horizontal"}editor={(- <div className="note-detail-split-editor">- {/* An edited source stays mounted while the diagram shows alone, which- keeps its undo history. "Search in note" and the arrow keys skip it- then. */}- {(isEditing || isSourceShown) && (- <CaretNavigationContext.Provider- value={isEditing && isSourceShown ? rootRef : null}- >- <Suspense fallback={null}>- <CodeEmbedEditor- key={isEditing ? "editing" : "read-only"}- note={null}- editor={isEditing ? editor : undefined}- content={source}- mime={MERMAID_ATTACHMENT_MIME}- tabWidth={undefined}- useTabs={undefined}- viewRef={viewRef}- isShown={isSourceShown}- onClose={setSource}- onChange={setSource}- onReady={setupEditor}- />- </Suspense>- </CaretNavigationContext.Provider>- )}- </div>+ <Suspense fallback={null}>+ <EmbedSourcePane+ rootRef={rootRef}+ isEditing={isEditing}+ isSourceShown={isSourceShown}+ note={null}+ editor={editor}+ content={source}+ mime={MERMAID_ATTACHMENT_MIME}+ tabWidth={undefined}+ useTabs={undefined}+ viewRef={viewRef}+ onContentChange={setSource}+ onReady={setupEditor}+ />+ </Suspense>)}isEditorMounted={editorMounted.current}isPreviewMounted={previewMounted.current}@@ -142,14 +133,11 @@ export default function MermaidDiagram({ content, editor }: SourceEmbedProps) {*/function useMermaidRender(source: string) {const { svg, error, render } = useSvgRender(renderMermaidSvg);- const isFirstRender = useRef(true);+ const renderedSource = useDebouncedValue(source, RENDER_DELAY);useEffect(() => {- const delay = isFirstRender.current ? 0 : RENDER_DELAY;- isFirstRender.current = false;- const timeout = setTimeout(() => render(source), delay);- return () => clearTimeout(timeout);- }, [ source ]);+ void render(renderedSource);+ }, [ renderedSource ]);return { svg, error };}