diff --git a/src/pages/EditorComponent.js b/src/pages/EditorComponent.js index 777d0e9..8c887bf 100644 --- a/src/pages/EditorComponent.js +++ b/src/pages/EditorComponent.js @@ -65,6 +65,41 @@ const OutputLayout = styled("div")(({ theme }) => ({ "@media (min-width: 1024px)": { height: "30vh", padding: "1rem", + width: "50%", + }, +})); + +const InputLayout = styled("div")(({ theme }) => ({ + backgroundColor: theme.palette.background.paper, + height: "20vh", + margin: "1rem 0", + overflow: "auto", + border: `2px solid ${theme.palette.divider}`, + borderRadius: "1rem", + padding: "1rem", + "@media (min-width: 1024px)": { + height: "30vh", + padding: "1rem", + width: "50%", + margin: "1rem 0 0 1rem", + }, +})); + +const StyledTextArea = styled("textarea")(({ theme }) => ({ + width: "100%", + height: "calc(100% - 40px)", + background: "transparent", + color: theme.palette.text.primary, + border: "none", + resize: "none", + outline: "none", + fontSize: "1rem", + "&::placeholder": { + color: theme.palette.text.secondary, + opacity: 0.8, + }, + "&:focus::placeholder": { + color: "transparent", }, })); @@ -79,6 +114,7 @@ const decodeFormat = (data) => { function EditorComponent() { const [code, setCode] = useState(null); + const [stdin, setStdin] = useState(""); const [output, setOutput] = useState([]); const [currentLanguage, setCurrentLanguage] = useState( LANGUAGES[0].DEFAULT_LANGUAGE @@ -133,9 +169,28 @@ function EditorComponent() { DEFAULT_LANGUAGE: selectedLanguage.DEFAULT_LANGUAGE, NAME: selectedLanguage.NAME, }); - setCode(selectedLanguage.HELLO_WORLD); + const savedCode = localStorage.getItem(`code-${selectedLanguage.DEFAULT_LANGUAGE}`); + if (savedCode !== null) { + setCode(savedCode); + } else { + setCode(selectedLanguage.HELLO_WORLD); + } }, [currentLanguage]); + useEffect(() => { + const handler = setTimeout(() => { + if (code) { + localStorage.setItem(`code-${currentLanguage}`, code); + } else { + localStorage.removeItem(`code-${currentLanguage}`); + } + }, 500); + + return () => { + clearTimeout(handler); + }; + }, [code]); + const handleEditorThemeChange = async (_, theme) => { if (["light", "vs-dark"].includes(theme.ID)) { setCurrentEditorTheme(theme); @@ -178,7 +233,7 @@ function EditorComponent() { body: JSON.stringify({ source_code: encodedCode, language_id: languageDetails.ID, - stdin: "", + stdin: btoa(stdin), expected_output: "", }), } @@ -208,16 +263,25 @@ function EditorComponent() { ) .then((response) => response.json()) .then((data) => { - if (!data.stdout) { - enqueueSnackbar("Please check the code", { variant: "error" }); - if (data.stderr) { - setOutput(decodeFormat(data.stderr)); - } else if (data.compile_output) { - setOutput(decodeFormat(data.compile_output)); - } - return; + const newOutput = []; + let errorMessage = []; + + if (data.stdout) { + newOutput.push(...decodeFormat(data.stdout)); } - setOutput(decodeFormat(data.stdout)); + if (data.stderr) { + newOutput.push(...decodeFormat(data.stderr)); + errorMessage.push("Error in code"); + } + if (data.compile_output) { + newOutput.push(...decodeFormat(data.compile_output)); + errorMessage.push("Compilation error"); + } + + if (errorMessage.length > 0) { + enqueueSnackbar(errorMessage.join(" and "), { variant: "error" }); + } + setOutput(newOutput); }) .catch((error) => { enqueueSnackbar("Error retrieving output: " + error.message, { @@ -229,7 +293,7 @@ function EditorComponent() { } catch (error) { enqueueSnackbar("Error: " + error.message, { variant: "error" }); } - }, [enqueueSnackbar, languageDetails]); + }, [enqueueSnackbar, languageDetails, stdin]); // import file const [isImporting, setIsImporting] = React.useState(false); @@ -651,49 +715,61 @@ function EditorComponent() { - -
- - Output - -
- - + Output + +
+ + +
-
-
- {Array.isArray(output) && output.length > 0 ? ( - output.map((result, i) => ( -
- {result} +
+ {Array.isArray(output) && output.length > 0 ? ( + output.map((result, i) => ( +
+ {result} +
+ )) + ) : ( +
+ No output yet. Run your code to see results!
- )) - ) : ( -
- No output yet. Run your code to see results! -
- )} -
- + )} +
+ + + + Input + + setStdin(e.target.value)} + placeholder="Provide all program input here before running" + /> + +
);