| import { toast } from 'svelte-sonner'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export async function copyToClipboard( |
| text: string, |
| successMessage = 'Copied to clipboard', |
| errorMessage = 'Failed to copy to clipboard' |
| ): Promise<boolean> { |
| try { |
| |
| if (navigator.clipboard && navigator.clipboard.writeText) { |
| await navigator.clipboard.writeText(text); |
| toast.success(successMessage); |
| return true; |
| } |
|
|
| |
| const textArea = document.createElement('textarea'); |
| textArea.value = text; |
| textArea.style.position = 'fixed'; |
| textArea.style.left = '-999999px'; |
| textArea.style.top = '-999999px'; |
| document.body.appendChild(textArea); |
| textArea.focus(); |
| textArea.select(); |
|
|
| const successful = document.execCommand('copy'); |
| document.body.removeChild(textArea); |
|
|
| if (successful) { |
| toast.success(successMessage); |
| return true; |
| } else { |
| throw new Error('execCommand failed'); |
| } |
| } catch (error) { |
| console.error('Failed to copy to clipboard:', error); |
| toast.error(errorMessage); |
| return false; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function copyCodeToClipboard( |
| rawCode: string, |
| successMessage = 'Code copied to clipboard', |
| errorMessage = 'Failed to copy code' |
| ): Promise<boolean> { |
| |
| const decodedCode = rawCode |
| .replace(/&/g, '&') |
| .replace(/</g, '<') |
| .replace(/>/g, '>') |
| .replace(/"/g, '"') |
| .replace(/'/g, "'"); |
|
|
| return copyToClipboard(decodedCode, successMessage, errorMessage); |
| } |
|
|