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:
40
_javascript/utils/clipboard.js
Normal file
40
_javascript/utils/clipboard.js
Normal 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);
|
||||
});
|
||||
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user