11// @vitest -environment happy-dom
22
3- import { act , useRef } from "react" ;
3+ import { act , useLayoutEffect , useRef } from "react" ;
44import { createRoot , type Root } from "react-dom/client" ;
55import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
66import { liveTime , usePlayerStore , type ZoomMode } from "../store/playerStore" ;
77import { useTimelinePlayhead } from "./useTimelinePlayhead" ;
8+ import { useTimelineScrollViewport } from "./useTimelineScrollViewport" ;
9+ import { useTimelineClipRenderWindow } from "./useTimelineClipRenderWindow" ;
10+ import { requestTimelineZoom , settleTimelineZoom } from "./timelineZoomInput" ;
811
912Object . assign ( globalThis , { IS_REACT_ACT_ENVIRONMENT : true } ) ;
1013
1114const ORIGIN = 32 ;
1215
13- function scrollBox ( scrollLeft : number ) {
16+ function scrollBox ( scrollLeft : number , clientWidth = 800 ) {
1417 const el = document . createElement ( "div" ) ;
1518 let left = scrollLeft ;
1619 Object . defineProperties ( el , {
17- clientWidth : { value : 800 } ,
20+ clientWidth : { value : clientWidth } ,
1821 scrollWidth : { value : 20_000 } ,
1922 scrollLeft : { get : ( ) => left , set : ( v : number ) => ( left = v ) } ,
2023 } ) ;
@@ -27,16 +30,24 @@ interface HarnessProps {
2730 scroll : HTMLDivElement ;
2831 dragging ?: boolean ;
2932 zoomMode ?: ZoomMode ;
33+ syncScrollViewport ?: ( scroll : HTMLDivElement ) => void ;
3034}
3135
32- function Harness ( { pps : fixedPps , scroll, dragging = false , zoomMode = "manual" } : HarnessProps ) {
36+ function Harness ( {
37+ pps : fixedPps ,
38+ scroll,
39+ dragging = false ,
40+ zoomMode = "manual" ,
41+ syncScrollViewport = ( ) => { } ,
42+ } : HarnessProps ) {
3343 const storePps = usePlayerStore ( ( s ) => s . timelinePps ) ;
3444 const pps = fixedPps ?? storePps ;
3545 const scrollRef = useRef ( scroll ) ;
3646 const durationRef = useRef ( 60 ) ;
3747 useTimelinePlayhead ( {
3848 playheadRef : { current : document . createElement ( "div" ) } ,
3949 scrollRef,
50+ syncScrollViewport,
4051 ppsRef : { current : pps } ,
4152 durationRef,
4253 isDragging : { current : dragging } ,
@@ -238,6 +249,63 @@ describe("useTimelinePlayhead follow while paused", () => {
238249 } ) ;
239250} ) ;
240251
252+ describe ( "useTimelinePlayhead committed viewport" , ( ) => {
253+ it ( "keeps the visible 59-second clip mounted when a pointer zoom commits" , ( ) => {
254+ vi . stubGlobal (
255+ "ResizeObserver" ,
256+ class {
257+ observe ( ) { }
258+ disconnect ( ) { }
259+ } ,
260+ ) ;
261+ usePlayerStore . setState ( {
262+ zoomMode : "manual" ,
263+ manualZoomPercent : 1000 ,
264+ timelineFitPps : 10 ,
265+ timelinePps : 100 ,
266+ } ) ;
267+ const scroll = scrollBox ( 5000 , 1080 ) ;
268+ const container = document . createElement ( "div" ) ;
269+ function Probe ( ) {
270+ const pps = usePlayerStore ( ( s ) => s . timelinePps ) ;
271+ const { viewport, setScrollRef, syncScrollViewport } = useTimelineScrollViewport (
272+ useRef ( scroll ) ,
273+ [ ] ,
274+ ) ;
275+ useLayoutEffect ( ( ) => setScrollRef ( scroll ) , [ setScrollRef ] ) ;
276+ const { renderTimeRange } = useTimelineClipRenderWindow ( {
277+ tracks : [ ] ,
278+ viewport,
279+ pixelsPerSecond : pps ,
280+ contentOrigin : ORIGIN ,
281+ duration : 100 ,
282+ } ) ;
283+ return (
284+ < >
285+ < Harness scroll = { scroll } syncScrollViewport = { syncScrollViewport } />
286+ { renderTimeRange . start <= 59 && renderTimeRange . end >= 59 && < span data-clip = "59" /> }
287+ < output > { viewport . scrollLeft } </ output >
288+ </ >
289+ ) ;
290+ }
291+ const root = createRoot ( container ) ;
292+ roots . push ( root ) ;
293+ try {
294+ act ( ( ) => root . render ( < Probe /> ) ) ;
295+ expect ( container . querySelector ( '[data-clip="59"]' ) ) . not . toBeNull ( ) ;
296+ act ( ( ) => {
297+ requestTimelineZoom ( 1090 , { time : 55.24 , x : 556 } ) ;
298+ settleTimelineZoom ( ) ;
299+ } ) ;
300+ expect ( scroll . scrollLeft ) . toBeCloseTo ( 5497.16 ) ;
301+ expect ( container . querySelector ( '[data-clip="59"]' ) ) . not . toBeNull ( ) ;
302+ expect ( Number ( container . querySelector ( "output" ) ?. textContent ) ) . toBeCloseTo ( 5497.16 ) ;
303+ } finally {
304+ vi . unstubAllGlobals ( ) ;
305+ }
306+ } ) ;
307+ } ) ;
308+
241309describe ( "useTimelinePlayhead wheel zoom" , ( ) => {
242310 beforeEach ( ( ) => {
243311 vi . useFakeTimers ( {
0 commit comments