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

            Line data    Source code
       1              : // import { useSelectedLiteGraphItems } from '@/composables/canvas/useSelectedLiteGraphItems' // Unused for now
       2              : import { t } from '@/i18n'
       3              : import { LGraphNode } from '@/lib/litegraph/src/litegraph'
       4              : import { useToast } from '@/components/ui/toast/toastStore'
       5              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
       6              : import {
       7              :   useCanvasStore,
       8              :   useTitleEditorStore
       9              : } from '@/renderer/core/canvas/canvasStore'
      10              : import { app } from '@/scripts/app'
      11              : import { useDialogService } from '@/services/dialogService'
      12              : import { isSelectOnly } from '@/utils/litegraphUtil'
      13              : 
      14              : /**
      15              :  * Composable for handling basic selection operations like copy, paste, duplicate, delete, rename
      16              :  */
      17         2355 : export function useSelectionOperations() {
      18              :   // const { getSelectedNodes } = useSelectedLiteGraphItems() // Unused for now
      19         2355 :   const canvasStore = useCanvasStore()
      20         2355 :   const toast = useToast()
      21         2355 :   const dialogService = useDialogService()
      22         2355 :   const titleEditorStore = useTitleEditorStore()
      23         2355 :   const workflowStore = useWorkflowStore()
      24              : 
      25         2355 :   const copySelection = () => {
      26            3 :     const canvas = app.canvas
      27            3 :     if (canvas.selectedItems.size === 0) {
      28            0 :       toast.warning(t('g.nothingToCopy'), {
      29              :         description: t('g.selectItemsToCopy'),
      30              :         duration: 3000
      31              :       })
      32            0 :       return
      33              :     }
      34              : 
      35            3 :     canvas.copyToClipboard()
      36            3 :     toast.success(t('g.copied'), {
      37              :       description: t('g.itemsCopiedToClipboard'),
      38              :       duration: 2000
      39              :     })
      40              :   }
      41              : 
      42         2355 :   const pasteSelection = () => {
      43              :     const canvas = app.canvas
      44            0 :     canvas.pasteFromClipboard({ connectInputs: false })
      45              : 
      46              :     // Trigger change tracking
      47            0 :     workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
      48              :   }
      49              : 
      50         2355 :   const duplicateSelection = () => {
      51            4 :     const canvas = app.canvas
      52            4 :     if (canvas.selectedItems.size === 0) {
      53            0 :       toast.warning(t('g.nothingToDuplicate'), {
      54              :         description: t('g.selectItemsToDuplicate'),
      55              :         duration: 3000
      56              :       })
      57            0 :       return
      58              :     }
      59              : 
      60              :     // Copy current selection
      61            4 :     canvas.copyToClipboard()
      62              : 
      63              :     // Clear selection to avoid confusion
      64            4 :     canvas.deselectAll()
      65              : 
      66              :     // Paste to create duplicates
      67            4 :     canvas.pasteFromClipboard({ connectInputs: false })
      68              : 
      69              :     // Trigger change tracking
      70            4 :     workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
      71              :   }
      72              : 
      73         2355 :   const deleteSelection = () => {
      74            2 :     const canvas = app.canvas
      75              :     // Picking nodes for the agent is not editing: deleting stays off until the
      76              :     // mode ends.
      77            2 :     if (isSelectOnly(canvas)) return
      78            2 :     if (canvas.selectedItems.size === 0) {
      79            0 :       toast.warning(t('g.nothingToDelete'), {
      80              :         description: t('g.selectItemsToDelete'),
      81              :         duration: 3000
      82              :       })
      83            0 :       return
      84              :     }
      85              : 
      86            2 :     canvas.deleteSelected()
      87            2 :     canvas.setDirty(true, true)
      88              : 
      89              :     // Trigger change tracking
      90            2 :     workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
      91              :   }
      92              : 
      93         2355 :   const renameSelection = async () => {
      94            6 :     const selectedItems = Array.from(canvasStore.selectedItems)
      95              : 
      96              :     // Handle single node selection
      97            6 :     if (selectedItems.length === 1) {
      98            4 :       const item = selectedItems[0]
      99              : 
     100              :       // For nodes, use the title editor
     101            4 :       if (item instanceof LGraphNode) {
     102            3 :         titleEditorStore.titleEditorTarget = item
     103            3 :         return
     104              :       }
     105              : 
     106              :       // For other items like groups, use prompt dialog
     107            4 :       const currentTitle = 'title' in item ? (item.title as string) : ''
     108            4 :       const newTitle = await dialogService.prompt({
     109              :         title: t('g.rename'),
     110              :         message: t('g.enterNewName'),
     111              :         defaultValue: currentTitle
     112              :       })
     113              : 
     114            4 :       if (newTitle && newTitle !== currentTitle) {
     115            1 :         if ('title' in item) {
     116              :           // Type-safe assignment for items with title property
     117            1 :           const titledItem = item as { title: string }
     118            1 :           titledItem.title = newTitle
     119            1 :           app.canvas.setDirty(true, true)
     120            1 :           workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
     121              :         }
     122              :       }
     123            1 :       return
     124              :     }
     125              : 
     126              :     // Handle multiple selections - batch rename
     127            2 :     if (selectedItems.length > 1) {
     128            2 :       const baseTitle = await dialogService.prompt({
     129              :         title: t('g.batchRename'),
     130              :         message: t('g.enterBaseName'),
     131              :         defaultValue: 'Item'
     132              :       })
     133              : 
     134            2 :       if (baseTitle) {
     135            2 :         selectedItems.forEach((item, index) => {
     136            4 :           if ('title' in item) {
     137              :             // Type-safe assignment for items with title property
     138            4 :             const titledItem = item as { title: string }
     139            4 :             titledItem.title = `${baseTitle} ${index + 1}`
     140              :           }
     141              :         })
     142            2 :         app.canvas.setDirty(true, true)
     143            2 :         workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
     144              :       }
     145            2 :       return
     146              :     }
     147              : 
     148            0 :     toast.warning(t('g.nothingToRename'), {
     149              :       description: t('g.selectItemsToRename'),
     150              :       duration: 3000
     151              :     })
     152              :   }
     153              : 
     154         2355 :   return {
     155              :     copySelection,
     156              :     pasteSelection,
     157              :     duplicateSelection,
     158              :     deleteSelection,
     159              :     renameSelection
     160              :   }
     161              : }
        

Generated by: LCOV version 2.0-1