@@ -3,38 +3,65 @@ import { render } from 'react-dom';
3
3
import MonacoEditor from 'react-monaco-editor' ;
4
4
5
5
interface AppState {
6
- code : string ;
6
+ input : string ;
7
+ output : string ;
8
+ windowSize : { width : number ; height : number ; } ;
7
9
}
8
10
9
11
class App extends React . Component < { } , AppState > {
10
- state = { code : '// hello ' }
11
- editorDidMount = ( editor : monaco . editor . ICodeEditor , m : typeof monaco ) => {
12
- console . log ( 'editorDidMount' , editor ) ;
13
- editor . focus ( ) ;
14
- }
15
- onChange = ( newValue : string , e : monaco . editor . IModelContentChangedEvent2 ) => {
16
- console . log ( 'onChange' , newValue , e ) ;
17
- }
18
- render ( ) {
19
- const code = this . state . code ;
20
- const options = {
21
- selectOnLineNumbers : true
22
- } ;
23
- return (
24
- < MonacoEditor
25
- width = "800"
26
- height = "600"
27
- language = "javascript"
28
- value = { code }
29
- options = { options }
30
- onChange = { this . onChange }
31
- editorDidMount = { this . editorDidMount }
32
- />
33
- ) ;
34
- }
12
+ static toolbarHeight = 50 ;
13
+ state = { input : '// hello ' , output : '// hello' , windowSize : { width : 0 , height : 0 } }
14
+ setWindowSize = ( ) => {
15
+ this . setState ( {
16
+ windowSize : { width : window . innerWidth , height : innerHeight }
17
+ } ) ;
18
+ }
19
+ componentWillMount ( ) {
20
+ this . setWindowSize ( )
21
+ window . addEventListener ( 'resize' , this . setWindowSize ) ;
22
+ }
23
+ editorDidMount = ( editor : monaco . editor . ICodeEditor , m : typeof monaco ) => {
24
+ console . log ( 'editorDidMount' , editor ) ;
25
+ editor . focus ( ) ;
26
+ }
27
+ onChange = ( newValue : string , e : monaco . editor . IModelContentChangedEvent2 ) => {
28
+ console . log ( 'onChange' , newValue , e ) ;
29
+ }
30
+ render ( ) {
31
+ const { input, output, windowSize } = this . state ;
32
+ return (
33
+ < div >
34
+ < Toolbar height = { App . toolbarHeight } />
35
+ < div style = { { display : 'flex' } } >
36
+ < MonacoEditor
37
+ width = { windowSize . width / 2 }
38
+ height = { windowSize . height - App . toolbarHeight }
39
+ language = 'javascript'
40
+ value = { input }
41
+ options = { { } }
42
+ onChange = { this . onChange }
43
+ editorDidMount = { this . editorDidMount }
44
+ />
45
+ < MonacoEditor
46
+ width = { windowSize . width / 2 }
47
+ height = { windowSize . height - App . toolbarHeight }
48
+ language = 'typescript'
49
+ value = { output }
50
+ options = { { readOnly : true } }
51
+ />
52
+ </ div >
53
+ </ div >
54
+ ) ;
55
+ }
35
56
}
36
57
58
+ const Toolbar = ( { height} : { height : number } ) => (
59
+ < div style = { { height : `${ height } px` } } >
60
+ Convert React code to TypeScript
61
+ </ div >
62
+ )
63
+
37
64
render (
38
- < App /> ,
39
- document . getElementById ( 'root' )
65
+ < App /> ,
66
+ document . getElementById ( 'root' )
40
67
) ;
0 commit comments