@@ -57,13 +57,28 @@ export const CodeBlock = (props: ICodeBlockProps) => {
5757 return
5858 }
5959
60- const lineHeightOption = editor . getOption ( monaco . editor . EditorOption . lineHeight )
61- const lineHeight = typeof lineHeightOption === 'number' ? lineHeightOption : FALLBACK_LINE_HEIGHT_PX
62- const lineCount = Math . max ( code . split ( LINE_BREAK_REGEX ) . length , 1 )
63- const estimatedHeight = lineCount * lineHeight + EDITOR_EXTRA_PADDING
64- const viewportCap = window . innerHeight * VIEWPORT_HEIGHT_RATIO
65- const clampedHeight = Math . min ( estimatedHeight , viewportCap )
66- setResolvedHeight ( `${ Math . round ( clampedHeight ) } px` )
60+ const estimateHeightFromLines = ( ) => {
61+ const lineHeightOption = editor . getOption ( monaco . editor . EditorOption . lineHeight )
62+ const lineHeight = typeof lineHeightOption === 'number' ? lineHeightOption : FALLBACK_LINE_HEIGHT_PX
63+ const lineCount = Math . max ( code . split ( LINE_BREAK_REGEX ) . length , 1 )
64+ return lineCount * lineHeight + EDITOR_EXTRA_PADDING
65+ }
66+
67+ const updateHeight = ( contentHeight ?: number ) => {
68+ const rawHeight =
69+ typeof contentHeight === 'number' ? contentHeight + EDITOR_EXTRA_PADDING : estimateHeightFromLines ( )
70+ const viewportCap = window . innerHeight * VIEWPORT_HEIGHT_RATIO
71+ const clampedHeight = Math . min ( rawHeight , viewportCap )
72+ setResolvedHeight ( `${ Math . round ( clampedHeight ) } px` )
73+ }
74+
75+ updateHeight ( editor . getContentHeight ( ) )
76+
77+ const disposable = editor . onDidContentSizeChange ( ( event ) => {
78+ updateHeight ( event . contentHeight )
79+ } )
80+
81+ editor . onDidDispose ( ( ) => disposable . dispose ( ) )
6782 }
6883
6984 const defaultMonacoOptions : MonacoEditor . IStandaloneEditorConstructionOptions = {
0 commit comments