Merge pull request #303 from budziq/clipboard

RFC - Initial implementation of clipboard handling
This commit is contained in:
Mathieu David 2017-05-31 22:22:15 +02:00 committed by GitHub
commit 3e80268a44
8 changed files with 92 additions and 0 deletions

View File

@ -171,6 +171,7 @@ impl Renderer for HtmlHandlebars {
book.write_file("highlight.css", &theme.highlight_css)?; book.write_file("highlight.css", &theme.highlight_css)?;
book.write_file("tomorrow-night.css", &theme.tomorrow_night_css)?; book.write_file("tomorrow-night.css", &theme.tomorrow_night_css)?;
book.write_file("highlight.js", &theme.highlight_js)?; book.write_file("highlight.js", &theme.highlight_js)?;
book.write_file("clipboard.min.js", &theme.clipboard_js)?;
book.write_file("_FontAwesome/css/font-awesome.css", theme::FONT_AWESOME)?; book.write_file("_FontAwesome/css/font-awesome.css", theme::FONT_AWESOME)?;
book.write_file("_FontAwesome/fonts/fontawesome-webfont.eot", theme::FONT_AWESOME_EOT)?; book.write_file("_FontAwesome/fonts/fontawesome-webfont.eot", theme::FONT_AWESOME_EOT)?;
book.write_file("_FontAwesome/fonts/fontawesome-webfont.svg", theme::FONT_AWESOME_SVG)?; book.write_file("_FontAwesome/fonts/fontawesome-webfont.svg", theme::FONT_AWESOME_SVG)?;

View File

@ -827,3 +827,25 @@ table thead td {
word-wrap: break-word /* Internet Explorer 5.5+ */; word-wrap: break-word /* Internet Explorer 5.5+ */;
} }
} }
.tooltiptext {
position: absolute;
visibility: hidden;
color: #fff;
background-color: #333;
-webkit-transform: translateX(-50%);
-moz-transform: translateX(-50%);
-o-transform: translateX(-50%);
-ms-transform: translateX(-50%);
transform: translateX(-50%); /* Center by moving tooltip 50% of its width left */
left: -8px; /* Half of the width of the icon */
top: -35px;
font-size: 0.8em;
text-align: center;
border-radius: 6px;
padding: 5px 8px;
margin: 5px;
z-index: 1000;
}
.tooltipped .tooltiptext {
visibility: visible;
}

View File

@ -191,15 +191,41 @@ $( document ).ready(function() {
buttons = pre_block.find(".buttons"); buttons = pre_block.find(".buttons");
} }
buttons.prepend("<i class=\"fa fa-play play-button\"></i>"); buttons.prepend("<i class=\"fa fa-play play-button\"></i>");
buttons.prepend("<i class=\"fa fa-copy clip-button\"><i class=\"tooltiptext\"></i></i>");
buttons.find(".play-button").click(function(e){ buttons.find(".play-button").click(function(e){
run_rust_code(pre_block); run_rust_code(pre_block);
}); });
buttons.find(".clip-button").mouseout(function(e){
hideTooltip(e.currentTarget);
});
}); });
var clipboardSnippets = new Clipboard('.clip-button', {
text: function(trigger) {
hideTooltip(trigger);
return trigger.parentElement.parentElement.textContent;
}
});
clipboardSnippets.on('success', function(e) {
e.clearSelection();
showTooltip(e.trigger, "Copied!");
});
clipboardSnippets.on('error', function(e) {
showTooltip(e.trigger, "Clipboard error!");
});
}); });
function hideTooltip(elem) {
elem.firstChild.innerText="";
elem.setAttribute('class', 'fa fa-copy clip-button');
}
function showTooltip(elem, msg) {
elem.firstChild.innerText=msg;
elem.setAttribute('class', 'fa fa-copy tooltipped');
}
function run_rust_code(code_block) { function run_rust_code(code_block) {
var result_block = code_block.find(".result"); var result_block = code_block.find(".result");

7
src/theme/clipboard.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -24,6 +24,14 @@
<!-- MathJax --> <!-- MathJax -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<!-- Fetch Clipboard.js from CDN but have a local fallback -->
<script src="https://cdn.jsdelivr.net/clipboard.js/1.6.1/clipboard.min.js"></script>
<script>
if (typeof Clipboard == 'undefined') {
document.write(unescape("%3Cscript src='clipboard.min.js'%3E%3C/script%3E"));
}
</script>
<!-- Fetch JQuery from CDN but have a local fallback --> <!-- Fetch JQuery from CDN but have a local fallback -->
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script> <script>

View File

@ -11,6 +11,7 @@ pub static HIGHLIGHT_JS: &'static [u8] = include_bytes!("highlight.js");
pub static TOMORROW_NIGHT_CSS: &'static [u8] = include_bytes!("tomorrow-night.css"); pub static TOMORROW_NIGHT_CSS: &'static [u8] = include_bytes!("tomorrow-night.css");
pub static HIGHLIGHT_CSS: &'static [u8] = include_bytes!("highlight.css"); pub static HIGHLIGHT_CSS: &'static [u8] = include_bytes!("highlight.css");
pub static JQUERY: &'static [u8] = include_bytes!("jquery-2.1.4.min.js"); pub static JQUERY: &'static [u8] = include_bytes!("jquery-2.1.4.min.js");
pub static CLIPBOARD_JS: &'static [u8] = include_bytes!("clipboard.min.js");
pub static FONT_AWESOME: &'static [u8] = include_bytes!("_FontAwesome/css/font-awesome.min.css"); pub static FONT_AWESOME: &'static [u8] = include_bytes!("_FontAwesome/css/font-awesome.min.css");
pub static FONT_AWESOME_EOT: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.eot"); pub static FONT_AWESOME_EOT: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.eot");
pub static FONT_AWESOME_SVG: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.svg"); pub static FONT_AWESOME_SVG: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.svg");
@ -36,6 +37,7 @@ pub struct Theme {
pub highlight_css: Vec<u8>, pub highlight_css: Vec<u8>,
pub tomorrow_night_css: Vec<u8>, pub tomorrow_night_css: Vec<u8>,
pub highlight_js: Vec<u8>, pub highlight_js: Vec<u8>,
pub clipboard_js: Vec<u8>,
pub jquery: Vec<u8>, pub jquery: Vec<u8>,
} }
@ -51,6 +53,7 @@ impl Theme {
highlight_css: HIGHLIGHT_CSS.to_owned(), highlight_css: HIGHLIGHT_CSS.to_owned(),
tomorrow_night_css: TOMORROW_NIGHT_CSS.to_owned(), tomorrow_night_css: TOMORROW_NIGHT_CSS.to_owned(),
highlight_js: HIGHLIGHT_JS.to_owned(), highlight_js: HIGHLIGHT_JS.to_owned(),
clipboard_js: CLIPBOARD_JS.to_owned(),
jquery: JQUERY.to_owned(), jquery: JQUERY.to_owned(),
}; };
@ -91,6 +94,12 @@ impl Theme {
let _ = f.read_to_end(&mut theme.highlight_js); let _ = f.read_to_end(&mut theme.highlight_js);
} }
// clipboard.js
if let Ok(mut f) = File::open(&src.join("clipboard.min.js")) {
theme.clipboard_js.clear();
let _ = f.read_to_end(&mut theme.clipboard_js);
}
// highlight.css // highlight.css
if let Ok(mut f) = File::open(&src.join("highlight.css")) { if let Ok(mut f) = File::open(&src.join("highlight.css")) {
theme.highlight_css.clear(); theme.highlight_css.clear();

View File

@ -8,3 +8,4 @@
@import 'theme-popup' @import 'theme-popup'
@import 'themes' @import 'themes'
@import 'print' @import 'print'
@import 'tooltip'

View File

@ -0,0 +1,18 @@
.tooltiptext {
position: absolute;
visibility: hidden;
color: #fff;
background-color: #333;
transform: translateX(-50%); /* Center by moving tooltip 50% of its width left */
left: -8px; /* Half of the width of the icon */
top: -35px;
font-size: 0.8em;
text-align: center;
border-radius: 6px;
padding: 5px 8px;
margin: 5px;
z-index: 1000;
}
.tooltipped .tooltiptext {
visibility: visible;
}