// Keep clipboard access in one place so HTTP deployments can use the fallback. export async function copyText(text) { if (text === null || text === undefined) return false; const value = String(text); try { if (globalThis.navigator?.clipboard?.writeText) { await globalThis.navigator.clipboard.writeText(value); return true; } } catch { // Permission denial can still allow a copy initiated by a user gesture. } const doc = globalThis.document; if (!doc?.body || typeof doc.execCommand !== 'function') return false; const activeElement = doc.activeElement; const selection = doc.getSelection?.(); const ranges = []; for (let i = 0; i < (selection?.rangeCount || 0); i++) { ranges.push(selection.getRangeAt(i).cloneRange()); } const inputSelection = typeof activeElement?.selectionStart === 'number' ? [ activeElement.selectionStart, activeElement.selectionEnd, activeElement.selectionDirection, ] : null; let textarea; try { textarea = doc.createElement('textarea'); textarea.value = value; textarea.readOnly = true; textarea.tabIndex = -1; Object.assign(textarea.style, { position: 'fixed', top: '0', left: '0', width: '1px', height: '1px', opacity: '0', fontSize: '16px', pointerEvents: 'none', }); // Modal focus traps must allow the temporary input to receive selection. const container = activeElement?.closest?.('[role="dialog"]') || doc.body; container.appendChild(textarea); textarea.focus({ preventScroll: true }); textarea.select(); textarea.setSelectionRange(0, value.length); return doc.execCommand('copy') === true; } catch { return false; } finally { textarea?.remove(); try { activeElement?.focus?.({ preventScroll: true }); if (inputSelection) activeElement.setSelectionRange(...inputSelection); if (selection && ranges.length) { selection.removeAllRanges(); ranges.forEach((range) => selection.addRange(range)); } } catch { // The original field may have unmounted while Clipboard API was pending. } } } // Promise-based callers keep their existing success/error callbacks. export async function writeClipboardText(text) { if (!(await copyText(text))) throw new Error('Clipboard copy failed'); } export async function writeClipboardImage(blob) { const clipboard = globalThis.navigator?.clipboard; const Item = globalThis.ClipboardItem; if (!clipboard?.write || !Item) throw new Error('Image clipboard unavailable'); let png = blob; if (blob.type !== 'image/png') { // Clipboard image support is PNG-only in common browsers; relabeling a JPEG // blob as PNG causes write() to reject it, so convert the bytes as well. const bitmap = await globalThis.createImageBitmap(blob); try { const canvas = document.createElement('canvas'); canvas.width = bitmap.width; canvas.height = bitmap.height; canvas.getContext('2d').drawImage(bitmap, 0, 0); png = await new Promise((resolve, reject) => { canvas.toBlob( (result) => result ? resolve(result) : reject(new Error('Image conversion failed')), 'image/png', ); }); } finally { bitmap.close(); } } await clipboard.write([new Item({ 'image/png': png })]); }