Skip to content

Commit 002a070

Browse files
authored
Merge pull request #122 from JeevaRamanathan/shortcut_issue
Fix: Code Editor Shortcut (Ctrl+Enter)
2 parents 337c119 + c749806 commit 002a070

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

src/pages/EditorComponent.js

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useRef, useEffect } from "react";
1+
import React, { useState, useRef, useEffect, useCallback } from "react";
22
import { FaPlay } from "react-icons/fa";
33
import Editor from "@monaco-editor/react";
44
import "../components/css/EditorComponent.css";
@@ -26,7 +26,6 @@ const StyledButton = styled(Button)({
2626
gap: "0.5rem",
2727
});
2828

29-
3029
const StyledLayout = styled("div")(({ theme }) => ({
3130
display: "flex",
3231
flexDirection: "column",
@@ -58,11 +57,13 @@ function EditorComponent() {
5857
LANGUAGES[0].DEFAULT_LANGUAGE
5958
);
6059
const [languageDetails, setLanguageDetails] = useState(LANGUAGES[0]);
61-
const [currentEditorTheme, setCurrentEditorTheme] = useState(EDITOR_THEMES[1]);
60+
const [currentEditorTheme, setCurrentEditorTheme] = useState(
61+
EDITOR_THEMES[1]
62+
);
6263
const [loading, setLoading] = useState(false);
6364
const { enqueueSnackbar } = useSnackbar();
6465
const editorRef = useRef(null);
65-
66+
const monacoRef = useRef(null);
6667

6768
const styles = {
6869
flex: {
@@ -83,21 +84,14 @@ function EditorComponent() {
8384
(lang) => lang.DEFAULT_LANGUAGE === currentLanguage
8485
);
8586
setLanguageDetails({
86-
LANGUAGE_ID: selectedLanguage.ID,
87+
ID: selectedLanguage.ID,
8788
LANGUAGE_NAME: selectedLanguage.NAME,
8889
DEFAULT_LANGUAGE: selectedLanguage.DEFAULT_LANGUAGE,
8990
NAME: selectedLanguage.NAME,
9091
});
9192
setCode(selectedLanguage.HELLO_WORLD);
9293
}, [currentLanguage]);
9394

94-
function handleEditorDidMount(editor, monaco) {
95-
editorRef.current = editor;
96-
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, () => {
97-
submitCode();
98-
});
99-
}
100-
10195
const handleEditorThemeChange = async (_, theme) => {
10296
if (["light", "vs-dark"].includes(theme.ID)) {
10397
setCurrentEditorTheme(theme);
@@ -115,7 +109,7 @@ function EditorComponent() {
115109
return language ? language.LOGO : null;
116110
};
117111

118-
async function submitCode() {
112+
const submitCode = useCallback(async () => {
119113
const codeToSubmit = editorRef.current.getValue();
120114
if (codeToSubmit === "") {
121115
enqueueSnackbar("Please enter valid code", { variant: "error" });
@@ -132,7 +126,7 @@ function EditorComponent() {
132126
},
133127
body: JSON.stringify({
134128
source_code: codeToSubmit,
135-
language_id: languageDetails.LANGUAGE_ID,
129+
language_id: languageDetails.ID,
136130
stdin: "",
137131
expected_output: "",
138132
}),
@@ -177,7 +171,27 @@ function EditorComponent() {
177171
} catch (error) {
178172
enqueueSnackbar("Error: " + error.message, { variant: "error" });
179173
}
180-
}
174+
}, [enqueueSnackbar, languageDetails]);
175+
176+
const handleEditorDidMount = useCallback(
177+
(editor, monaco) => {
178+
editorRef.current = editor;
179+
monacoRef.current = monaco;
180+
editor.addCommand(
181+
monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter,
182+
submitCode
183+
);
184+
},
185+
[submitCode]
186+
);
187+
188+
useEffect(() => {
189+
if (editorRef.current && monacoRef.current) {
190+
const editor = editorRef.current;
191+
const monaco = monacoRef.current;
192+
handleEditorDidMount(editor, monaco);
193+
}
194+
}, [languageDetails, handleEditorDidMount]);
181195

182196
function handleLanguageChange(_, value) {
183197
setCurrentLanguage(value.DEFAULT_LANGUAGE);
@@ -201,15 +215,32 @@ function EditorComponent() {
201215
>
202216
<div style={styles.flex}>
203217
<div style={{ display: "flex", alignItems: "center" }}>
204-
<img src="./images/custom-code-editor-rounded.svg" alt="Custom Code Editor icon" width={32} style={{ marginLeft: "0.5rem" }} />
205-
<span style={{ backgroundClip: "text", background: "linear-gradient(#2837BA 0%, #2F1888 100%)", WebkitBackgroundClip: "text", color: "transparent", marginLeft: "0.5rem", fontWeight: "bold", fontSize: "1.5em" }}>Custom Code Editor</span>
218+
<img
219+
src="./images/custom-code-editor-rounded.svg"
220+
alt="Custom Code Editor icon"
221+
width={32}
222+
style={{ marginLeft: "0.5rem" }}
223+
/>
224+
<span
225+
style={{
226+
backgroundClip: "text",
227+
background: "linear-gradient(#2837BA 0%, #2F1888 100%)",
228+
WebkitBackgroundClip: "text",
229+
color: "transparent",
230+
marginLeft: "0.5rem",
231+
fontWeight: "bold",
232+
fontSize: "1.5em",
233+
}}
234+
>
235+
Custom Code Editor
236+
</span>
206237
</div>
207238
<div style={{ display: "flex", alignItems: "center" }}>
208239
<ToggleTheme />
209240
<Stars />
210241
</div>
211242
</div>
212-
</Box >
243+
</Box>
213244
<StyledLayout>
214245
<Editor
215246
className="editor"
@@ -220,7 +251,7 @@ function EditorComponent() {
220251
language={languageDetails.DEFAULT_LANGUAGE}
221252
/>
222253
<div className="sidebar">
223-
{getLanguageLogoById(languageDetails.LANGUAGE_ID)}
254+
{getLanguageLogoById(languageDetails.ID)}
224255
<div style={{ fontWeight: "bold" }}>
225256
{languageDetails.LANGUAGE_NAME}
226257
</div>
@@ -269,7 +300,7 @@ function EditorComponent() {
269300
return <div key={i}>{result}</div>;
270301
})}
271302
</OutputLayout>
272-
</div >
303+
</div>
273304
);
274305
}
275306

0 commit comments

Comments
 (0)