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

Commit 7db9379

Browse files
committed
Simplify grid, add CodeMirror
1 parent ff5d220 commit 7db9379

File tree

7 files changed

+88
-42
lines changed

7 files changed

+88
-42
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Ignore everything in the output docs directory controlled by the build system.
22
/docs/
3-
!/docs/index.css
43
!/docs/index.html
54

65
# Ignore everything to do with building the app.wasm file.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
"dependencies": {
88
"@wasmer/wasi": "^0.12.0",
99
"@wasmer/wasmfs": "^0.12.0",
10+
"codemirror": "^5.65.3",
1011
"path-browserify": "^1.0.1",
1112
"react": "^18.0.0",
1213
"react-dom": "^18.0.0",
1314
"ruby-head-wasm-wasi": "^0.3.0"
1415
},
1516
"devDependencies": {
17+
"@types/codemirror": "^5.60.5",
1618
"@types/node": "^17.0.27",
1719
"@types/path-browserify": "^1.0.0",
1820
"@types/react": "^18.0.6",

src/Editor.tsx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1-
import React from "react";
1+
import React, { useEffect, useRef } from "react";
2+
import CodeMirror from "codemirror";
3+
4+
import "codemirror/mode/ruby/ruby";
5+
import "codemirror/lib/codemirror.css";
6+
import "codemirror/theme/xq-light.css";
27

38
type EditorProps = {
4-
cols: number,
59
value: string,
610
onChange: (value: string) => void
711
};
812

9-
const Editor: React.FC<EditorProps> = ({ cols, value, onChange }) => {
10-
const onSourceChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
11-
onChange(event.target.value);
12-
};
13+
const Editor: React.FC<EditorProps> = ({ value, onChange }) => {
14+
const elementRef = useRef<HTMLDivElement>(null);
15+
const editorRef = useRef<CodeMirror.Editor>(null);
16+
17+
useEffect(() => {
18+
if (elementRef.current && !editorRef.current) {
19+
const editor = CodeMirror(elementRef.current, {
20+
lineNumbers: true,
21+
mode: "ruby",
22+
value,
23+
theme: "xq-light"
24+
});
25+
26+
editor.on("change", () => {
27+
onChange(editor.getDoc().getValue());
28+
});
29+
30+
editorRef.current = editor;
31+
}
32+
});
1333

14-
return <textarea cols={cols} value={value} onChange={onSourceChange} />;
34+
return <div ref={elementRef} />;
1535
};
1636

1737
export default Editor;

src/Tree.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ function prettyPrint(ruby: Ruby, value: string) {
2626
}
2727

2828
type TreeProps = {
29-
cols: number,
3029
value: string
3130
};
3231

33-
const Tree: React.FC<TreeProps> = ({ cols, value }) => {
32+
const Tree: React.FC<TreeProps> = ({ value }) => {
3433
const [output, setOutput] = useState<string>(() => prettyPrint(ruby, value));
3534

3635
useEffect(() => {
@@ -55,8 +54,8 @@ const Tree: React.FC<TreeProps> = ({ cols, value }) => {
5554

5655
return (
5756
<textarea
58-
className={rubyState != "ready" ? "loading" : ""}
59-
cols={cols}
57+
cols={80}
58+
disabled={rubyState != "ready"}
6059
value={output}
6160
readOnly
6261
/>

docs/index.css renamed to src/index.css

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ html, body {
66
}
77

88
#root {
9-
display: flex;
10-
flex-direction: column;
9+
display: grid;
1110
height: 100%;
11+
grid-template-columns: repeat(2, 1fr);
12+
grid-template-rows: auto minmax(0, 1fr);
1213
}
1314

1415
nav {
1516
background-color: #fafafa;
1617
box-shadow: 0 1px 2px 0 #888;
1718
box-sizing: border-box;
1819
display: flex;
20+
grid-column: 1 / span 2;
1921
padding: 0.5em;
22+
z-index: 1;
2023
}
2124

2225
nav h1 {
@@ -33,20 +36,6 @@ nav h1, nav a {
3336
padding: 0 1em;
3437
}
3538

36-
main {
37-
box-sizing: border-box;
38-
display: flex;
39-
gap: 0.5em;
40-
height: 100%;
41-
padding: 0.5em;
42-
}
43-
44-
textarea {
45-
box-sizing: border-box;
46-
height: 100%;
47-
width: 100%;
48-
}
49-
50-
.loading {
51-
color: #ccc;
39+
.CodeMirror {
40+
height: 100% !important;
5241
}

src/index.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,33 @@ import React, { Suspense, useState } from "react";
22
import { createRoot } from "react-dom/client";
33

44
import initialSource from "./initialSource";
5-
import Editor from "./Editor";
5+
const Editor = React.lazy(() => import("./Editor"));
66
const Tree = React.lazy(() => import("./Tree"));
77

8-
type TreeFallbackProps = {
9-
cols: number
8+
import "./index.css";
9+
10+
// type TreeProps = { value: string };
11+
// const Tree: React.FC<TreeProps> = () => <textarea disabled readOnly value="Hello, world!" />;
12+
13+
type EditorFallbackProps = {
14+
value: string,
15+
onChange: (value: string) => void
16+
};
17+
18+
const EditorFallback: React.FC<EditorFallbackProps> = ({ value, onChange }) => {
19+
const onValueChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
20+
onChange(event.target.value);
21+
};
22+
23+
return <textarea value={value} onChange={onValueChange} />;
1024
};
1125

12-
const TreeFallback: React.FC<TreeFallbackProps> = ({ cols }) => (
13-
<textarea className="loading" cols={cols} readOnly>Loading...</textarea>
26+
const TreeFallback: React.FC = () => (
27+
<textarea disabled readOnly value="Loading..." />
1428
);
1529

1630
const App: React.FC = () => {
1731
const [source, setSource] = useState<string>(initialSource);
18-
const cols = 80;
1932

2033
return (
2134
<>
@@ -24,12 +37,12 @@ const App: React.FC = () => {
2437
<a href="https://ruby-syntax-tree.github.io/syntax_tree">Docs</a>
2538
<a href="https://github.com/ruby-syntax-tree/syntax_tree">Source</a>
2639
</nav>
27-
<main>
28-
<Editor cols={cols} value={source} onChange={setSource} />
29-
<Suspense fallback={<TreeFallback cols={cols} />}>
30-
<Tree cols={cols} value={source} />
31-
</Suspense>
32-
</main>
40+
<Suspense fallback={<EditorFallback value={source} onChange={setSource} />}>
41+
<Editor value={source} onChange={setSource} />
42+
</Suspense>
43+
<Suspense fallback={<TreeFallback />}>
44+
<Tree value={source} />
45+
</Suspense>
3346
</>
3447
);
3548
};

yarn.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
# yarn lockfile v1
33

44

5+
"@types/codemirror@^5.60.5":
6+
version "5.60.5"
7+
resolved "https://registry.yarnpkg.com/@types/codemirror/-/codemirror-5.60.5.tgz#5b989a3b4bbe657458cf372c92b6bfda6061a2b7"
8+
integrity sha512-TiECZmm8St5YxjFUp64LK0c8WU5bxMDt9YaAek1UqUb9swrSCoJhh92fWu1p3mTEqlHjhB5sY7OFBhWroJXZVg==
9+
dependencies:
10+
"@types/tern" "*"
11+
12+
"@types/estree@*":
13+
version "0.0.51"
14+
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.51.tgz#cfd70924a25a3fd32b218e5e420e6897e1ac4f40"
15+
integrity sha512-CuPgU6f3eT/XgKKPqKd/gLZV1Xmvf1a2R5POBOGQa6uv82xpls89HU5zKeVoyR8XzHd1RGNOlQlvUe3CFkjWNQ==
16+
517
"@types/node@^17.0.27":
618
version "17.0.27"
719
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.27.tgz#f4df3981ae8268c066e8f49995639f855469081e"
@@ -38,6 +50,13 @@
3850
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.2.tgz#1a62f89525723dde24ba1b01b092bf5df8ad4d39"
3951
integrity sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==
4052

53+
"@types/tern@*":
54+
version "0.23.4"
55+
resolved "https://registry.yarnpkg.com/@types/tern/-/tern-0.23.4.tgz#03926eb13dbeaf3ae0d390caf706b2643a0127fb"
56+
integrity sha512-JAUw1iXGO1qaWwEOzxTKJZ/5JxVeON9kvGZ/osgZaJImBnyjyn0cjovPsf6FNLmyGY8Vw9DoXZCMlfMkMwHRWg==
57+
dependencies:
58+
"@types/estree" "*"
59+
4160
"@wasmer/wasi@^0.12.0":
4261
version "0.12.0"
4362
resolved "https://registry.yarnpkg.com/@wasmer/wasi/-/wasi-0.12.0.tgz#89c7c5e5ba58f7dfae4e323359346639c4ec382a"
@@ -89,6 +108,11 @@ buffer@^5.5.0:
89108
base64-js "^1.3.1"
90109
ieee754 "^1.1.13"
91110

111+
codemirror@^5.65.3:
112+
version "5.65.3"
113+
resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.65.3.tgz#2d029930d5a293bc5fb96ceea64654803c0d4ac7"
114+
integrity sha512-kCC0iwGZOVZXHEKW3NDTObvM7pTIyowjty4BUqeREROc/3I6bWbgZDA3fGDwlA+rbgRjvnRnfqs9SfXynel1AQ==
115+
92116
csstype@^3.0.2:
93117
version "3.0.11"
94118
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.0.11.tgz#d66700c5eacfac1940deb4e3ee5642792d85cd33"

0 commit comments

Comments
 (0)