-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
242 lines (221 loc) · 7.02 KB
/
script.js
File metadata and controls
242 lines (221 loc) · 7.02 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2022 Mitchell Kember. Subject to the MIT License.
window.addEventListener("load", () => {
// Describes which cards get derived from which other cards, by HTML ID.
const derivations = {
asciimath: [],
tex: ["asciimath"],
mathml: ["tex"],
mathjaxHtmlCss: ["tex"],
katexHtmlCss: ["tex"],
nativeMathml: ["mathml"],
};
const graph = {};
let lastUpdateFn = () => { };
// Populate nodes in the card graph.
for (const [id, incoming] of Object.entries(derivations)) {
const element = document.getElementById(id);
let card = element;
while (!card.classList.contains("card")) {
card = card.parentElement;
}
const accessors = {};
if (element.tagName === "TEXTAREA") {
accessors.get = () => element.value;
accessors.set = (value) => (element.value = value);
} else {
accessors.get = () => element.innerHTML;
accessors.setHTML = (html) => (element.innerHTML = html);
accessors.setChild = (child) => {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
element.appendChild(child);
};
}
graph[id] = {
id,
element,
card,
incoming,
outgoing: [],
...accessors,
};
}
// Populate outgoing edges in the card graph.
for (const [id, incoming] of Object.entries(derivations)) {
for (const other of incoming) {
graph[other].outgoing.push(id);
}
}
// Depth-first search helper.
function dfs(x, prop, f) {
for (const id of x[prop]) {
const y = graph[id];
f(x, y);
dfs(y, prop, f);
}
}
// Helpers for marking cards as stale.
function markStale(node) {
node.card.classList.add("card--stale");
}
function markFresh(node) {
node.card.classList.remove("card--stale");
}
// Set up dropdown for choosing a sample equation.
const sampleSelect = document.getElementById("sampleSelect");
function onSampleSelectInput() {
graph.asciimath.set(sampleSelect.value);
graph.asciimath.update();
}
sampleSelect.addEventListener("input", onSampleSelectInput);
// Add event listeners for reactive updates.
for (const node of Object.values(graph)) {
if (!node.outgoing) {
continue;
}
node.update = () => {
dfs(node, "incoming", (_, y) => markStale(y));
markFresh(node);
dfs(node, "outgoing", (x, y) => {
markFresh(y);
propagate(x, y);
});
lastUpdateFn = node.update;
};
node.element.addEventListener("input", () => {
sampleSelect.value = "";
node.update();
});
}
const asciiMathParser = new AsciiMathParser();
// Set up checkbox for toggling display math mode.
const displayModeCheckbox = document.getElementById("displayMode");
displayModeCheckbox.addEventListener("input", () => lastUpdateFn());
// Set up radio buttons for controlling the TeX to MathML conversion.
const mathmlRadioButtons = Array.from(
document.querySelectorAll('input[name="mathmlGenerator"]')
);
let activeMathmlRadioButtton = mathmlRadioButtons.find((b) => b.checked);
for (const button of mathmlRadioButtons) {
button.addEventListener("input", () => {
activeMathmlRadioButtton = mathmlRadioButtons.find((b) => b.checked);
if (lastUpdateFn == graph.asciimath.update) {
lastUpdateFn();
} else {
graph.tex.update();
}
});
}
// Returns the MathML generator to use ("mathjax" or "katex").
function mathmlGenerator() {
activeMathmlRadioButtton.checked = true;
return activeMathmlRadioButtton.value;
}
// Special case for MathML converting to HTML+CSS.
const updateNativeMathml = graph.mathml.update;
graph.mathml.update = () => {
activeMathmlRadioButtton.checked = false;
markStale(graph.katexHtmlCss);
updateNativeMathml();
propagate(graph.mathml, graph.mathjaxHtmlCss);
};
// Add a <style> tag for injecting MathJax styles.
const mathjaxStyle = document.createElement("style");
document.head.appendChild(mathjaxStyle);
// Returns options to use for MathJax rendering in the given graph node.
function mathjaxOptions(node) {
return {
...MathJax.getMetricsFor(node.element),
display: displayModeCheckbox.checked,
};
}
// Returns options to use for KaTeX rendering to the given output format.
function katexOptions(output) {
return {
output,
displayMode: displayModeCheckbox.checked,
throwOnError: false,
};
}
// Set up dropdown for choosing native MathML font.
const mathmlFontLink = document.createElement("link");
mathmlFontLink.setAttribute("rel", "stylesheet");
document.head.appendChild(mathmlFontLink);
document.getElementById("mathmlFontSelect").addEventListener("input", function () {
if (this.value === "") {
mathmlFontLink.removeAttribute("href");
} else {
const name = this.value.replaceAll(" ", "");
mathmlFontLink.setAttribute("href",
`https://fred-wang.github.io/MathFonts/${name}/mathfonts.css`);
}
});
// Updates graph node y based on graph node x.
function propagate(x, y) {
switch (x.id) {
case "asciimath":
switch (y.id) {
case "tex":
y.set(asciiMathParser.parse(x.get()));
break;
}
break;
case "tex":
switch (y.id) {
case "mathml":
switch (mathmlGenerator()) {
case "mathjax":
y.set(MathJax.tex2mml(x.get(), mathjaxOptions(y)));
break;
case "katex":
y.set(
katex
.renderToString(x.get(), katexOptions("mathml"))
.replace(/^<span class="katex">/, "")
.replace(/<\/span>$/, "")
);
break;
}
break;
case "mathjaxHtmlCss":
y.setChild(MathJax.tex2chtml(x.get(), mathjaxOptions(y)));
mathjaxStyle.replaceWith(MathJax.chtmlStylesheet());
break;
case "katexHtmlCss":
katex.render(x.get(), y.element, katexOptions("html"));
break;
}
break;
case "mathml":
switch (y.id) {
case "mathjaxHtmlCss":
y.setChild(MathJax.mathml2chtml(x.get(), mathjaxOptions(y)));
mathjaxStyle.replaceWith(MathJax.chtmlStylesheet());
break;
case "nativeMathml":
y.setHTML(x.get());
break;
}
break;
}
}
// Set up slider to change the size of rendered math.
const sizeStyle = document.createElement("style");
document.head.appendChild(sizeStyle);
document.getElementById("sizeSlider").addEventListener("input", function () {
const value = 1.5 + 0.75 * this.value;
sizeStyle.innerHTML = `
.card-value--rendered {
font-size: ${value}em;
}
`;
});
// Allow cards to be dragged around.
Sortable.create(document.querySelector(".card-grid"), {
animation: 200,
handle: ".card-handle",
});
// Start with a sample equation.
onSampleSelectInput();
});