1
+ playground_crates = [ ] ;
2
+
1
3
$ ( document ) . ready ( function ( ) {
2
4
3
5
// url
@@ -219,7 +221,7 @@ $( document ).ready(function() {
219
221
pre_block . prepend ( "<div class=\"buttons\"></div>" ) ;
220
222
buttons = pre_block . find ( ".buttons" ) ;
221
223
}
222
- buttons . prepend ( "<i class=\"fa fa-play play-button\"></i>" ) ;
224
+ buttons . prepend ( "<i class=\"fa fa-play play-button hidden \"></i>" ) ;
223
225
buttons . prepend ( "<i class=\"fa fa-copy clip-button\"><i class=\"tooltiptext\"></i></i>" ) ;
224
226
225
227
let code_block = pre_block . find ( "code" ) . first ( ) ;
@@ -245,14 +247,7 @@ $( document ).ready(function() {
245
247
text : function ( trigger ) {
246
248
hideTooltip ( trigger ) ;
247
249
let playpen = $ ( trigger ) . parents ( ".playpen" ) ;
248
- let code_block = playpen . find ( "code" ) . first ( ) ;
249
-
250
- if ( window . ace && code_block . hasClass ( "editable" ) ) {
251
- let editor = window . ace . edit ( code_block . get ( 0 ) ) ;
252
- return editor . getValue ( ) ;
253
- } else {
254
- return code_block . get ( 0 ) . textContent ;
255
- }
250
+ return playpen_text ( playpen ) ;
256
251
}
257
252
} ) ;
258
253
clipboardSnippets . on ( 'success' , function ( e ) {
@@ -262,8 +257,54 @@ $( document ).ready(function() {
262
257
clipboardSnippets . on ( 'error' , function ( e ) {
263
258
showTooltip ( e . trigger , "Clipboard error!" ) ;
264
259
} ) ;
260
+
261
+ $ . ajax ( {
262
+ url : "https://play.rust-lang.org/meta/crates" ,
263
+ method : "POST" ,
264
+ crossDomain : true ,
265
+ dataType : "json" ,
266
+ contentType : "application/json" ,
267
+ 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 ) ;
271
+ } ) ;
272
+ } ,
273
+ } ) ;
274
+
265
275
} ) ;
266
276
277
+ function playpen_text ( playpen ) {
278
+ let code_block = playpen . find ( "code" ) . first ( ) ;
279
+
280
+ if ( window . ace && code_block . hasClass ( "editable" ) ) {
281
+ let editor = window . ace . edit ( code_block . get ( 0 ) ) ;
282
+ return editor . getValue ( ) ;
283
+ } else {
284
+ return code_block . get ( 0 ) . textContent ;
285
+ }
286
+ }
287
+
288
+ function update_play_button ( block , playground_crates ) {
289
+ //TODO skip if `no_run` is set
290
+ var pre_block = $ ( block ) ;
291
+ var play_button = pre_block . find ( ".play-button" ) ;
292
+
293
+ var txt = playpen_text ( pre_block ) ;
294
+
295
+ var re = / e x t e r n \s + c r a t e \s + ( [ a - z A - Z _ 0 - 9 ] + ) \s * ; / g;
296
+ var snippet_crates = [ ] ;
297
+ while ( item = re . exec ( txt ) )
298
+ snippet_crates . push ( item [ 1 ] ) ;
299
+
300
+ var all_available = snippet_crates . every ( elem => playground_crates . indexOf ( elem ) > - 1 ) ;
301
+ if ( all_available ) {
302
+ play_button . removeClass ( "hidden" ) ;
303
+ } else {
304
+ play_button . addClass ( "hidden" ) ;
305
+ }
306
+ }
307
+
267
308
function hideTooltip ( elem ) {
268
309
elem . firstChild . innerText = "" ;
269
310
elem . setAttribute ( 'class' , 'fa fa-copy clip-button' ) ;
0 commit comments