Skip to content

Commit 7ff579b

Browse files
Provide dynamic value for overlay background color
1 parent 5b87d8b commit 7ff579b

File tree

1 file changed

+60
-121
lines changed

1 file changed

+60
-121
lines changed

src/components/CodeEditor/CodeEditor.js

+60-121
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,19 @@ const CodeEditorTopPanel = styled.div`
3434
}
3535
`;
3636

37-
3837
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+
`;
5350

5451
const CodeEditorBottomPanel = styled.div`
5552
height: ${({ theme }) => theme.spacing07};
@@ -75,9 +72,9 @@ const ButtonPrimary = styled.button`
7572
cursor: pointer;
7673
background: ${({ theme }) => theme.colorPrimary600};
7774
}
78-
:disabled {
79-
color: #A0A0A6;
80-
background:${({ theme }) => theme.colorDisabled};
75+
:disabled {
76+
color: #a0a0a6;
77+
background: ${({ theme }) => theme.colorDisabled};
8178
cursor: not-allowed;
8279
}
8380
`;
@@ -111,16 +108,15 @@ const CodeEditorWindow = styled.div`
111108
top: 0;
112109
left: 0;
113110
position: fixed;
114-
overflow: hidden;
111+
overflow: hidden;
115112
`;
116113

117114
const CodeEditorWindowDocked = styled.div`
118115
height: 100%;
119116
width: 100%;
120117
position: static;
121118
transform: translate(0px, 0px);
122-
overflow: hidden;
123-
119+
overflow: hidden;
124120
`;
125121

126122
const MonacoEditor = styled.div`
@@ -131,11 +127,10 @@ const MonacoEditor = styled.div`
131127
position: relative;
132128
`;
133129

134-
135130
export const CodeEditor = ({
136131
onRunEvalscriptClick,
137132
portalId,
138-
defaultEditorTheme = 'dark',
133+
defaultEditorTheme = "dark",
139134
onChange,
140135
value,
141136
zIndex = 100,
@@ -144,22 +139,15 @@ export const CodeEditor = ({
144139
themeDark = defaultThemeDark,
145140
runEvalscriptButtonText = "Run evalscript",
146141
runningEvalscriptButtonText = "Running evalscript",
147-
readOnlyMessage = "Editor is in read only mode"
142+
readOnlyMessage = "Editor is in read only mode",
148143
}) => {
149-
150-
151144
const monacoEditorDOMRef = useRef();
152145
const monacoRef = useRef();
153146
const headerEditorRef = useRef();
154-
const editorRef = useRef()
147+
const editorRef = useRef();
155148
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);
163151

164152
const {
165153
editorPosition,
@@ -173,7 +161,6 @@ export const CodeEditor = ({
173161
isFullscreen,
174162
} = useFreeEditor(editorWindowRef, headerEditorRef);
175163

176-
177164
useEffect(() => {
178165
if (shouldTriggerRunEvalscriptAnimation) {
179166
setTimeout(() => {
@@ -196,34 +183,27 @@ export const CodeEditor = ({
196183
};
197184

198185
loader.init().then((monaco) => {
199-
200-
if (themeDark.name !== 'vs-dark') {
186+
if (themeDark.name !== "vs-dark") {
201187
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
203189
inherit: true, // can also be false to completely replace the builtin rules
204-
rules: [
205-
{}
206-
],
190+
rules: [{}],
207191
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+
},
212196
});
213197
}
214198

215-
216-
217-
if (themeLight.name !== 'vs') {
199+
if (themeLight.name !== "vs") {
218200
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
220202
inherit: true, // can also be false to completely replace the builtin rules
221-
rules: [
222-
{}
223-
],
203+
rules: [{}],
224204
colors: {
225-
'editor.background': `${themeLight.styles.colorBg500}`
226-
}
205+
"editor.background": `${themeLight.styles.colorBg500}`,
206+
},
227207
});
228208
}
229209

@@ -232,65 +212,58 @@ export const CodeEditor = ({
232212
theme: isDarkTheme ? themeDark.name : themeLight.name,
233213
readOnly: isReadOnly,
234214
scrollbar: {
235-
alwaysConsumeMouseWheel: isDocked ? false : true
236-
}
215+
alwaysConsumeMouseWheel: isDocked ? false : true,
216+
},
237217
});
238218

239-
240-
const messageContribution = editorRef.current.getContribution('editor.contrib.messageController');
219+
const messageContribution = editorRef.current.getContribution("editor.contrib.messageController");
241220
editorRef.current.onDidAttemptReadOnlyEdit(() => {
242221
messageContribution.showMessage(readOnlyMessage, editorRef.current.getPosition());
243222
});
244223

245224
monacoRef.current = monaco;
246225
editorRef.current.onDidChangeModelContent(() => {
247-
const code = editorRef.current.getValue();
248-
onChange(code)
226+
const code = editorRef.current.getValue();
227+
onChange(code);
249228

250-
checkAndApplyErrors()
229+
checkAndApplyErrors();
251230
});
252231
});
253232
}, [isDocked]);
254233

255-
256234
useEffect(() => {
257-
if(!editorRef.current) {
258-
return
235+
if (!editorRef.current) {
236+
return;
259237
}
260238

261-
const code = editorRef.current.getValue();
239+
const code = editorRef.current.getValue();
262240
if (value !== code) {
263-
editorRef.current.setValue(value)
241+
editorRef.current.setValue(value);
264242
}
265-
}, [value])
266-
267-
268-
243+
}, [value]);
269244

270245
useEffect(() => {
271246
if (!monacoRef.current) {
272-
return
247+
return;
273248
}
274249
if (isDarkTheme) {
275250
monacoRef.current.editor.setTheme(themeDark.name);
276251
} else {
277252
monacoRef.current.editor.setTheme(themeLight.name);
278253
}
279-
280254
}, [isDarkTheme]);
281255

282256
useEffect(() => {
283-
284257
if (!editorRef.current) {
285-
return
258+
return;
286259
}
287260

288261
if (isReadOnly) {
289-
editorRef.current.updateOptions({ readOnly: true })
262+
editorRef.current.updateOptions({ readOnly: true });
290263
} else {
291-
editorRef.current.updateOptions({ readOnly: false })
264+
editorRef.current.updateOptions({ readOnly: false });
292265
}
293-
}, [isReadOnly, editorRef.current])
266+
}, [isReadOnly, editorRef.current]);
294267

295268
const debounce = useCallback((func, wait, immediate) => {
296269
var timeout;
@@ -314,11 +287,6 @@ export const CodeEditor = ({
314287
};
315288
}, []);
316289

317-
318-
319-
320-
321-
322290
const checkAndApplyErrors = debounce(function () {
323291
const code = editorRef.current.getValue();
324292
JSHINT(code, JSHINT_CONFIG);
@@ -334,26 +302,17 @@ export const CodeEditor = ({
334302
: monacoRef.current.MarkerSeverity.Warning,
335303
};
336304
});
337-
monacoRef.current.editor.setModelMarkers(
338-
editorRef.current.getModel(),
339-
"test",
340-
errors
341-
);
305+
monacoRef.current.editor.setModelMarkers(editorRef.current.getModel(), "test", errors);
342306
}, 500);
343307

344308
function toggleTheme() {
345309
setIsDarkTheme((prev) => !prev);
346310
}
347311

348-
349312
if (isDocked) {
350313
return (
351314
<ThemeProvider
352-
theme={
353-
isDarkTheme
354-
? { ...variables, ...themeDark.styles }
355-
: { ...variables, ...themeLight.styles }
356-
}
315+
theme={isDarkTheme ? { ...variables, ...themeDark.styles } : { ...variables, ...themeLight.styles }}
357316
>
358317
<CodeEditorWindowDocked ref={editorWindowRef}>
359318
<CodeEditorTopPanel ref={headerEditorRef}>
@@ -363,10 +322,7 @@ export const CodeEditor = ({
363322
</CodeEditorIcon>
364323
</CodeEditorTopPanel>
365324
<MonacoEditor ref={monacoEditorDOMRef}>
366-
{isReadOnly &&
367-
<ReadonlyOverlay isDarkTheme={isDarkTheme}>
368-
</ReadonlyOverlay>
369-
}
325+
{isReadOnly && <ReadonlyOverlay isDarkTheme={isDarkTheme}></ReadonlyOverlay>}
370326
</MonacoEditor>
371327
</CodeEditorWindowDocked>
372328
</ThemeProvider>
@@ -375,11 +331,7 @@ export const CodeEditor = ({
375331

376332
return ReactDOM.createPortal(
377333
<ThemeProvider
378-
theme={
379-
isDarkTheme
380-
? { ...variables, ...themeDark.styles }
381-
: { ...variables, ...themeLight.styles }
382-
}
334+
theme={isDarkTheme ? { ...variables, ...themeDark.styles } : { ...variables, ...themeLight.styles }}
383335
>
384336
<CodeEditorWindow
385337
zIndex={zIndex}
@@ -389,10 +341,7 @@ export const CodeEditor = ({
389341
width={editorSize.width}
390342
ref={editorWindowRef}
391343
>
392-
<CodeEditorTopPanel
393-
ref={headerEditorRef}
394-
onMouseDown={onEditorMove}
395-
>
344+
<CodeEditorTopPanel ref={headerEditorRef} onMouseDown={onEditorMove}>
396345
{isDocked ? (
397346
<ArrowsExpandIcon onClick={handleDockedClick} />
398347
) : (
@@ -413,15 +362,8 @@ export const CodeEditor = ({
413362
</>
414363
)}
415364
</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>}
425367
</MonacoEditor>
426368
<CodeEditorBottomPanel>
427369
<ButtonPrimary
@@ -441,15 +383,12 @@ export const CodeEditor = ({
441383
)}
442384
</ButtonPrimary>
443385

444-
<CodeEditorIcon
445-
onMouseDown={handleResizeMouseDown}
446-
style={{ cursor: "nwse-resize", zIndex: 0 }}
447-
>
386+
<CodeEditorIcon onMouseDown={handleResizeMouseDown} style={{ cursor: "nwse-resize", zIndex: 0 }}>
448387
<CgArrowsExpandLeft />
449388
</CodeEditorIcon>
450389
</CodeEditorBottomPanel>
451390
</CodeEditorWindow>
452391
</ThemeProvider>,
453-
document.getElementById(portalId)
392+
document.getElementById(portalId),
454393
);
455-
};
394+
};

0 commit comments

Comments
 (0)