in src/components/chat/components/duo_chat_message/copy_code_element.js [11:47]
async initialize() {
if (!this.getCodeElement()) return;
const btn = createButton('Copy to clipboard', 'copy-to-clipboard');
this.appendChild(btn);
const tooltip = await createTooltip(btn, 'Copy to clipboard');
this.appendChild(tooltip);
btn.addEventListener('click', async () => {
// We fetch the code element on click instead because the structure of the HTML can change after
// the element is initialized (e.g. on sanitation), and we don't want to use an element that is
// not present in the DOM.
const codeElement = this.getCodeElement();
const textToCopy = codeElement.innerText;
const hasClipboardPermission = await checkClipboardPermissions();
try {
codeElement.dispatchEvent(
new CustomEvent('copy-code-snippet', {
bubbles: true,
cancelable: true,
detail: {
code: textToCopy,
},
})
);
if (hasClipboardPermission) {
await navigator.clipboard.writeText(textToCopy);
}
} catch (e) {
// eslint-disable-next-line no-console
console.warn('Failed to copy snippet:', e);
}
});
}