-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathCodeEditor.js
More file actions
222 lines (207 loc) · 6.2 KB
/
CodeEditor.js
File metadata and controls
222 lines (207 loc) · 6.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import React, { useContext, useRef, useEffect } from 'react';
import PropTypes from 'prop-types';
import { ThemeContext, Box } from 'grommet';
import { EditorView, keymap } from '@codemirror/view';
import { EditorState } from '@codemirror/state';
import { javascript } from '@codemirror/lang-javascript';
import { defaultKeymap, indentWithTab } from '@codemirror/commands';
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language';
import { tags } from '@lezer/highlight';
import { prism } from 'grommet-theme-hpe';
export const CodeEditor = ({ code, onChange }) => {
const theme = useContext(ThemeContext);
const editorRef = useRef(null);
const viewRef = useRef(null);
const onChangeRef = useRef(onChange);
const isDark = theme.dark;
// Keep onChange ref updated without triggering recreation
useEffect(() => {
onChangeRef.current = onChange;
}, [onChange]);
useEffect(() => {
if (!editorRef.current) return undefined;
// Don't recreate if editor already exists with same theme mode
if (viewRef.current) {
return;
}
// Get HPE theme colors
const getColor = colorName => {
const colorValue = theme.global.colors[colorName];
if (typeof colorValue === 'object') {
return colorValue[isDark ? 'dark' : 'light'] || colorValue;
}
return colorValue || colorName;
};
// Get the HPE prism theme for syntax highlighting colors
const hpePrismTheme = isDark ? prism.dark : prism.light;
// Create HPE theme extension
const hpeTheme = EditorView.theme(
{
'&': {
height: '100%',
fontSize: '14px',
fontFamily:
'ui-monospace, SFMono-Regular, SF Mono, Consolas, Liberation Mono, Menlo, monospace',
},
'.cm-editor': {
backgroundColor: getColor('background-front'),
color: getColor('text-default'),
border: `1px solid ${getColor('border-weak')}`,
borderRadius: '4px',
},
'.cm-scroller': {
backgroundColor: 'transparent',
lineHeight: '1.5',
},
'.cm-content': {
backgroundColor: 'transparent',
color: getColor('text-default'),
padding: '12px',
caretColor: getColor('text-strong'),
minHeight: '100px',
},
'.cm-content[contenteditable="true"]': {
outline: 'none',
},
'.cm-line': {
padding: '0',
},
'.cm-gutters': {
display: 'none',
},
'.cm-focused': {
outline: `2px solid ${getColor('border-selected')}`,
outlineOffset: '-2px',
},
'&.cm-focused .cm-cursor': {
borderLeftColor: getColor('text-strong'),
borderLeftWidth: '2px',
},
'&.cm-focused .cm-selectionBackground, ::selection': {
backgroundColor: isDark
? 'rgba(0, 255, 135, 0.2)'
: 'rgba(0, 125, 96, 0.2)',
},
'.cm-selectionBackground': {
backgroundColor: isDark
? 'rgba(255, 255, 255, 0.1)'
: 'rgba(0, 0, 0, 0.1)',
},
},
{ dark: isDark },
);
// Create HPE syntax highlighting style using exact prism theme colors
const hpeSyntaxHighlighting = HighlightStyle.define([
// Comments
{
tag: tags.comment,
color: hpePrismTheme.comment?.color,
fontStyle: 'italic',
},
// Keywords (import, export, const, let, var, function, etc.)
{
tag: [
tags.keyword,
tags.controlKeyword,
tags.definitionKeyword,
tags.modifier,
tags.moduleKeyword,
],
color: hpePrismTheme.keyword?.color,
fontWeight: '500',
},
// Strings
{
tag: [tags.string, tags.special(tags.string)],
color: hpePrismTheme.string?.color,
},
// Numbers
{
tag: [tags.number, tags.literal],
color: hpePrismTheme.number?.color,
},
// Component names and functions
{
tag: [
tags.variableName,
tags.function(tags.variableName),
tags.definition(tags.variableName),
],
color: hpePrismTheme['maybe-class-name']?.color,
},
// JSX Tags
{
tag: [tags.tagName],
color: hpePrismTheme.keyword?.color,
fontWeight: '500',
},
// Attributes
{
tag: [tags.attributeName, tags.propertyName],
color: hpePrismTheme['attr-name']?.color,
},
// Operators
{
tag: [tags.operator],
color: hpePrismTheme.operator?.color,
},
// Punctuation
{
tag: [tags.punctuation, tags.separator, tags.bracket],
color:
hpePrismTheme['code[class*="language-"]']?.color ||
getColor('text-default'),
},
// Boolean values
{
tag: [tags.bool],
color: hpePrismTheme.boolean?.color,
},
]);
// Create editor state
const state = EditorState.create({
doc: code,
extensions: [
EditorView.editable.of(true),
hpeTheme,
syntaxHighlighting(hpeSyntaxHighlighting),
keymap.of([...defaultKeymap, indentWithTab]),
javascript({ jsx: true }),
EditorView.updateListener.of(update => {
if (update.docChanged && onChangeRef.current) {
onChangeRef.current(update.state.doc.toString());
}
}),
],
});
// Create editor view
const view = new EditorView({
state,
parent: editorRef.current,
});
viewRef.current = view;
return () => {
if (viewRef.current) {
viewRef.current.destroy();
viewRef.current = null;
}
};
}, [isDark]); // Only recreate when theme mode changes
// Handle external code changes
useEffect(() => {
if (viewRef.current && viewRef.current.state.doc.toString() !== code) {
viewRef.current.dispatch({
changes: {
from: 0,
to: viewRef.current.state.doc.length,
insert: code,
},
});
}
}, [code]);
return <Box ref={editorRef} style={{ height: '100%' }} />;
};
CodeEditor.propTypes = {
code: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
};