LCOV - code coverage report
Current view: top level - src/composables/canvas - useSelectedLiteGraphItems.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 91.2 % 34 31
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { uniq } from 'es-toolkit'
       2              : 
       3              : import type { LGraphNode, Positionable } from '@/lib/litegraph/src/litegraph'
       4              : import { LGraphEventMode, Reroute } from '@/lib/litegraph/src/litegraph'
       5              : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
       6              : import { collectFromNodes } from '@/utils/graphTraversalUtil'
       7              : import { isLGraphGroup, isLGraphNode } from '@/utils/litegraphUtil'
       8              : 
       9              : /**
      10              :  * Composable for handling selected LiteGraph items filtering and operations.
      11              :  * This provides utilities for working with selected items on the canvas,
      12              :  * including filtering out items that should not be included in selection operations.
      13              :  */
      14      2459867 : export function useSelectedLiteGraphItems() {
      15      2459867 :   const canvasStore = useCanvasStore()
      16              : 
      17              :   /**
      18              :    * Items that should not show in the selection overlay are ignored.
      19              :    * @param item - The item to check.
      20              :    * @returns True if the item should be ignored, false otherwise.
      21              :    */
      22      2459867 :   const isIgnoredItem = (item: Positionable): boolean => {
      23          362 :     return item instanceof Reroute
      24              :   }
      25              : 
      26              :   /**
      27              :    * Filter out items that should not show in the selection overlay.
      28              :    * @param items - The Set of items to filter.
      29              :    * @returns The filtered Set of items.
      30              :    */
      31      2459867 :   const filterSelectableItems = (
      32              :     items: Set<Positionable>
      33              :   ): Set<Positionable> => {
      34          945 :     const result = new Set<Positionable>()
      35          945 :     for (const item of items) {
      36          362 :       if (!isIgnoredItem(item)) {
      37          362 :         result.add(item)
      38              :       }
      39              :     }
      40          945 :     return result
      41              :   }
      42              : 
      43              :   /**
      44              :    * Get the filtered selected items from the canvas.
      45              :    * @returns The filtered Set of selected items.
      46              :    */
      47      2459867 :   const getSelectableItems = (): Set<Positionable> => {
      48          945 :     const { selectedItems } = canvasStore.getCanvas()
      49          945 :     return filterSelectableItems(selectedItems)
      50              :   }
      51              : 
      52              :   /**
      53              :    * Check if there are any selectable items.
      54              :    * @returns True if there are selectable items, false otherwise.
      55              :    */
      56      2459867 :   const hasSelectableItems = (): boolean => {
      57            0 :     return getSelectableItems().size > 0
      58              :   }
      59              : 
      60              :   /**
      61              :    * Check if there are multiple selectable items.
      62              :    * @returns True if there are multiple selectable items, false otherwise.
      63              :    */
      64      2459867 :   const hasMultipleSelectableItems = (): boolean => {
      65            0 :     return getSelectableItems().size > 1
      66              :   }
      67              : 
      68              :   /**
      69              :    * The top-level selected nodes from the canonical selection set.
      70              :    * Shallow — does NOT expand subgraph children, unlike {@link getSelectedNodes}.
      71              :    * Mode toggles use this so they apply to the selected subgraph node, not its
      72              :    * descendants. Returns `[]` when the canvas is not yet available, preserving
      73              :    * the prior null-tolerance for callers wired to early-firing commands.
      74              :    */
      75      2459867 :   const getSelectedNodesShallow = (): LGraphNode[] =>
      76              :     uniq(
      77              :       [...(canvasStore.canvas?.selectedItems ?? [])].flatMap((item) => {
      78         3205 :         if (isLGraphNode(item)) return [item]
      79           47 :         if (isLGraphGroup(item)) return [...item.children].filter(isLGraphNode)
      80            5 :         return []
      81              :       })
      82              :     )
      83              : 
      84              :   /**
      85              :    * Get only the selected nodes (LGraphNode instances) from the canvas.
      86              :    * This filters out other types of selected items like groups or reroutes.
      87              :    * If a selected node is a subgraph, this also includes all nodes within it.
      88              :    * @returns Array of selected LGraphNode instances and their descendants.
      89              :    */
      90      2459867 :   const getSelectedNodes = (): LGraphNode[] => {
      91           24 :     const nodeArray = getSelectedNodesShallow()
      92              : 
      93              :     const hasSubgraphs = nodeArray.some(
      94              :       (node) => node.isSubgraphNode() && node.subgraph
      95              :     )
      96           24 :     if (!hasSubgraphs) return nodeArray
      97              : 
      98            0 :     return collectFromNodes(nodeArray)
      99              :   }
     100              : 
     101              :   /**
     102              :    * True iff every selected node is in `mode`. Mirrors the predicate used by
     103              :    * {@link toggleSelectedNodesMode} so labels match the toggle's effect.
     104              :    * An empty selection returns `false` (no node is in the mode).
     105              :    */
     106      2459867 :   const areAllSelectedNodesInMode = (mode: LGraphEventMode): boolean => {
     107        13423 :     const selectedNodeArray = getSelectedNodesShallow()
     108        13423 :     return (
     109              :       selectedNodeArray.length > 0 &&
     110              :       selectedNodeArray.every((node) => node.mode === mode)
     111              :     )
     112              :   }
     113              : 
     114              :   /**
     115              :    * Toggle the execution mode of all selected nodes
     116              :    *
     117              :    * - If any nodes are not already the specified node mode → all are set to specified mode
     118              :    * - Otherwise → set all nodes to ALWAYS
     119              :    *
     120              :    * @param mode - The LGraphEventMode to toggle to (e.g., NEVER for mute, BYPASS for bypass)
     121              :    */
     122      2459867 :   const toggleSelectedNodesMode = (mode: LGraphEventMode): void => {
     123           54 :     const selectedNodeArray = getSelectedNodesShallow()
     124           54 :     const newModeForSelectedNode = areAllSelectedNodesInMode(mode)
     125              :       ? LGraphEventMode.ALWAYS
     126              :       : mode
     127              : 
     128           54 :     for (const selectedNode of selectedNodeArray)
     129           87 :       selectedNode.mode = newModeForSelectedNode
     130              :   }
     131              : 
     132      2459867 :   return {
     133              :     isIgnoredItem,
     134              :     filterSelectableItems,
     135              :     getSelectableItems,
     136              :     hasSelectableItems,
     137              :     hasMultipleSelectableItems,
     138              :     getSelectedNodesShallow,
     139              :     getSelectedNodes,
     140              :     areAllSelectedNodesInMode,
     141              :     toggleSelectedNodesMode
     142              :   }
     143              : }
        

Generated by: LCOV version 2.0-1