LCOV - code coverage report
Current view: top level - src/renderer/core/canvas/litegraph - selectionAdapter.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 74.5 % 145 108
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { ClickSelectionIntent } from '@/core/selection/selectionIntent'
       2              : import type {
       3              :   LGraph,
       4              :   LGraphCanvas,
       5              :   SubgraphInputNode,
       6              :   SubgraphOutputNode
       7              : } from '@comfyorg/graph-model/entityRegistry'
       8              : import type {
       9              :   SelectableKey,
      10              :   SelectionCommand
      11              : } from '@/core/selection/selectionState'
      12              : import {
      13              :   parseSelectableKey,
      14              :   toSelectableKey
      15              : } from '@/core/selection/selectionState'
      16              : 
      17              : import type { Positionable } from '@comfyorg/graph-model/interfaces'
      18              : import { LGraphGroup } from '@/lib/litegraph/src/LGraphGroup'
      19              : import { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
      20              : import { Reroute } from '@/lib/litegraph/src/Reroute'
      21              : import { SubgraphIONodeBase } from '@/lib/litegraph/src/subgraph/SubgraphIONodeBase'
      22              : import { isSubgraph } from '@/lib/litegraph/src/subgraph/subgraphTypeGuards'
      23              : 
      24              : import { useSelectionStore } from '@/core/selection/selectionStore'
      25              : import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
      26              : import { toNodeId } from '@comfyorg/graph-model/nodeId'
      27              : import { toRerouteId } from '@comfyorg/graph-model/rerouteId'
      28              : 
      29              : type SelectableItem =
      30              :   | LGraphNode
      31              :   | LGraphGroup
      32              :   | Reroute
      33              :   | SubgraphInputNode
      34              :   | SubgraphOutputNode
      35              : 
      36              : export function selectableKeyOf(item: SelectableItem): SelectableKey
      37              : export function selectableKeyOf(item: Positionable): SelectableKey | undefined
      38        26173 : export function selectableKeyOf(item: Positionable): SelectableKey | undefined {
      39        26173 :   if (item instanceof LGraphNode) return toSelectableKey('node', item.id)
      40          205 :   if (item instanceof LGraphGroup) return toSelectableKey('group', item.id)
      41           27 :   if (item instanceof Reroute) return toSelectableKey('reroute', item.id)
      42            3 :   if (item instanceof SubgraphIONodeBase) return toSelectableKey('io', item.id)
      43              : }
      44              : 
      45        16081 : export function resolveSelectable(
      46              :   graph: LGraph,
      47              :   key: SelectableKey
      48              : ): Positionable | undefined {
      49        16081 :   const { kind, id } = parseSelectableKey(key)
      50        16081 :   switch (kind) {
      51              :     case 'node':
      52        15913 :       return graph.getNodeById(toNodeId(id)) ?? undefined
      53              :     case 'group':
      54          154 :       return graph._groups.find((group) => String(group.id) === id)
      55              :     case 'reroute':
      56           10 :       return graph.getReroute(toRerouteId(Number(id)))
      57              :     case 'io':
      58            4 :       if (!isSubgraph(graph)) return undefined
      59            4 :       return [graph.inputNode, graph.outputNode].find(
      60              :         (ioNode) => String(ioNode.id) === id
      61              :       )
      62              :   }
      63              : }
      64              : 
      65              : interface PlannedSelectionChange {
      66              :   key: SelectableKey
      67              :   item: Positionable
      68              : }
      69              : 
      70        11976 : function planSelectionChange(
      71              :   keys: Set<SelectableKey>,
      72              :   changes: PlannedSelectionChange[],
      73              :   item: Positionable,
      74              :   selected: boolean
      75              : ): boolean {
      76        11976 :   const key = selectableKeyOf(item)
      77        11976 :   if (!key || keys.has(key) === selected) return false
      78         9717 :   if (selected) keys.add(key)
      79          286 :   else keys.delete(key)
      80         1602 :   changes.push({ key, item })
      81         1602 :   return true
      82              : }
      83              : 
      84            4 : function groupDescendants(group: LGraphGroup): Positionable[] {
      85            4 :   const seen = new Set<Positionable>([group])
      86            4 :   const descendants: Positionable[] = []
      87            4 :   const stack = [...group._children]
      88            4 :   while (stack.length > 0) {
      89           10 :     const child = stack.pop()!
      90           10 :     if (seen.has(child)) continue
      91            8 :     seen.add(child)
      92            8 :     descendants.push(child)
      93           10 :     if (child instanceof LGraphGroup) stack.push(...child._children)
      94              :   }
      95            4 :   return descendants
      96              : }
      97              : 
      98            0 : function fireSelectionHooks(
      99              :   canvas: LGraphCanvas,
     100              :   item: Positionable,
     101              :   selected: boolean
     102              : ): void {
     103            0 :   if (selected) {
     104            0 :     item.onSelected?.()
     105            0 :     if (item instanceof LGraphNode) canvas.onNodeSelected?.(item)
     106              :   } else {
     107            0 :     item.onDeselected?.()
     108            0 :     if (item instanceof LGraphNode) canvas.onNodeDeselected?.(item)
     109              :   }
     110              : }
     111              : 
     112        11981 : function canChangeSelection(
     113              :   canvas: LGraphCanvas,
     114              :   graph: LGraph,
     115              :   item: Positionable,
     116              :   selected: boolean
     117              : ): boolean {
     118        11981 :   if (selected) {
     119         1642 :     return (
     120              :       ownsSelectable(canvas, item) &&
     121              :       (!canvas.selectOnly || item instanceof LGraphNode)
     122              :     )
     123              :   }
     124        11981 :   return (
     125              :     ownsSelectable(canvas, item) ||
     126              :     (item instanceof LGraphNode && graph.nodes.includes(item))
     127              :   )
     128              : }
     129              : 
     130            4 : function planGroupDescendantChanges(
     131              :   canvas: LGraphCanvas,
     132              :   graph: LGraph,
     133              :   group: LGraphGroup,
     134              :   selected: boolean,
     135              :   keys: Set<SelectableKey>,
     136              :   changes: PlannedSelectionChange[]
     137              : ): void {
     138            4 :   for (const child of groupDescendants(group)) {
     139            8 :     if (!canChangeSelection(canvas, graph, child, selected)) continue
     140            8 :     planSelectionChange(keys, changes, child, selected)
     141              :   }
     142              : }
     143              : 
     144        12080 : function collectSelectionChanges(
     145              :   canvas: LGraphCanvas,
     146              :   graph: LGraph,
     147              :   items: Iterable<Positionable>,
     148              :   selected: boolean,
     149              :   selectGroupChildren: boolean,
     150              :   keys: Set<SelectableKey>
     151              : ): PlannedSelectionChange[] {
     152        12080 :   const changes: PlannedSelectionChange[] = []
     153        12080 :   for (const item of items) {
     154        11973 :     if (!canChangeSelection(canvas, graph, item, selected)) continue
     155        11972 :     if (!planSelectionChange(keys, changes, item, selected)) continue
     156         1793 :     if (!(item instanceof LGraphGroup)) continue
     157              : 
     158          102 :     if (selected) item.recomputeInsideNodes()
     159          100 :     if (!selectGroupChildren) continue
     160            4 :     planGroupDescendantChanges(canvas, graph, item, selected, keys, changes)
     161              :   }
     162        12080 :   return changes
     163              : }
     164              : 
     165         1329 : function hasSelectionHooks(
     166              :   canvas: LGraphCanvas,
     167              :   changes: PlannedSelectionChange[],
     168              :   selected: boolean
     169              : ): boolean {
     170         1329 :   return changes.some(({ item }) =>
     171              :     selected
     172              :       ? Boolean(
     173              :           item.onSelected ||
     174              :           (item instanceof LGraphNode && canvas.onNodeSelected)
     175              :         )
     176              :       : Boolean(
     177              :           item.onDeselected ||
     178              :           (item instanceof LGraphNode && canvas.onNodeDeselected)
     179              :         )
     180              :   )
     181              : }
     182              : 
     183            0 : function applySelectionItemWithHooks(
     184              :   canvas: LGraphCanvas,
     185              :   graph: LGraph,
     186              :   scope: ReturnType<typeof graphScopeOf>,
     187              :   item: Positionable,
     188              :   selected: boolean
     189              : ): boolean {
     190            0 :   if (canvas.graph !== graph) return false
     191            0 :   if (!canChangeSelection(canvas, graph, item, selected)) return false
     192            0 :   const key = selectableKeyOf(item)
     193            0 :   if (!key) return false
     194              : 
     195            0 :   const store = useSelectionStore()
     196            0 :   if (store.isSelected(scope, key) === selected) return false
     197            0 :   if (selected && item instanceof LGraphGroup) item.recomputeInsideNodes()
     198            0 :   store.apply(scope, {
     199              :     type: selected ? 'selection.add' : 'selection.remove',
     200              :     key
     201              :   })
     202            0 :   fireSelectionHooks(canvas, item, selected)
     203            0 :   return true
     204              : }
     205              : 
     206            0 : function applySelectionWithHooks(
     207              :   canvas: LGraphCanvas,
     208              :   graph: LGraph,
     209              :   scope: ReturnType<typeof graphScopeOf>,
     210              :   items: Positionable[],
     211              :   selected: boolean,
     212              :   selectGroupChildren: boolean
     213              : ): boolean {
     214            0 :   let changed = false
     215            0 :   for (const item of items) {
     216            0 :     const itemChanged = applySelectionItemWithHooks(
     217              :       canvas,
     218              :       graph,
     219              :       scope,
     220              :       item,
     221              :       selected
     222              :     )
     223            0 :     changed ||= itemChanged
     224            0 :     if (!itemChanged || !(item instanceof LGraphGroup) || !selectGroupChildren)
     225            0 :       continue
     226            0 :     for (const child of groupDescendants(item)) {
     227            0 :       changed =
     228              :         applySelectionItemWithHooks(canvas, graph, scope, child, selected) ||
     229              :         changed
     230              :     }
     231              :   }
     232            0 :   return changed
     233              : }
     234              : 
     235        12080 : export function changeCanvasSelection(
     236              :   canvas: LGraphCanvas,
     237              :   items: Iterable<Positionable>,
     238              :   selected: boolean,
     239              :   selectGroupChildren = canvas.groupSelectChildren
     240              : ): boolean {
     241        12080 :   const { graph } = canvas
     242        12080 :   if (!graph) return false
     243              : 
     244        12080 :   const scope = graphScopeOf(graph)
     245        12080 :   const store = useSelectionStore()
     246        12080 :   const keys = new Set(store.selectedKeys(scope))
     247        12080 :   const itemList = [...items]
     248        12080 :   const changes = collectSelectionChanges(
     249              :     canvas,
     250              :     graph,
     251              :     itemList,
     252              :     selected,
     253              :     selectGroupChildren,
     254              :     keys
     255              :   )
     256        12080 :   if (changes.length === 0) return false
     257              : 
     258         1329 :   if (!hasSelectionHooks(canvas, changes, selected)) {
     259         1329 :     store.apply(scope, { type: 'selection.replace', keys: [...keys] })
     260         1329 :     return true
     261              :   }
     262              : 
     263            0 :   return applySelectionWithHooks(
     264              :     canvas,
     265              :     graph,
     266              :     scope,
     267              :     itemList,
     268              :     selected,
     269              :     selectGroupChildren
     270              :   )
     271              : }
     272              : 
     273         6249 : export function selectedCanvasNodes(canvas: LGraphCanvas): LGraphNode[] {
     274         6249 :   return [...canvas.selectedItems].filter((item) => item instanceof LGraphNode)
     275              : }
     276              : 
     277         1311 : export function clearCanvasSelection(
     278              :   canvas: LGraphCanvas,
     279              :   keepSelected?: Positionable
     280              : ): boolean {
     281         1311 :   if (!canvas.graph) return false
     282              : 
     283         1311 :   const selected = canvas.selectedItems
     284         1311 :   if (!selected.size) return false
     285              : 
     286         1311 :   const kept =
     287              :     keepSelected &&
     288              :     ownsSelectable(canvas, keepSelected) &&
     289              :     selected.has(keepSelected)
     290              :       ? keepSelected
     291              :       : undefined
     292         1311 :   const deselected = [...selected].filter(
     293              :     (item) => item !== kept && ownsSelectable(canvas, item)
     294              :   )
     295         1311 :   const keptKey = kept && selectableKeyOf(kept)
     296         1311 :   applyCanvasSelection(
     297              :     canvas,
     298              :     keptKey
     299              :       ? { type: 'selection.replace', keys: [keptKey] }
     300              :       : { type: 'selection.clear' }
     301              :   )
     302         1311 :   canvas.current_node = null
     303              : 
     304         1311 :   const changed = selected.size !== canvas.selectedItems.size
     305         1311 :   for (const item of deselected) item.onDeselected?.()
     306          327 :   return changed
     307              : }
     308              : 
     309          267 : export function replaceCanvasSelection(
     310              :   canvas: LGraphCanvas,
     311              :   items: Iterable<Positionable>,
     312              :   selectGroupChildren = canvas.groupSelectChildren
     313              : ): boolean {
     314          267 :   const desired = new Set(items)
     315              :   const dropped = [...canvas.selectedItems].filter((item) => !desired.has(item))
     316          267 :   const removed = changeCanvasSelection(canvas, dropped, false)
     317          267 :   const added = changeCanvasSelection(
     318              :     canvas,
     319              :     desired,
     320              :     true,
     321              :     selectGroupChildren
     322              :   )
     323          267 :   return removed || added
     324              : }
     325              : 
     326         1328 : export function applyClickSelection(
     327              :   canvas: LGraphCanvas,
     328              :   intent: ClickSelectionIntent,
     329              :   item?: Positionable
     330              : ): boolean {
     331         1328 :   switch (intent) {
     332              :     case 'none':
     333          143 :       return false
     334              :     case 'clear':
     335          234 :       return clearCanvasSelection(canvas)
     336              :     case 'replace': {
     337          739 :       const cleared = clearCanvasSelection(canvas, item)
     338          739 :       const selected = !!item && changeCanvasSelection(canvas, [item], true)
     339          739 :       return cleared || selected
     340              :     }
     341              :     case 'add':
     342          198 :       return !!item && changeCanvasSelection(canvas, [item], true)
     343              :     case 'remove':
     344           14 :       return !!item && changeCanvasSelection(canvas, [item], false)
     345              :   }
     346              : }
     347              : 
     348        10541 : export function applyCanvasSelection(
     349              :   canvas: LGraphCanvas,
     350              :   command: SelectionCommand
     351              : ): void {
     352        10541 :   applyGraphSelection(canvas.graph, command)
     353              : }
     354              : 
     355        10394 : export function releaseCanvasSelection(canvas: LGraphCanvas): void {
     356        10394 :   canvas.selected_group = null
     357              : }
     358              : 
     359          216 : export function clearGraphSelection(graph: LGraphCanvas['graph']): void {
     360          216 :   applyGraphSelection(graph, { type: 'selection.clear' })
     361              : }
     362              : 
     363            0 : export function updateGraphSelection(
     364              :   graph: LGraph,
     365              :   item: Positionable,
     366              :   selected: boolean
     367              : ): boolean {
     368            0 :   const key = selectableKeyOf(item)
     369            0 :   if (!key || resolveSelectable(graph, key) !== item) return false
     370              : 
     371            0 :   const store = useSelectionStore()
     372            0 :   const scope = graphScopeOf(graph)
     373            0 :   const wasSelected = store.isSelected(scope, key)
     374            0 :   if (wasSelected !== selected) {
     375            0 :     store.apply(scope, {
     376              :       type: selected ? 'selection.add' : 'selection.remove',
     377              :       key
     378              :     })
     379              :   }
     380            0 :   return wasSelected
     381              : }
     382              : 
     383        10757 : function applyGraphSelection(
     384              :   graph: LGraphCanvas['graph'],
     385              :   command: SelectionCommand
     386              : ): void {
     387        10757 :   if (!graph) return
     388        10757 :   useSelectionStore().apply(graphScopeOf(graph), command)
     389              : }
     390              : 
     391         1094 : export function isCanvasItemSelected(
     392              :   canvas: LGraphCanvas,
     393              :   item: Positionable
     394              : ): boolean {
     395         1094 :   const { graph } = canvas
     396         1094 :   const key = selectableKeyOf(item)
     397         1094 :   return (
     398              :     !!graph && !!key && useSelectionStore().isSelected(graphScopeOf(graph), key)
     399              :   )
     400              : }
     401              : 
     402        12913 : export function ownsSelectable(
     403              :   canvas: LGraphCanvas,
     404              :   item: Positionable
     405              : ): boolean {
     406        12913 :   const { graph } = canvas
     407        12913 :   const key = selectableKeyOf(item)
     408        12913 :   return !!graph && !!key && resolveSelectable(graph, key) === item
     409              : }
        

Generated by: LCOV version 2.0-1