@@ -34,10 +34,10 @@ function SpectrogramNavigator(props: SpectrogramNavigatorProps) {
3434 const draggingToPan = isZoomed && dragStart ;
3535
3636 const getPointerCoordinate = (
37- e : React . MouseEvent < SVGSVGElement , MouseEvent >
37+ e : React . MouseEvent < SVGSVGElement , MouseEvent > ,
3838 ) => {
3939 const boundingClientRect = svgRef . current ?. getBoundingClientRect ( ) ;
40- if ( boundingClientRect ) {
40+ if ( boundingClientRect && duration ) {
4141 const { left, right } = boundingClientRect ;
4242 let newTime = ( duration * ( e . clientX - left ) ) / ( right - left ) ;
4343 return newTime ;
@@ -53,10 +53,10 @@ function SpectrogramNavigator(props: SpectrogramNavigatorProps) {
5353 const newDragEnd = getPointerCoordinate ( e ) ;
5454 setDragEnd ( newDragEnd ) ;
5555 if ( newDragEnd ) {
56- if ( draggingToPan ) {
56+ if ( draggingToPan && duration ) {
5757 const newCenterTime = Math . min (
5858 Math . max ( zoomedDuration / 2 , newDragEnd ) ,
59- duration - zoomedDuration / 2
59+ duration - zoomedDuration / 2 ,
6060 ) ;
6161 setCenterTime ( newCenterTime ) ;
6262 setCurrentTime ( newCenterTime - zoomedDuration / 2 ) ;
@@ -65,7 +65,7 @@ function SpectrogramNavigator(props: SpectrogramNavigatorProps) {
6565 } ;
6666
6767 const onPointerUp = ( e : React . MouseEvent < SVGSVGElement , MouseEvent > ) => {
68- if ( draggingToZoom ) {
68+ if ( draggingToZoom && duration ) {
6969 if ( dragEnd - dragStart >= MINIMUM_ZOOM_WINDOW_DURATION ) {
7070 setStartTime ( dragStart ) ;
7171 setEndTime ( dragEnd ) ;
@@ -80,47 +80,53 @@ function SpectrogramNavigator(props: SpectrogramNavigatorProps) {
8080 setDragEnd ( null ) ;
8181 } ;
8282
83+ const placeholder_svg = < svg width = "100%" height = { height } /> ;
84+
8385 return (
8486 < div style = { { display : "flex" , flexDirection : "row" , gap : 5 } } >
8587 < button className = { theme } onClick = { zoomOut } >
8688 Zoom Out
8789 </ button >
88- < svg
89- ref = { svgRef }
90- width = "100%"
91- height = { height }
92- viewBox = { `0,0,${ duration } ,100` }
93- cursor = { isZoomed ? "grabbing" : "zoom-in" }
94- preserveAspectRatio = "none"
95- onPointerDown = { onPointerDown }
96- onPointerUp = { onPointerUp }
97- onPointerMove = { onPointerMove }
98- >
99- { children }
100- < rect
101- x = { 0 }
102- width = { startTime }
103- y = "0"
104- height = "100"
105- style = { { fill : "white" , opacity : 0.5 } }
106- />
107- < rect
108- x = { endTime }
109- width = { duration - endTime }
110- y = "0"
111- height = "100"
112- style = { { fill : "white" , opacity : 0.5 } }
113- />
114- { draggingToZoom && dragEnd > dragStart && (
90+ { duration ? (
91+ < svg
92+ ref = { svgRef }
93+ width = "100%"
94+ height = { height }
95+ viewBox = { `0,0,${ duration } ,100` }
96+ cursor = { isZoomed ? "grabbing" : "zoom-in" }
97+ preserveAspectRatio = "none"
98+ onPointerDown = { onPointerDown }
99+ onPointerUp = { onPointerUp }
100+ onPointerMove = { onPointerMove }
101+ >
102+ { children }
103+ < rect
104+ x = { 0 }
105+ width = { startTime }
106+ y = "0"
107+ height = "100"
108+ style = { { fill : "white" , opacity : 0.5 } }
109+ />
115110 < rect
116- x = { dragStart }
117- width = { dragEnd - dragStart }
111+ x = { endTime }
112+ width = { duration - endTime }
118113 y = "0"
119114 height = "100"
120- style = { { fill : "red " , opacity : 0.5 } }
115+ style = { { fill : "white " , opacity : 0.5 } }
121116 />
122- ) }
123- </ svg >
117+ { draggingToZoom && dragEnd > dragStart && (
118+ < rect
119+ x = { dragStart }
120+ width = { dragEnd - dragStart }
121+ y = "0"
122+ height = "100"
123+ style = { { fill : "red" , opacity : 0.5 } }
124+ />
125+ ) }
126+ </ svg >
127+ ) : (
128+ placeholder_svg
129+ ) }
124130 < button
125131 className = { theme }
126132 style = { { fontFamily : "monospace" } }
0 commit comments