From cd90fdd4077db3bec37f67fa4b3b141526cb98fc Mon Sep 17 00:00:00 2001 From: Michal Budzynski Date: Tue, 8 Aug 2017 00:05:33 +0200 Subject: [PATCH] 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 --- src/theme/book.css | 3 ++ src/theme/book.js | 59 +++++++++++++++++++++++++++++------ src/theme/stylus/general.styl | 4 +++ 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/theme/book.css b/src/theme/book.css index f1d73dbd..abf78685 100644 --- a/src/theme/book.css +++ b/src/theme/book.css @@ -19,6 +19,9 @@ code { .hidden { display: none; } +.play-button.hidden { + display: none; +} h2, h3 { margin-top: 2.5em; diff --git a/src/theme/book.js b/src/theme/book.js index 12e72019..5e4a37f9 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -1,3 +1,5 @@ +playground_crates = []; + $( document ).ready(function() { // url @@ -219,7 +221,7 @@ $( document ).ready(function() { pre_block.prepend("
"); buttons = pre_block.find(".buttons"); } - buttons.prepend(""); + buttons.prepend(""); buttons.prepend(""); let code_block = pre_block.find("code").first(); @@ -245,14 +247,7 @@ $( document ).ready(function() { text: function(trigger) { hideTooltip(trigger); let playpen = $(trigger).parents(".playpen"); - let code_block = playpen.find("code").first(); - - if (window.ace && code_block.hasClass("editable")) { - let editor = window.ace.edit(code_block.get(0)); - return editor.getValue(); - } else { - return code_block.get(0).textContent; - } + return playpen_text(playpen); } }); clipboardSnippets.on('success', function(e) { @@ -262,8 +257,54 @@ $( document ).ready(function() { clipboardSnippets.on('error', function(e) { showTooltip(e.trigger, "Clipboard error!"); }); + + $.ajax({ + url: "https://play.rust-lang.org/meta/crates", + method: "POST", + crossDomain: true, + dataType: "json", + contentType: "application/json", + success: function(response){ + playground_crates = response.crates.map(function(item) {return item["id"];} ); + $(".playpen").each(function(block){ + update_play_button(this, playground_crates); + }); + }, + }); + }); +function playpen_text(playpen) { + let code_block = playpen.find("code").first(); + + if (window.ace && code_block.hasClass("editable")) { + let editor = window.ace.edit(code_block.get(0)); + return editor.getValue(); + } else { + return code_block.get(0).textContent; + } +} + +function update_play_button(block, playground_crates) { + //TODO skip if `no_run` is set + var pre_block = $(block); + var play_button = pre_block.find(".play-button"); + + var txt = playpen_text(pre_block); + + var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g; + var snippet_crates = []; + while (item = re.exec(txt)) + snippet_crates.push(item[1]); + + var all_available = snippet_crates.every(elem => playground_crates.indexOf(elem) > -1); + if (all_available) { + play_button.removeClass("hidden"); + } else { + play_button.addClass("hidden"); + } +} + function hideTooltip(elem) { elem.firstChild.innerText=""; elem.setAttribute('class', 'fa fa-copy clip-button'); diff --git a/src/theme/stylus/general.styl b/src/theme/stylus/general.styl index 7d3a583e..210c3788 100644 --- a/src/theme/stylus/general.styl +++ b/src/theme/stylus/general.styl @@ -24,6 +24,10 @@ code { display: none; } +.play-button.hidden { + display: none; +} + h2, h3 { margin-top: 2.5em } h4, h5 { margin-top: 2em }