-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathMarkdownEditor.tsx
More file actions
128 lines (115 loc) · 3.72 KB
/
MarkdownEditor.tsx
File metadata and controls
128 lines (115 loc) · 3.72 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { lazy, Suspense, useMemo, useCallback, useEffect } from "react";
import useAppStore from "../store/store";
import { useMonaco } from "@monaco-editor/react";
import { useCodeSelection } from "../hooks/useCodeSelection";
import CodeSelectionMenu from "../components/CodeSelectionMenu";
import type { editor } from "monaco-editor";
import { registerAutocompletion } from "../ai-assistant/autocompletion";
const MonacoEditor = lazy(() =>
import("@monaco-editor/react").then((mod) => ({ default: mod.Editor }))
);
export default function MarkdownEditor({
value,
onChange,
onEditorReady,
}: {
value: string;
onChange?: (value: string | undefined) => void;
onEditorReady?: (editor: editor.IStandaloneCodeEditor) => void;
}) {
const { handleSelection, closeMenu, showMenu, selectedText, menuPosition, enableCodeSelectionMenu } = useCodeSelection();
const { backgroundColor, textColor, aiConfig, showLineNumbers } = useAppStore((state) => ({
backgroundColor: state.backgroundColor,
textColor: state.textColor,
aiConfig: state.aiConfig,
showLineNumbers: state.showLineNumbers,
}));
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",
"editorGhostText.foreground": "#9c9a9a"
},
});
};
defineTheme("lightTheme", "vs");
defineTheme("darkTheme", "vs-dark");
monaco.editor.setTheme(themeName);
}
}, [monaco, backgroundColor, textColor, themeName]);
const editorOptions: editor.IStandaloneEditorConstructionOptions = useMemo(() => ({
minimap: { enabled: false },
wordWrap: "on" as const,
automaticLayout: true,
scrollBeyondLastLine: false,
lineNumbers: showLineNumbers ? 'on' : 'off',
inlineSuggest: {
enabled: aiConfig?.enableInlineSuggestions !== false,
mode: "prefix",
suppressSuggestions: false,
fontFamily: "inherit",
keepOnBlur: true,
},
suggest: {
preview: true,
showInlineDetails: true,
},
quickSuggestions: false,
suggestOnTriggerCharacters: false,
acceptSuggestionOnCommitCharacter: false,
acceptSuggestionOnEnter: "off",
tabCompletion: "off",
}), [aiConfig?.enableInlineSuggestions, showLineNumbers]);
const handleEditorDidMount = (editorInstance: editor.IStandaloneCodeEditor) => {
editorInstance.onDidChangeCursorSelection(() => {
handleSelection(editorInstance);
});
if (monaco) {
registerAutocompletion('markdown', monaco);
}
if (onEditorReady) {
onEditorReady(editorInstance);
}
};
const handleChange = useCallback(
(val: string | undefined) => {
if (onChange) onChange(val);
},
[onChange]
);
return (
<div className="editorwrapper h-full w-full">
<Suspense fallback={<div>Loading Editor...</div>}>
<MonacoEditor
options={editorOptions}
language="markdown"
height="100%"
value={value}
onMount={handleEditorDidMount}
onChange={handleChange}
theme={themeName}
/>
</Suspense>
{enableCodeSelectionMenu && showMenu && selectedText && menuPosition && (
<CodeSelectionMenu
selectedText={selectedText}
position={menuPosition}
onClose={closeMenu}
editorType="markdown"
/>
)}
</div>
);
}