2021-05-30 22:15:57 +03:00
|
|
|
const selector = '[data-clipboard-target], [data-clipboard-text]';
|
2020-02-08 02:03:42 +03:00
|
|
|
|
2021-05-30 22:15:57 +03:00
|
|
|
// TODO: replace these with toast-style notifications
|
|
|
|
function onSuccess(btn) {
|
|
|
|
if (!btn.dataset.content) return;
|
|
|
|
$(btn).popup('destroy');
|
|
|
|
btn.dataset.content = btn.dataset.success;
|
|
|
|
$(btn).popup('show');
|
|
|
|
btn.dataset.content = btn.dataset.original;
|
|
|
|
}
|
|
|
|
function onError(btn) {
|
|
|
|
if (!btn.dataset.content) return;
|
|
|
|
$(btn).popup('destroy');
|
|
|
|
btn.dataset.content = btn.dataset.error;
|
|
|
|
$(btn).popup('show');
|
|
|
|
btn.dataset.content = btn.dataset.original;
|
|
|
|
}
|
2020-02-08 02:03:42 +03:00
|
|
|
|
2021-05-30 22:15:57 +03:00
|
|
|
export default async function initClipboard() {
|
|
|
|
for (const btn of document.querySelectorAll(selector) || []) {
|
|
|
|
btn.addEventListener('click', async () => {
|
|
|
|
let text;
|
|
|
|
if (btn.dataset.clipboardText) {
|
|
|
|
text = btn.dataset.clipboardText;
|
|
|
|
} else if (btn.dataset.clipboardTarget) {
|
|
|
|
text = document.querySelector(btn.dataset.clipboardTarget)?.value;
|
|
|
|
}
|
|
|
|
if (!text) return;
|
2020-02-08 02:03:42 +03:00
|
|
|
|
2021-05-30 22:15:57 +03:00
|
|
|
try {
|
|
|
|
await navigator.clipboard.writeText(text);
|
|
|
|
onSuccess(btn);
|
|
|
|
} catch {
|
|
|
|
onError(btn);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-02-08 02:03:42 +03:00
|
|
|
}
|