mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-03 14:54:37 +00:00
 Browsers place very heavy restrictions on what can be copied and pasted. See: - https://alexharri.com/blog/clipboard - https://www.w3.org/TR/clipboard-apis/#mandatory-data-types-x ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-6087-Experimental-cross-domain-application-copy-paste-28e6d73d36508154a0a8deeb392f43a4) by [Unito](https://www.unito.io)
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import { useEventListener } from '@vueuse/core'
|
|
|
|
import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
|
|
import { shouldIgnoreCopyPaste } from '@/workbench/eventHelpers'
|
|
|
|
const clipboardHTMLWrapper = [
|
|
'<meta charset="utf-8"><div><span data-metadata="',
|
|
'"></span></div><span style="white-space:pre-wrap;">Text</span>'
|
|
]
|
|
|
|
/**
|
|
* Adds a handler on copy that serializes selected nodes to JSON
|
|
*/
|
|
export const useCopy = () => {
|
|
const canvasStore = useCanvasStore()
|
|
|
|
useEventListener(document, 'copy', (e) => {
|
|
if (shouldIgnoreCopyPaste(e.target)) {
|
|
// Default system copy
|
|
return
|
|
}
|
|
// copy nodes and clear clipboard
|
|
const canvas = canvasStore.canvas
|
|
if (canvas?.selectedItems) {
|
|
const serializedData = canvas.copyToClipboard()
|
|
// clearData doesn't remove images from clipboard
|
|
e.clipboardData?.setData(
|
|
'text/html',
|
|
clipboardHTMLWrapper.join(btoa(serializedData))
|
|
)
|
|
e.preventDefault()
|
|
e.stopImmediatePropagation()
|
|
return false
|
|
}
|
|
})
|
|
}
|