-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
app.js
62 lines (57 loc) · 1.35 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React from 'react';
import Editor from './editor';
import Renderer from './renderer';
import compile from 'idyll-compiler';
import { hashCode } from './utils';
import './app.css';
import initialValue from './initial.idl';
class App extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
idyllMarkup: initialValue,
idyllHash: '',
error: null,
ast: compile(initialValue)
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(value) {
const hash = hashCode(value.trim());
if (hash === this.state.hashCode) {
return;
}
try {
const ast = compile(value);
this.setState({
idyllHash: hash,
idyllMarkup: value,
ast: ast,
error: null
})
} catch(e) {
this.setState({
error: e.message
})
}
}
render() {
const { idyllMarkup, ast, error, idyllHash } = this.state;
return (
<div className={"container"}>
<Editor initialValue={idyllMarkup} onChange={this.handleChange} />
<Renderer ast={ast} idyllMarkup={idyllMarkup} idyllHash={idyllHash} />
{
error && (
<div className={'error-display'}>
<pre>
{error}
</pre>
</div>
)
}
</div>
)
}
}
export default App;