@@ -34,22 +34,19 @@ const CodeEditorTopPanel = styled.div`
34
34
}
35
35
` ;
36
36
37
-
38
37
const ReadonlyOverlay = styled . div `
39
- position: absolute;
40
- top: 0;
41
- left: 0;
42
- height: 100%;
43
- width: 100%;
44
- background: rgba(0,0,0,0.5);
45
- z-index: 2;
46
- color: white;
47
- display: flex;
48
- align-items: center;
49
- justify-content: center;
50
-
51
- `
52
-
38
+ position: absolute;
39
+ top: 0;
40
+ left: 0;
41
+ height: 100%;
42
+ width: 100%;
43
+ background: ${ ( { theme } ) => theme . colorOverlayBg } ;
44
+ z-index: 2;
45
+ color: white;
46
+ display: flex;
47
+ align-items: center;
48
+ justify-content: center;
49
+ ` ;
53
50
54
51
const CodeEditorBottomPanel = styled . div `
55
52
height: ${ ( { theme } ) => theme . spacing07 } ;
@@ -75,9 +72,9 @@ const ButtonPrimary = styled.button`
75
72
cursor: pointer;
76
73
background: ${ ( { theme } ) => theme . colorPrimary600 } ;
77
74
}
78
- :disabled {
79
- color: #A0A0A6 ;
80
- background:${ ( { theme } ) => theme . colorDisabled } ;
75
+ :disabled {
76
+ color: #a0a0a6 ;
77
+ background: ${ ( { theme } ) => theme . colorDisabled } ;
81
78
cursor: not-allowed;
82
79
}
83
80
` ;
@@ -111,16 +108,15 @@ const CodeEditorWindow = styled.div`
111
108
top: 0;
112
109
left: 0;
113
110
position: fixed;
114
- overflow: hidden;
111
+ overflow: hidden;
115
112
` ;
116
113
117
114
const CodeEditorWindowDocked = styled . div `
118
115
height: 100%;
119
116
width: 100%;
120
117
position: static;
121
118
transform: translate(0px, 0px);
122
- overflow: hidden;
123
-
119
+ overflow: hidden;
124
120
` ;
125
121
126
122
const MonacoEditor = styled . div `
@@ -131,11 +127,10 @@ const MonacoEditor = styled.div`
131
127
position: relative;
132
128
` ;
133
129
134
-
135
130
export const CodeEditor = ( {
136
131
onRunEvalscriptClick,
137
132
portalId,
138
- defaultEditorTheme = ' dark' ,
133
+ defaultEditorTheme = " dark" ,
139
134
onChange,
140
135
value,
141
136
zIndex = 100 ,
@@ -144,22 +139,15 @@ export const CodeEditor = ({
144
139
themeDark = defaultThemeDark ,
145
140
runEvalscriptButtonText = "Run evalscript" ,
146
141
runningEvalscriptButtonText = "Running evalscript" ,
147
- readOnlyMessage = "Editor is in read only mode"
142
+ readOnlyMessage = "Editor is in read only mode" ,
148
143
} ) => {
149
-
150
-
151
144
const monacoEditorDOMRef = useRef ( ) ;
152
145
const monacoRef = useRef ( ) ;
153
146
const headerEditorRef = useRef ( ) ;
154
- const editorRef = useRef ( )
147
+ const editorRef = useRef ( ) ;
155
148
const editorWindowRef = useRef ( ) ;
156
- const [
157
- shouldTriggerRunEvalscriptAnimation ,
158
- setShouldTriggerRunEvalscriptAnimation ,
159
- ] = useState ( false ) ;
160
- const [ isDarkTheme , setIsDarkTheme ] = useState (
161
- defaultEditorTheme === "dark" ? true : false
162
- ) ;
149
+ const [ shouldTriggerRunEvalscriptAnimation , setShouldTriggerRunEvalscriptAnimation ] = useState ( false ) ;
150
+ const [ isDarkTheme , setIsDarkTheme ] = useState ( defaultEditorTheme === "dark" ? true : false ) ;
163
151
164
152
const {
165
153
editorPosition,
@@ -173,7 +161,6 @@ export const CodeEditor = ({
173
161
isFullscreen,
174
162
} = useFreeEditor ( editorWindowRef , headerEditorRef ) ;
175
163
176
-
177
164
useEffect ( ( ) => {
178
165
if ( shouldTriggerRunEvalscriptAnimation ) {
179
166
setTimeout ( ( ) => {
@@ -196,34 +183,27 @@ export const CodeEditor = ({
196
183
} ;
197
184
198
185
loader . init ( ) . then ( ( monaco ) => {
199
-
200
- if ( themeDark . name !== 'vs-dark' ) {
186
+ if ( themeDark . name !== "vs-dark" ) {
201
187
monaco . editor . defineTheme ( themeDark . name , {
202
- base : ' vs-dark' , // can also be vs-dark or hc-black
188
+ base : " vs-dark" , // can also be vs-dark or hc-black
203
189
inherit : true , // can also be false to completely replace the builtin rules
204
- rules : [
205
- { }
206
- ] ,
190
+ rules : [ { } ] ,
207
191
colors : {
208
- ' editor.background' : `${ themeDark . styles . colorBg500 } ` ,
209
- ' editor.lineHighlightBackground' : `${ themeDark . styles . colorBg400 } ` ,
210
- ' editor.lineHighlightBorder' : `${ themeDark . styles . colorBg400 } `
211
- }
192
+ " editor.background" : `${ themeDark . styles . colorBg500 } ` ,
193
+ " editor.lineHighlightBackground" : `${ themeDark . styles . colorBg400 } ` ,
194
+ " editor.lineHighlightBorder" : `${ themeDark . styles . colorBg400 } ` ,
195
+ } ,
212
196
} ) ;
213
197
}
214
198
215
-
216
-
217
- if ( themeLight . name !== 'vs' ) {
199
+ if ( themeLight . name !== "vs" ) {
218
200
monaco . editor . defineTheme ( themeLight . name , {
219
- base : 'vs' , // can also be vs-dark or hc-black
201
+ base : "vs" , // can also be vs-dark or hc-black
220
202
inherit : true , // can also be false to completely replace the builtin rules
221
- rules : [
222
- { }
223
- ] ,
203
+ rules : [ { } ] ,
224
204
colors : {
225
- ' editor.background' : `${ themeLight . styles . colorBg500 } `
226
- }
205
+ " editor.background" : `${ themeLight . styles . colorBg500 } ` ,
206
+ } ,
227
207
} ) ;
228
208
}
229
209
@@ -232,65 +212,58 @@ export const CodeEditor = ({
232
212
theme : isDarkTheme ? themeDark . name : themeLight . name ,
233
213
readOnly : isReadOnly ,
234
214
scrollbar : {
235
- alwaysConsumeMouseWheel : isDocked ? false : true
236
- }
215
+ alwaysConsumeMouseWheel : isDocked ? false : true ,
216
+ } ,
237
217
} ) ;
238
218
239
-
240
- const messageContribution = editorRef . current . getContribution ( 'editor.contrib.messageController' ) ;
219
+ const messageContribution = editorRef . current . getContribution ( "editor.contrib.messageController" ) ;
241
220
editorRef . current . onDidAttemptReadOnlyEdit ( ( ) => {
242
221
messageContribution . showMessage ( readOnlyMessage , editorRef . current . getPosition ( ) ) ;
243
222
} ) ;
244
223
245
224
monacoRef . current = monaco ;
246
225
editorRef . current . onDidChangeModelContent ( ( ) => {
247
- const code = editorRef . current . getValue ( ) ;
248
- onChange ( code )
226
+ const code = editorRef . current . getValue ( ) ;
227
+ onChange ( code ) ;
249
228
250
- checkAndApplyErrors ( )
229
+ checkAndApplyErrors ( ) ;
251
230
} ) ;
252
231
} ) ;
253
232
} , [ isDocked ] ) ;
254
233
255
-
256
234
useEffect ( ( ) => {
257
- if ( ! editorRef . current ) {
258
- return
235
+ if ( ! editorRef . current ) {
236
+ return ;
259
237
}
260
238
261
- const code = editorRef . current . getValue ( ) ;
239
+ const code = editorRef . current . getValue ( ) ;
262
240
if ( value !== code ) {
263
- editorRef . current . setValue ( value )
241
+ editorRef . current . setValue ( value ) ;
264
242
}
265
- } , [ value ] )
266
-
267
-
268
-
243
+ } , [ value ] ) ;
269
244
270
245
useEffect ( ( ) => {
271
246
if ( ! monacoRef . current ) {
272
- return
247
+ return ;
273
248
}
274
249
if ( isDarkTheme ) {
275
250
monacoRef . current . editor . setTheme ( themeDark . name ) ;
276
251
} else {
277
252
monacoRef . current . editor . setTheme ( themeLight . name ) ;
278
253
}
279
-
280
254
} , [ isDarkTheme ] ) ;
281
255
282
256
useEffect ( ( ) => {
283
-
284
257
if ( ! editorRef . current ) {
285
- return
258
+ return ;
286
259
}
287
260
288
261
if ( isReadOnly ) {
289
- editorRef . current . updateOptions ( { readOnly : true } )
262
+ editorRef . current . updateOptions ( { readOnly : true } ) ;
290
263
} else {
291
- editorRef . current . updateOptions ( { readOnly : false } )
264
+ editorRef . current . updateOptions ( { readOnly : false } ) ;
292
265
}
293
- } , [ isReadOnly , editorRef . current ] )
266
+ } , [ isReadOnly , editorRef . current ] ) ;
294
267
295
268
const debounce = useCallback ( ( func , wait , immediate ) => {
296
269
var timeout ;
@@ -314,11 +287,6 @@ export const CodeEditor = ({
314
287
} ;
315
288
} , [ ] ) ;
316
289
317
-
318
-
319
-
320
-
321
-
322
290
const checkAndApplyErrors = debounce ( function ( ) {
323
291
const code = editorRef . current . getValue ( ) ;
324
292
JSHINT ( code , JSHINT_CONFIG ) ;
@@ -334,26 +302,17 @@ export const CodeEditor = ({
334
302
: monacoRef . current . MarkerSeverity . Warning ,
335
303
} ;
336
304
} ) ;
337
- monacoRef . current . editor . setModelMarkers (
338
- editorRef . current . getModel ( ) ,
339
- "test" ,
340
- errors
341
- ) ;
305
+ monacoRef . current . editor . setModelMarkers ( editorRef . current . getModel ( ) , "test" , errors ) ;
342
306
} , 500 ) ;
343
307
344
308
function toggleTheme ( ) {
345
309
setIsDarkTheme ( ( prev ) => ! prev ) ;
346
310
}
347
311
348
-
349
312
if ( isDocked ) {
350
313
return (
351
314
< ThemeProvider
352
- theme = {
353
- isDarkTheme
354
- ? { ...variables , ...themeDark . styles }
355
- : { ...variables , ...themeLight . styles }
356
- }
315
+ theme = { isDarkTheme ? { ...variables , ...themeDark . styles } : { ...variables , ...themeLight . styles } }
357
316
>
358
317
< CodeEditorWindowDocked ref = { editorWindowRef } >
359
318
< CodeEditorTopPanel ref = { headerEditorRef } >
@@ -363,10 +322,7 @@ export const CodeEditor = ({
363
322
</ CodeEditorIcon >
364
323
</ CodeEditorTopPanel >
365
324
< MonacoEditor ref = { monacoEditorDOMRef } >
366
- { isReadOnly &&
367
- < ReadonlyOverlay isDarkTheme = { isDarkTheme } >
368
- </ ReadonlyOverlay >
369
- }
325
+ { isReadOnly && < ReadonlyOverlay isDarkTheme = { isDarkTheme } > </ ReadonlyOverlay > }
370
326
</ MonacoEditor >
371
327
</ CodeEditorWindowDocked >
372
328
</ ThemeProvider >
@@ -375,11 +331,7 @@ export const CodeEditor = ({
375
331
376
332
return ReactDOM . createPortal (
377
333
< ThemeProvider
378
- theme = {
379
- isDarkTheme
380
- ? { ...variables , ...themeDark . styles }
381
- : { ...variables , ...themeLight . styles }
382
- }
334
+ theme = { isDarkTheme ? { ...variables , ...themeDark . styles } : { ...variables , ...themeLight . styles } }
383
335
>
384
336
< CodeEditorWindow
385
337
zIndex = { zIndex }
@@ -389,10 +341,7 @@ export const CodeEditor = ({
389
341
width = { editorSize . width }
390
342
ref = { editorWindowRef }
391
343
>
392
- < CodeEditorTopPanel
393
- ref = { headerEditorRef }
394
- onMouseDown = { onEditorMove }
395
- >
344
+ < CodeEditorTopPanel ref = { headerEditorRef } onMouseDown = { onEditorMove } >
396
345
{ isDocked ? (
397
346
< ArrowsExpandIcon onClick = { handleDockedClick } />
398
347
) : (
@@ -413,15 +362,8 @@ export const CodeEditor = ({
413
362
</ >
414
363
) }
415
364
</ CodeEditorTopPanel >
416
- < MonacoEditor
417
- style = { { height : editorSize . height - 96 } }
418
- ref = { monacoEditorDOMRef }
419
- >
420
-
421
- { isReadOnly &&
422
- < ReadonlyOverlay isDarkTheme = { isDarkTheme } >
423
- </ ReadonlyOverlay >
424
- }
365
+ < MonacoEditor style = { { height : editorSize . height - 96 } } ref = { monacoEditorDOMRef } >
366
+ { isReadOnly && < ReadonlyOverlay isDarkTheme = { isDarkTheme } > </ ReadonlyOverlay > }
425
367
</ MonacoEditor >
426
368
< CodeEditorBottomPanel >
427
369
< ButtonPrimary
@@ -441,15 +383,12 @@ export const CodeEditor = ({
441
383
) }
442
384
</ ButtonPrimary >
443
385
444
- < CodeEditorIcon
445
- onMouseDown = { handleResizeMouseDown }
446
- style = { { cursor : "nwse-resize" , zIndex : 0 } }
447
- >
386
+ < CodeEditorIcon onMouseDown = { handleResizeMouseDown } style = { { cursor : "nwse-resize" , zIndex : 0 } } >
448
387
< CgArrowsExpandLeft />
449
388
</ CodeEditorIcon >
450
389
</ CodeEditorBottomPanel >
451
390
</ CodeEditorWindow >
452
391
</ ThemeProvider > ,
453
- document . getElementById ( portalId )
392
+ document . getElementById ( portalId ) ,
454
393
) ;
455
- } ;
394
+ } ;
0 commit comments