Merge pull request #824 from hgiesel/mathjax3plus

Improvements to MathJax and ts hooks
This commit is contained in:
Damien Elmes 2020-11-16 09:19:23 +10:00 committed by GitHub
commit 54eb554393
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 48 deletions

View File

@ -1,22 +1,28 @@
window.MathJax = { window.MathJax = {
tex: { tex: {
displayMath: [["\\[", "\\]"]], displayMath: [["\\[", "\\]"]],
processRefs: false, processRefs: false,
processEnvironments: false, processEnvironments: false,
packages: ['base', 'ams', 'noerrors', 'noundefined', 'mhchem'] packages: {
}, "[+]": ["noerrors", "mhchem"],
startup: { },
typeset: false },
}, startup: {
options: { typeset: false,
renderActions: { pageReady: () => {
addMenu: [], console.log("page is ready");
checkLoading: [] return MathJax.startup.defaultPageReady();
},
},
options: {
renderActions: {
addMenu: [],
checkLoading: [],
},
ignoreHtmlClass: "tex2jax_ignore",
processHtmlClass: "tex2jax_process",
},
loader: {
load: ["[tex]/noerrors", "[tex]/mhchem"],
}, },
ignoreHtmlClass: 'tex2jax_ignore',
processHtmlClass: 'tex2jax_process'
},
loader: {
load: ['[tex]/noerrors', '[tex]/mhchem']
}
}; };

View File

@ -11,10 +11,14 @@ var aFade = 0;
var onUpdateHook; var onUpdateHook;
var onShownHook; var onShownHook;
function _runHook(arr) { function _runHook(arr: () => Promise<any>[]): Promise<any[]> {
var promises = [];
for (var i = 0; i < arr.length; i++) { for (var i = 0; i < arr.length; i++) {
arr[i](); promises.push(arr[i]());
} }
return Promise.all(promises);
} }
function _updateQA(html, fadeTime, onupdate, onshown) { function _updateQA(html, fadeTime, onupdate, onshown) {
@ -32,35 +36,39 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
onUpdateHook = [onupdate]; onUpdateHook = [onupdate];
onShownHook = [onshown]; onShownHook = [onshown];
// fade out current text
var qa = $("#qa"); var qa = $("#qa");
qa.fadeTo(fadeTime, 0, function () {
// update text
try {
qa.html(html);
} catch (err) {
qa.html(
(
`Invalid HTML on card: ${String(err).substring(0, 2000)}\n` +
String(err.stack).substring(0, 2000)
).replace(/\n/g, "<br />")
);
}
_runHook(onUpdateHook);
// @ts-ignore // fade out current text
MathJax.startup.promise qa.fadeOut(fadeTime)
// render mathjax .promise()
// @ts-ignore // update text
.then(MathJax.typesetPromise) .then(() => {
// and reveal when processing is done try {
.then(function () { qa.html(html);
qa.fadeTo(fadeTime, 1, function () { } catch (err) {
_runHook(onShownHook); qa.html(
_updatingQA = false; (
}); `Invalid HTML on card: ${String(err).substring(0, 2000)}\n` +
}); String(err.stack).substring(0, 2000)
}); ).replace(/\n/g, "<br />")
);
}
})
.then(() => _runHook(onUpdateHook))
.then(() =>
// @ts-ignore wait for mathjax to ready
MathJax.startup.promise.then(() => {
// @ts-ignore clear MathJax buffer
MathJax.typesetClear();
// @ts-ignore typeset
return MathJax.typesetPromise(qa.slice(0, 1));
})
)
// and reveal when processing is done
.then(() => qa.fadeIn(fadeTime).promise())
.then(() => _runHook(onShownHook))
.then(() => (_updatingQA = false));
} }
function _showQuestion(q, bodyclass) { function _showQuestion(q, bodyclass) {