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

Commit a4671c9

Browse files
committed
Add some toggles
1 parent d941d38 commit a4671c9

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

docs/index.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
<h1>Syntax Tree</h1>
1313
<a href="https://ruby-syntax-tree.github.io/syntax_tree">Docs</a>
1414
<a href="https://github.com/ruby-syntax-tree/syntax_tree">Source</a>
15-
<span><button type="button" id="format">Format</button></span>
15+
<span><button type="button" id="format" disabled>Format</button></span>
16+
17+
<div class="toggles">
18+
<span><button type="button" value="prettyPrint" disabled>AST</button></span>
19+
<span><button type="button" value="disasm" disabled>ISEQ</button></span>
20+
</div>
1621
</nav>
1722
<textarea id="editor"># frozen_string_literal: true
1823

@@ -73,7 +78,7 @@ <h1>Syntax Tree</h1>
7378
end
7479
end
7580
</textarea>
76-
<textarea id="tree" disabled readonly>Loading...</textarea>
81+
<textarea id="output" disabled readonly>Loading...</textarea>
7782
</main>
7883
<script type="module" src="index.js"></script>
7984
</body>

src/index.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ nav h1, nav a, nav span {
3636
padding: 0 1em;
3737
}
3838

39+
.toggles {
40+
margin-left: auto;
41+
padding-right: 1em;
42+
}
43+
3944
textarea {
4045
font-size: 0.9em;
4146
overflow-wrap: normal;

src/index.ts

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import "./index.css";
22

3+
type SourceChangedEvent = { source: string };
4+
type DisplayChangedEvent = { kind: "prettyPrint" | "disasm" };
5+
36
Promise.all([
47
// We're going to load the editor asynchronously so that we can get to
58
// first-paint faster. This works out nicely since we can use a textarea until
@@ -21,18 +24,52 @@ Promise.all([
2124
// of the actual functional one is just going to display "Loading...".
2225
import("./createRuby").then(({ default: createRuby }) => createRuby())
2326
]).then(([editor, ruby]) => {
24-
// First, grab a reference to the tree element so that we can update it. Then,
25-
// set it initially to the tree represented by the source already there.
26-
const tree = document.getElementById("tree") as HTMLTextAreaElement;
27-
tree.value = ruby.prettyPrint(editor.getDoc().getValue());
28-
tree.disabled = false;
27+
// First, grab a reference to the output element so that we can update it.
28+
// Then, set it initially to the output represented by the source.
29+
const output = document.getElementById("output") as HTMLTextAreaElement;
30+
output.value = ruby.prettyPrint(editor.getDoc().getValue());
31+
output.disabled = false;
32+
33+
// This is the function that will be used to display the output from the
34+
// source.
35+
let displayFunction = ruby.prettyPrint;
36+
37+
// Handle a custom event here for if the display option changed.
38+
output.addEventListener("display-changed", (event: CustomEvent<DisplayChangedEvent>) => {
39+
displayFunction = ruby[event.detail.kind];
40+
41+
try {
42+
output.value = displayFunction(editor.getDoc().getValue());
43+
} catch (error) {
44+
// For now, just ignoring the error. Eventually I'd like to make this mark
45+
// an error state on the editor to give feedback to the user.
46+
}
47+
});
48+
49+
// Hook into both the output toggles to make sure they send out the correct
50+
// event information.
51+
const toggles = document.getElementsByClassName("toggles")[0];
52+
53+
toggles.querySelectorAll("button").forEach((button) => {
54+
button.disabled = (button.value === "prettyPrint");
55+
56+
button.addEventListener("click", () => {
57+
toggles.querySelectorAll("button").forEach((toggle) => {
58+
toggle.disabled = (button.value === toggle.value);
59+
});
60+
61+
output.dispatchEvent(new CustomEvent<DisplayChangedEvent>("display-changed", {
62+
detail: { kind: button.value as DisplayChangedEvent["kind"] }
63+
}));
64+
});
65+
});
2966

3067
// We're going to handle updates to the source through a custom event. This
3168
// turns out to be faster than handling the change event directly on the
3269
// editor since it blocks updates to the UI until the event handled returns.
33-
tree.addEventListener("source-changed", (event: CustomEvent<{ source: string }>) => {
70+
output.addEventListener("source-changed", (event: CustomEvent<SourceChangedEvent>) => {
3471
try {
35-
tree.value = ruby.prettyPrint(event.detail.source);
72+
output.value = displayFunction(event.detail.source);
3673
} catch (error) {
3774
// For now, just ignoring the error. Eventually I'd like to make this mark
3875
// an error state on the editor to give feedback to the user.
@@ -42,14 +79,17 @@ Promise.all([
4279
// Attach to the editor and dispatch custom source-changed events whenever the
4380
// value is updated in the editor.
4481
editor.on("change", () => {
45-
tree.dispatchEvent(new CustomEvent<{ source: string }>("source-changed", {
82+
output.dispatchEvent(new CustomEvent<SourceChangedEvent>("source-changed", {
4683
detail: { source: editor.getDoc().getValue() }
4784
}));
4885
});
4986

5087
// Attach to the format button to update the source whenever the button is
5188
// clicked.
52-
document.getElementById("format").addEventListener("click", () => {
89+
const format = document.getElementById("format");
90+
format.disabled = false;
91+
92+
format.addEventListener("click", () => {
5393
editor.getDoc().setValue(ruby.format(editor.getValue()));
5494
});
5595
});

0 commit comments

Comments
 (0)