forked from accordproject/template-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdownEditor.tsx
More file actions
77 lines (67 loc) · 1.93 KB
/
MarkdownEditor.tsx
File metadata and controls
77 lines (67 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { lazy, Suspense, useMemo, useCallback, useEffect } from "react";
import useAppStore from "../store/store";
import { useMonaco } from "@monaco-editor/react";
const MonacoEditor = lazy(() =>
import("@monaco-editor/react").then((mod) => ({ default: mod.Editor }))
);
export default function MarkdownEditor({
value,
onChange,
}: {
value: string;
onChange?: (value: string | undefined) => void;
}) {
const backgroundColor = useAppStore((state) => state.backgroundColor);
const textColor = useAppStore((state) => state.textColor);
const monaco = useMonaco();
const themeName = useMemo(
() => (backgroundColor ? "darkTheme" : "lightTheme"),
[backgroundColor]
);
useEffect(() => {
if (monaco) {
const defineTheme = (name: string, base: "vs" | "vs-dark") => {
monaco.editor.defineTheme(name, {
base,
inherit: true,
rules: [],
colors: {
"editor.background": backgroundColor,
"editor.foreground": textColor,
"editor.lineHighlightBorder": "#EDE8DC",
},
});
};
defineTheme("lightTheme", "vs");
defineTheme("darkTheme", "vs-dark");
monaco.editor.setTheme(themeName);
}
}, [monaco, backgroundColor, textColor, themeName]);
const editorOptions = {
minimap: { enabled: false },
wordWrap: "on" as const,
automaticLayout: true,
scrollBeyondLastLine: false,
};
const options = useMemo(() => editorOptions, []);
const handleChange = useCallback(
(val: string | undefined) => {
if (onChange) onChange(val);
},
[onChange]
);
return (
<div className="editorwrapper">
<Suspense fallback={<div>Loading Editor...</div>}>
<MonacoEditor
options={options}
language="markdown"
height="60vh"
value={value}
onChange={handleChange}
theme={themeName}
/>
</Suspense>
</div>
);
}