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

            Line data    Source code
       1              : import { useSelectionState } from '@/composables/graph/useSelectionState'
       2              : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
       3              : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
       4              : import { TitleMode } from '@comfyorg/graph-model/globalEnums'
       5              : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
       6              : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
       7              : import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations'
       8              : import { LayoutSource } from '@/renderer/core/layout/types'
       9              : import type { Point2D as Point } from '@comfyorg/shared-frontend-utils/geometry'
      10              : import { app } from '@/scripts/app'
      11              : import type { NodeId } from '@comfyorg/graph-model/nodeId'
      12              : 
      13              : export type ArrangeLayout = 'vertical' | 'horizontal' | 'grid'
      14              : 
      15              : export const DEFAULT_ARRANGE_GAP = 12
      16              : export const MIN_ARRANGE_GAP = 0
      17              : export const MAX_ARRANGE_GAP = 48
      18              : 
      19              : interface NodeBox {
      20              :   id: NodeId
      21              :   posX: number
      22              :   posY: number
      23              :   visualWidth: number
      24              :   visualHeight: number
      25              :   titleHeight: number
      26              : }
      27              : 
      28              : interface ArrangeUpdate {
      29              :   nodeId: NodeId
      30              :   position: Point
      31              : }
      32              : 
      33         2355 : const titleHeightOf = (node: LGraphNode): number => {
      34            0 :   const mode = node.title_mode
      35            0 :   if (mode === TitleMode.TRANSPARENT_TITLE || mode === TitleMode.NO_TITLE) {
      36            0 :     return 0
      37              :   }
      38            0 :   return LiteGraph.NODE_TITLE_HEIGHT
      39              : }
      40              : 
      41         2355 : const toBox = (node: LGraphNode): NodeBox => {
      42            0 :   const titleHeight = titleHeightOf(node)
      43            0 :   const [width, height] = node.renderingSize
      44            0 :   return {
      45              :     id: node.id,
      46              :     posX: node.pos[0],
      47              :     posY: node.pos[1],
      48              :     visualWidth: width,
      49              :     visualHeight: height + titleHeight,
      50              :     titleHeight
      51              :   }
      52              : }
      53              : 
      54         2355 : const byTopDown = (a: NodeBox, b: NodeBox) => a.posY - b.posY || a.posX - b.posX
      55              : 
      56         2355 : const byLeftRight = (a: NodeBox, b: NodeBox) =>
      57              :   a.posX - b.posX || a.posY - b.posY
      58              : 
      59         2355 : const findAnchor = (boxes: NodeBox[]): NodeBox =>
      60              :   boxes.reduce((best, box) =>
      61              :     box.posX + box.posY < best.posX + best.posY ? box : best
      62              :   )
      63              : 
      64         2355 : const cumulativeOffsets = (
      65              :   sizes: number[],
      66              :   origin: number,
      67              :   gap: number
      68              : ): number[] => {
      69            0 :   const offsets: number[] = [origin]
      70            0 :   for (let i = 1; i < sizes.length; i++) {
      71            0 :     offsets.push(offsets[i - 1] + sizes[i - 1] + gap)
      72              :   }
      73            0 :   return offsets
      74              : }
      75              : 
      76         2355 : const arrangeVertical = (
      77              :   boxes: NodeBox[],
      78              :   anchor: NodeBox,
      79              :   gap: number
      80              : ): ArrangeUpdate[] => {
      81            0 :   const sorted = [...boxes].sort(byTopDown)
      82            0 :   let visualTop = anchor.posY - anchor.titleHeight
      83            0 :   return sorted.map((box) => {
      84            0 :     const update: ArrangeUpdate = {
      85              :       nodeId: box.id,
      86              :       position: { x: anchor.posX, y: visualTop + box.titleHeight }
      87              :     }
      88            0 :     visualTop += box.visualHeight + gap
      89            0 :     return update
      90              :   })
      91              : }
      92              : 
      93         2355 : const arrangeHorizontal = (
      94              :   boxes: NodeBox[],
      95              :   anchor: NodeBox,
      96              :   gap: number
      97              : ): ArrangeUpdate[] => {
      98            0 :   const sorted = [...boxes].sort(byLeftRight)
      99            0 :   const visualTop = anchor.posY - anchor.titleHeight
     100            0 :   let cursorX = anchor.posX
     101            0 :   return sorted.map((box) => {
     102            0 :     const update: ArrangeUpdate = {
     103              :       nodeId: box.id,
     104              :       position: { x: cursorX, y: visualTop + box.titleHeight }
     105              :     }
     106            0 :     cursorX += box.visualWidth + gap
     107            0 :     return update
     108              :   })
     109              : }
     110              : 
     111         2355 : const arrangeGrid = (
     112              :   boxes: NodeBox[],
     113              :   anchor: NodeBox,
     114              :   gap: number
     115              : ): ArrangeUpdate[] => {
     116            0 :   const sorted = [...boxes].sort(byTopDown)
     117            0 :   const cols = Math.ceil(Math.sqrt(sorted.length))
     118            0 :   const rows = Math.ceil(sorted.length / cols)
     119              : 
     120            0 :   const colWidths = Array.from({ length: cols }, () => 0)
     121            0 :   const rowHeights = Array.from({ length: rows }, () => 0)
     122            0 :   sorted.forEach((box, i) => {
     123            0 :     const col = i % cols
     124            0 :     const row = Math.floor(i / cols)
     125            0 :     if (box.visualWidth > colWidths[col]) colWidths[col] = box.visualWidth
     126            0 :     if (box.visualHeight > rowHeights[row]) rowHeights[row] = box.visualHeight
     127              :   })
     128              : 
     129            0 :   const colX = cumulativeOffsets(colWidths, anchor.posX, gap)
     130            0 :   const rowVisualTop = cumulativeOffsets(
     131              :     rowHeights,
     132              :     anchor.posY - anchor.titleHeight,
     133              :     gap
     134              :   )
     135              : 
     136            0 :   return sorted.map((box, i) => {
     137            0 :     const col = i % cols
     138            0 :     const row = Math.floor(i / cols)
     139            0 :     return {
     140              :       nodeId: box.id,
     141              :       position: {
     142              :         x: colX[col],
     143              :         y: rowVisualTop[row] + box.titleHeight
     144              :       }
     145              :     }
     146              :   })
     147              : }
     148              : 
     149            0 : export function computeArrangement(
     150              :   nodes: LGraphNode[],
     151              :   layout: ArrangeLayout,
     152              :   gap: number = DEFAULT_ARRANGE_GAP
     153              : ): ArrangeUpdate[] {
     154            0 :   if (nodes.length < 2) return []
     155            0 :   const boxes = nodes.map(toBox)
     156            0 :   const anchor = findAnchor(boxes)
     157            0 :   if (layout === 'vertical') return arrangeVertical(boxes, anchor, gap)
     158            0 :   if (layout === 'horizontal') return arrangeHorizontal(boxes, anchor, gap)
     159            0 :   return arrangeGrid(boxes, anchor, gap)
     160              : }
     161              : 
     162              : interface ArrangeOptions {
     163              :   gap?: number
     164              :   captureUndo?: boolean
     165              : }
     166              : 
     167           29 : export function useArrangeNodes() {
     168           29 :   const { selectedNodes, hasMultipleSelection } = useSelectionState()
     169           29 :   const mutations = useLayoutMutations(LayoutSource.Canvas)
     170           29 :   const workflowStore = useWorkflowStore()
     171           29 :   const canvasStore = useCanvasStore()
     172              : 
     173           29 :   const arrangeNodes = (
     174              :     layout: ArrangeLayout,
     175              :     { gap = DEFAULT_ARRANGE_GAP, captureUndo = true }: ArrangeOptions = {}
     176              :   ) => {
     177            0 :     if (!hasMultipleSelection.value) return
     178            0 :     const { rootGraphId } = canvasStore
     179            0 :     if (!rootGraphId) return
     180              : 
     181            0 :     const updates = computeArrangement(selectedNodes.value, layout, gap)
     182            0 :     if (updates.length === 0) return
     183              : 
     184            0 :     mutations.batchMoveNodes(rootGraphId, updates)
     185            0 :     app.canvas.setDirty(true, true)
     186            0 :     if (captureUndo) {
     187            0 :       workflowStore.activeWorkflow?.changeTracker.captureCanvasState()
     188              :     }
     189              :   }
     190              : 
     191           29 :   return { arrangeNodes, canArrange: hasMultipleSelection }
     192              : }
        

Generated by: LCOV version 2.0-1