1
- playground_crates = [ ] ;
2
-
3
1
$ ( document ) . ready ( function ( ) {
4
2
5
3
// url
@@ -19,7 +17,7 @@ $( document ).ready(function() {
19
17
tabReplace : ' ' , // 4 spaces
20
18
languages : [ ] , // Languages used for auto-detection
21
19
} ) ;
22
-
20
+
23
21
if ( window . ace ) {
24
22
// language-rust class needs to be removed for editable
25
23
// blocks or highlightjs will capture events
@@ -33,7 +31,7 @@ $( document ).ready(function() {
33
31
hljs . highlightBlock ( block ) ;
34
32
} ) ;
35
33
}
36
-
34
+
37
35
// Adding the hljs class gives code blocks the color css
38
36
// even if highlighting doesn't apply
39
37
$ ( 'code' ) . addClass ( 'hljs' ) ;
@@ -130,27 +128,27 @@ $( document ).ready(function() {
130
128
131
129
function set_theme ( theme ) {
132
130
let ace_theme ;
133
-
131
+
134
132
if ( theme == 'coal' || theme == 'navy' ) {
135
133
$ ( "[href='ayu-highlight.css']" ) . prop ( 'disabled' , true ) ;
136
134
$ ( "[href='tomorrow-night.css']" ) . prop ( 'disabled' , false ) ;
137
135
$ ( "[href='highlight.css']" ) . prop ( 'disabled' , true ) ;
138
-
136
+
139
137
ace_theme = "ace/theme/tomorrow_night" ;
140
138
} else if ( theme == 'ayu' ) {
141
139
$ ( "[href='ayu-highlight.css']" ) . prop ( 'disabled' , false ) ;
142
140
$ ( "[href='tomorrow-night.css']" ) . prop ( 'disabled' , true ) ;
143
141
$ ( "[href='highlight.css']" ) . prop ( 'disabled' , true ) ;
144
-
142
+
145
143
ace_theme = "ace/theme/tomorrow_night" ;
146
144
} else {
147
145
$ ( "[href='ayu-highlight.css']" ) . prop ( 'disabled' , true ) ;
148
146
$ ( "[href='tomorrow-night.css']" ) . prop ( 'disabled' , true ) ;
149
147
$ ( "[href='highlight.css']" ) . prop ( 'disabled' , false ) ;
150
-
148
+
151
149
ace_theme = "ace/theme/dawn" ;
152
150
}
153
-
151
+
154
152
if ( window . ace && window . editors ) {
155
153
window . editors . forEach ( function ( editor ) {
156
154
editor . setTheme ( ace_theme ) ;
@@ -265,9 +263,10 @@ $( document ).ready(function() {
265
263
dataType : "json" ,
266
264
contentType : "application/json" ,
267
265
success : function ( response ) {
268
- playground_crates = response . crates . map ( function ( item ) { return item [ "id" ] ; } ) ;
269
- $ ( ".playpen" ) . each ( function ( block ) {
270
- update_play_button ( this , playground_crates ) ;
266
+ // get list of crates available in the rust playground
267
+ let playground_crates = response . crates . map ( function ( item ) { return item [ "id" ] ; } ) ;
268
+ $ ( ".playpen" ) . each ( function ( block ) {
269
+ handle_crate_list_update ( $ ( this ) , playground_crates ) ;
271
270
} ) ;
272
271
} ,
273
272
} ) ;
@@ -285,19 +284,47 @@ function playpen_text(playpen) {
285
284
}
286
285
}
287
286
288
- function update_play_button ( block , playground_crates ) {
289
- //TODO skip if `no_run` is set
290
- var pre_block = $ ( block ) ;
287
+ function handle_crate_list_update ( playpen_block , playground_crates ) {
288
+ // update the play buttons after receiving the response
289
+ update_play_button ( playpen_block , playground_crates ) ;
290
+
291
+ // and install on change listener to dynamically update ACE editors
292
+ if ( window . ace ) {
293
+ let code_block = playpen_block . find ( "code" ) . first ( ) ;
294
+ if ( code_block . hasClass ( "editable" ) ) {
295
+ let editor = window . ace . edit ( code_block . get ( 0 ) ) ;
296
+ editor . on ( "change" , function ( e ) {
297
+ update_play_button ( playpen_block , playground_crates ) ;
298
+ } ) ;
299
+ }
300
+ }
301
+ }
302
+
303
+ // updates the visibility of play button based on `no_run` class and
304
+ // used crates vs ones available on http://play.rust-lang.org
305
+ function update_play_button ( pre_block , playground_crates ) {
291
306
var play_button = pre_block . find ( ".play-button" ) ;
292
307
293
- var txt = playpen_text ( pre_block ) ;
308
+ var classes = pre_block . find ( "code" ) . attr ( "class" ) . split ( " " ) ;
309
+ // skip if code is `no_run`
310
+ if ( classes . indexOf ( "no_run" ) > - 1 ) {
311
+ play_button . addClass ( "hidden" ) ;
312
+ return ;
313
+ }
294
314
315
+ // get list of `extern crate`'s from snippet
316
+ var txt = playpen_text ( pre_block ) ;
295
317
var re = / e x t e r n \s + c r a t e \s + ( [ a - z A - Z _ 0 - 9 ] + ) \s * ; / g;
296
318
var snippet_crates = [ ] ;
297
- while ( item = re . exec ( txt ) )
319
+ while ( item = re . exec ( txt ) ) {
298
320
snippet_crates . push ( item [ 1 ] ) ;
321
+ }
322
+
323
+ // check if all used crates are available on play.rust-lang.org
324
+ var all_available = snippet_crates . every ( function ( elem ) {
325
+ return playground_crates . indexOf ( elem ) > - 1 ;
326
+ } ) ;
299
327
300
- var all_available = snippet_crates . every ( elem => playground_crates . indexOf ( elem ) > - 1 ) ;
301
328
if ( all_available ) {
302
329
play_button . removeClass ( "hidden" ) ;
303
330
} else {
@@ -341,15 +368,7 @@ function run_rust_code(code_block) {
341
368
result_block = code_block . find ( ".result" ) ;
342
369
}
343
370
344
- let text ;
345
-
346
- let inner_code_block = code_block . find ( "code" ) . first ( ) ;
347
- if ( window . ace && inner_code_block . hasClass ( "editable" ) ) {
348
- let editor = window . ace . edit ( inner_code_block . get ( 0 ) ) ;
349
- text = editor . getValue ( ) ;
350
- } else {
351
- text = inner_code_block . text ( ) ;
352
- }
371
+ let text = playpen_text ( code_block ) ; ;
353
372
354
373
var params = {
355
374
version : "stable" ,
0 commit comments