1- import React , { useState , Dispatch , SetStateAction } from 'react' ;
1+ import React , { Dispatch , SetStateAction , useState } from 'react' ;
22import { Button , Card , CardContent , CardHeader } from '@material-ui/core' ;
3- import MonacoEditor , { EditorDidMount } from 'react- monaco-editor' ;
3+ import { ControlledEditor , ControlledEditorOnChange } from '@ monaco-editor/react ' ;
44import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api' ;
5- import ResizeObserver from 'react-resize-detector' ;
65import { queryCache } from 'react-query' ;
6+ import ResizeObserver from 'react-resize-detector' ;
77
88const COMPILE_AND_RUN_BUTTON_ID = 'compile-and-run-button-id' ;
99
@@ -16,10 +16,11 @@ interface YourCodeProps {
1616
1717const YourCode : React . FC < YourCodeProps > = ( props : YourCodeProps ) => {
1818 const { handleCompileAndRun, loading, setSource, source } = props ;
19+
1920 const [ editor , setEditor ] = useState < monacoEditor . editor . IStandaloneCodeEditor | null > ( null ) ;
2021 const [ monaco , setMonaco ] = useState < typeof monacoEditor | null > ( null ) ;
2122
22- const handleUploadSource = ( v : string ) => {
23+ const handleUploadSource : ControlledEditorOnChange = ( e , v ) => {
2324 setSource ( v as string ) ;
2425 } ;
2526
@@ -28,9 +29,9 @@ const YourCode: React.FC<YourCodeProps> = (props: YourCodeProps) => {
2829 scrollBeyondLastLine : false
2930 } ;
3031
31- const EditorDidMountHandle = (
32+ const handleEditorDidMount = (
33+ _ : ( ) => string ,
3234 editor : monacoEditor . editor . IStandaloneCodeEditor ,
33- monaco : typeof monacoEditor
3435 ) => {
3536 editor . addAction ( {
3637 id : COMPILE_AND_RUN_BUTTON_ID ,
@@ -40,9 +41,7 @@ const YourCode: React.FC<YourCodeProps> = (props: YourCodeProps) => {
4041 document . getElementById ( COMPILE_AND_RUN_BUTTON_ID ) ?. click ( ) ;
4142 }
4243 } ) ;
43-
4444 setEditor ( editor ) ;
45- setMonaco ( monaco ) ;
4645 } ;
4746
4847 return (
@@ -57,8 +56,8 @@ const YourCode: React.FC<YourCodeProps> = (props: YourCodeProps) => {
5756 color = "secondary"
5857 style = { {
5958 textTransform : 'none' ,
60- width : 140
6159 } }
60+ size = "small"
6261 onClick = { ( ) => {
6362 // stop the web-worker
6463 queryCache . cancelQueries ( [ 'compiler' ] ) ;
@@ -75,33 +74,36 @@ const YourCode: React.FC<YourCodeProps> = (props: YourCodeProps) => {
7574 id = { COMPILE_AND_RUN_BUTTON_ID }
7675 variant = "contained"
7776 color = "primary"
77+ size = "small"
7878 onClick = { ( e ) => handleCompileAndRun ( e ) }
7979 style = { {
8080 textTransform : 'none'
8181 } } >
82- Compile and run
82+ Run
8383 </ Button >
8484 ) }
8585 </ div >
8686 }
87- title = "Your code:"
8887 />
8988
9089 < CardContent >
91- < div
92- style = { {
93- height : 'calc(100vh - 160px)'
94- } } >
90+ < ResizeObserver
91+ onResize = { ( width , height ) => {
92+ if ( editor ) {
93+ editor . layout ( ) ;
94+ }
95+ } } >
9596 < div
9697 style = { {
97- height : 'calc(100vh - 174px )'
98+ height : 'calc(100vh - 160px )'
9899 } } >
99- < MonacoEditor
100+ < ControlledEditor
100101 language = "javascript"
101102 value = { source }
102103 onChange = { handleUploadSource }
103104 options = { options }
104- editorDidMount = { EditorDidMountHandle }
105+ editorDidMount = { handleEditorDidMount }
106+
105107 />
106108 </ div >
107109 </ ResizeObserver >
0 commit comments