You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB

4 years ago
<script src="<%- theme.plugins.clipboard %>"></script>
5 years ago
<script>
4 years ago
function wait(callback, seconds) {
var timelag = null;
timelag = window.setTimeout(callback, seconds);
}
5 years ago
!function (e, t, a) {
var initCopyCode = function(){
var copyHtml = '';
copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
copyHtml += '<i class="fas fa-copy"></i><span>COPY</span>';
5 years ago
copyHtml += '</button>';
$(".highlight .code pre").before(copyHtml);
4 years ago
$(".article pre code").before(copyHtml);
5 years ago
var clipboard = new ClipboardJS('.btn-copy', {
target: function(trigger) {
return trigger.nextElementSibling;
}
});
clipboard.on('success', function(e) {
let $btn = $(e.trigger);
4 years ago
$btn.addClass('copied');
let $icon = $($btn.find('i'));
$icon.removeClass('fa-copy');
4 years ago
$icon.addClass('fa-check-circle');
let $span = $($btn.find('span'));
4 years ago
$span[0].innerText = 'COPIED';
4 years ago
wait(function () { // 等待两秒钟后恢复
$icon.removeClass('fa-check-circle');
$icon.addClass('fa-copy');
$span[0].innerText = 'COPY';
}, 2000);
5 years ago
});
clipboard.on('error', function(e) {
e.clearSelection();
let $btn = $(e.trigger);
$btn.addClass('copy-failed');
let $icon = $($btn.find('i'));
$icon.removeClass('fa-copy');
4 years ago
$icon.addClass('fa-times-circle');
let $span = $($btn.find('span'));
$span[0].innerText = 'COPY FAILED';
4 years ago
wait(function () { // 等待两秒钟后恢复
$icon.removeClass('fa-times-circle');
$icon.addClass('fa-copy');
$span[0].innerText = 'COPY';
}, 2000);
5 years ago
});
}
initCopyCode();
}(window, document);
</script>