11import React from "react"
22import {
33 FocusString ,
4+ getFocusExtremes ,
45 getFocusIndexes ,
56 Tween ,
67 useLayoutEffect ,
@@ -38,6 +39,7 @@ function useDimensions(
3839 focus : Tween < FocusString > ,
3940 minColumns : number ,
4041 lineNumbers : boolean ,
42+ rows : number | "focus" | undefined ,
4143 deps : React . DependencyList
4244) : { element : React . ReactNode ; dimensions : Dimensions } {
4345 const [ dimensions , setDimensions ] =
@@ -61,7 +63,35 @@ function useDimensions(
6163 . trimEnd ( )
6264 . split ( newlineRe )
6365
64- const lineCount = lines . length
66+ const originalLineCount = lines . length
67+
68+ if ( rows ) {
69+ // make the lines match the requested number of rows
70+ const heightInLines =
71+ rows === "focus"
72+ ? focusHeightInLines ( focus , lines )
73+ : rows
74+ let i = lines . length
75+
76+ while ( i < heightInLines ) {
77+ lines . push ( "" )
78+ i ++
79+ }
80+
81+ // remove extra lines to match the requested rows
82+ while ( i > heightInLines ) {
83+ lines . pop ( )
84+ i --
85+ }
86+
87+ // if we removed prevLongestLine, add it back
88+ if (
89+ prevLongestLine &&
90+ ! lines . includes ( prevLongestLine )
91+ ) {
92+ lines [ lines . length - 1 ] = prevLongestLine
93+ }
94+ }
6595
6696 // avod setting the ref more than once https://github.com/code-hike/codehike/issues/232
6797 let prevLineRefSet = false
@@ -78,7 +108,7 @@ function useDimensions(
78108 < div ref = { ref } key = { i } >
79109 { lineNumbers ? (
80110 < span className = "ch-code-line-number" >
81- _{ lineCount }
111+ _{ originalLineCount }
82112 </ span >
83113 ) : undefined }
84114 < div
@@ -244,3 +274,11 @@ function useWindowWidth() {
244274 } , [ ] )
245275 return width
246276}
277+
278+ function focusHeightInLines (
279+ focus : Tween < FocusString > ,
280+ lines : any [ ]
281+ ) : number {
282+ const [ start , end ] = getFocusExtremes ( focus . prev , lines )
283+ return end - start + 1
284+ }
0 commit comments