Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions app/Editor.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use client";
import {useEffect, useRef} from "react";
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) {
Expand All @@ -20,16 +21,31 @@ export function Editor({initialCode, onUserInput = () => {}}) {
}, [initialCode]);

useEffect(() => {
const onInput = (code) => {
onUserInput(code);
setNeedRerun(true);
};

if (editorRef.current) {
editorRef.current.on("userInput", onUserInput);
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={() => editorRef.current.run()}>Run</button>
<button onClick={() => editorRef.current.stop()}>Stop</button>
<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>
Expand Down