2023-03-13 15:20:59 +03:00
|
|
|
/**
|
|
|
|
* Clipboard functions
|
|
|
|
*
|
|
|
|
* Dependencies:
|
|
|
|
* - popper.js (https://github.com/popperjs/popper-core)
|
|
|
|
* - clipboard.js (https://github.com/zenorocha/clipboard.js)
|
|
|
|
*/
|
|
|
|
|
2023-04-10 05:54:27 +03:00
|
|
|
const clipboardSelector = '.code-header>button';
|
2024-04-17 01:10:01 +03:00
|
|
|
|
|
|
|
const ICON_DEFAULT = 'far fa-clipboard';
|
2023-03-13 15:20:59 +03:00
|
|
|
const ICON_SUCCESS = 'fas fa-check';
|
2024-04-17 01:10:01 +03:00
|
|
|
|
2023-03-13 15:20:59 +03:00
|
|
|
const ATTR_TIMEOUT = 'timeout';
|
|
|
|
const ATTR_TITLE_SUCCEED = 'data-title-succeed';
|
2023-04-10 05:54:27 +03:00
|
|
|
const ATTR_TITLE_ORIGIN = 'data-bs-original-title';
|
2023-03-13 15:20:59 +03:00
|
|
|
const TIMEOUT = 2000; // in milliseconds
|
|
|
|
|
|
|
|
function isLocked(node) {
|
2024-04-17 01:10:01 +03:00
|
|
|
if (node.hasAttribute(ATTR_TIMEOUT)) {
|
|
|
|
let timeout = node.getAttribute(ATTR_TIMEOUT);
|
2023-03-13 15:20:59 +03:00
|
|
|
if (Number(timeout) > Date.now()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2024-04-17 01:10:01 +03:00
|
|
|
|
2023-03-13 15:20:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function lock(node) {
|
2024-04-17 01:10:01 +03:00
|
|
|
node.setAttribute(ATTR_TIMEOUT, Date.now() + TIMEOUT);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function unlock(node) {
|
2024-04-17 01:10:01 +03:00
|
|
|
node.removeAttribute(ATTR_TIMEOUT);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showTooltip(btn) {
|
2024-04-17 01:10:01 +03:00
|
|
|
const succeedTitle = btn.getAttribute(ATTR_TITLE_SUCCEED);
|
|
|
|
btn.setAttribute(ATTR_TITLE_ORIGIN, succeedTitle);
|
|
|
|
bootstrap.Tooltip.getInstance(btn).show();
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function hideTooltip(btn) {
|
2024-04-17 01:10:01 +03:00
|
|
|
bootstrap.Tooltip.getInstance(btn).hide();
|
|
|
|
btn.removeAttribute(ATTR_TITLE_ORIGIN);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function setSuccessIcon(btn) {
|
2024-04-17 01:10:01 +03:00
|
|
|
const icon = btn.children[0];
|
|
|
|
icon.setAttribute('class', ICON_SUCCESS);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function resumeIcon(btn) {
|
2024-04-17 01:10:01 +03:00
|
|
|
const icon = btn.children[0];
|
|
|
|
icon.setAttribute('class', ICON_DEFAULT);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function initClipboard() {
|
2024-04-17 01:10:01 +03:00
|
|
|
const clipboardList = document.querySelectorAll(clipboardSelector);
|
|
|
|
|
|
|
|
if (clipboardList.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:20:59 +03:00
|
|
|
// Initial the clipboard.js object
|
2024-04-17 01:10:01 +03:00
|
|
|
const clipboard = new ClipboardJS(clipboardSelector, {
|
|
|
|
target: (trigger) => {
|
|
|
|
const codeBlock = trigger.parentNode.nextElementSibling;
|
|
|
|
return codeBlock.querySelector('code .rouge-code');
|
|
|
|
}
|
|
|
|
});
|
2023-03-13 15:20:59 +03:00
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
[...clipboardList].map(
|
|
|
|
(elem) =>
|
|
|
|
new bootstrap.Tooltip(elem, {
|
|
|
|
placement: 'left'
|
|
|
|
})
|
|
|
|
);
|
2023-03-13 15:20:59 +03:00
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
clipboard.on('success', (e) => {
|
|
|
|
const trigger = e.trigger;
|
2023-03-13 15:20:59 +03:00
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
e.clearSelection();
|
2023-03-13 15:20:59 +03:00
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
if (isLocked(trigger)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-13 15:20:59 +03:00
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
setSuccessIcon(trigger);
|
|
|
|
showTooltip(trigger);
|
|
|
|
lock(trigger);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
hideTooltip(trigger);
|
|
|
|
resumeIcon(trigger);
|
|
|
|
unlock(trigger);
|
|
|
|
}, TIMEOUT);
|
|
|
|
});
|
2023-03-13 15:20:59 +03:00
|
|
|
|
|
|
|
/* --- Post link sharing --- */
|
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
const btnCopyLink = document.getElementById('copy-link');
|
2023-08-02 00:18:35 +03:00
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
btnCopyLink.addEventListener('click', (e) => {
|
|
|
|
const target = e.target;
|
2023-03-13 15:20:59 +03:00
|
|
|
|
|
|
|
if (isLocked(target)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy URL to clipboard
|
|
|
|
navigator.clipboard.writeText(window.location.href).then(() => {
|
2024-04-17 01:10:01 +03:00
|
|
|
const defaultTitle = target.getAttribute(ATTR_TITLE_ORIGIN);
|
|
|
|
const succeedTitle = target.getAttribute(ATTR_TITLE_SUCCEED);
|
|
|
|
|
2023-03-13 15:20:59 +03:00
|
|
|
// Switch tooltip title
|
2024-04-17 01:10:01 +03:00
|
|
|
target.setAttribute(ATTR_TITLE_ORIGIN, succeedTitle);
|
|
|
|
bootstrap.Tooltip.getInstance(target).show();
|
|
|
|
|
2023-03-13 15:20:59 +03:00
|
|
|
lock(target);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2024-04-17 01:10:01 +03:00
|
|
|
target.setAttribute(ATTR_TITLE_ORIGIN, defaultTitle);
|
2023-03-13 15:20:59 +03:00
|
|
|
unlock(target);
|
|
|
|
}, TIMEOUT);
|
|
|
|
});
|
|
|
|
});
|
2023-08-02 00:18:35 +03:00
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
btnCopyLink.addEventListener('mouseleave', (e) => {
|
|
|
|
bootstrap.Tooltip.getInstance(e.target).hide();
|
2023-08-02 00:18:35 +03:00
|
|
|
});
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|