diff --git a/source/index.html.md b/source/index.html.md index 27cf28a..1bf6780 100644 --- a/source/index.html.md +++ b/source/index.html.md @@ -15,6 +15,8 @@ includes: - errors search: true + +code_clipboard: true --- # Introduction diff --git a/source/javascripts/all_nosearch.js b/source/javascripts/all_nosearch.js index b18c1d8..8fa3158 100644 --- a/source/javascripts/all_nosearch.js +++ b/source/javascripts/all_nosearch.js @@ -1,4 +1,5 @@ //= require ./lib/_energize +//= require ./app/_copy //= require ./app/_toc //= require ./app/_lang diff --git a/source/javascripts/app/_copy.js b/source/javascripts/app/_copy.js new file mode 100644 index 0000000..f1e3a43 --- /dev/null +++ b/source/javascripts/app/_copy.js @@ -0,0 +1,15 @@ +function copyToClipboard(container) { + const el = document.createElement('textarea'); + el.value = container.textContent; + document.body.appendChild(el); + el.select(); + document.execCommand('copy'); + document.body.removeChild(el); +} + +function setupCodeCopy() { + $('pre.highlight').prepend('
'); + $('.copy-clipboard').on('click', function() { + copyToClipboard(this.parentNode.children[1]); + }); +} diff --git a/source/layouts/layout.erb b/source/layouts/layout.erb index eba77da..c125d66 100644 --- a/source/layouts/layout.erb +++ b/source/layouts/layout.erb @@ -47,6 +47,12 @@ under the License. <% else %> <%= javascript_include_tag "all_nosearch" %> <% end %> + + <% if current_page.data.code_clipboard %> + + <% end %> diff --git a/source/stylesheets/print.css.scss b/source/stylesheets/print.css.scss index adf8b66..aea88c3 100644 --- a/source/stylesheets/print.css.scss +++ b/source/stylesheets/print.css.scss @@ -144,4 +144,10 @@ body { aside.success:before { @extend %icon-ok-sign; } -} \ No newline at end of file +} + +.copy-clipboard { + @media print { + display: none + } +} diff --git a/source/stylesheets/screen.css.scss b/source/stylesheets/screen.css.scss index ed23b31..deec095 100644 --- a/source/stylesheets/screen.css.scss +++ b/source/stylesheets/screen.css.scss @@ -617,3 +617,16 @@ html, body { .highlight, .highlight .w { background-color: $code-bg; } + +.copy-clipboard { + float: right; + fill: #9DAAB6; + cursor: pointer; + opacity: 0.4; + height: 18px; + width: 18px; +} + +.copy-clipboard:hover { + opacity: 0.8; +}