From e567d22f1cc0fe0ffd80375be0381edab8cfb9c8 Mon Sep 17 00:00:00 2001 From: Michal Budzynski Date: Wed, 31 May 2017 01:14:01 +0200 Subject: [PATCH 1/3] Initial implementation of clipboard handling --- src/theme/book.css | 19 +++++++++++++++++++ src/theme/book.js | 20 ++++++++++++++++++++ src/theme/index.hbs | 3 +++ src/theme/stylus/book.styl | 1 + src/theme/stylus/tooltip.styl | 17 +++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 src/theme/stylus/tooltip.styl diff --git a/src/theme/book.css b/src/theme/book.css index 355d7603..8c093abb 100644 --- a/src/theme/book.css +++ b/src/theme/book.css @@ -827,3 +827,22 @@ table thead td { word-wrap: break-word /* Internet Explorer 5.5+ */; } } +.tooltiptext { + visibility: hidden; + background-color: #000; + color: #fff; + opacity: 0.8; + -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)"; + filter: alpha(opacity=80); + text-align: center; + border-radius: 6px; + padding: 5px 8px; + margin: 5px; + left: -250%; + bottom: 100%; + position: absolute; + z-index: 1000; +} +.tooltipped .tooltiptext { + visibility: visible; +} diff --git a/src/theme/book.js b/src/theme/book.js index 424bac56..20661baa 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -191,15 +191,35 @@ $( document ).ready(function() { buttons = pre_block.find(".buttons"); } buttons.prepend(""); + buttons.prepend(""); buttons.find(".play-button").click(function(e){ run_rust_code(pre_block); }); + buttons.find(".clip-button").mouseout(function(e){ + e.currentTarget.setAttribute('class', 'fa fa-copy clip-button'); + }); }); + var clipboardSnippets = new Clipboard('.clip-button', { + text: function(trigger) { + return trigger.parentElement.parentElement.textContent; + } + }); + clipboardSnippets.on('success', function(e) { + e.clearSelection(); + showTooltip(e, "Copied!"); + }); + clipboardSnippets.on('error', function(e) { + showTooltip(e, "Clipboard error!"); + }); }); +function showTooltip(elem, msg) { + elem.trigger.firstChild.innerText=msg; + elem.trigger.setAttribute('class', 'fa fa-copy tooltipped'); +} function run_rust_code(code_block) { var result_block = code_block.find(".result"); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index b3985fbc..5e5387f4 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -24,6 +24,9 @@ + + + - + + diff --git a/src/theme/mod.rs b/src/theme/mod.rs index 6082ff51..3570cb24 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -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 HIGHLIGHT_CSS: &'static [u8] = include_bytes!("highlight.css"); 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_EOT: &'static [u8] = include_bytes!("_FontAwesome/fonts/fontawesome-webfont.eot"); 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, pub tomorrow_night_css: Vec, pub highlight_js: Vec, + pub clipboard_js: Vec, pub jquery: Vec, } @@ -51,6 +53,7 @@ impl Theme { highlight_css: HIGHLIGHT_CSS.to_owned(), tomorrow_night_css: TOMORROW_NIGHT_CSS.to_owned(), highlight_js: HIGHLIGHT_JS.to_owned(), + clipboard_js: CLIPBOARD_JS.to_owned(), jquery: JQUERY.to_owned(), }; @@ -91,6 +94,12 @@ impl Theme { 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 if let Ok(mut f) = File::open(&src.join("highlight.css")) { theme.highlight_css.clear();