Skip to content

Commit e3e0771

Browse files
authored
Merge pull request #262 from code-hike/rows
Change code height with `rows`
2 parents cbfac22 + fb2e24d commit e3e0771

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

packages/mdx/dev/content/rows.mdx

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<CH.Code rows={4} showCopyButton={false}>
2+
3+
```js
4+
console.log(2)
5+
```
6+
7+
</CH.Code>
8+
9+
<CH.Code rows={4} showCopyButton={false} lineNumbers >
10+
11+
```js
12+
console.log(1)
13+
console.log(2)
14+
console.log(3)
15+
console.log(4)
16+
console.log(5)
17+
console.log(6)
18+
console.log(7)
19+
```
20+
21+
</CH.Code>
22+
23+
<CH.Code rows="focus" showCopyButton={false} lineNumbers >
24+
25+
```js focus=3:4,6[1:11]
26+
console.log(1)
27+
console.log(2)
28+
console.log(3)
29+
console.log(4)
30+
console.log(5)
31+
console.log(6)
32+
console.log(7)
33+
```
34+
35+
</CH.Code>
36+
37+
<CH.Code rows={4} style={{width: 200 }} lineNumbers >
38+
39+
```js
40+
console.log(1)
41+
console.log(2)
42+
console.log(3)
43+
console.log(4)
44+
console.log(5)
45+
// focus
46+
console.log(600000)
47+
console.log(7)
48+
```
49+
50+
</CH.Code>
51+
52+
<CH.Code rows={6} lineNumbers >
53+
54+
```js foo.js
55+
console.log(1)
56+
```
57+
58+
```js bar.js
59+
console.log(1)
60+
console.log(2)
61+
console.log(3)
62+
console.log(4)
63+
console.log(5)
64+
console.log(7)
65+
```
66+
67+
</CH.Code>

packages/mdx/src/mdx-client/code.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function mergeCodeConfig<T>(
5555
showExpandButton == null
5656
? props.codeConfig?.showExpandButton
5757
: showExpandButton,
58+
rows: props.rows,
5859
debug: props.debug ?? props.codeConfig?.debug,
5960
}
6061
return { ...rest, codeConfig }

packages/mdx/src/smooth-code/code-tween.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export type CodeConfig = {
4848
showCopyButton?: boolean
4949
showExpandButton?: boolean
5050
staticMediaQuery?: string
51+
rows?: number | "focus"
5152
debug?: boolean
5253
}
5354

@@ -85,6 +86,7 @@ export function CodeTween({
8586
map(tween, tween => tween.focus),
8687
config.minColumns || DEFAULT_MIN_COLUMNS,
8788
config.lineNumbers || false,
89+
config.rows,
8890
[config.parentHeight]
8991
)
9092

packages/mdx/src/smooth-code/use-dimensions.tsx

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react"
22
import {
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+
}

packages/mdx/src/utils/focus.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function parseExtremes(part: string) {
102102
/**
103103
* Return the first and last indexes to focus, both included
104104
*/
105-
function getFocusExtremes(
105+
export function getFocusExtremes(
106106
focus: FocusString,
107107
lines: any[]
108108
): [number, number] {

0 commit comments

Comments
 (0)