ily.li/assets/ts/copyable_text.ts

24 lines
927 B
TypeScript
Raw Normal View History

2020-09-23 13:30:48 +02:00
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.copyable-text').forEach(el => {
const contentEl = el.querySelector('.content');
const selection = window.getSelection();
if (contentEl && selection) {
contentEl.addEventListener('click', () => {
selection.selectAllChildren(contentEl);
});
el.querySelector('.copy-button')?.addEventListener('click', () => {
selection.selectAllChildren(contentEl);
document.execCommand('copy');
});
}
});
document.querySelectorAll<HTMLElement>('.copy-button[data-content]').forEach(el => {
el.addEventListener('click', () => {
navigator.clipboard.writeText(`${el.dataset.content}`)
.then(() => console.log('copy success'))
.catch(console.error);
});
});
2020-09-23 13:30:48 +02:00
});