fix(webui): fix arrow key frame rounding and duplicate event bubbling

- Use Math.round instead of Math.floor for keyboard arrow frame navigation in ModelViewer
- Add e.stopPropagation() to RulerSlider keyboard events to prevent duplicate bubbling to window listener
This commit is contained in:
yiling
2026-10-09 12:25:05 +08:00
parent 7c264ee447
commit 8446eaba94
3 changed files with 14 additions and 2 deletions
@@ -104,6 +104,14 @@ describe('ModelViewer', () => {
fireEvent.keyDown(window, { key: 'ArrowRight' }) fireEvent.keyDown(window, { key: 'ArrowRight' })
expect(onTimeUpdate).toHaveBeenCalledWith(expect.closeTo(1 / 6, 2)) expect(onTimeUpdate).toHaveBeenCalledWith(expect.closeTo(1 / 6, 2))
// Press ArrowRight second time -> advance to frame 2 (30 deg = 2/6 sec)
fireEvent.keyDown(window, { key: 'ArrowRight' })
expect(onTimeUpdate).toHaveBeenCalledWith(expect.closeTo(2 / 6, 2))
// Press ArrowLeft -> back 1 frame (frame 1 = 1/6 sec)
fireEvent.keyDown(window, { key: 'ArrowLeft' })
expect(onTimeUpdate).toHaveBeenCalledWith(expect.closeTo(1 / 6, 2))
// Press ArrowLeft -> back 1 frame (frame 0 = 0 sec) // Press ArrowLeft -> back 1 frame (frame 0 = 0 sec)
fireEvent.keyDown(window, { key: 'ArrowLeft' }) fireEvent.keyDown(window, { key: 'ArrowLeft' })
expect(onTimeUpdate).toHaveBeenCalledWith(0) expect(onTimeUpdate).toHaveBeenCalledWith(0)
@@ -167,11 +167,11 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
togglePlay() togglePlay()
} else if (e.key === 'ArrowLeft') { } else if (e.key === 'ArrowLeft') {
e.preventDefault() e.preventDefault()
const currentFrame = Math.floor(currentTime * FPS) const currentFrame = Math.round(currentTime * FPS)
seekToFrame(currentFrame - 1) seekToFrame(currentFrame - 1)
} else if (e.key === 'ArrowRight') { } else if (e.key === 'ArrowRight') {
e.preventDefault() e.preventDefault()
const currentFrame = Math.floor(currentTime * FPS) const currentFrame = Math.round(currentTime * FPS)
seekToFrame(currentFrame + 1) seekToFrame(currentFrame + 1)
} }
} }
@@ -85,15 +85,19 @@ export const RulerSlider: React.FC<RulerSliderProps> = ({
if (disabled) return if (disabled) return
if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') { if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') {
e.preventDefault() e.preventDefault()
e.stopPropagation()
onChange((((normalizedDegree - STEP) % 360) + 360) % 360) onChange((((normalizedDegree - STEP) % 360) + 360) % 360)
} else if (e.key === 'ArrowRight' || e.key === 'ArrowUp') { } else if (e.key === 'ArrowRight' || e.key === 'ArrowUp') {
e.preventDefault() e.preventDefault()
e.stopPropagation()
onChange((normalizedDegree + STEP) % 360) onChange((normalizedDegree + STEP) % 360)
} else if (e.key === 'Home') { } else if (e.key === 'Home') {
e.preventDefault() e.preventDefault()
e.stopPropagation()
onChange(0) onChange(0)
} else if (e.key === 'End') { } else if (e.key === 'End') {
e.preventDefault() e.preventDefault()
e.stopPropagation()
onChange(345) onChange(345)
} }
}, },