Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Fixing copy code functionality for bash command #6465

Merged
merged 1 commit into from
May 26, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions docs/_static/js/copycode.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*Copy code to clipboard*/
LANG_GP = {'default':'>>> ', 'python':'>>> ' , 'scala':'scala>', 'julia':'julia> ', 'r':'> ', 'perl':'pdl>' , 'cpp':'', 'bash':''};
LANG_GP = {'default':'>>> ', 'python':'>>> ' , 'scala':'scala>', 'julia':'julia> ', 'r':'> ', 'perl':'pdl>' , 'cpp':'', 'bash':'$ '};

function addBtn() {
copyBtn = '<button type="button" class="btn btn-primary copy-btn" data-toggle="tooltip"' +
copyBtn = '<button type="button" class="btn btn-primary copy-btn" data-toggle="tooltip"' +
'data-placement="bottom" title="Copy to clipboard"><i class="fa fa-copy"></i></button>'
for (var lang in LANG_GP) {
codeBlock = $('div .highlight-' + lang);
Expand All @@ -24,10 +24,10 @@ function html2clipboard(content) {
tmpEl.style.position = "absolute";
tmpEl.style.pointerEvents = "none";
tmpEl.style.zIndex = -1;

tmpEl.innerHTML = content;
document.body.appendChild(tmpEl);

var range = document.createRange();
range.selectNode(tmpEl);
window.getSelection().addRange(range);
Expand All @@ -43,28 +43,28 @@ $(document).ready(function(){
$(this).attr('title', 'Copy to clipboard').tooltip('fixTitle');
}
);

clipboard = new Clipboard('.copy-btn', {
target: function(trigger) {
return trigger.parentNode.querySelector('.highlight');
}
});

clipboard.on('success', function(e) {
//Deal with codes with leading gap
var btnClass = e.trigger.classList;
var lang = btnClass[btnClass.length - 1];
var lines = e.text.split('\n');
var hasGap = false;
var continueSign = '...';

e.clearSelection();

for(var i = 0; i < lines.length; ++i) {
lines[i] = lines[i].replace(/^\s+|\s+$/g, "");
if(!hasGap && lines[i].startsWith(LANG_GP[lang])) hasGap = true;
}

if(hasGap) {
var content = '';
for(var i = 0; i < lines.length; ++i) {
Expand All @@ -81,10 +81,10 @@ $(document).ready(function(){
.tooltip('fixTitle')
.tooltip('show');
});

clipboard.on('error', function(e) {
$(e.trigger).attr('title', 'Copy failed. Try again.')
.tooltip('fixTitle')
.tooltip('show');
});
});
});