mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-01 13:59:54 +00:00
Prior to the release of subgraphs, there was a single graph accessed through `app.graph`. Now that there's multiple graphs, there's a lot of code that needs to be reviewed and potentially updated depending on if it cares about nearby nodes, all nodes, or something else requiring specific attention. This was done by simply changing the type of `app.graph` to unknown so the typechecker will complain about every place it's currently used. References were then updated to `app.rootGraph` if the previous usage was correct, or actually rewritten. By not getting rid of `app.graph`, this change already ensures that there's no loss of functionality for custom nodes, but the prior typing of `app.graph` can always be restored if future dissuasion of `app.graph` usage creates issues. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-7399-Cleanup-app-graph-usage-2c76d73d365081178743dfdcf07f44d0) by [Unito](https://www.unito.io)
86 lines
3.5 KiB
TypeScript
86 lines
3.5 KiB
TypeScript
import type { Ref } from 'vue'
|
|
|
|
import { useSharedCanvasPositionConversion } from '@/composables/element/useCanvasPositionConversion'
|
|
import { usePragmaticDroppable } from '@/composables/usePragmaticDragAndDrop'
|
|
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
|
import { LiteGraph } from '@/lib/litegraph/src/litegraph'
|
|
import { useWorkflowService } from '@/platform/workflow/core/services/workflowService'
|
|
import { ComfyWorkflow } from '@/platform/workflow/management/stores/workflowStore'
|
|
import { app as comfyApp } from '@/scripts/app'
|
|
import { useLitegraphService } from '@/services/litegraphService'
|
|
import { ComfyModelDef } from '@/stores/modelStore'
|
|
import type { ModelNodeProvider } from '@/stores/modelToNodeStore'
|
|
import { useModelToNodeStore } from '@/stores/modelToNodeStore'
|
|
import { ComfyNodeDefImpl } from '@/stores/nodeDefStore'
|
|
import type { RenderedTreeExplorerNode } from '@/types/treeExplorerTypes'
|
|
|
|
export const useCanvasDrop = (canvasRef: Ref<HTMLCanvasElement | null>) => {
|
|
const modelToNodeStore = useModelToNodeStore()
|
|
const litegraphService = useLitegraphService()
|
|
const workflowService = useWorkflowService()
|
|
|
|
usePragmaticDroppable(() => canvasRef.value, {
|
|
getDropEffect: (args): Exclude<DataTransfer['dropEffect'], 'none'> =>
|
|
args.source.data.type === 'tree-explorer-node' ? 'copy' : 'move',
|
|
onDrop: async (event) => {
|
|
const loc = event.location.current.input
|
|
const dndData = event.source.data
|
|
|
|
if (dndData.type === 'tree-explorer-node') {
|
|
const node = dndData.data as RenderedTreeExplorerNode
|
|
const conv = useSharedCanvasPositionConversion()
|
|
const basePos = conv.clientPosToCanvasPos([loc.clientX, loc.clientY])
|
|
|
|
if (node.data instanceof ComfyNodeDefImpl) {
|
|
const nodeDef = node.data
|
|
const pos = [...basePos]
|
|
// Add an offset on y to make sure after adding the node, the cursor
|
|
// is on the node (top left corner)
|
|
pos[1] += LiteGraph.NODE_TITLE_HEIGHT
|
|
litegraphService.addNodeOnGraph(nodeDef, { pos })
|
|
} else if (node.data instanceof ComfyModelDef) {
|
|
const model = node.data
|
|
const pos = basePos
|
|
const nodeAtPos = comfyApp.canvas.graph?.getNodeOnPos(pos[0], pos[1])
|
|
let targetProvider: ModelNodeProvider | null = null
|
|
let targetGraphNode: LGraphNode | null = null
|
|
if (nodeAtPos) {
|
|
const providers = modelToNodeStore.getAllNodeProviders(
|
|
model.directory
|
|
)
|
|
for (const provider of providers) {
|
|
if (provider.nodeDef.name === nodeAtPos.comfyClass) {
|
|
targetGraphNode = nodeAtPos
|
|
targetProvider = provider
|
|
}
|
|
}
|
|
}
|
|
if (!targetGraphNode) {
|
|
const provider = modelToNodeStore.getNodeProvider(model.directory)
|
|
if (provider) {
|
|
targetGraphNode = litegraphService.addNodeOnGraph(
|
|
provider.nodeDef,
|
|
{
|
|
pos
|
|
}
|
|
)
|
|
targetProvider = provider
|
|
}
|
|
}
|
|
if (targetGraphNode) {
|
|
const widget = targetGraphNode.widgets?.find(
|
|
(widget) => widget.name === targetProvider?.key
|
|
)
|
|
if (widget) {
|
|
widget.value = model.file_name
|
|
}
|
|
}
|
|
} else if (node.data instanceof ComfyWorkflow) {
|
|
const workflow = node.data
|
|
await workflowService.insertWorkflow(workflow, { position: basePos })
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|