Creates a new instance of Clipboard for snippet buttons #68

This commit is contained in:
Zeno Rocha
2015-10-14 17:23:44 -07:00
parent 91d9064f75
commit dbe4b1ff97
2 changed files with 66 additions and 50 deletions

View File

@ -1,17 +1,21 @@
(function() {
'use strict';
var snippets = document.querySelectorAll('.snippet');
var identifier = 0;
var snippets = Array.prototype.slice.call(document.querySelectorAll('pre.snippet'));
[].forEach.call(snippets, function(snippet) {
snippet.firstChild.insertAdjacentHTML('beforebegin', '<button class="btn" data-clipboard-snippet><img class="clippy" width="13" src="assets/images/clippy.svg" alt="Copy to clipboard"></button>');
});
snippets.forEach(function(snippet) {
snippet.id = 'snip' + identifier;
var clipboardSnippets = new Clipboard('[data-clipboard-snippet]', {
target: function(trigger) {
return trigger.nextElementSibling;
}
});
var copyBtn = document.createElement('span');
clipboardSnippets.on('success', function(e) {
e.clearSelection();
copyBtn.className = 'btn clip';
copyBtn.setAttribute('data-clipboard-target', '#snip' + identifier++);
showTooltip(e.trigger, 'Copied!');
});
snippet.parentElement.insertBefore(copyBtn, snippet);
});
})();
clipboardSnippets.on('error', function(e) {
showTooltip(e.trigger, fallbackMessage(e.action));
});