|
@@ -1,11 +1,33 @@
|
|
-import React, { useState } from 'react';
|
|
|
|
|
|
+import React, { useState, useEffect } from 'react';
|
|
import style from './Console.module.scss';
|
|
import style from './Console.module.scss';
|
|
|
|
|
|
export const Console = ({ isOpen, close }) => {
|
|
export const Console = ({ isOpen, close }) => {
|
|
- const [output, setOutput] = useState('');
|
|
|
|
|
|
+ const [output, setOutput] = useState([]);
|
|
|
|
+ const [command, setCommand] = useState('');
|
|
|
|
|
|
- const handleClear = () => {
|
|
|
|
- setOutput('');
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ window.console = {
|
|
|
|
+ log: (...args) => setOutput((output) => [...output, args.join(' ')]),
|
|
|
|
+ error: (...args) => setOutput((output) => [...output, args.join(' ')]),
|
|
|
|
+ clear: () => setOutput([]),
|
|
|
|
+ };
|
|
|
|
+ }, []);
|
|
|
|
+
|
|
|
|
+ const handleCommandChange = (event) => {
|
|
|
|
+ setCommand(event.target.value);
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const handleKeyDown = (event) => {
|
|
|
|
+ if (event.key === 'Enter') {
|
|
|
|
+ try {
|
|
|
|
+ const result = eval(event.target.value);
|
|
|
|
+ setOutput((output) => [...output, result]);
|
|
|
|
+ } catch (e) {
|
|
|
|
+ setOutput((output) => [...output, e.toString()]);
|
|
|
|
+ }
|
|
|
|
+ setCommand('');
|
|
|
|
+ event.preventDefault();
|
|
|
|
+ }
|
|
};
|
|
};
|
|
|
|
|
|
if (isOpen) {
|
|
if (isOpen) {
|
|
@@ -13,13 +35,22 @@ export const Console = ({ isOpen, close }) => {
|
|
<div className={style.console}>
|
|
<div className={style.console}>
|
|
<div className={style.header}>
|
|
<div className={style.header}>
|
|
<h2>Console</h2>
|
|
<h2>Console</h2>
|
|
- <button onClick={handleClear}>Clear</button>
|
|
|
|
|
|
+ <button onClick={() => window.console.clear()}>Clear</button>
|
|
<button onClick={close}>x</button>
|
|
<button onClick={close}>x</button>
|
|
</div>
|
|
</div>
|
|
- <div className={style.out}>{output}</div>
|
|
|
|
|
|
+ <div className={style.out}>
|
|
|
|
+ {output.map((item, index) => (
|
|
|
|
+ <div key={index}>{item}</div>
|
|
|
|
+ ))}
|
|
|
|
+ </div>
|
|
<div className={style.entryField}>
|
|
<div className={style.entryField}>
|
|
<span>></span>
|
|
<span>></span>
|
|
- <textarea onChange={(event) => setOutput(event.target.value)} value={output} />
|
|
|
|
|
|
+ <input
|
|
|
|
+ type="text"
|
|
|
|
+ value={command}
|
|
|
|
+ onChange={handleCommandChange}
|
|
|
|
+ onKeyDown={handleKeyDown}
|
|
|
|
+ />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
);
|