Skip to content

Commit f6f0036

Browse files
authored
Merge pull request #7 from HealthSamurai/codemirror
Update CodeEditor. Support highlight, onChange, defaultValue props
2 parents 0245ce9 + 2fe7c07 commit f6f0036

1 file changed

Lines changed: 59 additions & 41 deletions

File tree

  • packages/react-components/src/components/code-editor

packages/react-components/src/components/code-editor/index.tsx

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,19 @@ const baseTheme = EditorView.baseTheme({
3737
backgroundColor: "var(--color-bg-primary)",
3838
height: "100%",
3939
width: "100%",
40+
fontSize: "14px",
41+
paddingTop: "8px",
42+
paddingBottom: "8px",
4043
},
4144
".cm-scroller": {
4245
overflow: "auto",
4346
},
4447
".cm-content": {
4548
fontFamily: "var(--font-family-mono)",
49+
padding: "0",
50+
},
51+
"&.cm-focused": {
52+
outline: "none",
4653
},
4754
".cm-gutter": {
4855
fontFamily: "var(--font-family-mono)",
@@ -51,6 +58,9 @@ const baseTheme = EditorView.baseTheme({
5158
backgroundColor: "var(--color-bg-primary)",
5259
border: "none",
5360
},
61+
".cm-lineNumbers": {
62+
paddingLeft: "16px",
63+
},
5464
".cm-activeLineGutter": {
5565
backgroundColor: "var(--color-bg-primary)",
5666
color: "var(--color-text-primary)",
@@ -68,7 +78,13 @@ const customHighlightStyle = HighlightStyle.define([
6878
{ tag: tags.null, color: "#569cd6" },
6979
]);
7080

71-
export function CodeEditor() {
81+
export function CodeEditor({
82+
defaultValue,
83+
onChange,
84+
}: {
85+
defaultValue?: string;
86+
onChange?: (value: string) => void;
87+
}) {
7288
const editorRef = React.useRef(null);
7389

7490
React.useEffect(() => {
@@ -78,46 +94,48 @@ export function CodeEditor() {
7894

7995
const view = new EditorView({
8096
parent: editorRef.current,
81-
82-
extensions: [
83-
lineNumbers(),
84-
foldGutter(),
85-
highlightSpecialChars(),
86-
history(),
87-
drawSelection(),
88-
dropCursor(),
89-
EditorState.allowMultipleSelections.of(true),
90-
indentOnInput(),
91-
syntaxHighlighting(customHighlightStyle),
92-
bracketMatching(),
93-
closeBrackets(),
94-
autocompletion(),
95-
rectangularSelection(),
96-
crosshairCursor(),
97-
highlightActiveLine(),
98-
highlightActiveLineGutter(),
99-
highlightSelectionMatches(),
100-
baseTheme,
101-
keymap.of([
102-
// Closed-brackets aware backspace
103-
...closeBracketsKeymap,
104-
// A large set of basic bindings
105-
...defaultKeymap,
106-
// Search-related keys
107-
...searchKeymap,
108-
// Redo/undo keys
109-
...historyKeymap,
110-
// Code folding bindings
111-
...foldKeymap,
112-
// Autocompletion keys
113-
...completionKeymap,
114-
// Keys related to the linter system
115-
...lintKeymap,
116-
]),
117-
json(),
118-
linter(jsonParseLinter(), { delay: 300 }),
119-
lintGutter(),
120-
],
97+
state: EditorState.create({
98+
doc: defaultValue ?? "",
99+
extensions: [
100+
// Все расширения должны быть здесь
101+
lineNumbers(),
102+
foldGutter(),
103+
highlightSpecialChars(),
104+
history(),
105+
drawSelection(),
106+
dropCursor(),
107+
EditorState.allowMultipleSelections.of(true),
108+
indentOnInput(),
109+
json(), // JSON должен быть перед syntaxHighlighting
110+
syntaxHighlighting(customHighlightStyle),
111+
bracketMatching(),
112+
closeBrackets(),
113+
autocompletion(),
114+
rectangularSelection(),
115+
crosshairCursor(),
116+
highlightActiveLine(),
117+
highlightActiveLineGutter(),
118+
highlightSelectionMatches(),
119+
baseTheme,
120+
keymap.of([
121+
...closeBracketsKeymap,
122+
...defaultKeymap,
123+
...searchKeymap,
124+
...historyKeymap,
125+
...foldKeymap,
126+
...completionKeymap,
127+
...lintKeymap,
128+
]),
129+
linter(jsonParseLinter(), { delay: 300 }),
130+
lintGutter(),
131+
// Listener для onChange
132+
EditorView.updateListener.of(update => {
133+
if (update.docChanged && onChange) {
134+
onChange(update.view.state.doc.toString());
135+
}
136+
})
137+
]
138+
}),
121139
});
122140

123141
return () => view.destroy();

0 commit comments

Comments
 (0)