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
68 lines (60 loc) · 1.74 KB
/
MarkdownEditor.tsx
File metadata and controls
68 lines (60 loc) · 1.74 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
import { useCallback, useMemo } from 'react';
import * as monaco from 'monaco-editor';
import Editor from '@monaco-editor/react';
import useAppStore from '../store/store';
interface MarkdownEditorProps {
value: string;
onChange: (value: string) => void;
}
export default function MarkdownEditor({ value, onChange }: MarkdownEditorProps) {
const backgroundColor = useAppStore((state) => state.backgroundColor);
const textColor = useAppStore((state) => state.textColor);
const themeName = useMemo(
() => (backgroundColor ? 'darkTheme' : 'lightTheme'),
[backgroundColor]
);
const handleEditorWillMount = useCallback((monaco: typeof import('monaco-editor')) => {
const defineTheme = (name: string, base: monaco.editor.BuiltinTheme) => {
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');
}, [backgroundColor, textColor]);
const options = useMemo(
() => ({
minimap: { enabled: false },
wordWrap: 'on' as 'on',
automaticLayout: true,
scrollBeyondLastLine: false,
}),
[]
);
const handleChange = useCallback(
(val?: string) => {
if (val !== undefined && onChange) onChange(val);
},
[onChange]
);
return (
<div className="editorwrapper">
<Editor
options= {options}
language="markdown"
height="60vh"
value={value}
onChange={handleChange}
beforeMount={handleEditorWillMount}
theme={themeName}
/>
</div>
);
}