import rangeParser from 'parse-numeric-range' import { useMemo } from 'react' import CodeBlock, { CodeBlockProps } from './CodeBlock/CodeBlock' interface ScrollableCodeBlockProps extends CodeBlockProps { highlightLines: string children: string /** * Shows an application toolbar at the top */ showToolbar?: boolean } const MAX_HEIGHT = 520 const CODE_LINE_HEIGHT = 22 const TOP_OFFSET = 4 const ScrollableCodeBlock = ({ highlightLines: highlightLinesRange, children, showToolbar, ...props }: ScrollableCodeBlockProps) => { const highlightLines = useMemo( () => rangeParser(highlightLinesRange ?? ''), [highlightLinesRange] ) const firstLine = Math.min(...highlightLines) const lastLine = Math.max(...highlightLines) const firstLinePosition = firstLine * CODE_LINE_HEIGHT const lastLinePosition = lastLine * CODE_LINE_HEIGHT const middlePosition = (firstLinePosition + lastLinePosition) / 2 const position = Math.max(middlePosition - MAX_HEIGHT / 2 + TOP_OFFSET, 0) return (
{showToolbar && (
)}
{/* */} {children + '\n'.repeat(100)}
) } export default ScrollableCodeBlock