Skip to content

Commit

Permalink
Add clipboard.js for snippets.
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Feb 1, 2017
1 parent c63daca commit 2781d70
Show file tree
Hide file tree
Showing 6 changed files with 822 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,18 @@ module.exports = function(grunt) {
'src/js/vendor/lib/jquery.js',
'src/js/vendor/lib/jquery.dataTables.js',
'src/js/vendor/*.js',
'src/js/*.js'
'src/js/*.js',
'!src/js/vendor/clipboard.js',
'!src/js/clipboard-init.js'
],
dest: 'build/js/plugins.js'
},
clipboard: {
src: [
'src/js/vendor/clipboard.js',
'src/js/clipboard-init.js'
],
dest: 'build/js/clipboard.min.js'
}
},

Expand Down
28 changes: 28 additions & 0 deletions src/js/clipboard-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* jshint browser:true */
/* global Clipboard:true */

(function() {
'use strict';

var selectors = document.querySelectorAll('pre code[class^="lang-"]');

Array.prototype.forEach.call(selectors, function(selector){

var btnHtml = '<div class="clipboard">' +
'<span class="btn btn-clipboard" title="Copy to clipboard">Copy</span>' +
'</div>';
selector.insertAdjacentHTML('beforebegin', btnHtml);

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

clipboard.on('success', function (e) {
e.clearSelection();
});

});

})();
Loading

0 comments on commit 2781d70

Please sign in to comment.