This repository has been archived by the owner on May 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
tex.jsx
220 lines (194 loc) · 6.22 KB
/
tex.jsx
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"use strict";
/**
* For math rendered using KaTex and/or MathJax. Use me like <TeX>2x + 3</TeX>.
*/
/* global katex, MathJax, Khan */
// TODO(joel) - require MathJax / katex so they don't have to be global
const PureRenderMixin = require('react-addons-pure-render-mixin');
const React = require('react');
const ReactDOM = require('react-dom');
const katexA11y = require('./katex-a11y.js');
let pendingScripts = [];
let pendingCallbacks = [];
let needsProcess = false;
const process = (script, callback) => {
pendingScripts.push(script);
pendingCallbacks.push(callback);
if (!needsProcess) {
needsProcess = true;
setTimeout(doProcess, 0);
}
};
const loadMathJax = (callback) => {
if (typeof MathJax !== "undefined") {
callback();
} else if (typeof Khan !== "undefined" && Khan.mathJaxLoaded) {
Khan.mathJaxLoaded.then(callback);
} else {
throw new Error(
"MathJax wasn't loaded before it was needed by <TeX/>");
}
};
const doProcess = () => {
loadMathJax(() => {
MathJax.Hub.Queue(function() {
const oldElementScripts = MathJax.Hub.elementScripts;
MathJax.Hub.elementScripts = (element) => pendingScripts;
try {
return MathJax.Hub.Process(null, () => {
// Trigger all of the pending callbacks before clearing them
// out.
for (const callback of pendingCallbacks) {
callback();
}
pendingScripts = [];
pendingCallbacks = [];
needsProcess = false;
});
} catch (e) {
// IE8 requires `catch` in order to use `finally`
throw e;
} finally {
MathJax.Hub.elementScripts = oldElementScripts;
}
});
});
};
// Make content only visible to screen readers.
// Both collegeboard.org and Bootstrap 3 use this exact implementation.
const srOnly = {
border: 0,
clip: "rect(0,0,0,0)",
height: "1px",
margin: "-1px",
overflow: "hidden",
padding: 0,
position: "absolute",
width: "1px",
};
const TeX = React.createClass({
propTypes: {
children: React.PropTypes.node,
onClick: React.PropTypes.func,
onRender: React.PropTypes.func,
style: React.PropTypes.any,
},
mixins: [PureRenderMixin],
getDefaultProps: function() {
return {
// Called after math is rendered or re-rendered
onRender: function() {},
onClick: null,
};
},
componentDidMount: function() {
this._root = ReactDOM.findDOMNode(this);
if (this.refs.katex.childElementCount > 0) {
// If we already rendered katex in the render function, we don't
// need to render anything here.
this.props.onRender(this._root);
return;
}
const text = this.props.children;
this.setScriptText(text);
process(this.script, () => this.props.onRender(this._root));
},
componentDidUpdate: function(prevProps, prevState) {
// If we already rendered katex in the render function, we don't
// need to render anything here.
if (this.refs.katex.childElementCount > 0) {
if (this.script) {
// If we successfully rendered KaTeX, check if there's
// lingering MathJax from the last render, and if so remove it.
loadMathJax(() => {
const jax = MathJax.Hub.getJaxFor(this.script);
if (jax) {
jax.Remove();
}
});
}
this.props.onRender();
return;
}
const newText = this.props.children;
if (this.script) {
loadMathJax(() => {
MathJax.Hub.Queue(() => {
const jax = MathJax.Hub.getJaxFor(this.script);
if (jax) {
return jax.Text(newText, this.props.onRender);
} else {
this.setScriptText(newText);
process(this.script, this.props.onRender);
}
});
});
} else {
this.setScriptText(newText);
process(this.script, this.props.onRender);
}
},
componentWillUnmount: function() {
if (this.script) {
loadMathJax(() => {
const jax = MathJax.Hub.getJaxFor(this.script);
if (jax) {
jax.Remove();
}
});
}
},
setScriptText: function(text) {
if (!this.script) {
this.script = document.createElement("script");
this.script.type = "math/tex";
ReactDOM.findDOMNode(this.refs.mathjax).appendChild(this.script);
}
if ("text" in this.script) {
// IE8, etc
this.script.text = text;
} else {
this.script.textContent = text;
}
},
render: function() {
let katexHtml = null;
try {
katexHtml = {
__html: katex.renderToString(this.props.children),
};
} catch (e) {
/* jshint -W103 */
if (e.__proto__ !== katex.ParseError.prototype) {
/* jshint +W103 */
throw e;
}
}
let katexA11yHtml = null;
if (katexHtml) {
try {
katexA11yHtml = {
__html: katexA11y.renderString(this.props.children),
};
} catch (e) {
// Nothing
}
}
return <span
style={this.props.style}
onClick={this.props.onClick}
>
<span ref="mathjax" />
<span
ref="katex"
dangerouslySetInnerHTML={katexHtml}
aria-hidden={!!katexHtml && !!katexA11yHtml}
/>
<span
dangerouslySetInnerHTML={katexA11yHtml}
style={srOnly}
/>
</span>;
},
});
module.exports = TeX;