11import cn from 'classnames' ;
2- import { debounce } from 'lodash' ;
32import * as React from 'react' ;
4- import useResizeObserver from 'use-resize-observer' ;
53
64import { Classes } from '../../../classes' ;
7- import { SINGLE_LINE_SIZE } from './consts' ;
85import { useSlicedBlocks } from './hooks/useSlicedBlocks' ;
96import { ObservableSet } from './ObservableSet' ;
107import { SingleCodeBlock } from './SingleCodeBlock' ;
@@ -15,29 +12,17 @@ export interface IBlockCodeViewerProps extends React.HTMLAttributes<HTMLPreEleme
1512 showLineNumbers : boolean ;
1613}
1714
18- function calculateMaxLines ( height : number ) {
19- return Math . floor ( Math . min ( window . innerHeight , height ) / SINGLE_LINE_SIZE ) + 1 ;
20- }
21-
2215const BlockCodeViewer : React . FC < IBlockCodeViewerProps > = ( { className, language, value, showLineNumbers, ...rest } ) => {
2316 const nodeRef = React . useRef < HTMLPreElement | null > ( null ) ;
24- const [ maxLines , setMaxLines ] = React . useState < number | null > ( null ) ;
17+ const maxLines = 100 ;
2518 const [ observer , setObserver ] = React . useState < IntersectionObserver > ( ) ;
2619 const viewportSet = React . useRef ( new ObservableSet ( ) ) ;
27- const slicedBlocks = useSlicedBlocks ( value , maxLines === null ? null : Math . max ( 0 , maxLines - 1 ) ) ;
28- const lineNumberCharacterCount = String (
29- slicedBlocks !== null && maxLines !== null ? slicedBlocks . length * maxLines : 0 ,
30- ) . length ;
31-
32- React . useLayoutEffect ( ( ) => {
33- if ( nodeRef . current !== null ) {
34- setMaxLines ( calculateMaxLines ( window . innerHeight ) ) ; // we have to use window here, as element may not ave any height at this time
35- }
36- } , [ nodeRef ] ) ;
20+ const slicedBlocks = useSlicedBlocks ( value ?? '' , maxLines - 1 ) ;
21+ const lineNumberCharacterCount = String ( slicedBlocks . length * maxLines ) . length ;
3722
3823 React . useEffect ( ( ) => {
3924 const { current : viewport } = viewportSet ;
40- if ( nodeRef . current === null || maxLines === null ) {
25+ if ( nodeRef . current === null ) {
4126 return ;
4227 }
4328
@@ -71,21 +56,7 @@ const BlockCodeViewer: React.FC<IBlockCodeViewerProps> = ({ className, language,
7156 setObserver ( void 0 ) ;
7257 observer . disconnect ( ) ;
7358 } ;
74- } , [ nodeRef , maxLines ] ) ;
75-
76- useResizeObserver ( {
77- onResize : debounce (
78- ( { height } ) => {
79- const newMaxLines = calculateMaxLines ( height ) ;
80- if ( newMaxLines !== maxLines ) {
81- setMaxLines ( newMaxLines ) ;
82- }
83- } ,
84- 250 ,
85- { leading : true } ,
86- ) ,
87- ref : nodeRef ,
88- } ) ;
59+ } , [ nodeRef ] ) ;
8960
9061 return (
9162 < pre
0 commit comments