adding copy buttons to code blocks

This commit is contained in:
Chris Holdgraf
2018-07-05 17:51:51 -07:00
parent 4fcf41a6c6
commit c6fbf47774
8 changed files with 70 additions and 13 deletions

22
docs/_static/custom.css vendored Normal file
View File

@@ -0,0 +1,22 @@
button.copybtn {
webkit-transition: opacity .3s ease-in-out;
-o-transition: opacity .3s ease-in-out;
transition: opacity .3s ease-in-out;
opacity: 0;
padding: 2px 6px;
position: absolute;
right: 4px;
top: 4px;
}
div.highlight:hover .copybtn, div.highlight .copybtn:focus {
opacity: .3;
}
div.highlight .copybtn:hover {
opacity: 1;
}
div.highlight {
position: relative;
}

25
docs/_static/custom.js vendored Normal file
View File

@@ -0,0 +1,25 @@
// Set up copy/paste for code blocks
function addCopyButtonToCode(){
// get all <code> elements
var allCodeBlocksElements = $( "div.highlight pre" );
allCodeBlocksElements.each(function(ii) {
// add different id for each code block
// target
var currentId = "codeblock" + (ii + 1);
$(this).attr('id', currentId);
//trigger
var clipButton = '<button class="btn copybtn" data-clipboard-target="#' + currentId + '"><img src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard"></button>';
$(this).after(clipButton);
});
new Clipboard('.btn');
}
$(document).ready(function () {
// Highlight current page in sidebar
console.log('hi');
addCopyButtonToCode();
});