LCOV - code coverage report
Current view: top level - src/composables/element - useDomClipping.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 100.0 % 33 33
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type {
       2              :   Bounds as Rect,
       3              :   Bounds,
       4              :   PanZoom,
       5              :   RectTuple
       6              : } from '@comfyorg/shared-frontend-utils/geometry'
       7              : import type { CSSProperties, Ref } from 'vue'
       8              : import { ref } from 'vue'
       9              : 
      10          560 : function intersect(a: Rect, b: Rect): RectTuple | null {
      11          560 :   const x1 = Math.max(a.x, b.x)
      12          560 :   const y1 = Math.max(a.y, b.y)
      13          560 :   const x2 = Math.min(a.x + a.width, b.x + b.width)
      14          560 :   const y2 = Math.min(a.y + a.height, b.y + b.height)
      15              : 
      16          560 :   if (x1 >= x2 || y1 >= y2) {
      17          492 :     return null
      18              :   }
      19              : 
      20           68 :   return [x1, y1, x2 - x1, y2 - y1]
      21              : }
      22              : 
      23              : interface ClippingOptions {
      24              :   margin?: number
      25              : }
      26              : 
      27              : interface UseDomClippingReturn {
      28              :   style: Ref<CSSProperties>
      29              :   updateClipPath: (
      30              :     element: HTMLElement,
      31              :     canvasElement: HTMLCanvasElement,
      32              :     isSelected: boolean,
      33              :     selectedArea?: Bounds & PanZoom
      34              :   ) => void
      35              : }
      36              : 
      37         4558 : export function useDomClipping(
      38              :   options: ClippingOptions = {}
      39              : ): UseDomClippingReturn {
      40         4558 :   const style = ref<CSSProperties>({})
      41         4558 :   const { margin = 4 } = options
      42              : 
      43         4558 :   const calculateClipPath = (
      44              :     elementRect: DOMRect,
      45              :     canvasRect: DOMRect,
      46              :     isSelected: boolean,
      47              :     selectedArea?: Bounds & PanZoom
      48              :   ): string => {
      49         1407 :     if (!isSelected && selectedArea) {
      50          560 :       const { scale, offset } = selectedArea
      51              : 
      52          560 :       const intersection = intersect(
      53              :         {
      54              :           x: elementRect.left - canvasRect.left,
      55              :           y: elementRect.top - canvasRect.top,
      56              :           width: elementRect.width,
      57              :           height: elementRect.height
      58              :         },
      59              :         {
      60              :           x: (selectedArea.x + offset[0] - margin) * scale,
      61              :           y: (selectedArea.y + offset[1] - margin) * scale,
      62              :           width: (selectedArea.width + 2 * margin) * scale,
      63              :           height: (selectedArea.height + 2 * margin) * scale
      64              :         }
      65              :       )
      66              : 
      67          560 :       if (!intersection) {
      68          492 :         return ''
      69              :       }
      70              : 
      71           68 :       const clipX =
      72              :         (intersection[0] - elementRect.left + canvasRect.left) / scale + 'px'
      73           68 :       const clipY =
      74              :         (intersection[1] - elementRect.top + canvasRect.top) / scale + 'px'
      75           68 :       const clipWidth = intersection[2] / scale + 'px'
      76           68 :       const clipHeight = intersection[3] / scale + 'px'
      77              : 
      78           68 :       return `polygon(0% 0%, 0% 100%, ${clipX} 100%, ${clipX} ${clipY}, calc(${clipX} + ${clipWidth}) ${clipY}, calc(${clipX} + ${clipWidth}) calc(${clipY} + ${clipHeight}), ${clipX} calc(${clipY} + ${clipHeight}), ${clipX} 100%, 100% 100%, 100% 0%)`
      79              :     }
      80              : 
      81          847 :     return ''
      82              :   }
      83              : 
      84         4558 :   let pendingRaf = 0
      85              : 
      86         4558 :   const updateClipPath = (
      87              :     element: HTMLElement,
      88              :     canvasElement: HTMLCanvasElement,
      89              :     isSelected: boolean,
      90              :     selectedArea?: Bounds & PanZoom
      91              :   ) => {
      92         1804 :     if (pendingRaf) cancelAnimationFrame(pendingRaf)
      93         1804 :     pendingRaf = requestAnimationFrame(() => {
      94         1407 :       pendingRaf = 0
      95         1407 :       const elementRect = element.getBoundingClientRect()
      96         1407 :       const canvasRect = canvasElement.getBoundingClientRect()
      97              : 
      98         1407 :       const clipPath = calculateClipPath(
      99              :         elementRect,
     100              :         canvasRect,
     101              :         isSelected,
     102              :         selectedArea
     103              :       )
     104              : 
     105         1407 :       style.value = {
     106              :         clipPath: clipPath || 'none',
     107              :         willChange: 'clip-path'
     108              :       }
     109              :     })
     110              :   }
     111              : 
     112         4558 :   return {
     113              :     style,
     114              :     updateClipPath
     115              :   }
     116              : }
        

Generated by: LCOV version 2.0-1