1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2025-12-18 21:53:26 +00:00

Add clipboard JS

This commit is contained in:
Cotes Chung
2021-09-10 22:01:18 +08:00
parent 2537283beb
commit 5e9a7b319e
6 changed files with 45 additions and 27 deletions

View File

@@ -0,0 +1,40 @@
/*
* Initial the clipboard.js object, see: <https://github.com/zenorocha/clipboard.js>
*
* Dependencies:
* - popper.js (https://github.com/popperjs/popper-core)
* - clipboard.js (https://github.com/zenorocha/clipboard.js)
*/
$(function() {
const btnSelector = '.code-header>button';
var clipboard = new ClipboardJS(btnSelector, {
target(trigger) {
return trigger.parentNode.nextElementSibling;
}
});
function setTooltip(btn, msg) {
$(btn).tooltip('hide')
.attr('data-original-title', msg)
.tooltip('show');
}
function hideTooltip(btn) {
setTimeout(function() {
$(btn).tooltip('hide');
}, 1000);
}
$(btnSelector).tooltip({
trigger: 'click',
placement: 'left'
});
clipboard.on('success', function(e) {
e.clearSelection();
setTooltip(e.trigger, 'Copied!');
hideTooltip(e.trigger);
});
});

View File

@@ -1,22 +0,0 @@
/*
* Copy current page url to clipboard.
*/
function copyLink(url, msg) {
if (!url || 0 === url.length) {
url = window.location.href;
}
const $temp = $("<input>");
$("body").append($temp);
$temp.val(url).select();
document.execCommand("copy");
$temp.remove();
let feedback = "Link copied successfully!";
if (msg && msg.length > 0) {
feedback = msg;
}
alert(feedback);
}