mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 13:34:33 +00:00
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:
@@ -1,11 +1,13 @@
|
||||
import { useScreenSize } from '@/ui/hooks/useScreenSize'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { getBestTranscode } from '@/ui/lib/media'
|
||||
import React, { useCallback, useEffect, useRef, useState, useImperativeHandle } from 'react'
|
||||
import React, { useEffect, useRef, useState, useImperativeHandle } from 'react'
|
||||
import DrawingCanvas from '@/ui/components/drawing-canvas'
|
||||
import { ImageControlBar } from './image-control-bar'
|
||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||
import { FileViewerProps, MediaController } from '../types'
|
||||
import { centeredPan, fitScale, isZoomed, zoomAtPoint } from '../pan-zoom'
|
||||
import { usePanZoomGestures } from '../use-pan-zoom'
|
||||
|
||||
export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
({ file, annotations, shareId, children, allowDownload }, ref) => {
|
||||
@@ -28,29 +30,25 @@ export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
|
||||
const [zoom, setZoom] = useState(1)
|
||||
const [pan, setPan] = useState({ x: 0, y: 0 })
|
||||
const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false)
|
||||
const [copied, setCopied] = useState(false)
|
||||
|
||||
const observerRef = useRef<ResizeObserver | null>(null)
|
||||
const containerRef = useRef<HTMLDivElement | null>(null)
|
||||
|
||||
const containerRef = useCallback((node: HTMLDivElement | null) => {
|
||||
if (observerRef.current) {
|
||||
observerRef.current.disconnect()
|
||||
observerRef.current = null
|
||||
}
|
||||
|
||||
if (node !== null) {
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
const entry = entries[0]
|
||||
if (entry) {
|
||||
setContainerSize({
|
||||
width: entry.contentRect.width,
|
||||
height: entry.contentRect.height,
|
||||
})
|
||||
}
|
||||
})
|
||||
observer.observe(node)
|
||||
observerRef.current = observer
|
||||
}
|
||||
useEffect(() => {
|
||||
const node = containerRef.current
|
||||
if (!node) return
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
const entry = entries[0]
|
||||
if (entry) {
|
||||
setContainerSize({
|
||||
width: entry.contentRect.width,
|
||||
height: entry.contentRect.height,
|
||||
})
|
||||
}
|
||||
})
|
||||
observer.observe(node)
|
||||
return () => observer.disconnect()
|
||||
}, [])
|
||||
|
||||
const imgW = file.media?.metadata?.originalWidth ?? 1920
|
||||
@@ -58,44 +56,43 @@ export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
const conW = containerSize.width
|
||||
const conH = containerSize.height
|
||||
|
||||
let baseScale = 1
|
||||
const baseScale = fitScale(conW, conH, imgW, imgH)
|
||||
|
||||
if (conW > 0 && conH > 0) {
|
||||
baseScale = Math.min(conW / imgW, conH / imgH)
|
||||
}
|
||||
|
||||
// Reset/Fit logic
|
||||
// Auto-fit when the container size changes, unless the user has manually
|
||||
// zoomed (their view is preserved across resizes, e.g. sidebar toggles).
|
||||
useEffect(() => {
|
||||
if (conW > 0 && conH > 0) {
|
||||
setZoom(baseScale)
|
||||
// Center
|
||||
const x = (conW - imgW * baseScale) / 2
|
||||
const y = (conH - imgH * baseScale) / 2
|
||||
setPan({ x, y })
|
||||
}
|
||||
}, [file.id, conW, conH, baseScale, imgW, imgH])
|
||||
if (conW <= 0 || conH <= 0) return
|
||||
if (hasManuallyZoomed) return
|
||||
setZoom(baseScale)
|
||||
setPan(centeredPan(conW, conH, imgW, imgH, baseScale))
|
||||
}, [conW, conH, baseScale, imgW, imgH, hasManuallyZoomed])
|
||||
|
||||
const handleZoom = (factor: number) => {
|
||||
const cx = conW / 2
|
||||
const cy = conH / 2
|
||||
|
||||
const newZoom = zoom * factor
|
||||
if (newZoom < 0.01 || newZoom > 50) return
|
||||
|
||||
const newPanX = cx - (cx - pan.x) * (newZoom / zoom)
|
||||
const newPanY = cy - (cy - pan.y) * (newZoom / zoom)
|
||||
|
||||
setZoom(newZoom)
|
||||
setPan({ x: newPanX, y: newPanY })
|
||||
setHasManuallyZoomed(true)
|
||||
const next = zoomAtPoint(zoom, pan, factor, conW / 2, conH / 2)
|
||||
setZoom(next.zoom)
|
||||
setPan(next.pan)
|
||||
}
|
||||
|
||||
const handleFit = () => {
|
||||
setHasManuallyZoomed(false)
|
||||
setZoom(baseScale)
|
||||
const x = (conW - imgW * baseScale) / 2
|
||||
const y = (conH - imgH * baseScale) / 2
|
||||
setPan({ x, y })
|
||||
setPan(centeredPan(conW, conH, imgW, imgH, baseScale))
|
||||
}
|
||||
|
||||
usePanZoomGestures({
|
||||
containerRef,
|
||||
zoom,
|
||||
pan,
|
||||
baseScale,
|
||||
onZoomChange: (next) => {
|
||||
setHasManuallyZoomed(true)
|
||||
setZoom(next.zoom)
|
||||
setPan(next.pan)
|
||||
},
|
||||
onPanChange: setPan,
|
||||
})
|
||||
|
||||
const bestUrl = getBestTranscode(file.media?.imageTranscodes, screenWidth)?.url ?? ''
|
||||
|
||||
const handleDownload = async () => {
|
||||
@@ -178,7 +175,7 @@ export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
|
||||
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
||||
{children}
|
||||
<div ref={containerRef} className="flex-1 relative overflow-hidden">
|
||||
<div ref={containerRef} className="flex-1 relative overflow-hidden touch-none">
|
||||
{bestUrl ? (
|
||||
<DrawingCanvas
|
||||
width={conW}
|
||||
@@ -191,7 +188,7 @@ export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
annotations={displayAnnotations}
|
||||
scale={zoom}
|
||||
offset={pan}
|
||||
onPan={setPan}
|
||||
onPan={isZoomed(zoom, baseScale) ? setPan : undefined}
|
||||
className="absolute inset-0 z-0"
|
||||
isDrawing={isDrawing}
|
||||
currentTool={currentTool}
|
||||
|
||||
Reference in New Issue
Block a user