Skip to content

Commit 27ecef1

Browse files
authored
fix(Mermaid): support configurable mermaid theme via options (#982)
1 parent f0656aa commit 27ecef1

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

demo/src/components/Playground.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ export const Playground = memo<PlaygroundProps>((props) => {
206206
enabled: storyAdditionalControls?.mermaidAutoSaveEnabled ?? true,
207207
delay: storyAdditionalControls?.mermaidAutoSaveDelay ?? 1000,
208208
},
209+
theme: {dark: 'dark', light: 'forest'},
209210
})
210211
.use(FoldingHeading)
211212
.use(YfmHtmlBlock, {

packages/editor/src/extensions/additional/Mermaid/MermaidNodeView/MermaidView.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ import './Mermaid.scss';
2626

2727
const b = cnMermaid;
2828

29-
const MermaidPreview: React.FC<{mermaidInstance: Mermaid | null; text: string}> = ({
30-
mermaidInstance,
31-
text = '',
32-
}) => {
29+
const MermaidPreview: React.FC<{
30+
mermaidInstance: Mermaid | null;
31+
text: string;
32+
options: MermaidOptions;
33+
}> = ({mermaidInstance, text = '', options}) => {
3334
const [svg, setSvg] = useState<string>();
3435
const [error, setError] = useState<string | null>(null);
3536

@@ -42,7 +43,11 @@ const MermaidPreview: React.FC<{mermaidInstance: Mermaid | null; text: string}>
4243
// Validates syntax and throws error if text is invalid
4344
await mermaidInstance.parse(text);
4445

45-
mermaidInstance.initialize({theme: theme === 'dark' ? 'dark' : 'forest'});
46+
if (options.theme) {
47+
mermaidInstance.initialize({
48+
theme: theme === 'dark' ? options.theme.dark : options.theme.light,
49+
});
50+
}
4651

4752
const {svg: S} = await mermaidInstance.render(`mermaid-${Date.now()}`, text);
4853

@@ -55,7 +60,7 @@ const MermaidPreview: React.FC<{mermaidInstance: Mermaid | null; text: string}>
5560
};
5661

5762
p();
58-
}, [mermaidInstance, text, theme]);
63+
}, [mermaidInstance, text, theme, options.theme]);
5964

6065
if (error) {
6166
return <div className={b('Error')}>{error && <div>{error}</div>}</div>;
@@ -74,17 +79,17 @@ const DiagramEditMode: React.FC<{
7479
onSave: (v: string) => void;
7580
onCancel: () => void;
7681
options: MermaidOptions;
77-
}> = ({initialText, onSave, onCancel, mermaidInstance, options: {autoSave}}) => {
82+
}> = ({initialText, onSave, onCancel, mermaidInstance, options}) => {
7883
const {value, handleChange, handleManualSave, isSaveDisabled, isAutoSaveEnabled} = useAutoSave({
7984
initialValue: initialText || '',
8085
onSave,
8186
onClose: onCancel,
82-
autoSave,
87+
autoSave: options.autoSave,
8388
});
8489

8590
return (
8691
<div className={b()}>
87-
<MermaidPreview mermaidInstance={mermaidInstance} text={value} />
92+
<MermaidPreview mermaidInstance={mermaidInstance} text={value} options={options} />
8893
<div className={b('Editor')}>
8994
<div>
9095
<TextArea
@@ -173,6 +178,7 @@ export const MermaidView: React.FC<{
173178
<MermaidPreview
174179
mermaidInstance={mermaidInstance}
175180
text={node.attrs[MermaidConsts.NodeAttrs.content]}
181+
options={options}
176182
/>
177183
<div>
178184
<Button

packages/editor/src/extensions/additional/Mermaid/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type {MermaidConfig} from 'mermaid' with {'resolution-mode': 'import'};
2+
13
import type {Action, ExtensionAuto, ExtensionDeps, NodeViewConstructor} from '../../../core';
24

35
import {WMermaidNodeView} from './MermaidNodeView';
@@ -11,6 +13,10 @@ export type MermaidOptions = {
1113
enabled: boolean;
1214
delay?: number;
1315
};
16+
theme?: {
17+
dark: MermaidConfig['theme'];
18+
light: MermaidConfig['theme'];
19+
};
1420
};
1521

1622
export const Mermaid: ExtensionAuto<MermaidOptions> = (builder, options) => {

0 commit comments

Comments
 (0)