LCOV - code coverage report
Current view: top level - packages/viewport3d/src/load3d - load3dContextMenuGuard.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 39.1 % 23 9
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { Point2D as PointerPosition } from '@comfyorg/shared-frontend-utils/geometry'
       2              : 
       3            0 : function exceedsClickThreshold(
       4              :   start: PointerPosition,
       5              :   end: PointerPosition,
       6              :   threshold: number
       7              : ): boolean {
       8            0 :   return (end.x - start.x) ** 2 + (end.y - start.y) ** 2 > threshold ** 2
       9              : }
      10              : 
      11              : type ContextMenuGuardOptions = {
      12              :   isDisabled?: () => boolean
      13              :   dragThreshold?: number
      14              : }
      15              : 
      16           27 : export function attachContextMenuGuard(
      17              :   target: HTMLElement,
      18              :   onMenu: (event: MouseEvent) => void,
      19              :   { isDisabled = () => false, dragThreshold = 5 }: ContextMenuGuardOptions = {}
      20              : ): () => void {
      21           27 :   const abort = new AbortController()
      22           27 :   const { signal } = abort
      23              : 
      24           27 :   let start = { x: 0, y: 0 }
      25           27 :   let moved = false
      26              : 
      27           27 :   target.addEventListener(
      28              :     'mousedown',
      29              :     (e) => {
      30            0 :       if (e.button === 2) {
      31            0 :         start = { x: e.clientX, y: e.clientY }
      32            0 :         moved = false
      33              :       }
      34              :     },
      35              :     { signal }
      36              :   )
      37              : 
      38           27 :   target.addEventListener(
      39              :     'mousemove',
      40              :     (e) => {
      41            0 :       if (
      42              :         e.buttons === 2 &&
      43              :         exceedsClickThreshold(
      44              :           start,
      45              :           { x: e.clientX, y: e.clientY },
      46              :           dragThreshold
      47              :         )
      48              :       ) {
      49            0 :         moved = true
      50              :       }
      51              :     },
      52              :     { signal }
      53              :   )
      54              : 
      55           27 :   target.addEventListener(
      56              :     'contextmenu',
      57              :     (e) => {
      58            0 :       if (isDisabled()) return
      59              : 
      60            0 :       const wasDragging =
      61              :         moved ||
      62              :         exceedsClickThreshold(
      63              :           start,
      64              :           { x: e.clientX, y: e.clientY },
      65              :           dragThreshold
      66              :         )
      67              : 
      68            0 :       moved = false
      69              : 
      70            0 :       if (wasDragging) return
      71              : 
      72            0 :       e.preventDefault()
      73            0 :       e.stopPropagation()
      74            0 :       onMenu(e)
      75              :     },
      76              :     { signal }
      77              :   )
      78              : 
      79           27 :   return () => abort.abort()
      80              : }
        

Generated by: LCOV version 2.0-1