forked from facebookarchive/draft-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeXBlock.js
173 lines (152 loc) · 4.28 KB
/
TeXBlock.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Copyright (c) 2013-present, Facebook, Inc. All rights reserved.
*
* This file provided by Facebook is for non-commercial testing and evaluation
* purposes only. Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
'use strict';
import katex from 'katex';
import React from 'react';
class KatexOutput extends React.Component {
constructor(props) {
super(props);
this._timer = null;
}
_update() {
if (this._timer) {
clearTimeout(this._timer);
}
this._timer = setTimeout(() => {
katex.render(
this.props.content,
this.refs.container,
{displayMode: true}
);
}, 0);
}
componentDidMount() {
this._update();
}
componentWillReceiveProps(nextProps) {
if (nextProps.content !== this.props.content) {
this._update();
}
}
componentWillUnmount() {
clearTimeout(this._timer);
this._timer = null;
}
render() {
return <div ref="container" onClick={this.props.onClick} />;
}
}
export default class TeXBlock extends React.Component {
constructor(props) {
super(props);
this.state = {editMode: false};
this._onClick = () => {
if (this.state.editMode) {
return;
}
this.setState({
editMode: true,
texValue: this._getValue(),
}, () => {
this._startEdit();
});
};
this._onValueChange = evt => {
var value = evt.target.value;
var invalid = false;
try {
katex.__parse(value);
} catch (e) {
invalid = true;
} finally {
this.setState({
invalidTeX: invalid,
texValue: value,
});
}
};
this._save = () => {
var entityKey = this.props.block.getEntityAt(0);
var newContentState = this.props.contentState.mergeEntityData(entityKey, {content: this.state.texValue});
this.setState({
invalidTeX: false,
editMode: false,
texValue: null,
}, this._finishEdit.bind(this, newContentState));
};
this._remove = () => {
this.props.blockProps.onRemove(this.props.block.getKey());
};
this._startEdit = () => {
this.props.blockProps.onStartEdit(this.props.block.getKey());
};
this._finishEdit = (newContentState) => {
this.props.blockProps.onFinishEdit(this.props.block.getKey(), newContentState);
};
}
_getValue() {
return this.props.contentState
.getEntity(this.props.block.getEntityAt(0))
.getData()['content'];
}
render() {
var texContent = null;
if (this.state.editMode) {
if (this.state.invalidTeX) {
texContent = '';
} else {
texContent = this.state.texValue;
}
} else {
texContent = this._getValue();
}
var className = 'TeXEditor-tex';
if (this.state.editMode) {
className += ' TeXEditor-activeTeX';
}
var editPanel = null;
if (this.state.editMode) {
var buttonClass = 'TeXEditor-saveButton';
if (this.state.invalidTeX) {
buttonClass += ' TeXEditor-invalidButton';
}
editPanel =
<div className="TeXEditor-panel">
<textarea
className="TeXEditor-texValue"
onChange={this._onValueChange}
ref="textarea"
value={this.state.texValue}
/>
<div className="TeXEditor-buttons">
<button
className={buttonClass}
disabled={this.state.invalidTeX}
onClick={this._save}>
{this.state.invalidTeX ? 'Invalid TeX' : 'Done'}
</button>
<button className="TeXEditor-removeButton" onClick={this._remove}>
Remove
</button>
</div>
</div>;
}
return (
<div className={className}>
<KatexOutput content={texContent} onClick={this._onClick} />
{editPanel}
</div>
);
}
}