Skip to content

Commit cd90fdd

Browse files
committed
first prototype of play-button enabling only if crate list supported
also minor refactor of clipboard handling TODO: - `no_run` support - test with ACE - disable play button with tooltip instead of hiding
1 parent a6a7c95 commit cd90fdd

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

src/theme/book.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ code {
1919
.hidden {
2020
display: none;
2121
}
22+
.play-button.hidden {
23+
display: none;
24+
}
2225
h2,
2326
h3 {
2427
margin-top: 2.5em;

src/theme/book.js

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
playground_crates = [];
2+
13
$( document ).ready(function() {
24

35
// url
@@ -219,7 +221,7 @@ $( document ).ready(function() {
219221
pre_block.prepend("<div class=\"buttons\"></div>");
220222
buttons = pre_block.find(".buttons");
221223
}
222-
buttons.prepend("<i class=\"fa fa-play play-button\"></i>");
224+
buttons.prepend("<i class=\"fa fa-play play-button hidden\"></i>");
223225
buttons.prepend("<i class=\"fa fa-copy clip-button\"><i class=\"tooltiptext\"></i></i>");
224226

225227
let code_block = pre_block.find("code").first();
@@ -245,14 +247,7 @@ $( document ).ready(function() {
245247
text: function(trigger) {
246248
hideTooltip(trigger);
247249
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);
256251
}
257252
});
258253
clipboardSnippets.on('success', function(e) {
@@ -262,8 +257,54 @@ $( document ).ready(function() {
262257
clipboardSnippets.on('error', function(e) {
263258
showTooltip(e.trigger, "Clipboard error!");
264259
});
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+
265275
});
266276

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 = /extern\s+crate\s+([a-zA-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+
267308
function hideTooltip(elem) {
268309
elem.firstChild.innerText="";
269310
elem.setAttribute('class', 'fa fa-copy clip-button');

src/theme/stylus/general.styl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ code {
2424
display: none;
2525
}
2626

27+
.play-button.hidden {
28+
display: none;
29+
}
30+
2731
h2, h3 { margin-top: 2.5em }
2832
h4, h5 { margin-top: 2em }
2933

0 commit comments

Comments
 (0)