mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-05 23:50:08 +00:00
## Summary <!-- One sentence describing what changed and why. --> Added feature to drag image into workflow to create a load image node if the image does not have workflow meta data. Also added tests for usePaste.ts as I extracted code to be reusable there and there wasn't any tests. ## Changes - **What**: <!-- Core functionality added/modified --> app.ts handleFile updated, usePaste.ts usePaste updated with new method pasteImageNode ## Review Focus <!-- Fixes #ISSUE_NUMBER --> Not sure if it has an issue, just has a notion task. https://www.notion.so/comfy-org/Drag-in-an-image-that-s-not-a-workflow-and-being-able-to-directly-loading-it-as-Load-Image-2156d73d365080c4851ffc1425e06caf ## Screenshots (if applicable) <!-- Add screenshots or video recording to help explain your changes --> https://github.com/user-attachments/assets/0403e4f1-2a99-4939-bf01-3d9e8f9834bb ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-7898-Drag-image-to-load-image-2e26d73d36508187abdff986e8087370) by [Unito](https://www.unito.io) --------- Co-authored-by: Alexander Brown <drjkl@comfy.org>
172 lines
5.2 KiB
TypeScript
172 lines
5.2 KiB
TypeScript
import { useEventListener } from '@vueuse/core'
|
|
|
|
import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph'
|
|
import { LiteGraph } from '@/lib/litegraph/src/litegraph'
|
|
import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema'
|
|
import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
|
|
import { app } from '@/scripts/app'
|
|
import { useWorkspaceStore } from '@/stores/workspaceStore'
|
|
import { isAudioNode, isImageNode, isVideoNode } from '@/utils/litegraphUtil'
|
|
import { shouldIgnoreCopyPaste } from '@/workbench/eventHelpers'
|
|
|
|
function pasteClipboardItems(data: DataTransfer): boolean {
|
|
const rawData = data.getData('text/html')
|
|
const match = rawData.match(/data-metadata="([A-Za-z0-9+/=]+)"/)?.[1]
|
|
if (!match) return false
|
|
try {
|
|
// Decode UTF-8 safe base64
|
|
const binaryString = atob(match)
|
|
const bytes = Uint8Array.from(binaryString, (c) => c.charCodeAt(0))
|
|
const decodedData = new TextDecoder().decode(bytes)
|
|
useCanvasStore().getCanvas()._deserializeItems(JSON.parse(decodedData), {})
|
|
return true
|
|
} catch (err) {
|
|
console.error(err)
|
|
}
|
|
return false
|
|
}
|
|
|
|
function pasteItemsOnNode(
|
|
items: DataTransferItemList,
|
|
node: LGraphNode | null,
|
|
contentType: string
|
|
): void {
|
|
if (!node) return
|
|
|
|
const filteredItems = Array.from(items).filter((item) =>
|
|
item.type.startsWith(contentType)
|
|
)
|
|
|
|
const blob = filteredItems[0]?.getAsFile()
|
|
if (!blob) return
|
|
|
|
node.pasteFile?.(blob)
|
|
node.pasteFiles?.(
|
|
Array.from(filteredItems)
|
|
.map((i) => i.getAsFile())
|
|
.filter((f) => f !== null)
|
|
)
|
|
}
|
|
|
|
export function pasteImageNode(
|
|
canvas: LGraphCanvas,
|
|
items: DataTransferItemList,
|
|
imageNode: LGraphNode | null = null
|
|
): void {
|
|
const { graph, graph_mouse: [posX, posY] } = canvas
|
|
|
|
if (!imageNode) {
|
|
// No image node selected: add a new one
|
|
const newNode = LiteGraph.createNode('LoadImage')
|
|
if (newNode) {
|
|
newNode.pos = [posX, posY]
|
|
imageNode = graph?.add(newNode) ?? null
|
|
}
|
|
graph?.change()
|
|
}
|
|
|
|
pasteItemsOnNode(items, imageNode, 'image')
|
|
}
|
|
|
|
/**
|
|
* Adds a handler on paste that extracts and loads images or workflows from pasted JSON data
|
|
*/
|
|
export const usePaste = () => {
|
|
const workspaceStore = useWorkspaceStore()
|
|
const canvasStore = useCanvasStore()
|
|
|
|
useEventListener(document, 'paste', async (e) => {
|
|
if (shouldIgnoreCopyPaste(e.target)) {
|
|
// Default system copy
|
|
return
|
|
}
|
|
// ctrl+shift+v is used to paste nodes with connections
|
|
// this is handled by litegraph
|
|
if (workspaceStore.shiftDown) return
|
|
|
|
const { canvas } = canvasStore
|
|
if (!canvas) return
|
|
|
|
const { graph } = canvas
|
|
let data: DataTransfer | string | null = e.clipboardData
|
|
if (!data) throw new Error('No clipboard data on clipboard event')
|
|
|
|
const { items } = data
|
|
|
|
const currentNode = canvas.current_node as LGraphNode
|
|
const isNodeSelected = currentNode?.is_selected
|
|
|
|
const isImageNodeSelected = isNodeSelected && isImageNode(currentNode)
|
|
const isVideoNodeSelected = isNodeSelected && isVideoNode(currentNode)
|
|
const isAudioNodeSelected = isNodeSelected && isAudioNode(currentNode)
|
|
|
|
let audioNode: LGraphNode | null = isAudioNodeSelected ? currentNode : null
|
|
const imageNode: LGraphNode | null = isImageNodeSelected
|
|
? currentNode
|
|
: null
|
|
const videoNode: LGraphNode | null = isVideoNodeSelected
|
|
? currentNode
|
|
: null
|
|
|
|
// Look for image paste data
|
|
for (const item of items) {
|
|
if (item.type.startsWith('image/')) {
|
|
pasteImageNode(canvas as LGraphCanvas, items, imageNode)
|
|
return
|
|
} else if (item.type.startsWith('video/')) {
|
|
if (!videoNode) {
|
|
// No video node selected: add a new one
|
|
// TODO: when video node exists
|
|
} else {
|
|
pasteItemsOnNode(items, videoNode, 'video')
|
|
return
|
|
}
|
|
} else if (item.type.startsWith('audio/')) {
|
|
if (!audioNode) {
|
|
// No audio node selected: add a new one
|
|
const newNode = LiteGraph.createNode('LoadAudio')
|
|
if (newNode) {
|
|
newNode.pos = [canvas.graph_mouse[0], canvas.graph_mouse[1]]
|
|
audioNode = graph?.add(newNode) ?? null
|
|
}
|
|
graph?.change()
|
|
}
|
|
pasteItemsOnNode(items, audioNode, 'audio')
|
|
return
|
|
}
|
|
}
|
|
if (pasteClipboardItems(data)) return
|
|
|
|
// No image found. Look for node data
|
|
data = data.getData('text/plain')
|
|
let workflow: ComfyWorkflowJSON | null = null
|
|
try {
|
|
data = data.slice(data.indexOf('{'))
|
|
workflow = JSON.parse(data)
|
|
} catch (err) {
|
|
try {
|
|
data = data.slice(data.indexOf('workflow\n'))
|
|
data = data.slice(data.indexOf('{'))
|
|
workflow = JSON.parse(data)
|
|
} catch (error) {
|
|
workflow = null
|
|
}
|
|
}
|
|
|
|
if (workflow && workflow.version && workflow.nodes && workflow.extra) {
|
|
await app.loadGraphData(workflow)
|
|
} else {
|
|
if (
|
|
(e.target instanceof HTMLTextAreaElement &&
|
|
e.target.type === 'textarea') ||
|
|
(e.target instanceof HTMLInputElement && e.target.type === 'text')
|
|
) {
|
|
return
|
|
}
|
|
|
|
// Litegraph default paste
|
|
canvas.pasteFromClipboard()
|
|
}
|
|
})
|
|
}
|