LCOV - code coverage report
Current view: top level - src/composables/graph - useSubgraphOperations.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 87.5 % 48 42
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { useSelectedLiteGraphItems } from '@/composables/canvas/useSelectedLiteGraphItems'
       2              : import { useToast } from '@/components/ui/toast/toastStore'
       3              : import { t } from '@/i18n'
       4              : import { SubgraphNode } from '@/lib/litegraph/src/litegraph'
       5              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
       6              : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
       7              : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
       8              : import { useSubgraphStore } from '@/stores/subgraphStore'
       9              : 
      10              : /**
      11              :  * Composable for handling subgraph-related operations
      12              :  */
      13         2356 : export function useSubgraphOperations() {
      14         2356 :   const { getSelectedNodes } = useSelectedLiteGraphItems()
      15         2356 :   const canvasStore = useCanvasStore()
      16         2356 :   const workflowStore = useWorkflowStore()
      17         2356 :   const nodeOutputStore = useNodeOutputStore()
      18         2356 :   const subgraphStore = useSubgraphStore()
      19         2356 :   const toast = useToast()
      20              : 
      21         2356 :   const convertToSubgraph = () => {
      22           27 :     const canvas = canvasStore.getCanvas()
      23           27 :     const graph = canvas.subgraph ?? canvas.graph
      24           27 :     if (!graph) {
      25            0 :       return null
      26              :     }
      27              : 
      28              :     const res = graph.convertToSubgraph(canvas.selectedItems)
      29           27 :     const { node } = res
      30           27 :     canvas.select(node)
      31              :     // Trigger change tracking
      32           27 :     workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
      33              :   }
      34              : 
      35         2356 :   const doUnpack = (
      36              :     subgraphNodes: SubgraphNode[],
      37              :     skipMissingNodes: boolean
      38              :   ) => {
      39           14 :     const canvas = canvasStore.getCanvas()
      40           14 :     const graph = canvas.subgraph ?? canvas.graph
      41           14 :     if (!graph) return
      42              : 
      43           14 :     let changed = false
      44           14 :     let refused = false
      45           14 :     for (const subgraphNode of subgraphNodes) {
      46           14 :       if (!graph.unpackSubgraph(subgraphNode, { skipMissingNodes })) {
      47            2 :         refused = true
      48            2 :         continue
      49              :       }
      50           12 :       nodeOutputStore.revokeSubgraphPreviews(subgraphNode, graph)
      51           12 :       changed = true
      52              :     }
      53           14 :     if (refused) {
      54            2 :       toast.error(t('g.error'), {
      55              :         description: t('g.subgraphUnpackFailed')
      56              :       })
      57              :     }
      58           14 :     if (changed) {
      59           12 :       workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
      60              :     }
      61              :   }
      62              : 
      63         2356 :   const unpackSubgraph = () => {
      64           14 :     const canvas = canvasStore.getCanvas()
      65              :     const graph = canvas.subgraph ?? canvas.graph
      66              : 
      67           14 :     if (!graph) {
      68            0 :       return
      69              :     }
      70              : 
      71              :     const selectedItems = Array.from(canvas.selectedItems)
      72           14 :     const subgraphNodes = selectedItems.filter(
      73              :       (item): item is SubgraphNode => item instanceof SubgraphNode
      74              :     )
      75              : 
      76           14 :     if (subgraphNodes.length === 0) {
      77            0 :       return
      78              :     }
      79           14 :     doUnpack(subgraphNodes, true)
      80              :   }
      81              : 
      82         2356 :   const addSubgraphToLibrary = async () => {
      83              :     const selectedItems = Array.from(canvasStore.selectedItems)
      84              :     const subgraphNodes = selectedItems.filter(
      85              :       (item): item is SubgraphNode => item instanceof SubgraphNode
      86              :     )
      87            1 :     if (subgraphNodes.length !== 1) {
      88            0 :       return
      89              :     }
      90            1 :     await subgraphStore.publishSubgraph()
      91              :   }
      92              : 
      93         2356 :   const isSubgraphSelected = (): boolean => {
      94              :     const selectedItems = Array.from(canvasStore.selectedItems)
      95            0 :     return selectedItems.some((item) => item instanceof SubgraphNode)
      96              :   }
      97              : 
      98         2356 :   const hasSelectableNodes = (): boolean => {
      99            0 :     return getSelectedNodes().length > 0
     100              :   }
     101              : 
     102         2356 :   return {
     103              :     convertToSubgraph,
     104              :     unpackSubgraph,
     105              :     addSubgraphToLibrary,
     106              :     isSubgraphSelected,
     107              :     hasSelectableNodes
     108              :   }
     109              : }
        

Generated by: LCOV version 2.0-1