Skip to content

Commit de6f0a3

Browse files
committedDec 19, 2013
Add 'Link Code' button to rspec example
1 parent e6f0721 commit de6f0a3

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed
 

‎source/javascripts/playground-rspec.js.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ def initialize
2020

2121
@result = Element['#result-frame']
2222

23-
@ruby.value = RUBY
24-
2523
Element.find('#run-code').on(:click) { run_code }
24+
@link = Element.find('#link-code')
25+
26+
hash = `decodeURIComponent(location.hash)`
27+
28+
if hash.start_with? '#code:'
29+
@ruby.value = hash[6..-1]
30+
else
31+
@ruby.value = RUBY.strip
32+
end
33+
2634
run_code
2735
end
2836

2937
def run_code
38+
@link[:href] = "#code:#{`encodeURIComponent(#{@ruby.value})`}"
3039
js = Opal.compile @ruby.value
3140

3241
update_iframe(<<-HTML)
@@ -48,6 +57,8 @@ def run_code
4857
</body>
4958
</html>
5059
HTML
60+
rescue => e
61+
alert "#{e.class}: #{e.message}"
5162
end
5263

5364
def update_iframe(html)

‎source/rspec.html.haml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
!!!
32
%html
43
%head
@@ -15,10 +14,13 @@
1514
%a.navbar-brand Opal Playground - RSpec
1615
.container
1716
.collapse.navbar-collapse
18-
%ul.nav.navbar-nav
17+
%ul.nav.navbar-nav(style="padding: 7px")
1918
%li
2019
%button.btn.btn-primary#run-code
2120
Run Code (Cmd+Enter)
21+
%li
22+
%a.btn.btn-default#link-code(style="padding: 6px")
23+
Link Code
2224

2325
.row
2426
.col-md-6(style="height: 100%")

0 commit comments

Comments
 (0)
Please sign in to comment.