Files
shumai/packages/webui/components/viewers/pan-zoom.test.ts
T
Yiling-J 5ad3fdbf69 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.
2026-08-12 10:54:42 +08:00

125 lines
3.8 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
MAX_ZOOM,
MIN_ZOOM,
centeredPan,
clampZoom,
fitScale,
isZoomed,
wheelZoomFactor,
zoomAtPoint,
} from './pan-zoom'
describe('fitScale', () => {
it('fits a wide media into a narrower container by width', () => {
expect(fitScale(100, 100, 200, 100)).toBe(0.5)
})
it('fits a tall media into a shorter container by height', () => {
expect(fitScale(100, 100, 100, 200)).toBe(0.5)
})
it('keeps aspect ratio (min of both axes)', () => {
expect(fitScale(100, 50, 200, 200)).toBe(0.25)
})
it('returns 1 for empty container or media dimensions', () => {
expect(fitScale(0, 100, 100, 100)).toBe(1)
expect(fitScale(100, 0, 100, 100)).toBe(1)
expect(fitScale(100, 100, 0, 100)).toBe(1)
expect(fitScale(100, 100, 100, 0)).toBe(1)
})
})
describe('centeredPan', () => {
it('centers a smaller-than-container media', () => {
expect(centeredPan(1000, 1000, 500, 500, 1)).toEqual({ x: 250, y: 250 })
})
it('centers a scaled media', () => {
expect(centeredPan(1000, 800, 200, 100, 2)).toEqual({ x: 300, y: 300 })
})
})
describe('isZoomed', () => {
it('is false at or below fit scale', () => {
expect(isZoomed(1, 1)).toBe(false)
expect(isZoomed(0.5, 1)).toBe(false)
})
it('is true above the fit scale plus epsilon', () => {
expect(isZoomed(1.2, 1)).toBe(true)
expect(isZoomed(1.01, 1)).toBe(false)
})
})
describe('clampZoom', () => {
it('clamps to the configured bounds', () => {
expect(clampZoom(0.001)).toBe(MIN_ZOOM)
expect(clampZoom(1000)).toBe(MAX_ZOOM)
expect(clampZoom(2)).toBe(2)
})
})
describe('zoomAtPoint', () => {
it('keeps the anchor point fixed while zooming', () => {
const result = zoomAtPoint(1, { x: 0, y: 0 }, 2, 100, 50)
expect(result.zoom).toBe(2)
// Anchor (100, 50) must map to itself: pan' = px - (px - pan) * ratio
expect(result.pan).toEqual({ x: -100, y: -50 })
})
it('zooms about the container center like the old center-zoom behavior', () => {
// With pan (0,0) and anchor at center (50, 50), doubling zoom pushes pan
// to (-50, -50).
const result = zoomAtPoint(1, { x: 0, y: 0 }, 2, 50, 50)
expect(result.pan).toEqual({ x: -50, y: -50 })
})
it('zooming out keeps the anchor fixed', () => {
const result = zoomAtPoint(2, { x: -100, y: -50 }, 0.5, 100, 50)
expect(result.zoom).toBe(1)
expect(result.pan).toEqual({ x: 0, y: 0 })
})
it('clamps zoom while still keeping the anchor fixed', () => {
const result = zoomAtPoint(40, { x: 10, y: 20 }, 2, 5, 5)
expect(result.zoom).toBe(MAX_ZOOM)
// ratio is 50/40 = 1.25 once clamped; the anchor (5, 5) stays fixed
expect(result.pan).toEqual({ x: 11.25, y: 23.75 })
})
it('keeps pan untouched when already at the bound (ratio 1)', () => {
const result = zoomAtPoint(MAX_ZOOM, { x: 10, y: 20 }, 2, 5, 5)
expect(result.zoom).toBe(MAX_ZOOM)
expect(result.pan).toEqual({ x: 10, y: 20 })
})
it('clamps at the minimum zoom', () => {
const result = zoomAtPoint(0.05, { x: 10, y: 20 }, 0.1, 5, 5)
expect(result.zoom).toBe(MIN_ZOOM)
})
})
describe('wheelZoomFactor', () => {
it('zooms in for negative delta (scroll up / pinch out)', () => {
expect(wheelZoomFactor(-100)).toBeGreaterThan(1)
})
it('zooms out for positive delta (scroll down / pinch in)', () => {
expect(wheelZoomFactor(100)).toBeLessThan(1)
})
it('normalizes line and page delta modes to pixel-like scales', () => {
// deltaMode 1 = lines: multiply by 16
expect(wheelZoomFactor(-6.25, 1)).toBe(wheelZoomFactor(-100, 0))
// deltaMode 2 = pages: multiply by 100
expect(wheelZoomFactor(-1, 2)).toBe(wheelZoomFactor(-100, 0))
})
it('clamps extreme deltas to avoid jumps', () => {
expect(wheelZoomFactor(-1_000_000)).toBe(2)
expect(wheelZoomFactor(1_000_000)).toBe(0.5)
})
})