|
@@ -1,105 +1,26 @@
|
|
|
-import { useState, useRef, useEffect } from 'react';
|
|
|
-import styles from './Console.module.scss';
|
|
|
+import React, { useState } from 'react';
|
|
|
+import style from './Console.module.scss';
|
|
|
|
|
|
-export const Console = () => {
|
|
|
- const [isConsoleOpen, setIsConsoleOpen] = useState(false);
|
|
|
- const [consoleInput, setConsoleInput] = useState('');
|
|
|
- const [consoleOutput, setConsoleOutput] = useState([]);
|
|
|
+export const Console = ({ isOpen, close }) => {
|
|
|
+ const [output, setOutput] = useState('');
|
|
|
|
|
|
- const inputRef = useRef(null);
|
|
|
-
|
|
|
- const handleConsoleButtonClick = () => {
|
|
|
- setIsConsoleOpen(true);
|
|
|
- inputRef.current.focus();
|
|
|
- };
|
|
|
-
|
|
|
- const handleConsoleInputSubmit = (event) => {
|
|
|
- event.preventDefault();
|
|
|
- const userInput = consoleInput.trim();
|
|
|
- if (!userInput) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- const result = eval(userInput);
|
|
|
- setConsoleOutput((prevState) => [...prevState, { type: 'output', data: result }]);
|
|
|
- } catch (error) {
|
|
|
- setConsoleOutput((prevState) => [
|
|
|
- ...prevState,
|
|
|
- { type: 'error', data: `${error.name}: ${error.message}` },
|
|
|
- ]);
|
|
|
- }
|
|
|
- setConsoleInput('');
|
|
|
- };
|
|
|
-
|
|
|
- const handleClearButtonClick = () => {
|
|
|
- setConsoleOutput([]);
|
|
|
- inputRef.current.focus();
|
|
|
+ const handleClear = () => {
|
|
|
+ setOutput('');
|
|
|
};
|
|
|
|
|
|
- const handleCloseButtonClick = () => {
|
|
|
- setIsConsoleOpen(false);
|
|
|
- setConsoleOutput([]);
|
|
|
- };
|
|
|
-
|
|
|
- useEffect(() => {
|
|
|
- const handleKeyDown = (event) => {
|
|
|
- if (event.key === 'Escape') {
|
|
|
- setIsConsoleOpen(false);
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- window.addEventListener('keydown', handleKeyDown);
|
|
|
-
|
|
|
- return () => {
|
|
|
- window.removeEventListener('keydown', handleKeyDown);
|
|
|
- };
|
|
|
- }, []);
|
|
|
-
|
|
|
- return (
|
|
|
- <footer className={styles.console}>
|
|
|
- {!isConsoleOpen && (
|
|
|
- <button className={styles.consoleButton} onClick={handleConsoleButtonClick}>
|
|
|
- Console.log
|
|
|
- </button>
|
|
|
- )}
|
|
|
- {isConsoleOpen && (
|
|
|
- <div className={styles.consoleWrapper}>
|
|
|
- <div className={styles.consoleHeader}>
|
|
|
- <button className={styles.closeButton} onClick={handleCloseButtonClick}>
|
|
|
- X
|
|
|
- </button>
|
|
|
- <button className={styles.minimizeButton} onClick={() => setIsConsoleOpen(false)}>
|
|
|
- −
|
|
|
- </button>
|
|
|
- <button className={styles.maximizeButton} onClick={() => setIsConsoleOpen(true)}>
|
|
|
- □
|
|
|
- </button>
|
|
|
- </div>
|
|
|
- <div className={styles.consoleBody}>
|
|
|
- {consoleOutput.map((output, index) => (
|
|
|
- <div key={index} className={output.type === 'error' ? styles.error : styles.output}>
|
|
|
- {output.data}
|
|
|
- </div>
|
|
|
- ))}
|
|
|
- <form onSubmit={handleConsoleInputSubmit}>
|
|
|
- <input
|
|
|
- type="text"
|
|
|
- name="consoleInput"
|
|
|
- className={styles.input}
|
|
|
- value={consoleInput}
|
|
|
- onChange={(event) => setConsoleInput(event.target.value)}
|
|
|
- ref={inputRef}
|
|
|
- />
|
|
|
- </form>
|
|
|
- </div>
|
|
|
- <div className={styles.consoleFooter}>
|
|
|
- <button className={styles.clearButton} onClick={handleClearButtonClick}>
|
|
|
- Clear
|
|
|
- </button>
|
|
|
- </div>
|
|
|
+ if (isOpen) {
|
|
|
+ return (
|
|
|
+ <div className={style.console}>
|
|
|
+ <div className={style.header}>
|
|
|
+ <h2>Console</h2>
|
|
|
+ <button onClick={handleClear}>Clear</button>
|
|
|
+ <button onClick={close}>x</button>
|
|
|
</div>
|
|
|
- )}
|
|
|
- </footer>
|
|
|
- );
|
|
|
+ <div className={style.out}>{output}</div>
|
|
|
+ <textarea onChange={(event) => setOutput(event.target.value)} value={output} />
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
};
|