Skip to content

Commit a818aca

Browse files
author
Mohsen Azimi
committed
side-by-side
1 parent 6bde8fa commit a818aca

File tree

3 files changed

+63
-30
lines changed

3 files changed

+63
-30
lines changed

docs/editor.tsx

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,65 @@ import { render } from 'react-dom';
33
import MonacoEditor from 'react-monaco-editor';
44

55
interface AppState {
6-
code: string;
6+
input: string;
7+
output: string;
8+
windowSize: { width: number; height: number; };
79
}
810

911
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+
}
3556
}
3657

58+
const Toolbar = ({height}: {height: number}) => (
59+
<div style={{height: `${height}px`}}>
60+
Convert React code to TypeScript
61+
</div>
62+
)
63+
3764
render(
38-
<App />,
39-
document.getElementById('root')
65+
<App />,
66+
document.getElementById('root')
4067
);

docs/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
<head>
44
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
55
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
6+
<style>
7+
html, body, #root {
8+
margin: 0;
9+
height: 100%;
10+
}
11+
</style>
612
</head>
713
<body>
814
<div id="root"></div>
9-
<script src="dist/editor.js"></script>
1015
</body>
1116
</html>

docs/webpack.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const config: webpack.Configuration = {
2020
},
2121
plugins: [
2222
new HTMLWebpackPlugin({
23-
template: './index.html'
23+
template: './index.html',
24+
inject: 'body'
2425
}),
2526
new CopyWebpackPlugin([
2627
{

0 commit comments

Comments
 (0)