@@ -3,7 +3,7 @@ import useBaseUrl from '@docusaurus/useBaseUrl';
3
3
import { Editor as Monaco , useMonaco } from '@monaco-editor/react' ;
4
4
import type monaco_editor from 'monaco-editor' ;
5
5
import type { editor } from 'monaco-editor' ;
6
- import { memo , useEffect , useMemo } from 'react' ;
6
+ import { memo , useCallback , useEffect , useMemo } from 'react' ;
7
7
8
8
interface Props {
9
9
script : string ;
@@ -21,6 +21,7 @@ const MONACO_THEMES: Record<ColorMode, string> = {
21
21
22
22
function TSGraphvizLiveEditor ( {
23
23
script,
24
+ // Auto resize the editor to fit the content height
24
25
onMount,
25
26
readOnly,
26
27
} : Props ) : JSX . Element {
@@ -31,6 +32,40 @@ function TSGraphvizLiveEditor({
31
32
( ) => ( colorMode === 'dark' ? MONACO_THEMES . dark : MONACO_THEMES . light ) ,
32
33
[ colorMode ] ,
33
34
) ;
35
+ const onMountCallback = useCallback (
36
+ onMount ??
37
+ ( ( editor : editor . IStandaloneCodeEditor ) => {
38
+ const lineHeight = editor . getOption (
39
+ monaco . editor . EditorOption . lineHeight ,
40
+ ) ;
41
+ const currentLayout = editor . getLayoutInfo ( ) ;
42
+ const MIN_HEIGHT = 100 ;
43
+ const MAX_HEIGHT = 500 ;
44
+ const PADDING = 10 ;
45
+ resize ( ) ;
46
+
47
+ // Auto-resize on content changes
48
+ if ( ! readOnly ) {
49
+ editor . getModel ( ) . onDidChangeContent ( ( ) => {
50
+ resize ( ) ;
51
+ } ) ;
52
+ }
53
+ function resize ( ) {
54
+ const newHeight = Math . min (
55
+ Math . max (
56
+ editor . getModel ( ) . getLineCount ( ) * lineHeight + PADDING ,
57
+ MIN_HEIGHT ,
58
+ ) ,
59
+ MAX_HEIGHT ,
60
+ ) ;
61
+ editor . layout ( {
62
+ height : newHeight ,
63
+ width : currentLayout . contentWidth ,
64
+ } ) ;
65
+ }
66
+ } ) ,
67
+ [ monaco , readOnly ] ,
68
+ ) ;
34
69
const dtsUrl = useBaseUrl ( '/dts.json' ) ;
35
70
useEffect ( ( ) => {
36
71
if ( monaco ) {
@@ -70,7 +105,7 @@ function TSGraphvizLiveEditor({
70
105
lineDecorationsWidth : 0 ,
71
106
readOnly,
72
107
} }
73
- onMount = { onMount }
108
+ onMount = { onMountCallback }
74
109
/>
75
110
) : null }
76
111
</ >
0 commit comments