web/_javascript/utils/clipboard.js

124 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-10-25 14:26:44 +03:00
/**
* Clipboard functions
2021-09-10 17:01:18 +03:00
*
* Dependencies:
* - popper.js (https://github.com/popperjs/popper-core)
* - clipboard.js (https://github.com/zenorocha/clipboard.js)
*/
2021-09-15 09:36:37 +03:00
2022-10-25 14:26:44 +03:00
$(function () {
const btnSelector = '.code-header>button';
const ICON_SUCCESS = 'fas fa-check';
const ATTR_TIMEOUT = 'timeout';
const ATTR_TITLE_SUCCEED = 'data-title-succeed';
const ATTR_TITLE_ORIGIN = 'data-original-title';
const TIMEOUT = 2000; // in milliseconds
function isLocked(node) {
if ($(node)[0].hasAttribute(ATTR_TIMEOUT)) {
let timeout = $(node).attr(ATTR_TIMEOUT);
if (Number(timeout) > Date.now()) {
return true;
}
}
return false;
}
2022-10-25 14:26:44 +03:00
function lock(node) {
$(node).attr(ATTR_TIMEOUT, Date.now() + TIMEOUT);
}
function unlock(node) {
$(node).removeAttr(ATTR_TIMEOUT);
}
/* --- Copy code block --- */
2022-10-25 14:26:44 +03:00
// Initial the clipboard.js object
const clipboard = new ClipboardJS(btnSelector, {
target(trigger) {
let codeBlock = trigger.parentNode.nextElementSibling;
return codeBlock.querySelector('code .rouge-code');
}
});
2022-10-25 14:26:44 +03:00
$(btnSelector).tooltip({
trigger: 'hover',
placement: 'left'
});
2022-10-25 14:26:44 +03:00
function getIcon(btn) {
let iconNode = $(btn).children();
return iconNode.attr('class');
2021-09-10 17:01:18 +03:00
}
2022-10-25 14:26:44 +03:00
const ICON_DEFAULT = getIcon(btnSelector);
function showTooltip(btn) {
const succeedTitle = $(btn).attr(ATTR_TITLE_SUCCEED);
$(btn).attr(ATTR_TITLE_ORIGIN, succeedTitle).tooltip('show');
}
2022-10-25 14:26:44 +03:00
function hideTooltip(btn) {
$(btn).tooltip('hide').removeAttr(ATTR_TITLE_ORIGIN);
}
2022-10-25 14:26:44 +03:00
function setSuccessIcon(btn) {
let btnNode = $(btn);
let iconNode = btnNode.children();
iconNode.attr('class', ICON_SUCCESS);
}
2022-10-25 14:26:44 +03:00
function resumeIcon(btn) {
let btnNode = $(btn);
let iconNode = btnNode.children();
iconNode.attr('class', ICON_DEFAULT);
}
2022-10-25 14:26:44 +03:00
clipboard.on('success', (e) => {
e.clearSelection();
2022-10-25 14:26:44 +03:00
const trigger = e.trigger;
if (isLocked(trigger)) {
return;
}
2022-10-25 14:26:44 +03:00
setSuccessIcon(trigger);
showTooltip(trigger);
lock(trigger);
2022-10-25 14:26:44 +03:00
setTimeout(() => {
hideTooltip(trigger);
resumeIcon(trigger);
unlock(trigger);
}, TIMEOUT);
});
/* --- Post link sharing --- */
$('#copy-link').on('click',(e) => {
2022-10-25 14:26:44 +03:00
let target = $(e.target);
if (isLocked(target)) {
return;
}
2021-09-15 09:36:37 +03:00
2022-10-25 14:26:44 +03:00
// Copy URL to clipboard
navigator.clipboard
.writeText(window.location.href)
.then(() => {
const defaultTitle = target.attr(ATTR_TITLE_ORIGIN);
const succeedTitle = target.attr(ATTR_TITLE_SUCCEED);
// Switch tooltip title
target.attr(ATTR_TITLE_ORIGIN, succeedTitle).tooltip('show');
lock(target);
setTimeout(() => {
target.attr(ATTR_TITLE_ORIGIN, defaultTitle);
unlock(target);
}, TIMEOUT);
});
2022-10-25 14:26:44 +03:00
});
2021-09-10 17:01:18 +03:00
});