Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit e41c253

Browse files
author
Barrie Hadfield
committed
getting there
1 parent 52255e5 commit e41c253

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

source/_components.html.erb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
class CodeMirror < React::Component::Base
44
param :code, type: String
5-
define_state :output_code
65
define_state :error_message
6+
77
before_mount do
8-
state.output_code! params.code
98
state.error_message! ""
109
end
10+
1111
after_mount do
1212
@editor = `CodeMirror(document.getElementById("code"), {
1313
value: #{params.code},
@@ -18,23 +18,32 @@ class CodeMirror < React::Component::Base
1818
theme: 'github'
1919
});`
2020
`#{@editor}.on('change', #{lambda {on_change} })`
21+
execute_code
2122
end
23+
2224
render(DIV) do
2325
div(id: 'code')
26+
br
27+
div(id: 'result')
2428
p { state.error_message }
25-
p { state.output_code }
2629
end
2730

2831
def on_change
2932
state.error_message! ""
30-
compiled_code = Opal.compile `#{@editor}.getValue()`
31-
state.output_code! compiled_code
33+
execute_code
34+
end
35+
36+
after_mount :execute_code
37+
after_update :execute_code
38+
39+
def execute_code
40+
compiled_code = Opal::Compiler.new(`#{@editor}.getValue()`).compile
41+
`ReactDOM.unmountComponentAtNode(document.getElementById("result"));`
3242
`eval(#{compiled_code})`
43+
Element['#result'].render{ Alfie() }
3344
rescue Exception => e
3445
@time_out = after(0.5) do
35-
# Element[mount_node].render { div.playgroundError { e.message } }
3646
state.error_message! e.message
37-
state.output_code! ""
3847
end
3948
end
4049

source/index.html.md.erb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<div data-reactrb-mount="CodeMirror" data-code="class TestApp < React::Component::Base
2-
param :name
3-
before_mount { @timer = every(1) { force_update! } }
1+
<div data-reactrb-mount="CodeMirror" data-code="class Alfie < React::Component::Base
42
render(DIV) do
5-
h1 {'The current time is #{Time.now}'}
6-
h2 { 'here hello #{params.name}' }.on(:click) do
3+
h1 { 'Alfie is here' }.on(:click) do
74
alert 'you clicked'
85
end
96
end
10-
end"></div>
7+
end
8+
"></div>
9+
10+
<!-- Element['#playground'].render{ Alfie() } -->
1111

1212
<!-- <form><textarea id="code" name="code">
1313
class TestApp < React::Component::Base

0 commit comments

Comments
 (0)