44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
// Generated, do not edit. sha256:2c4f9a283b8c1358df232fdf7f2ef98f6443ae5dd830e97908059c85c34cc9d7
|
|
import { t } from '../i18n/index.js';
|
|
import { esc } from './escape.js';
|
|
|
|
const GLYPH = '⧉';
|
|
|
|
export function copyButton(text, title) {
|
|
if (text == null || text === '') return '';
|
|
const ttl = esc(title || t('action.copy'));
|
|
return `<button type="button" class="copy-btn" data-copy="${esc(text)}" title="${ttl}" aria-label="${ttl}">${GLYPH}</button>`;
|
|
}
|
|
|
|
const CLEAR_AFTER_MS = 20000;
|
|
let clearTimer = null;
|
|
let pendingClear = false;
|
|
|
|
function clearClipboard() {
|
|
navigator.clipboard.writeText('')
|
|
.then(() => { pendingClear = false; })
|
|
.catch(() => { pendingClear = true; window.addEventListener('focus', onFocusClear, { once: true }); });
|
|
}
|
|
function onFocusClear() { if (pendingClear) clearClipboard(); }
|
|
function scheduleClear() {
|
|
clearTimeout(clearTimer);
|
|
pendingClear = false;
|
|
clearTimer = setTimeout(clearClipboard, CLEAR_AFTER_MS);
|
|
}
|
|
|
|
let installed = false;
|
|
function install() {
|
|
if (installed) return;
|
|
installed = true;
|
|
document.addEventListener('click', e => {
|
|
const btn = e.target.closest('[data-copy]');
|
|
if (!btn) return;
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
navigator.clipboard.writeText(btn.dataset.copy)
|
|
.then(() => { btn.classList.add('is-copied'); setTimeout(() => btn.classList.remove('is-copied'), 1200); scheduleClear(); })
|
|
.catch(() => {});
|
|
}, true);
|
|
}
|
|
install();
|