-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEditor.jsx
More file actions
55 lines (48 loc) · 1.35 KB
/
Editor.jsx
File metadata and controls
55 lines (48 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use client";
import {useEffect, useRef, useState} from "react";
import {createEditor} from "../src/editor.js";
export function Editor({initialCode, onUserInput = () => {}}) {
const containerRef = useRef(null);
const editorRef = useRef(null);
const [needRerun, setNeedRerun] = useState(false);
useEffect(() => {
if (containerRef.current) {
containerRef.current.innerHTML = "";
editorRef.current = createEditor(containerRef.current, {code: initialCode});
editorRef.current.run();
}
return () => {
if (editorRef.current) {
editorRef.current.destroy();
}
};
}, [initialCode]);
useEffect(() => {
const onInput = (code) => {
onUserInput(code);
setNeedRerun(true);
};
if (editorRef.current) {
editorRef.current.on("userInput", onInput);
}
}, [initialCode, onUserInput]);
function onRun() {
setNeedRerun(false);
editorRef.current.run();
}
function onStop() {
setNeedRerun(false);
editorRef.current.stop();
}
return (
<div style={{height: "calc(100vh - 115px)"}}>
<div>
<button onClick={onRun}>{needRerun ? "Run (Updated)" : "Run"}</button>
<button onClick={onStop}>Stop</button>
</div>
<div ref={containerRef} style={{height: "100%", overflow: "auto"}}>
<pre>{initialCode}</pre>
</div>
</div>
);
}