Skip to content

Build your personal knowledge base with Trilium Notes

fix(mermaid): preview not rendering when read-only (closes #8419)

Elian Doran17f3ffd00cb1
diff --git a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx
index 6553b650a9..237556b5e7 100644
--- a/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx
+++ b/apps/client/src/widgets/type_widgets/helpers/SplitEditor.tsx
@@ -8,7 +8,7 @@ import { DEFAULT_GUTTER_SIZE } from "../../../services/resizer";
import utils, { isMobile } from "../../../services/utils";
import ActionButton, { ActionButtonProps } from "../../react/ActionButton";
import Admonition from "../../react/Admonition";
-import { useNoteLabelBoolean, useTriliumOption } from "../../react/hooks";
+import { useNoteBlob, useNoteLabelBoolean, useTriliumOption } from "../../react/hooks";
import { EditableCode, EditableCodeProps } from "../code/Code";
export interface SplitEditorProps extends EditableCodeProps {
@@ -30,12 +30,22 @@ export interface SplitEditorProps extends EditableCodeProps {
* - Can display errors to the user via {@link setError}.
* - Horizontal or vertical orientation for the editor/preview split, adjustable via the switch split orientation button floating button.
*/
-export default function SplitEditor({ note, error, splitOptions, previewContent, previewButtons, className, editorBefore, forceOrientation, ...editorProps }: SplitEditorProps) {
- const splitEditorOrientation = useSplitOrientation(forceOrientation);
- const [ readOnly ] = useNoteLabelBoolean(note, "readOnly");
+export default function SplitEditor(props: SplitEditorProps) {
+ const [ readOnly ] = useNoteLabelBoolean(props.note, "readOnly");
+
+ if (readOnly) {
+ return <ReadOnlyView {...props} />;
+ }
+
+ return <EditorWithSplit {...props} />;
+
+}
+
+function EditorWithSplit({ note, error, splitOptions, previewContent, previewButtons, className, editorBefore, forceOrientation, ...editorProps }: SplitEditorProps) {
const containerRef = useRef<HTMLDivElement>(null);
+ const splitEditorOrientation = useSplitOrientation(forceOrientation);
- const editor = (!readOnly &&
+ const editor = (
<div className="note-detail-split-editor-col">
{editorBefore}
<div className="note-detail-split-editor">
@@ -53,19 +63,14 @@ export default function SplitEditor({ note, error, splitOptions, previewContent,
</div>
);
- const preview = (
- <div className="note-detail-split-preview-col">
- <div className={`note-detail-split-preview ${error ? "on-error" : ""}`}>
- {previewContent}
- </div>
- <div className="btn-group btn-group-sm map-type-switcher content-floating-buttons preview-buttons bottom-right" role="group">
- {previewButtons}
- </div>
- </div>
- );
+ const preview = <PreviewContainer
+ error={error}
+ previewContent={previewContent}
+ previewButtons={previewButtons}
+ />;
useEffect(() => {
- if (!utils.isDesktop() || !containerRef.current || readOnly) return;
+ if (!utils.isDesktop() || !containerRef.current) return;
const elements = Array.from(containerRef.current?.children) as HTMLElement[];
const splitInstance = Split(elements, {
rtl: glob.isRtl,
@@ -76,10 +81,10 @@ export default function SplitEditor({ note, error, splitOptions, previewContent,
});
return () => splitInstance.destroy();
- }, [ readOnly, splitEditorOrientation ]);
+ }, [ splitEditorOrientation ]);
return (
- <div ref={containerRef} className={`note-detail-split note-detail-printable ${`split-${splitEditorOrientation}`} ${readOnly ? "split-read-only" : ""} ${className ?? ""}`}>
+ <div ref={containerRef} className={`note-detail-split note-detail-printable ${`split-${splitEditorOrientation}`} ${className ?? ""}`}>
{splitEditorOrientation === "horizontal"
? <>{editor}{preview}</>
: <>{preview}{editor}</>}
@@ -87,6 +92,43 @@ export default function SplitEditor({ note, error, splitOptions, previewContent,
);
}
+function ReadOnlyView({ ...props }: SplitEditorProps) {
+ const { note, onContentChanged } = props;
+ const content = useNoteBlob(note);
+ const onContentChangedRef = useRef(onContentChanged);
+
+ useEffect(() => {
+ onContentChangedRef.current = onContentChanged;
+ });
+
+ useEffect(() => {
+ onContentChangedRef.current?.(content?.content ?? "");
+ }, [ content ]);
+
+ return (
+ <div className={`note-detail-split note-detail-printable ${props.className} split-read-only`}>
+ <PreviewContainer {...props} />
+ </div>
+ );
+}
+
+function PreviewContainer({ error, previewContent, previewButtons }: {
+ error?: string | null;
+ previewContent: ComponentChildren;
+ previewButtons?: ComponentChildren;
+}) {
+ return (
+ <div className="note-detail-split-preview-col">
+ <div className={`note-detail-split-preview ${error ? "on-error" : ""}`}>
+ {previewContent}
+ </div>
+ <div className="btn-group btn-group-sm map-type-switcher content-floating-buttons preview-buttons bottom-right" role="group">
+ {previewButtons}
+ </div>
+ </div>
+ );
+}
+
export function PreviewButton(props: Omit<ActionButtonProps, "titlePosition">) {
return <ActionButton
{...props}
diff --git a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx
index 9a1c1dd4e7..a587eba63a 100644
--- a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx
+++ b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx
@@ -1,13 +1,14 @@
+import { RefObject } from "preact";
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
+import svgPanZoom from "svg-pan-zoom";
+
import { t } from "../../../services/i18n";
-import SplitEditor, { PreviewButton, SplitEditorProps } from "./SplitEditor";
-import { RawHtmlBlock } from "../../react/RawHtml";
import server from "../../../services/server";
-import svgPanZoom from "svg-pan-zoom";
-import { RefObject } from "preact";
-import { useElementSize, useTriliumEvent } from "../../react/hooks";
-import utils from "../../../services/utils";
import toast from "../../../services/toast";
+import utils from "../../../services/utils";
+import { useElementSize, useTriliumEvent } from "../../react/hooks";
+import { RawHtmlBlock } from "../../react/RawHtml";
+import SplitEditor, { PreviewButton, SplitEditorProps } from "./SplitEditor";
interface SvgSplitEditorProps extends Omit<SplitEditorProps, "previewContent"> {
/**
@@ -144,7 +145,7 @@ export default function SvgSplitEditor({ ntxId, note, attachmentName, renderSvg,
}
{...props}
/>
- )
+ );
}
function useResizer(containerRef: RefObject<HTMLDivElement>, noteId: string, svg: string | undefined) {
@@ -181,7 +182,7 @@ function useResizer(containerRef: RefObject<HTMLDivElement>, noteId: string, svg
lastPanZoom.current = {
pan: zoomInstance.getPan(),
zoom: zoomInstance.getZoom()
- }
+ };
zoomRef.current = undefined;
zoomInstance.destroy();
};