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'
@@ -55,12 +55,9 @@ function useCodeEditorContext() {
55
55
}
56
56
57
57
/**
58
- * NOTE: once monaco editor mounts it doesn't update it's internal onChange state.
59
- * Since most of the time onChange methods are declared inside the render body of a component
60
- * we should use the latest references of onChange.
61
- * Please see: https://github.com/react-monaco-editor/react-monaco-editor/issues/704
62
- * Thus as a hack we are using this objects reference to call the latest onChange reference
63
- */
58
+ * TODO: can be removed with this new merge into react-monaco-editor :)
59
+ * see: https://github.com/react-monaco-editor/react-monaco-editor/pull/955
60
+ * */
64
61
const _onChange = {
65
62
onChange : null ,
66
63
}
@@ -90,6 +87,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
90
87
cleanData = false ,
91
88
onBlur,
92
89
onFocus,
90
+ adjustEditorHeightToContent,
93
91
} ) => {
94
92
if ( cleanData ) {
95
93
value = cleanKubeManifest ( value )
@@ -103,6 +101,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
103
101
const [ state , dispatch ] = useReducer ( memoisedReducer , initialState ( { mode, theme, value, diffView, noParsing } ) )
104
102
const [ , json , yamlCode , error ] = useJsonYaml ( state . code , tabSize , state . mode , ! state . noParsing )
105
103
const [ , originalJson , originlaYaml ] = useJsonYaml ( defaultValue , tabSize , state . mode , ! state . noParsing )
104
+ const [ contentHeight , setContentHeight ] = useState ( height )
106
105
monaco . editor . defineTheme ( CodeEditorThemesKeys . vsDarkDT , {
107
106
base : 'vs-dark' ,
108
107
inherit : true ,
@@ -167,10 +166,24 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
167
166
}
168
167
}
169
168
169
+ if ( adjustEditorHeightToContent ) {
170
+ setContentHeight ( editor . getContentHeight ( ) )
171
+ editor . onDidContentSizeChange ( ( ) => {
172
+ setContentHeight ( editor . getContentHeight ( ) )
173
+ } )
174
+ }
175
+
170
176
editorRef . current = editor
171
177
monacoRef . current = monaco
172
178
}
173
179
180
+ const editorHeight = useMemo ( ( ) => {
181
+ if ( ! adjustEditorHeightToContent ) {
182
+ return height
183
+ }
184
+ return contentHeight
185
+ } , [ height , contentHeight , adjustEditorHeightToContent ] )
186
+
174
187
useEffect ( ( ) => {
175
188
if ( ! validatorSchema ) {
176
189
return
@@ -283,7 +296,9 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
283
296
}
284
297
285
298
return (
286
- < CodeEditorContext . Provider value = { { dispatch, state, handleLanguageChange, error, defaultValue, height } } >
299
+ < CodeEditorContext . Provider
300
+ value = { { dispatch, state, handleLanguageChange, error, defaultValue, height : editorHeight } }
301
+ >
287
302
{ children }
288
303
{ loading ? (
289
304
< CodeEditorPlaceholder customLoader = { customLoader } />
@@ -301,7 +316,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
301
316
options = { diffViewOptions }
302
317
theme = { state . theme . toLowerCase ( ) . split ( ' ' ) . join ( '-' ) }
303
318
editorDidMount = { editorDidMount }
304
- height = { height }
319
+ height = { editorHeight }
305
320
width = "100%"
306
321
/>
307
322
) : (
@@ -312,7 +327,7 @@ const CodeEditor: React.FC<CodeEditorInterface> & CodeEditorComposition = React.
312
327
options = { options }
313
328
onChange = { handleOnChange }
314
329
editorDidMount = { editorDidMount }
315
- height = { height }
330
+ height = { editorHeight }
316
331
width = "100%"
317
332
/>
318
333
) }
0 commit comments