Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5e11f87

Browse files
committed
Move refs up
1 parent 7db9379 commit 5e11f87

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

src/Editor.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import React, { useEffect, useRef } from "react";
2-
import CodeMirror from "codemirror";
2+
import CodeMirror, { Editor } from "codemirror";
33

44
import "codemirror/mode/ruby/ruby";
55
import "codemirror/lib/codemirror.css";
66
import "codemirror/theme/xq-light.css";
77

88
type EditorProps = {
9-
value: string,
9+
editorRef: React.MutableRefObject<Editor>,
10+
initialValue: string,
1011
onChange: (value: string) => void
1112
};
1213

13-
const Editor: React.FC<EditorProps> = ({ value, onChange }) => {
14+
const Editor: React.FC<EditorProps> = ({ editorRef, initialValue, onChange }) => {
1415
const elementRef = useRef<HTMLDivElement>(null);
15-
const editorRef = useRef<CodeMirror.Editor>(null);
1616

1717
useEffect(() => {
1818
if (elementRef.current && !editorRef.current) {
1919
const editor = CodeMirror(elementRef.current, {
2020
lineNumbers: true,
2121
mode: "ruby",
22-
value,
22+
value: initialValue,
2323
theme: "xq-light"
2424
});
2525

src/Tree.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ import React, { useEffect, useState } from "react";
22

33
import createRuby, { Ruby } from "./createRuby";
44

5-
// Create a singleton since we don't actually want there to be multiple virtual
6-
// machines running even if there are multiple Tree components.
7-
let ruby: Ruby = {
8-
format(source) {
9-
return source;
10-
},
11-
prettyPrint(source) {
12-
return "Loading...";
13-
}
14-
};
15-
165
// Track a state that represents where the ruby constant is at any given time.
176
let rubyState: "initial" | "creating" | "ready" = "initial";
187

@@ -26,28 +15,29 @@ function prettyPrint(ruby: Ruby, value: string) {
2615
}
2716

2817
type TreeProps = {
18+
rubyRef: React.MutableRefObject<Ruby>,
2919
value: string
3020
};
3121

32-
const Tree: React.FC<TreeProps> = ({ value }) => {
33-
const [output, setOutput] = useState<string>(() => prettyPrint(ruby, value));
22+
const Tree: React.FC<TreeProps> = ({ rubyRef, value }) => {
23+
const [output, setOutput] = useState<string>(() => prettyPrint(rubyRef.current, value));
3424

3525
useEffect(() => {
3626
switch (rubyState) {
3727
case "initial":
3828
rubyState = "creating";
3929

4030
createRuby().then((newRuby) => {
41-
ruby = newRuby;
31+
rubyRef.current = newRuby;
4232
rubyState = "ready";
43-
setOutput(prettyPrint(ruby, value));
33+
setOutput(prettyPrint(rubyRef.current, value));
4434
});
4535

4636
break;
4737
case "creating":
4838
break;
4939
case "ready":
50-
setOutput(prettyPrint(ruby, value));
40+
setOutput(prettyPrint(rubyRef.current, value));
5141
break;
5242
}
5343
}, [value]);

src/index.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ nav a {
3232
vertical-align: middle;
3333
}
3434

35-
nav h1, nav a {
35+
nav h1, nav a, nav span {
3636
padding: 0 1em;
3737
}
3838

39+
textarea {
40+
font-size: 0.9em;
41+
}
42+
3943
.CodeMirror {
4044
height: 100% !important;
4145
}

src/index.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import React, { Suspense, useState } from "react";
1+
import React, { Suspense, useRef, useState } from "react";
22
import { createRoot } from "react-dom/client";
3+
import type { Editor } from "codemirror";
34

5+
import type { Ruby } from "./createRuby";
46
import initialSource from "./initialSource";
7+
58
const Editor = React.lazy(() => import("./Editor"));
69
const Tree = React.lazy(() => import("./Tree"));
710

811
import "./index.css";
912

10-
// type TreeProps = { value: string };
11-
// const Tree: React.FC<TreeProps> = () => <textarea disabled readOnly value="Hello, world!" />;
12-
1313
type EditorFallbackProps = {
1414
value: string,
1515
onChange: (value: string) => void
@@ -30,18 +30,35 @@ const TreeFallback: React.FC = () => (
3030
const App: React.FC = () => {
3131
const [source, setSource] = useState<string>(initialSource);
3232

33+
const editorRef = useRef<Editor>(null);
34+
const rubyRef = useRef<Ruby>({
35+
format(source) {
36+
return source;
37+
},
38+
prettyPrint(source) {
39+
return "Loading...";
40+
}
41+
});
42+
43+
const onFormat = () => {
44+
if (editorRef.current) {
45+
editorRef.current.getDoc().setValue(rubyRef.current.format(source));
46+
}
47+
};
48+
3349
return (
3450
<>
3551
<nav>
3652
<h1>Syntax Tree</h1>
3753
<a href="https://ruby-syntax-tree.github.io/syntax_tree">Docs</a>
3854
<a href="https://github.com/ruby-syntax-tree/syntax_tree">Source</a>
55+
<span><button type="button" onClick={onFormat}>Format</button></span>
3956
</nav>
4057
<Suspense fallback={<EditorFallback value={source} onChange={setSource} />}>
41-
<Editor value={source} onChange={setSource} />
58+
<Editor editorRef={editorRef} initialValue={source} onChange={setSource} />
4259
</Suspense>
4360
<Suspense fallback={<TreeFallback />}>
44-
<Tree value={source} />
61+
<Tree rubyRef={rubyRef} value={source} />
4562
</Suspense>
4663
</>
4764
);

0 commit comments

Comments
 (0)