mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 05:54:44 +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:
@@ -2,9 +2,11 @@ import { client } from '@/ui/api/client'
|
||||
import DrawingCanvas from '@/ui/components/drawing-canvas'
|
||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||
import * as pdfjsLib from 'pdfjs-dist'
|
||||
import React, { useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
|
||||
import React, { useEffect, useImperativeHandle, useRef, useState } from 'react'
|
||||
import { FileViewerProps, MediaController } from '../types'
|
||||
import { PdfControlBar } from './pdf-control-bar'
|
||||
import { centeredPan, fitScale, zoomAtPoint } from '../pan-zoom'
|
||||
import { usePanZoomGestures } from '../use-pan-zoom'
|
||||
|
||||
import pdfworker from '@/ui/public/pdf.worker.min.mjs' with { type: 'file' }
|
||||
|
||||
@@ -121,28 +123,24 @@ export const PdfViewer = 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 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 fileUrl = file.media?.pdfTranscode?.url
|
||||
@@ -253,41 +251,43 @@ export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
const pdfW = pageDimensions.width
|
||||
const pdfH = pageDimensions.height
|
||||
|
||||
let baseScale = 1
|
||||
if (conW > 0 && conH > 0 && pdfW > 0 && pdfH > 0) {
|
||||
baseScale = Math.min(conW / pdfW, conH / pdfH)
|
||||
}
|
||||
const baseScale = fitScale(conW, conH, pdfW, pdfH)
|
||||
|
||||
// Auto-fit when the container or page changes, unless the user has
|
||||
// manually zoomed (their view is preserved across resizes / page flips).
|
||||
useEffect(() => {
|
||||
if (conW > 0 && conH > 0 && pdfW > 0 && pdfH > 0) {
|
||||
setZoom(baseScale)
|
||||
const x = (conW - pdfW * baseScale) / 2
|
||||
const y = (conH - pdfH * baseScale) / 2
|
||||
setPan({ x, y })
|
||||
}
|
||||
}, [file.id, currentPage, conW, conH, baseScale, pdfW, pdfH])
|
||||
if (conW <= 0 || conH <= 0 || pdfW <= 0 || pdfH <= 0) return
|
||||
if (hasManuallyZoomed) return
|
||||
setZoom(baseScale)
|
||||
setPan(centeredPan(conW, conH, pdfW, pdfH, baseScale))
|
||||
}, [conW, conH, baseScale, pdfW, pdfH, 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 - pdfW * baseScale) / 2
|
||||
const y = (conH - pdfH * baseScale) / 2
|
||||
setPan({ x, y })
|
||||
setPan(centeredPan(conW, conH, pdfW, pdfH, baseScale))
|
||||
}
|
||||
|
||||
usePanZoomGestures({
|
||||
containerRef,
|
||||
zoom,
|
||||
pan,
|
||||
baseScale,
|
||||
onZoomChange: (next) => {
|
||||
setHasManuallyZoomed(true)
|
||||
setZoom(next.zoom)
|
||||
setPan(next.pan)
|
||||
},
|
||||
onPanChange: setPan,
|
||||
})
|
||||
|
||||
const handleDownload = async () => {
|
||||
const key = file.media?.original?.key
|
||||
if (!key || !file.id) return
|
||||
@@ -319,7 +319,7 @@ export const PdfViewer = 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">
|
||||
{loading ? (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-black/40 z-20">
|
||||
<div className="w-8 h-8 border-4 border-white/30 border-t-white rounded-full animate-spin" />
|
||||
@@ -337,7 +337,6 @@ export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
annotations={displayAnnotations}
|
||||
scale={zoom}
|
||||
offset={pan}
|
||||
onPan={setPan}
|
||||
className="absolute inset-0 z-0"
|
||||
isDrawing={isDrawing}
|
||||
currentTool={currentTool}
|
||||
|
||||
Reference in New Issue
Block a user