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

Commit f5bed1d

Browse files
committed
Simplify by removing react
1 parent 5e11f87 commit f5bed1d

12 files changed

+98
-234
lines changed

bin/build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const wasmPlugin = require("./wasmPlugin");
77

88
esbuild.build({
99
bundle: true,
10-
entryPoints: [path.join(__dirname, "../src/index.tsx")],
10+
entryPoints: [path.join(__dirname, "../src/index.ts")],
1111
format: "esm",
1212
minify: true,
1313
outdir: path.join(__dirname, "../docs"),

bin/serve

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const serveOptions = {
1111

1212
const buildOptions = {
1313
bundle: true,
14-
entryPoints: [path.join(__dirname, "../src/index.tsx")],
14+
entryPoints: [path.join(__dirname, "../src/index.ts")],
1515
format: "esm",
1616
outdir: path.join(__dirname, "../docs"),
1717
plugins: [wasmPlugin],

docs/index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
<link rel="stylesheet" type="text/css" href="index.css">
88
</head>
99
<body>
10-
<noscript>JavaScript must be enabled in order to run this application.</noscript>
11-
<div id="root"></div>
10+
<main>
11+
<nav>
12+
<h1>Syntax Tree</h1>
13+
<a href="https://ruby-syntax-tree.github.io/syntax_tree">Docs</a>
14+
<a href="https://github.com/ruby-syntax-tree/syntax_tree">Source</a>
15+
<span><button type="button" id="format">Format</button></span>
16+
</nav>
17+
<textarea id="editor"></textarea>
18+
<textarea id="tree" disabled readonly>Loading...</textarea>
19+
</main>
1220
<script type="module" src="index.js"></script>
1321
</body>
1422
</html>

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@
99
"@wasmer/wasmfs": "^0.12.0",
1010
"codemirror": "^5.65.3",
1111
"path-browserify": "^1.0.1",
12-
"react": "^18.0.0",
13-
"react-dom": "^18.0.0",
1412
"ruby-head-wasm-wasi": "^0.3.0"
1513
},
1614
"devDependencies": {
1715
"@types/codemirror": "^5.60.5",
1816
"@types/node": "^17.0.27",
1917
"@types/path-browserify": "^1.0.0",
20-
"@types/react": "^18.0.6",
21-
"@types/react-dom": "^18.0.2",
2218
"esbuild": "^0.14.38"
2319
}
2420
}

src/CodeMirror.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import CodeMirror from "codemirror";
2+
3+
import "codemirror/mode/ruby/ruby";
4+
import "codemirror/lib/codemirror.css";
5+
import "codemirror/theme/xq-light.css";
6+
7+
export default CodeMirror;

src/Editor.tsx

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Tree.tsx

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ html, body {
55
margin: 0;
66
}
77

8-
#root {
8+
main {
99
display: grid;
1010
height: 100%;
1111
grid-template-columns: repeat(2, 1fr);

src/index.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import "./index.css";
2+
3+
import type { Editor } from "codemirror";
4+
import type { Ruby } from "./createRuby";
5+
import initialSource from "./initialSource";
6+
7+
let editorElement = document.getElementById("editor") as HTMLTextAreaElement;
8+
const formatElement = document.getElementById("format");
9+
const treeElement = document.getElementById("tree") as HTMLTextAreaElement;
10+
11+
// First, set the initial value of the editor element. It's just going to be a
12+
// textarea until we've loaded the editor chunk.
13+
editorElement.value = initialSource;
14+
15+
let editor: Editor = null;
16+
let ruby: Ruby = null;
17+
18+
type SourceChangedEvent = {
19+
source: string
20+
};;
21+
22+
// This function is called to set up the event handlers once both the editor and
23+
// the ruby chunk have been loaded.
24+
function synchronize() {
25+
// We're going to handle updates to the source through a custom event. This
26+
// turns out to be faster than handling the change event directly on the
27+
// editor since it blocks updates to the UI until the event handled returns.
28+
document.body.addEventListener("source-changed", (event: CustomEvent<SourceChangedEvent>) => {
29+
try {
30+
treeElement.value = ruby.prettyPrint(event.detail.source);
31+
} catch (error) {
32+
// For now, just ignoring the error. Eventually I'd like to make this mark
33+
// an error state on the editor to give feedback to the user.
34+
}
35+
});
36+
37+
// Attach to the editor and dispatch custom source-changed events whenever the
38+
// value is updated in the editor.
39+
editor.on("change", () => {
40+
document.body.dispatchEvent(new CustomEvent<SourceChangedEvent>("source-changed", {
41+
detail: { source: editor.getDoc().getValue() }
42+
}));
43+
});
44+
45+
// Attach to the format button to update the source whenever the button is
46+
// clicked.
47+
formatElement.addEventListener("click", () => {
48+
editor.getDoc().setValue(ruby.format(editor.getValue()));
49+
});
50+
51+
// Finally, un-disable the tree element.
52+
treeElement.disabled = false;
53+
}
54+
55+
import("./CodeMirror").then(({ default: CodeMirror }) => {
56+
const newEditorElement = document.createElement("div");
57+
editorElement.replaceWith(newEditorElement);
58+
59+
editor = CodeMirror(newEditorElement, {
60+
lineNumbers: true,
61+
mode: "ruby",
62+
theme: "xq-light",
63+
value: editorElement.value
64+
});
65+
66+
if (ruby) {
67+
synchronize();
68+
}
69+
});
70+
71+
import("./createRuby").then(({ default: createRuby }) => createRuby()).then((created) => {
72+
ruby = created;
73+
74+
if (editor) {
75+
synchronize();
76+
}
77+
});

src/index.tsx

Lines changed: 0 additions & 67 deletions
This file was deleted.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"noImplicitAny": true,
1010
"target": "es5"
1111
},
12-
"files": ["src/index.tsx"]
12+
"files": ["src/index.ts"]
1313
}

0 commit comments

Comments
 (0)