mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-20 06:20:11 +00:00
Backport of #9299 to `cloud/1.42` Automatically created by backport workflow. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-10525-backport-cloud-1-42-fix-handle-clipboard-errors-in-Copy-Image-and-useCopyToClipboard-32e6d73d3650810280fcd2a56d8808f0) by [Unito](https://www.unito.io) Co-authored-by: Christian Byrne <cbyrne@comfy.org>
66 lines
1.4 KiB
TypeScript
66 lines
1.4 KiB
TypeScript
import { useClipboard } from '@vueuse/core'
|
|
import { useToast } from 'primevue/usetoast'
|
|
|
|
import { t } from '@/i18n'
|
|
|
|
function legacyCopy(text: string): boolean {
|
|
const textarea = document.createElement('textarea')
|
|
textarea.setAttribute('readonly', '')
|
|
textarea.value = text
|
|
textarea.style.position = 'fixed'
|
|
textarea.style.left = '-9999px'
|
|
textarea.style.top = '-9999px'
|
|
document.body.appendChild(textarea)
|
|
textarea.select()
|
|
try {
|
|
return document.execCommand('copy')
|
|
} finally {
|
|
textarea.remove()
|
|
}
|
|
}
|
|
|
|
export function useCopyToClipboard() {
|
|
const { copy, isSupported } = useClipboard()
|
|
const toast = useToast()
|
|
|
|
async function copyToClipboard(text: string) {
|
|
let success = false
|
|
|
|
try {
|
|
if (isSupported.value) {
|
|
await copy(text)
|
|
success = true
|
|
}
|
|
} catch {
|
|
// Modern clipboard API failed, fall through to legacy
|
|
}
|
|
|
|
if (!success) {
|
|
try {
|
|
success = legacyCopy(text)
|
|
} catch {
|
|
// Legacy also failed
|
|
}
|
|
}
|
|
|
|
toast.add(
|
|
success
|
|
? {
|
|
severity: 'success',
|
|
summary: t('g.success'),
|
|
detail: t('clipboard.successMessage'),
|
|
life: 3000
|
|
}
|
|
: {
|
|
severity: 'error',
|
|
summary: t('g.error'),
|
|
detail: t('clipboard.errorMessage')
|
|
}
|
|
)
|
|
}
|
|
|
|
return {
|
|
copyToClipboard
|
|
}
|
|
}
|