-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath-tex-controller.js
107 lines (97 loc) · 3.54 KB
/
math-tex-controller.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
(function (global) {
'use strict';
const document = global.document,
states = {start: 1, loading: 2, ready: 3, typesetting: 4, error: 5};
let mathjaxHub,
typesets = [],
state = states.start,
styleNode,
src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js';
function getStyleNode() {
const styleNodes = document.querySelectorAll('style');
const sn = Array.prototype.filter.call(styleNodes, function(n) {
return n.sheet && n.sheet.cssRules.length > 100
&& n.sheet.cssRules[0].selectorText === '.mjx-chtml';
});
styleNode = sn[0];
}
// precondition: state === states.ready
function flush_typesets() {
if (!typesets.length) return;
const jaxs = [], items = [];
typesets.forEach(function(item) {
const script = document.createElement('script'),
div = document.createElement('div');
script.type = item[1] ? 'math/tex; mode=display' : 'math/tex';
script.text = item[0];
div.style.position = 'fixed';
div.style.top = 0;
div.style.left = '99999px';
div.appendChild(script);
document.body.appendChild(div);
jaxs.push(script);
items.push([div, item[2]]);
});
typesets = [];
state = states.typesetting;
mathjaxHub.Queue(['Typeset', mathjaxHub, jaxs]);
mathjaxHub.Queue(function() {
if (!styleNode)
getStyleNode();
items.forEach(function(item) {
const div = item[0];
const result = div.firstElementChild.tagName === 'SPAN' ? div.firstElementChild : null;
item[1](result, styleNode);
document.body.removeChild(div);
});
state = states.ready;
flush_typesets();
});
}
function load_library() {
state = states.loading;
global.MathJax = {
skipStartupTypeset: true,
showMathMenu: false,
jax: ['input/TeX', 'output/CommonHTML'],
TeX: {
extensions: ['AMSmath.js', 'AMSsymbols.js', 'noErrors.js', 'noUndefined.js']
},
AuthorInit: function () {
mathjaxHub = global.MathJax.Hub;
mathjaxHub.Register.StartupHook('End', function() {
state = states.ready;
flush_typesets();
});
}
};
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
script.async = true;
script.onerror = function () {
console.warn('Error loading MathJax library ' + src);
state = states.error;
typesets = [];
};
document.head.appendChild(script);
}
class MathTexController extends HTMLElement {
connectedCallback() {
if (this.hasAttribute('src'))
src = this.getAttribute('src');
if (!this.hasAttribute('lazy'))
load_library();
}
typeset(math, displayMode, cb) {
if (state === states.error)
return;
typesets.push([math, displayMode, cb]);
if (state === states.start)
load_library();
else if (state === states.ready)
flush_typesets();
}
}
global.customElements.define('math-tex-controller', MathTexController);
})(window);