Files
docmost/apps/client/src/features/editor/components/code-block/mermaid-view.tsx
T
Alexander Schaber 32c7ecd9cf feat: set mermaid theme based on computed color scheme (#1397)
Use Mantine's `useComputedColorScheme` hook to dynamically configure mermaid's theme.
- When the computed color scheme is "light", the theme is set to "default".
- Otherwise, it is set to "dark".
2025-07-25 00:22:27 +01:00

57 lines
1.5 KiB
TypeScript

import { NodeViewProps } from "@tiptap/react";
import { useEffect, useState } from "react";
import mermaid from "mermaid";
import { v4 as uuidv4 } from "uuid";
import classes from "./code-block.module.css";
import { useTranslation } from "react-i18next";
import { useComputedColorScheme } from "@mantine/core";
const computedColorScheme = useComputedColorScheme();
mermaid.initialize({
startOnLoad: false,
suppressErrorRendering: true,
theme: computedColorScheme === "light" ? "default" : "dark",
});
interface MermaidViewProps {
props: NodeViewProps;
}
export default function MermaidView({ props }: MermaidViewProps) {
const { t } = useTranslation();
const { node } = props;
const [preview, setPreview] = useState<string>("");
useEffect(() => {
const id = `mermaid-${uuidv4()}`;
if (node.textContent.length > 0) {
mermaid
.render(id, node.textContent)
.then((item) => {
setPreview(item.svg);
})
.catch((err) => {
if (props.editor.isEditable) {
setPreview(
`<div class="${classes.error}">${t("Mermaid diagram error:")} ${err}</div>`,
);
} else {
setPreview(
`<div class="${classes.error}">${t("Invalid Mermaid diagram")}</div>`,
);
}
});
}
}, [node.textContent]);
return (
<div
className={classes.mermaid}
contentEditable={false}
dangerouslySetInnerHTML={{ __html: preview }}
></div>
);
}