1
1
import React from "react"
2
2
import {
3
3
FocusString ,
4
+ getFocusExtremes ,
4
5
getFocusIndexes ,
5
6
Tween ,
6
7
useLayoutEffect ,
@@ -38,6 +39,7 @@ function useDimensions(
38
39
focus : Tween < FocusString > ,
39
40
minColumns : number ,
40
41
lineNumbers : boolean ,
42
+ rows : number | "focus" | undefined ,
41
43
deps : React . DependencyList
42
44
) : { element : React . ReactNode ; dimensions : Dimensions } {
43
45
const [ dimensions , setDimensions ] =
@@ -61,7 +63,35 @@ function useDimensions(
61
63
. trimEnd ( )
62
64
. split ( newlineRe )
63
65
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
+ }
65
95
66
96
// avod setting the ref more than once https://github.com/code-hike/codehike/issues/232
67
97
let prevLineRefSet = false
@@ -78,7 +108,7 @@ function useDimensions(
78
108
< div ref = { ref } key = { i } >
79
109
{ lineNumbers ? (
80
110
< span className = "ch-code-line-number" >
81
- _{ lineCount }
111
+ _{ originalLineCount }
82
112
</ span >
83
113
) : undefined }
84
114
< div
@@ -244,3 +274,11 @@ function useWindowWidth() {
244
274
} , [ ] )
245
275
return width
246
276
}
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