LCOV - code coverage report
Current view: top level - src/scripts - clipspace.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 1.4 % 74 1
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
       2              : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
       3              : import type { ResultItem } from '@comfyorg/api-client/executionTypes'
       4              : import { useApp } from '@/scripts/appInstance'
       5              : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
       6              : import { fullResolutionSrc } from '@/utils/hdrFormatUtil'
       7              : 
       8              : export type Clipspace = {
       9              :   widgets?: Pick<IBaseWidget, 'type' | 'name' | 'value'>[] | null
      10              :   imgs?: HTMLImageElement[] | null
      11              :   original_imgs?: HTMLImageElement[] | null
      12              :   images?: ResultItem[] | null
      13              :   selectedIndex: number
      14              :   img_paste_mode: string
      15              :   paintedIndex: number
      16              :   combinedIndex?: number
      17              : }
      18              : 
      19              : /**
      20              :  * Content clipboard shared by the mask editor and clipspace extensions.
      21              :  * `ComfyApp` exposes it as static members for custom-node compatibility.
      22              :  */
      23         2356 : export const clipspace: {
      24              :   current: Clipspace | null
      25              :   invalidateHandler: (() => void) | null
      26              :   returnNode: LGraphNode | null
      27              :   maskEditorIsOpened: (() => boolean) | null
      28              : } = {
      29              :   current: null,
      30              :   invalidateHandler: null,
      31              :   returnNode: null,
      32              :   maskEditorIsOpened: null
      33              : }
      34              : 
      35            0 : export function copyToClipspace(node: LGraphNode) {
      36            0 :   let widgets = null
      37            0 :   if (node.widgets) {
      38            0 :     widgets = node.widgets.map(({ type, name, value }) => ({
      39              :       type,
      40              :       name,
      41              :       value
      42              :     }))
      43              :   }
      44              : 
      45            0 :   let imgs = undefined
      46            0 :   let orig_imgs = undefined
      47            0 :   if (node.imgs != undefined) {
      48            0 :     imgs = []
      49            0 :     orig_imgs = []
      50              : 
      51            0 :     for (let i = 0; i < node.imgs.length; i++) {
      52            0 :       imgs[i] = new Image()
      53            0 :       imgs[i].src = fullResolutionSrc(node.imgs[i])
      54            0 :       orig_imgs[i] = imgs[i]
      55              :     }
      56              :   }
      57              : 
      58            0 :   let selectedIndex = 0
      59            0 :   if (node.imageIndex) {
      60            0 :     selectedIndex = node.imageIndex
      61              :   }
      62              : 
      63            0 :   const paintedIndex = imgs ? imgs.length + 1 : 1
      64            0 :   const combinedIndex = imgs ? imgs.length + 2 : 2
      65              : 
      66              :   // for vueNodes mode
      67            0 :   const images = useNodeOutputStore().getNodeOutputs(node)?.images
      68              : 
      69            0 :   clipspace.current = {
      70              :     widgets: widgets,
      71              :     imgs: imgs,
      72              :     original_imgs: orig_imgs,
      73              :     images: images,
      74              :     selectedIndex: selectedIndex,
      75              :     img_paste_mode: 'selected', // reset to default im_paste_mode state on copy action
      76              :     paintedIndex: paintedIndex,
      77              :     combinedIndex: combinedIndex
      78              :   }
      79              : 
      80            0 :   clipspace.returnNode = null
      81              : 
      82            0 :   if (clipspace.invalidateHandler) {
      83            0 :     clipspace.invalidateHandler()
      84              :   }
      85              : }
      86              : 
      87            0 : export function pasteFromClipspace(node: LGraphNode) {
      88            0 :   if (clipspace.current) {
      89              :     // image paste
      90            0 :     let combinedImgSrc: string | undefined
      91            0 :     if (
      92              :       clipspace.current.combinedIndex !== undefined &&
      93              :       clipspace.current.imgs &&
      94              :       clipspace.current.combinedIndex < clipspace.current.imgs.length
      95              :     ) {
      96            0 :       combinedImgSrc =
      97              :         clipspace.current.imgs[clipspace.current.combinedIndex].src
      98              :     }
      99            0 :     const selectedImage =
     100              :       clipspace.current.images?.[clipspace.current.selectedIndex]
     101            0 :     if (clipspace.current.images) {
     102            0 :       if (clipspace.current.img_paste_mode !== 'selected') {
     103            0 :         useNodeOutputStore().setNodeOutputImages(node, clipspace.current.images)
     104            0 :       } else if (selectedImage) {
     105            0 :         useNodeOutputStore().setNodeOutputImages(node, [selectedImage])
     106              :       }
     107              :     }
     108            0 :     if (clipspace.current.imgs && node.imgs) {
     109              :       // deep-copy to cut link with clipspace
     110            0 :       if (clipspace.current['img_paste_mode'] == 'selected') {
     111            0 :         const selectedPreview = clipspace.current.imgs.at(
     112              :           clipspace.current.selectedIndex
     113              :         )
     114            0 :         if (clipspace.current.selectedIndex >= 0 && selectedPreview) {
     115            0 :           const img = new Image()
     116            0 :           img.src = selectedPreview.src
     117            0 :           node.imgs = [img]
     118            0 :           node.imageIndex = 0
     119              :         }
     120              :       } else {
     121            0 :         const imgs = []
     122            0 :         for (let i = 0; i < clipspace.current.imgs.length; i++) {
     123            0 :           imgs[i] = new Image()
     124            0 :           imgs[i].src = clipspace.current.imgs[i].src
     125            0 :           node.imgs = imgs
     126              :         }
     127              :       }
     128              :     }
     129              : 
     130              :     // Paste the RGB canvas if paintedindex exists
     131            0 :     if (clipspace.current.imgs?.[clipspace.current.paintedIndex] && node.imgs) {
     132            0 :       const paintedImg = new Image()
     133            0 :       paintedImg.src =
     134              :         clipspace.current.imgs[clipspace.current.paintedIndex].src
     135            0 :       node.imgs.push(paintedImg) // Add the RGB canvas to the node's images
     136              :     }
     137              : 
     138              :     // Store only combined image inside the node if it exists
     139            0 :     if (node.imgs && combinedImgSrc) {
     140            0 :       const combinedImg = new Image()
     141            0 :       combinedImg.src = combinedImgSrc
     142            0 :       node.imgs = [combinedImg]
     143              :     }
     144              : 
     145            0 :     if (node.widgets) {
     146            0 :       if (selectedImage) {
     147            0 :         const index = node.widgets.findIndex((obj) => obj.name === 'image')
     148            0 :         if (index >= 0) {
     149            0 :           if (
     150              :             node.widgets[index].type != 'image' &&
     151              :             typeof node.widgets[index].value == 'string' &&
     152              :             selectedImage.filename
     153              :           ) {
     154            0 :             node.widgets[index].value =
     155              :               (selectedImage.subfolder ? selectedImage.subfolder + '/' : '') +
     156              :               selectedImage.filename +
     157              :               (selectedImage.type ? ` [${selectedImage.type}]` : '')
     158              :           } else {
     159            0 :             node.widgets[index].value = selectedImage
     160              :           }
     161              :         }
     162              :       }
     163            0 :       if (clipspace.current.widgets) {
     164            0 :         clipspace.current.widgets.forEach(({ type, name, value }) => {
     165            0 :           const prop = node.widgets?.find(
     166              :             (obj) => obj.type === type && obj.name === name
     167              :           )
     168            0 :           if (prop && prop.type != 'button') {
     169            0 :             const valueObj = value as Record<string, unknown> | undefined
     170            0 :             if (
     171              :               prop.type != 'image' &&
     172              :               typeof prop.value == 'string' &&
     173              :               valueObj?.filename
     174              :             ) {
     175            0 :               const resultItem = value as ResultItem
     176            0 :               prop.value =
     177              :                 (resultItem.subfolder ? resultItem.subfolder + '/' : '') +
     178              :                 resultItem.filename +
     179              :                 (resultItem.type ? ` [${resultItem.type}]` : '')
     180              :             } else {
     181            0 :               prop.value = value
     182            0 :               prop.callback?.(value)
     183              :             }
     184              :           }
     185              :         })
     186              :       }
     187              :     }
     188              : 
     189            0 :     useApp().canvas.setDirty(true)
     190              :   }
     191              : }
        

Generated by: LCOV version 2.0-1