Skip to content

Commit

Permalink
Merge branch 'nishant0708:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
swarnaliiiiii authored Sep 18, 2024
2 parents d468461 + dc57737 commit e56abf5
Show file tree
Hide file tree
Showing 12 changed files with 1,607 additions and 174 deletions.
1,191 changes: 1,170 additions & 21 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"eslint-plugin-react": "^7.35.0",
"eslint-plugin-react-hooks": "^4.6.2",
"flex-splitter-directive": "^0.5.1",
"react-icons": "^5.3.0"
"react-icons": "^5.3.0",
"react-markdown": "^9.0.1"
},
"description": "- Primary Background color : #1A1A1A\r - secondary Background Color : #282828\r - text color : white (primary)\r - secondary text color : #Eff2f699\r - secondary text color : #Eff1f6bf(Darker for headings)",
"main": "index.js",
Expand Down
17 changes: 16 additions & 1 deletion src/Body/Body.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,19 @@
{
width: 28.5%;
overflow-x: hidden;
}
}
.body-contents-small
{
background-color:rgb(211 229 255 / 40%);
border-radius: 10px;
position: relative;
margin-top: 9px;
}
.body-contents-small p
{
font-size: 50px;
transform: rotate(90deg);
position: absolute;
top: 35%;
right: 0%;
}
58 changes: 46 additions & 12 deletions src/Body/Body.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useRef, useEffect, useState } from "react";
import PropTypes from "prop-types";
import "flex-splitter-directive";
import "flex-splitter-directive/styles.min.css";
Expand All @@ -8,20 +8,54 @@ import Test from "../Test/Test";
import "./Body.css";

const Body = ({ question }) => {
const bodyContentsRef = useRef(null); // Reference for body-contents
const [isSmallWidth, setIsSmallWidth] = useState(false); // State to track if width < 200px
const [output, setOutput] = useState(""); // State to store the output

// useEffect to observe the width of body-contents
useEffect(() => {
const observer = new ResizeObserver((entries) => {
if (bodyContentsRef.current) {
const { width } = entries[0].contentRect;
setIsSmallWidth(width < 200); // Set state based on width
}
});

if (bodyContentsRef.current) {
observer.observe(bodyContentsRef.current);
}

// Cleanup
return () => {
if (bodyContentsRef.current) {
observer.unobserve(bodyContentsRef.current);
}
observer.disconnect();
};
}, []);

return (
<>
<div className="compiler-body" data-flex-splitter-horizontal>
{/* Pass the question as a prop to the Questions component */}
<Questions question={question} />
<div role="separator" tabIndex="1"></div>
<div className="body-contents" data-flex-splitter-vertical>

<Editor question={question} />
<div className="compiler-body" data-flex-splitter-horizontal>
<Questions question={question} />

<div role="separator" tabIndex="1"></div>

{/* Conditionally render based on width */}
{isSmallWidth ? (
<div className="body-contents-small" ref={bodyContentsRef}>
{/* Render alternate content when width is less than 200px */}
<p>Code</p>
</div>
) : (
<div className="body-contents" data-flex-splitter-vertical ref={bodyContentsRef}>
{/* Pass setOutput function to Editor to update the output */}
<Editor question={question} onOutput={setOutput} />
<div role="separator" tabIndex="1"></div>
<Test />
{/* Pass the output state to the Test component */}
<Test output={output} />
</div>
</div>
</>
)}
</div>
);
};

Expand Down
48 changes: 47 additions & 1 deletion src/Editor/Editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,50 @@
justify-content: center;
align-items: center;
gap: 5px;
}
}

.modal-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.75);
z-index: 1000;
}

.modal {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 20px;
border-radius: 10px;
outline: none;
width: 300px;
z-index: 1001;
}

.modal-content {
display: flex;
flex-direction: column;
gap: 10px;
}

.modal-content input {
padding: 5px;
}

.modal-content button {
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
}

.modal-content button:hover {
background-color: #45a049;
}
165 changes: 110 additions & 55 deletions src/Editor/Editor.jsx
Original file line number Diff line number Diff line change
@@ -1,72 +1,127 @@
import React, { useRef, useEffect } from "react";
import React, { useRef, useEffect, useState } from "react";
import PropTypes from "prop-types";
import "./Editor.css";
import { Editor as Ed } from "@monaco-editor/react";
import { FaCode } from "react-icons/fa";
import { FaPlay } from "react-icons/fa6";
import { FaCode, FaPlay } from "react-icons/fa";
import axios from "axios";
import Modal from "react-modal"; // Import Modal component

const Editor = ({ question }) => {
const editorContainerRef = useRef(null);
const editorRef = useRef(null);
Modal.setAppElement("#root"); // Set the root element for accessibility

const handleEditorDidMount = (editor) => {
editorRef.current = editor;
// Initial layout call
editor.layout();
const Editor = ({ question, onOutput }) => {
const editorContainerRef = useRef(null);
const editorRef = useRef(null);
const [input, setInput] = useState("");
const [isModalOpen, setIsModalOpen] = useState(false); // State for modal visibility

const handleEditorDidMount = (editor) => {
editorRef.current = editor;
editor.layout();
};

useEffect(() => {
const observer = new ResizeObserver(() => {
if (editorRef.current) {
editorRef.current.layout();
}
});

if (editorContainerRef.current) {
observer.observe(editorContainerRef.current);
}

return () => {
if (editorContainerRef.current) {
observer.unobserve(editorContainerRef.current);
}
observer.disconnect();
};
}, []);

useEffect(() => {
const observer = new ResizeObserver(() => {
if (editorRef.current) {
editorRef.current.layout();
}
});
// Handle "Run" button click
const handleRunClick = async () => {
const code = editorRef.current.getValue();

if (editorContainerRef.current) {
observer.observe(editorContainerRef.current);
try {
const response = await axios.post(
"http://localhost:5000/student/compile",
{
code,
language: question?.compilerReq,
input: input || "", // Send user input if available
}
);

const { output, waitingForInput } = response.data;

if (waitingForInput) {
setIsModalOpen(true); // Open the modal if input is required
}

onOutput(output);
} catch (error) {
console.error("Error running code:", error);
}
};

// Handle input submission
const handleInputSubmit = async () => {
setIsModalOpen(false); // Close the modal
await handleRunClick(); // Re-run the code with the new input
};

return (
<div className="compiler-editor">
<div className="editor-header">
<div className="editor-code">
<FaCode />
<div>Code</div>
</div>
<div className="editor-run" onClick={handleRunClick}>
<FaPlay size={15} />
<div>Run</div>
</div>
</div>
<div className="editor-editor" ref={editorContainerRef}>
<Ed
theme="vs-dark"
defaultLanguage={question?.compilerReq}
defaultValue="// Write Code Here"
className="editor-monaco"
onMount={handleEditorDidMount}
/>
</div>

// Cleanup on unmount
return () => {
if (editorContainerRef.current) {
observer.unobserve(editorContainerRef.current);
}
observer.disconnect();
};
}, []);

return (
<div className="compiler-editor">
<div className="editor-header">
<div className="editor-code">
<FaCode/>
<div>Code</div>
</div>
<div className="editor-run">
<FaPlay size={15}/>
<div>Run</div>
</div>
</div>
<div className="editor-editor" ref={editorContainerRef}>
<Ed
theme="vs-dark"
defaultLanguage={question?.compilerReq}
defaultValue= "// Write Code Here"
className="editor-monaco"
onMount={handleEditorDidMount}
/>
</div>
{/* Modal for input prompt */}
<Modal
isOpen={isModalOpen}
onRequestClose={() => setIsModalOpen(false)}
contentLabel="Input Modal"
className="modal"
overlayClassName="modal-overlay"
>
<div className="modal-content">
<h2>Input Required</h2>
<label>Enter Input:</label>
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
/>
<button onClick={handleInputSubmit}>Submit</button>
</div>
);
</Modal>
</div>
);
};

// Define prop types for the Editor component
Editor.propTypes = {
question: PropTypes.shape({
compilerReq: PropTypes.string.isRequired,
description: PropTypes.string.isRequired,
image: PropTypes.string,
}),
question: PropTypes.shape({
compilerReq: PropTypes.string.isRequired,
description: PropTypes.string.isRequired,
image: PropTypes.string,
}),
onOutput: PropTypes.func.isRequired,
};

export default Editor;
Loading

0 comments on commit e56abf5

Please sign in to comment.