1
- import React from "react" ;
2
- import {
3
- Dropdown ,
4
- IDropdownOption ,
5
- PrimaryButton ,
6
- Stack ,
7
- Label ,
8
- mergeStyleSets
9
- } from "office-ui-fabric-react" ;
10
- import { initializeIcons } from "office-ui-fabric-react/lib/Icons" ;
11
- // import Editor from "react-simple-code-editor";
12
- // import Prism from "prismjs";
13
- // require("prismjs/components/prism-typescript");
14
- // import "./prism-modified.css";
15
- initializeIcons ( ) ;
16
-
17
- import * as typescript from "typescript" ;
18
- //const Editor = React.lazy(() => import('./Editor'));
19
- //import {Editor} from ("./Editor");
20
- declare const ts : typeof typescript ;
21
-
22
- const options : IDropdownOption [ ] = [
23
- { key : "12" , text : "12" } ,
24
- { key : "14" , text : "14" } ,
25
- { key : "16" , text : "16" } ,
26
- { key : "18" , text : "18" } ,
27
- { key : "20" , text : "20" } ,
28
- { key : "22" , text : "22" } ,
29
- { key : "24" , text : "24" }
30
- ] ;
31
-
32
- const initialCode = `const text: string = "hello world";
33
- ReactDOM.render(<div>{text}</div>, document.getElementById('output'));` ;
1
+ import React from 'react' ;
2
+ import { PrimaryButton , Stack , Label , mergeStyleSets } from 'office-ui-fabric-react' ;
3
+ // import { ITranspileOutput } from '../transpiler/transpile.types';
34
4
35
5
const classNames = mergeStyleSets ( {
36
6
code : {
37
- fontFamily : " monospace" ,
7
+ fontFamily : ' monospace' ,
38
8
fontSize : 13 ,
39
- lineHeight : " 1.5"
9
+ lineHeight : ' 1.5'
40
10
} ,
41
11
renderSection : {
42
- backgroundColor : " red"
12
+ backgroundColor : ' red'
43
13
} ,
44
14
error : {
45
- backgroundColor : " #FEF0F0" ,
46
- color : " #FF5E79"
15
+ backgroundColor : ' #FEF0F0' ,
16
+ color : ' #FF5E79'
47
17
} ,
48
18
editor : {
49
19
width : 800 ,
@@ -55,183 +25,61 @@ interface IAppState {
55
25
code : string ;
56
26
JScode : string ;
57
27
error ?: string ;
58
- options : IDropdownOption [ ] ;
59
28
fontSize ?: string ;
60
29
editorHidden ?: boolean ;
61
- editor : any ;
30
+ editor ?: JSX . Element ;
62
31
currentTime : number ;
63
32
}
64
33
65
34
export class App extends React . Component {
66
35
public state : IAppState = {
67
- code : "" ,
68
- JScode : "" ,
69
- options : options ,
36
+ code : '' ,
37
+ JScode : '' ,
70
38
editor : undefined ,
71
39
currentTime : 0 ,
72
40
editorHidden : true
73
41
} ;
74
42
75
- private timer : any ;
76
-
77
- private changeFontSize = (
78
- event : React . FormEvent ,
79
- option : IDropdownOption | undefined ,
80
- index : number | undefined
81
- ) : void => {
82
- if ( typeof index != "undefined" ) {
83
- this . setState ( { fontSize : parseInt ( options [ index ] . key as string ) } ) ;
84
- }
85
- } ;
86
-
87
- private buttonClicked = ( ) : void => {
88
- if ( this . state . editorHidden ) {
89
- import ( "./Editor" ) . then ( ( editor ) => {
90
- let TSeditor = (
91
- < div >
92
- < div >
93
- < Label > Typescript + React editor</ Label >
94
- </ div >
95
- { /* <div className={classNames.editor} id="editor" hidden = {this.state.editorHidden}/> */ }
96
- < React . Suspense fallback = { < div > Loading...</ div > } >
97
- < editor . Editor
98
- width = { 800 }
99
- height = { 500 }
100
- code = ''
101
- language = "typescript"
102
- />
103
- </ React . Suspense >
104
- </ div >
105
- )
106
- this . setState ( { editorHidden : false , editor : TSeditor } ) ;
107
- } ) ;
108
- } else {
109
- this . setState ( { editor : null , editorHidden : true } )
110
- }
111
- }
112
-
113
- decrementTimeRemaining = ( ) => {
114
- if ( this . state . currentTime < 1 ) {
115
- this . setState ( {
116
- currentTime : this . state . currentTime + 1
117
- } ) ;
118
- } else {
119
- if ( this . state . editor != undefined ) {
120
- let editorText = this . state . editor . getValue1 ( ) ;
121
- if ( this . state . editor !== undefined ) {
122
- if ( editorText != this . state . code ) {
123
- this . updateCodeTS ( editorText ) ;
124
- }
125
- }
126
- clearInterval ( this . timer ! ) ;
127
- this . resetTimer ( ) ;
128
- }
129
- }
130
- } ;
131
-
132
- private resetTimer = ( ) => {
133
- this . setState ( {
134
- currentTime : 0
135
- } ) ;
136
- this . timer = setInterval ( ( ) => {
137
- this . decrementTimeRemaining ( ) ;
138
- } , 1000 ) ;
139
- } ;
140
-
141
- public componentDidMount ( ) {
142
- this . resetTimer ( ) ;
143
- //this.createEditor();
144
- this . updateCodeTS ( initialCode ) ;
145
- }
146
-
147
- public componentDidUpdate ( prevProps : { } , prevState : IAppState ) {
148
- if ( prevState . code != this . state . code ) {
149
- this . _eval ( ) ;
150
- }
151
- }
152
-
153
- private updateCodeTS = ( code : string ) => {
154
- try {
155
- const compilerOptions = { module : ts . ModuleKind . None } ;
156
- const transpiled = ts . transpileModule ( code , {
157
- compilerOptions : compilerOptions ,
158
- moduleName : "myMod"
159
- } ) ;
160
- this . setState ( {
161
- code : code ,
162
- JScode : transpiled ,
163
- error : undefined
164
- } ) ;
165
- console . log ( transpiled . outputText ) ;
166
- } catch ( ex ) {
167
- this . setState ( {
168
- code : code ,
169
- error : ex . message
170
- } ) ;
171
- }
172
- } ;
173
-
174
- private _eval = ( ) => {
175
- try {
176
- eval ( this . state . JScode ) ;
177
- this . setState ( { error : undefined } ) ;
178
- } catch ( ex ) {
179
- this . setState ( { error : ex . message } ) ;
180
- }
181
- } ;
182
-
183
- render ( ) {
184
- let dropdown = (
185
- < Stack horizontal padding = { 10 } gap = { 10 } >
186
- < Stack . Item >
187
- < Label > Select code font size:</ Label >
188
- </ Stack . Item >
189
- < Stack . Item >
190
- < Dropdown
191
- options = { this . state . options }
192
- defaultSelectedKey = "18"
193
- onChange = { this . changeFontSize }
194
- styles = { { dropdown : { width : 100 } } }
195
- />
196
- </ Stack . Item >
197
- </ Stack >
198
- ) ;
199
-
200
- let TSeditor = (
201
- < div >
202
- < div >
203
- < Label > Typescript + React editor</ Label >
204
- </ div >
205
- { /* <div className={classNames.editor} id="editor" hidden = {this.state.editorHidden}/> */ }
206
- < React . Suspense fallback = { < div > Loading...</ div > } >
207
- { /* <Editor
208
- width = {800}
209
- height = {500}
210
- code = ''
211
- language = "typescript"
212
- /> */ }
213
- </ React . Suspense >
214
- </ div >
215
- ) ;
216
-
217
- let editor = (
218
- < Stack style = { { backgroundColor : "lightgray" } } gap = { 4 } >
219
- < Stack . Item > { dropdown } </ Stack . Item >
220
- < Stack . Item > { TSeditor } </ Stack . Item >
221
- < Stack . Item >
222
- { this . state . error !== undefined && (
223
- < Label className = { classNames . error } > `{ this . state . error } `</ Label >
224
- ) }
225
- </ Stack . Item >
43
+ public render ( ) {
44
+ const editor = (
45
+ < Stack style = { { backgroundColor : 'lightgray' } } gap = { 4 } >
46
+ { ! this . state . editorHidden && this . state . editor }
47
+ { this . state . error !== undefined && < Label className = { classNames . error } > `{ this . state . error } `</ Label > }
226
48
</ Stack >
227
49
) ;
228
50
229
51
return (
230
52
< div >
231
- { < PrimaryButton onClick = { this . buttonClicked } /> }
232
- { /* {!this.state.editorHidden && editor} */ }
233
- { this . state . editor }
53
+ < PrimaryButton onClick = { this . buttonClicked } />
54
+ { ! this . state . editorHidden && editor }
234
55
</ div >
235
56
) ;
236
57
}
58
+
59
+ private onChange = ( newVal : string ) => {
60
+ console . log ( 'on change test' , newVal ) ;
61
+ } ;
62
+
63
+ private buttonClicked = ( ) : void => {
64
+ if ( this . state . editorHidden ) {
65
+ // require.ensure([], require => {
66
+ // const Editor = require('../components/Editor').Editor;
67
+ // this.setState({
68
+ // editor: (
69
+ // <div>
70
+ // <div>
71
+ // <Label>Typescript + React editor</Label>
72
+ // </div>
73
+ // <React.Suspense fallback={<div>Loading...</div>}>
74
+ // <Editor width={800} height={500} code="dfgxsfgfsgfgssfgs" language="typescript" onChange={this.onChange} />
75
+ // </React.Suspense>
76
+ // </div>
77
+ // ),
78
+ // editorHidden: false
79
+ // });
80
+ // });
81
+ } else {
82
+ this . setState ( { editor : null , editorHidden : true } ) ;
83
+ }
84
+ } ;
237
85
}
0 commit comments