fix(webui): content-only pinch-zoom and two-finger pan in media viewers (#298)

* fix(webui): add content-only pinch-zoom and two-finger pan to media viewers

Intercept trackpad pinch (ctrl+wheel), touchscreen pinch (two touches),
and two-finger move inside the image, video, and PDF viewers so they
transform only the media content instead of triggering browser page
zoom (matching Frame.io behavior).

- New shared pan-zoom math (zoomAtPoint, fitScale, isZoomed) and a
  usePanZoomGestures hook using native non-passive wheel/touch listeners
  with touch-action: none on the media container.
- Plain wheel / two-finger scroll pans the content, but only once the
  user has zoomed in; drag-pan is gated the same way.
- Image and PDF viewers no longer reset zoom to fit on container resizes
  while the user has manually zoomed (matches the video viewer).
- DrawingCanvas ignores multi-touch moves so single-pointer pan/draw
  doesn't fight the pinch handler; the video viewer keeps play/pause on
  the container div to avoid double-toggling when the overlay is active.

Includes unit tests for the pan-zoom math.

* fix(webui): disable click-drag pan for pdf and video viewers

When zoomed, PDF and video previously panned on left-button (or
single-finger touch) drag via the shared DrawingCanvas. That conflicts
with left-click semantics: PDF wants the drag for text selection and
video reserves the click for play/pause.

Remove the gated onPan from both viewers so the Konva overlay stays
pointer-events:none when not drawing - clicks pass straight through
(video click-to-play now works cleanly at any zoom) - while panning
remains available via mouse wheel and two-finger gestures. Image keeps
drag-pan.
This commit is contained in:
Yiling-J
2026-08-12 10:54:42 +08:00
committed by GitHub
parent 851ae49c84
commit 5ad3fdbf69
7 changed files with 494 additions and 120 deletions
@@ -10,6 +10,8 @@ import { VideoControlBar, type PlayerState, type DisplayTranscode } from './vide
import DrawingCanvas from '@/ui/components/drawing-canvas'
import { useAnnotationStore } from '@/ui/stores/annotation-store'
import { FileViewerProps, MediaController } from '../types'
import { centeredPan, fitScale, zoomAtPoint } from '../pan-zoom'
import { usePanZoomGestures } from '../use-pan-zoom'
const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
(
@@ -121,16 +123,34 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
const [videoHtmlEl, setVideoHtmlEl] = useState<HTMLVideoElement | undefined>(undefined)
const videoRef = useRef<HTMLVideoElement | null>(null)
const [zoom, setZoom] = useState(1)
const [pan, setPan] = useState({ x: 0, y: 0 })
const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false)
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
const vidW = data.media?.metadata?.originalWidth || 1920
const vidH = data.media?.metadata?.originalHeight || 1080
const baseScale = fitScale(containerSize.width, containerSize.height, vidW, vidH)
const handleZoomChange = (newZoom: number) => {
setZoom(newZoom)
setHasManuallyZoomed(true)
const next = zoomAtPoint(
zoom,
pan,
newZoom / zoom,
containerSize.width / 2,
containerSize.height / 2,
)
setZoom(next.zoom)
setPan(next.pan)
}
const handleZoomReset = () => {
setHasManuallyZoomed(false)
if (containerSize.width > 0 && containerSize.height > 0) {
const scale = fitScale(containerSize.width, containerSize.height, vidW, vidH)
setZoom(scale)
setPan(centeredPan(containerSize.width, containerSize.height, vidW, vidH, scale))
}
}
// Store
@@ -161,17 +181,27 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
return () => observer.disconnect()
}, [])
// Fit to screen initial / responsive resize
// Fit to screen initial / responsive resize (preserves manual zoom)
useEffect(() => {
if (containerSize.width > 0 && containerSize.height > 0) {
const vidW = data.media?.metadata?.originalWidth || 1920
const vidH = data.media?.metadata?.originalHeight || 1080
const scale = Math.min(containerSize.width / vidW, containerSize.height / vidH)
if (!hasManuallyZoomed) {
setZoom(scale)
}
}
}, [containerSize.width, containerSize.height, data.media?.metadata, hasManuallyZoomed])
if (containerSize.width <= 0 || containerSize.height <= 0) return
if (hasManuallyZoomed) return
const scale = fitScale(containerSize.width, containerSize.height, vidW, vidH)
setZoom(scale)
setPan(centeredPan(containerSize.width, containerSize.height, vidW, vidH, scale))
}, [containerSize.width, containerSize.height, vidW, vidH, hasManuallyZoomed])
usePanZoomGestures({
containerRef,
zoom,
pan,
baseScale,
onZoomChange: (next) => {
setHasManuallyZoomed(true)
setZoom(next.zoom)
setPan(next.pan)
},
onPanChange: setPan,
})
// Cleanup ref when player is disposed
useEffect(() => {
@@ -580,13 +610,8 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
return () => document.removeEventListener('fullscreenchange', onFsChange)
}, [])
// Calculate center pan
const vidW = data.media?.metadata?.originalWidth || 1920
const vidH = data.media?.metadata?.originalHeight || 1080
// Center pan is owned by `pan` state (set by auto-fit / gestures).
const scale = zoom
const panX = (containerSize.width - vidW * scale) / 2
const panY = (containerSize.height - vidH * scale) / 2
if (!hasMedia) {
return (
@@ -615,7 +640,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
<div
ref={containerRef}
className={cn(
'flex-1 bg-black cursor-pointer relative flex items-center justify-center overflow-hidden min-h-0',
'flex-1 bg-black cursor-pointer relative flex items-center justify-center overflow-hidden min-h-0 touch-none',
)}
onClick={togglePlay}
data-vjs-player
@@ -647,9 +672,11 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
videoElement={videoHtmlEl}
annotations={displayAnnotations}
scale={scale}
offset={{ x: panX, y: panY }}
offset={pan}
className="absolute inset-0"
onClick={togglePlay}
// Play/pause is handled by the video-area div's onClick; a
// Konva-level onClick would double-toggle once the overlay
// becomes interactive (zoomed).
// Drawing Props
isDrawing={isDrawing}
currentTool={currentTool}