Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change code height with rows #262

Merged
merged 2 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/mdx/dev/content/rows.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<CH.Code rows={4} showCopyButton={false}>

```js
console.log(2)
```

</CH.Code>

<CH.Code rows={4} showCopyButton={false} lineNumbers >

```js
console.log(1)
console.log(2)
console.log(3)
console.log(4)
console.log(5)
console.log(6)
console.log(7)
```

</CH.Code>

<CH.Code rows="focus" showCopyButton={false} lineNumbers >

```js focus=3:4,6[1:11]
console.log(1)
console.log(2)
console.log(3)
console.log(4)
console.log(5)
console.log(6)
console.log(7)
```

</CH.Code>

<CH.Code rows={4} style={{width: 200 }} lineNumbers >

```js
console.log(1)
console.log(2)
console.log(3)
console.log(4)
console.log(5)
// focus
console.log(600000)
console.log(7)
```

</CH.Code>

<CH.Code rows={6} lineNumbers >

```js foo.js
console.log(1)
```

```js bar.js
console.log(1)
console.log(2)
console.log(3)
console.log(4)
console.log(5)
console.log(7)
```

</CH.Code>
1 change: 1 addition & 0 deletions packages/mdx/src/mdx-client/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function mergeCodeConfig<T>(
showExpandButton == null
? props.codeConfig?.showExpandButton
: showExpandButton,
rows: props.rows,
debug: props.debug ?? props.codeConfig?.debug,
}
return { ...rest, codeConfig }
Expand Down
2 changes: 2 additions & 0 deletions packages/mdx/src/smooth-code/code-tween.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type CodeConfig = {
showCopyButton?: boolean
showExpandButton?: boolean
staticMediaQuery?: string
rows?: number | "focus"
debug?: boolean
}

Expand Down Expand Up @@ -85,6 +86,7 @@ export function CodeTween({
map(tween, tween => tween.focus),
config.minColumns || DEFAULT_MIN_COLUMNS,
config.lineNumbers || false,
config.rows,
[config.parentHeight]
)

Expand Down
42 changes: 40 additions & 2 deletions packages/mdx/src/smooth-code/use-dimensions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react"
import {
FocusString,
getFocusExtremes,
getFocusIndexes,
Tween,
useLayoutEffect,
Expand Down Expand Up @@ -38,6 +39,7 @@ function useDimensions(
focus: Tween<FocusString>,
minColumns: number,
lineNumbers: boolean,
rows: number | "focus" | undefined,
deps: React.DependencyList
): { element: React.ReactNode; dimensions: Dimensions } {
const [dimensions, setDimensions] =
Expand All @@ -61,7 +63,35 @@ function useDimensions(
.trimEnd()
.split(newlineRe)

const lineCount = lines.length
const originalLineCount = lines.length

if (rows) {
// make the lines match the requested number of rows
const heightInLines =
rows === "focus"
? focusHeightInLines(focus, lines)
: rows
let i = lines.length

while (i < heightInLines) {
lines.push("")
i++
}

// remove extra lines to match the requested rows
while (i > heightInLines) {
lines.pop()
i--
}

// if we removed prevLongestLine, add it back
if (
prevLongestLine &&
!lines.includes(prevLongestLine)
) {
lines[lines.length - 1] = prevLongestLine
}
}

// avod setting the ref more than once https://github.com/code-hike/codehike/issues/232
let prevLineRefSet = false
Expand All @@ -78,7 +108,7 @@ function useDimensions(
<div ref={ref} key={i}>
{lineNumbers ? (
<span className="ch-code-line-number">
_{lineCount}
_{originalLineCount}
</span>
) : undefined}
<div
Expand Down Expand Up @@ -244,3 +274,11 @@ function useWindowWidth() {
}, [])
return width
}

function focusHeightInLines(
focus: Tween<FocusString>,
lines: any[]
): number {
const [start, end] = getFocusExtremes(focus.prev, lines)
return end - start + 1
}
2 changes: 1 addition & 1 deletion packages/mdx/src/utils/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function parseExtremes(part: string) {
/**
* Return the first and last indexes to focus, both included
*/
function getFocusExtremes(
export function getFocusExtremes(
focus: FocusString,
lines: any[]
): [number, number] {
Expand Down