fix(webui): clear markup annotations when rotating 3d model (#474)

This commit is contained in:
Yiling-J
2026-10-13 15:21:12 +08:00
committed by GitHub
parent f3da916905
commit 3263580e89
7 changed files with 128 additions and 1 deletions
@@ -12,6 +12,7 @@ export function FileViewer({
onPlay,
onPause,
onTimeUpdate,
onSeek,
annotations,
startTime,
shareId,
@@ -30,6 +31,7 @@ export function FileViewer({
onPlay={onPlay}
onPause={onPause}
onTimeUpdate={onTimeUpdate}
onSeek={onSeek}
annotations={annotations}
startTime={startTime}
shareId={shareId}
@@ -156,6 +156,11 @@ export function MobileFileDetail({
setSelectedCommentId(null)
}
const handleSeek = () => {
setAnnotations([])
setSelectedCommentId(null)
}
const handleCommentSelect = (comment: CommentInfo) => {
setSelectedCommentId(comment.id || null)
const newAnnotations = comment.annotations as Annotation[]
@@ -238,6 +243,7 @@ export function MobileFileDetail({
mediaControllerRef={mediaControllerRef}
onPlay={handlePlay}
onTimeUpdate={setCurrentTime}
onSeek={handleSeek}
annotations={annotations}
startTime={startTime}
shareId={shareId}
@@ -385,6 +385,11 @@ export function PublicShareManager({
setSelectedCommentId(null)
}
const handleSeek = () => {
setAnnotations([])
setSelectedCommentId(null)
}
const updateCompareSearch = (patch: Record<string, unknown>) => {
if (!viewingFileId) return
navigate({
@@ -656,6 +661,7 @@ export function PublicShareManager({
mediaControllerRef={mediaControllerRef}
onPlay={handlePlay}
onTimeUpdate={setCurrentTime}
onSeek={handleSeek}
annotations={annotations}
startTime={startTime}
shareId={shareId}
@@ -5,9 +5,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ModelViewer } from './model-viewer'
import type { MediaController } from '../types'
import type { AssetInfo } from '@shumai/dtos'
import type { Annotation } from '@/ui/types'
vi.mock('@/ui/components/drawing-canvas', () => ({
default: () => <div data-testid="drawing-canvas" />,
default: (props: { annotations?: unknown[] }) => (
<div data-testid="drawing-canvas" data-annotation-count={props.annotations?.length ?? 0} />
),
}))
vi.mock('@/ui/paraglide/messages.js', () => ({
@@ -476,4 +479,91 @@ describe('ModelViewer', () => {
const { queryByTitle } = render(<ModelViewer file={assetWithoutOriginal} />)
expect(queryByTitle('Download asset')).toBeNull()
})
describe('onSeek callback for rotation interactions', () => {
it('calls onSeek when dragging the viewer surface to rotate', () => {
const onSeek = vi.fn()
const { getByTestId } = render(<ModelViewer file={mockAsset} onSeek={onSeek} />)
const surface = getByTestId('model-viewer-surface')
// Pointer down at x=100
fireEvent.pointerDown(surface, {
button: 0,
clientX: 100,
pointerId: 1,
})
expect(onSeek).not.toHaveBeenCalled()
// Drag right by +24px (2 frames rotation)
fireEvent.pointerMove(surface, {
clientX: 124,
pointerId: 1,
})
expect(onSeek).toHaveBeenCalledTimes(1)
})
it('calls onSeek when changing degree via slider', () => {
const onSeek = vi.fn()
const { getByTestId } = render(<ModelViewer file={mockAsset} onSeek={onSeek} />)
const minusBtn = getByTestId('ruler-step-minus')
expect(minusBtn).toBeDefined()
fireEvent.click(minusBtn)
expect(onSeek).toHaveBeenCalledTimes(1)
})
it('calls onSeek when pressing keyboard arrow keys to rotate', () => {
const onSeek = vi.fn()
render(<ModelViewer file={mockAsset} onSeek={onSeek} />)
fireEvent.keyDown(window, { key: 'ArrowLeft' })
expect(onSeek).toHaveBeenCalledTimes(1)
fireEvent.keyDown(window, { key: 'ArrowRight' })
expect(onSeek).toHaveBeenCalledTimes(2)
})
it('does not call onSeek when programmatic seekTo is called via ref', () => {
const onSeek = vi.fn()
const ref = createRef<MediaController>()
render(<ModelViewer ref={ref} file={mockAsset} onSeek={onSeek} />)
act(() => {
ref.current?.seekTo(1.5)
})
expect(onSeek).not.toHaveBeenCalled()
})
it('clears displayed annotations when container onSeek empties annotations prop upon rotation', () => {
const initialAnnotation: Annotation = {
type: 'arrow',
color: '#ef4444',
points: [
[0, 0],
[100, 100],
],
}
function Harness() {
const [annotations, setAnnotations] = React.useState<Annotation[]>([initialAnnotation])
return (
<ModelViewer
file={mockAsset}
annotations={annotations}
onSeek={() => setAnnotations([])}
/>
)
}
const { getByTestId } = render(<Harness />)
const canvas = getByTestId('drawing-canvas')
expect(canvas.getAttribute('data-annotation-count')).toBe('1')
// Rotate via ruler step minus
fireEvent.click(getByTestId('ruler-step-minus'))
// Markup annotation disappears
expect(canvas.getAttribute('data-annotation-count')).toBe('0')
})
})
})
@@ -23,6 +23,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
onPlay,
onPause,
onTimeUpdate,
onSeek,
annotations,
startTime,
shareId,
@@ -55,6 +56,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
// Drag-to-rotate state
const [isDraggingRotation, setIsDraggingRotation] = useState(false)
const dragStartRef = useRef<{ clientX: number; frame: number }>({ clientX: 0, frame: 0 })
const hasTriggeredSeekInDragRef = useRef(false)
const videoSrc = data.media?.videoTranscodes?.[0]?.url || data.media?.videoPreview?.url || ''
@@ -152,6 +154,11 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
onTimeUpdateRef.current = onTimeUpdate
}, [onTimeUpdate])
const onSeekRef = useRef(onSeek)
useEffect(() => {
onSeekRef.current = onSeek
}, [onSeek])
// Synchronized playback frame loop (via requestVideoFrameCallback / requestAnimationFrame)
useEffect(() => {
if (!isPlaying) return
@@ -225,6 +232,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
const seekToDegree = useCallback(
(degree: number) => {
onSeekRef.current?.()
videoRef.current?.pause()
const frame = Math.round((((degree % 360) + 360) % 360) / 15) % TOTAL_FRAMES
seekToFrame(frame)
@@ -280,10 +288,12 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
togglePlay()
} else if (e.key === 'ArrowLeft') {
e.preventDefault()
onSeekRef.current?.()
const currentFrame = secondToFrame(currentTime)
seekToFrame(currentFrame - 1)
} else if (e.key === 'ArrowRight') {
e.preventDefault()
onSeekRef.current?.()
const currentFrame = secondToFrame(currentTime)
seekToFrame(currentFrame + 1)
}
@@ -305,6 +315,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
clientX: e.clientX,
frame: currentFrame,
}
hasTriggeredSeekInDragRef.current = false
setIsDraggingRotation(true)
}
@@ -314,12 +325,17 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
// 12px drag movement corresponds to 1 frame (15°)
// Dragging right (deltaX > 0) rotates turntable counterclockwise (decreasing frame index)
const deltaFrames = Math.round(deltaX / 12)
if (deltaFrames !== 0 && !hasTriggeredSeekInDragRef.current) {
hasTriggeredSeekInDragRef.current = true
onSeekRef.current?.()
}
const targetFrame = dragStartRef.current.frame - deltaFrames
seekToFrame(targetFrame)
}
const handleViewerPointerUp = (e: React.PointerEvent<HTMLDivElement>) => {
if (!isDraggingRotation) return
hasTriggeredSeekInDragRef.current = false
setIsDraggingRotation(false)
try {
e.currentTarget.releasePointerCapture(e.pointerId)
@@ -17,6 +17,7 @@ export interface FileViewerProps {
onPlay?: () => void
onPause?: () => void
onTimeUpdate?: (time: number) => void
onSeek?: () => void
startTime?: number
shareId?: string
/** When false, the viewer hides its download affordances. Defaults to true. */
@@ -609,6 +609,11 @@ function FileViewPage() {
setSelectedCommentId(null)
}
const handleSeek = () => {
setAnnotations([])
setSelectedCommentId(null)
}
if (isMobile && !isCompareMode) {
return (
<MobileFileDetail
@@ -702,6 +707,7 @@ function FileViewPage() {
mediaControllerRef={mediaControllerRef}
onPlay={handlePlay}
onTimeUpdate={setCurrentTime}
onSeek={handleSeek}
annotations={annotations}
startTime={startTime}
>