Line data Source code
1 : import { useEventListener } from '@vueuse/core'
2 :
3 : import {
4 : CANVAS_CLIPBOARD_ID_KEY,
5 : CANVAS_CLIPBOARD_KEY
6 : } from '@/lib/litegraph/src/canvas/clipboardStorage'
7 : import { reportError } from '@comfyorg/telemetry/reportError'
8 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
9 : import {
10 : hasTextSelection,
11 : shouldIgnoreCopyPaste
12 : } from '@/workbench/eventHelpers'
13 :
14 2356 : const metadataHtmlPrefix =
15 : '<meta charset="utf-8"><div><span data-comfy-metadata="'
16 2356 : const legacyMetadataHtmlPrefix =
17 : '<meta charset="utf-8"><div><span data-metadata="'
18 2356 : const metadataHtmlSuffix =
19 : '"></span></div><span style="white-space:pre-wrap;">Text</span>'
20 2356 : const chromiumHtmlWrappers = [
21 : { before: '', after: '' },
22 : { before: "<meta charset='utf-8'>", after: '' },
23 : {
24 : before: '<html>\r\n<body>\r\n<!--StartFragment-->',
25 : after: '<!--EndFragment-->\r\n</body>\r\n</html>'
26 : }
27 : ]
28 :
29 17 : function clipboardHtml(base64Data: string): string {
30 17 : return `${metadataHtmlPrefix}${base64Data}${metadataHtmlSuffix}`
31 : }
32 :
33 162 : function between(text: string, before: string, after: string) {
34 162 : if (!text.startsWith(before) || !text.endsWith(after)) return undefined
35 20 : return text.slice(before.length, text.length - after.length)
36 : }
37 :
38 27 : function readMetadata(html: string): string | undefined {
39 27 : return chromiumHtmlWrappers
40 : .flatMap(({ before, after }) =>
41 : [metadataHtmlPrefix, legacyMetadataHtmlPrefix].map((prefix) =>
42 : between(html, before + prefix, metadataHtmlSuffix + after)
43 : )
44 : )
45 : .find((base64Data) => base64Data !== undefined)
46 : }
47 :
48 2356 : const clipboardByteChunkSize = 0x8000
49 :
50 17 : function bytesToBinaryString(bytes: Uint8Array): string {
51 17 : const chunks: string[] = []
52 :
53 17 : for (
54 : let offset = 0;
55 : offset < bytes.length;
56 : offset += clipboardByteChunkSize
57 : ) {
58 17 : chunks.push(
59 : String.fromCharCode(
60 : ...bytes.subarray(offset, offset + clipboardByteChunkSize)
61 : )
62 : )
63 : }
64 :
65 17 : return chunks.join('')
66 : }
67 :
68 17 : function encodeClipboardData(data: string): string {
69 17 : return btoa(bytesToBinaryString(new TextEncoder().encode(data)))
70 : }
71 :
72 : type ClipboardHtmlParse =
73 : | { status: 'absent' }
74 : | { status: 'unreadable'; cause: unknown }
75 : | { status: 'read'; text: string; payload: unknown }
76 :
77 27 : export function parseClipboardHtml(html: string): ClipboardHtmlParse {
78 27 : const base64Data = readMetadata(html)
79 27 : if (!base64Data) return { status: 'absent' }
80 21 : try {
81 20 : const bytes = Uint8Array.from(atob(base64Data), (c) => c.charCodeAt(0))
82 20 : const text = new TextDecoder().decode(bytes)
83 20 : return { status: 'read', text, payload: JSON.parse(text) }
84 : } catch (cause) {
85 0 : return { status: 'unreadable', cause }
86 : }
87 : }
88 :
89 : /**
90 : * Adds a handler on copy that serializes selected nodes to JSON
91 : */
92 2356 : export const useCopy = () => {
93 2355 : const canvasStore = useCanvasStore()
94 2355 : let keyboardCopyId: string | null = null
95 :
96 2355 : useEventListener(document, 'copy', (e) => {
97 25 : if (shouldIgnoreCopyPaste(e.target)) {
98 : // Default system copy
99 8 : if (
100 : keyboardCopyId !== null &&
101 : hasTextSelection(e.target) &&
102 : localStorage.getItem(CANVAS_CLIPBOARD_ID_KEY) === keyboardCopyId
103 : ) {
104 0 : localStorage.removeItem(CANVAS_CLIPBOARD_KEY)
105 0 : localStorage.removeItem(CANVAS_CLIPBOARD_ID_KEY)
106 0 : keyboardCopyId = null
107 : }
108 8 : return
109 : }
110 : // copy nodes and clear clipboard
111 17 : const canvas = canvasStore.canvas
112 25 : if (canvas?.selectedItems) {
113 17 : const serializedData = canvas.copyToClipboard()
114 17 : keyboardCopyId = localStorage.getItem(CANVAS_CLIPBOARD_ID_KEY)
115 17 : try {
116 17 : const base64Data = encodeClipboardData(serializedData)
117 : // clearData doesn't remove images from clipboard
118 17 : e.clipboardData?.setData('text/html', clipboardHtml(base64Data))
119 : } catch (error) {
120 0 : reportError(error, {
121 : errorType: 'error_writing_clipboard_metadata',
122 : surface: 'graph'
123 : })
124 : }
125 17 : e.preventDefault()
126 17 : e.stopImmediatePropagation()
127 17 : return false
128 : }
129 : })
130 : }
|