1
1
import "./index.css" ;
2
2
3
+ type SourceChangedEvent = { source : string } ;
4
+ type DisplayChangedEvent = { kind : "prettyPrint" | "disasm" } ;
5
+
3
6
Promise . all ( [
4
7
// We're going to load the editor asynchronously so that we can get to
5
8
// first-paint faster. This works out nicely since we can use a textarea until
@@ -21,18 +24,52 @@ Promise.all([
21
24
// of the actual functional one is just going to display "Loading...".
22
25
import ( "./createRuby" ) . then ( ( { default : createRuby } ) => createRuby ( ) )
23
26
] ) . 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
+ } ) ;
29
66
30
67
// We're going to handle updates to the source through a custom event. This
31
68
// turns out to be faster than handling the change event directly on the
32
69
// 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 > ) => {
34
71
try {
35
- tree . value = ruby . prettyPrint ( event . detail . source ) ;
72
+ output . value = displayFunction ( event . detail . source ) ;
36
73
} catch ( error ) {
37
74
// For now, just ignoring the error. Eventually I'd like to make this mark
38
75
// an error state on the editor to give feedback to the user.
@@ -42,14 +79,17 @@ Promise.all([
42
79
// Attach to the editor and dispatch custom source-changed events whenever the
43
80
// value is updated in the editor.
44
81
editor . on ( "change" , ( ) => {
45
- tree . dispatchEvent ( new CustomEvent < { source : string } > ( "source-changed" , {
82
+ output . dispatchEvent ( new CustomEvent < SourceChangedEvent > ( "source-changed" , {
46
83
detail : { source : editor . getDoc ( ) . getValue ( ) }
47
84
} ) ) ;
48
85
} ) ;
49
86
50
87
// Attach to the format button to update the source whenever the button is
51
88
// clicked.
52
- document . getElementById ( "format" ) . addEventListener ( "click" , ( ) => {
89
+ const format = document . getElementById ( "format" ) ;
90
+ format . disabled = false ;
91
+
92
+ format . addEventListener ( "click" , ( ) => {
53
93
editor . getDoc ( ) . setValue ( ruby . format ( editor . getValue ( ) ) ) ;
54
94
} ) ;
55
95
} ) ;
0 commit comments