Skip to content

Commit

Permalink
Add clipboard.js for snippets.
Browse files Browse the repository at this point in the history
TODO: Fix events.
  • Loading branch information
XhmikosR committed Jul 20, 2016
1 parent 946ccfe commit f0c839a
Show file tree
Hide file tree
Showing 6 changed files with 817 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,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
36 changes: 36 additions & 0 deletions src/js/clipboard-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* 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();
selector.setAttribute('title', 'Copied!');
//e.trigger;
});

clipboard.on('error', function (/*e*/) {
var fallbackMsg = /Mac/i.test(navigator.userAgent) ? 'Press \u2318 to copy' : 'Press Ctrl-C to copy';
selector.setAttribute('title', fallbackMsg);
//e.trigger;
});

});

})();
Loading

0 comments on commit f0c839a

Please sign in to comment.