14
14
* limitations under the License.
15
15
*/
16
16
17
- import React , { useEffect , useReducer , useRef } from 'react'
17
+ import React , { useEffect , useMemo , useReducer , useRef , useState } from 'react'
18
18
import MonacoEditor , { MonacoDiffEditor } from 'react-monaco-editor'
19
19
import YAML from 'yaml'
20
20
import ReactGA from 'react-ga4'
@@ -66,12 +66,9 @@ function useCodeEditorContext() {
66
66
}
67
67
68
68
/**
69
- * NOTE: once monaco editor mounts it doesn't update it's internal onChange state.
70
- * Since most of the time onChange methods are declared inside the render body of a component
71
- * we should use the latest references of onChange.
72
- * Please see: https://github.com/react-monaco-editor/react-monaco-editor/issues/704
73
- * Thus as a hack we are using this objects reference to call the latest onChange reference
74
- */
69
+ * TODO: can be removed with this new merge into react-monaco-editor :)
70
+ * see: https://github.com/react-monaco-editor/react-monaco-editor/pull/955
71
+ * */
75
72
const _onChange = {
76
73
onChange : null
77
74
}
@@ -101,6 +98,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
101
98
cleanData = false ,
102
99
onBlur,
103
100
onFocus,
101
+ adjustEditorHeightToContent,
104
102
} ) => {
105
103
if ( cleanData ) {
106
104
value = cleanKubeManifest ( value )
@@ -114,6 +112,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
114
112
const [ state , dispatch ] = useReducer ( memoisedReducer , initialState ( { mode, theme, value, diffView, noParsing } ) )
115
113
const [ , json , yamlCode , error ] = useJsonYaml ( state . code , tabSize , state . mode , ! state . noParsing )
116
114
const [ , originalJson , originlaYaml ] = useJsonYaml ( defaultValue , tabSize , state . mode , ! state . noParsing )
115
+ const [ contentHeight , setContentHeight ] = useState ( height )
117
116
monaco . editor . defineTheme ( CodeEditorThemesKeys . vsDarkDT , {
118
117
base : 'vs-dark' ,
119
118
inherit : true ,
@@ -166,10 +165,24 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
166
165
}
167
166
}
168
167
168
+ if ( adjustEditorHeightToContent ) {
169
+ setContentHeight ( editor . getContentHeight ( ) )
170
+ editor . onDidContentSizeChange ( ( ) => {
171
+ setContentHeight ( editor . getContentHeight ( ) )
172
+ } )
173
+ }
174
+
169
175
editorRef . current = editor
170
176
monacoRef . current = monaco
171
177
}
172
178
179
+ const editorHeight = useMemo ( ( ) => {
180
+ if ( ! adjustEditorHeightToContent ) {
181
+ return height
182
+ }
183
+ return contentHeight
184
+ } , [ height , contentHeight , adjustEditorHeightToContent ] )
185
+
173
186
useEffect ( ( ) => {
174
187
if ( ! validatorSchema ) {
175
188
return
@@ -282,7 +295,9 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
282
295
}
283
296
284
297
return (
285
- < CodeEditorContext . Provider value = { { dispatch, state, handleLanguageChange, error, defaultValue, height } } >
298
+ < CodeEditorContext . Provider
299
+ value = { { dispatch, state, handleLanguageChange, error, defaultValue, height : editorHeight } }
300
+ >
286
301
{ children }
287
302
{ loading ? (
288
303
< CodeEditorPlaceholder customLoader = { customLoader } />
@@ -300,7 +315,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
300
315
options = { diffViewOptions }
301
316
theme = { state . theme . toLowerCase ( ) . split ( ' ' ) . join ( '-' ) }
302
317
editorDidMount = { editorDidMount }
303
- height = { height }
318
+ height = { editorHeight }
304
319
width = "100%"
305
320
/>
306
321
) : (
@@ -311,7 +326,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
311
326
options = { options }
312
327
onChange = { handleOnChange }
313
328
editorDidMount = { editorDidMount }
314
- height = { height }
329
+ height = { editorHeight }
315
330
width = "100%"
316
331
/>
317
332
) }
0 commit comments