-
Preview goes here
+
+ {previewContent}
+
{previewButtons}
diff --git a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx
index a1362313a..8f45d806c 100644
--- a/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx
+++ b/apps/client/src/widgets/type_widgets/helpers/SvgSplitEditor.tsx
@@ -1,10 +1,43 @@
+import { useState } from "preact/hooks";
import { t } from "../../../services/i18n";
import SplitEditor, { PreviewButton, SplitEditorProps } from "./SplitEditor";
+import { RawHtmlBlock } from "../../react/RawHtml";
+
+interface SvgSplitEditorProps extends Omit
{
+ /**
+ * Called upon when the SVG preview needs refreshing, such as when the editor has switched to a new note or the content has switched.
+ *
+ * The method must return a valid SVG string that will be automatically displayed in the preview.
+ *
+ * @param content the content of the note, in plain text.
+ */
+ renderSvg(content: string): string | Promise;
+}
+
+export default function SvgSplitEditor({ renderSvg, ...props }: SvgSplitEditorProps) {
+ const [ svg, setSvg ] = useState();
+ const [ error, setError ] = useState();
+
+ async function onContentChanged(content: string) {
+ try {
+ const svg = await renderSvg(content);
+
+ // Rendering was successful.
+ setError(null);
+ setSvg(svg);
+ } catch (e) {
+ // Rendering failed.
+ setError((e as Error)?.message);
+ }
+ }
-export default function SvgSplitEditor(props: SplitEditorProps) {
return (
+ )}
previewButtons={
<>
;
-
/**
* Called to obtain the name of the note attachment (without .svg extension) that will be used for storing the preview.
*/
diff --git a/apps/client/src/widgets/type_widgets_old/mermaid.ts b/apps/client/src/widgets/type_widgets_old/mermaid.ts
index c8642c993..f3ef73abe 100644
--- a/apps/client/src/widgets/type_widgets_old/mermaid.ts
+++ b/apps/client/src/widgets/type_widgets_old/mermaid.ts
@@ -2,8 +2,6 @@ import type { EditorConfig } from "@triliumnext/codemirror";
import { getMermaidConfig, loadElkIfNeeded, postprocessMermaidSvg } from "../../services/mermaid.js";
import AbstractSvgSplitTypeWidget from "./abstract_svg_split_type_widget.js";
-let idCounter = 1;
-let registeredErrorReporter = false;
export class MermaidTypeWidget extends AbstractSvgSplitTypeWidget {
@@ -16,21 +14,7 @@ export class MermaidTypeWidget extends AbstractSvgSplitTypeWidget {
}
async renderSvg(content: string) {
- const mermaid = (await import("mermaid")).default;
- await loadElkIfNeeded(mermaid, content);
- if (!registeredErrorReporter) {
- // (await import("./linters/mermaid.js")).default();
- registeredErrorReporter = true;
- }
- mermaid.initialize({
- startOnLoad: false,
- ...(getMermaidConfig() as any),
- });
-
- idCounter++;
- const { svg } = await mermaid.render(`mermaid-graph-${idCounter}`, content);
- return postprocessMermaidSvg(svg);
}
}