From 80232f4d332376bdc97c1d21bec29c400561df05 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sun, 15 Nov 2020 13:58:51 +0100 Subject: [PATCH 1/4] Load require package in Mathjax --- qt/aqt/data/web/js/mathjax.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/qt/aqt/data/web/js/mathjax.js b/qt/aqt/data/web/js/mathjax.js index adce72648..c97f7b8a1 100644 --- a/qt/aqt/data/web/js/mathjax.js +++ b/qt/aqt/data/web/js/mathjax.js @@ -3,7 +3,14 @@ window.MathJax = { displayMath: [["\\[", "\\]"]], processRefs: false, processEnvironments: false, - packages: ['base', 'ams', 'noerrors', 'noundefined', 'mhchem'] + packages: [ + 'base', + 'ams', + 'noerrors', + 'noundefined', + 'mhchem', + 'require', + ] }, startup: { typeset: false @@ -17,6 +24,10 @@ window.MathJax = { processHtmlClass: 'tex2jax_process' }, loader: { - load: ['[tex]/noerrors', '[tex]/mhchem'] + load: [ + '[tex]/noerrors', + '[tex]/mhchem', + '[tex]/require', + ] } }; From 4229f2108449bd49ccbb4dd0f2b7a27a79e59d1f Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sun, 15 Nov 2020 20:56:50 +0100 Subject: [PATCH 2/4] Include default MathJax packages * by default load all the default tex-chtml packages, which additionally include: * require: using \require{package-name} to load a package * autoload: using a command from a different package automatically loads it * configmacros: allows for definition of predefined macros --- qt/aqt/data/web/js/mathjax.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/qt/aqt/data/web/js/mathjax.js b/qt/aqt/data/web/js/mathjax.js index c97f7b8a1..6cae47bdb 100644 --- a/qt/aqt/data/web/js/mathjax.js +++ b/qt/aqt/data/web/js/mathjax.js @@ -3,17 +3,19 @@ window.MathJax = { displayMath: [["\\[", "\\]"]], processRefs: false, processEnvironments: false, - packages: [ - 'base', - 'ams', - 'noerrors', - 'noundefined', - 'mhchem', - 'require', - ] + packages: { + '[+]': [ + 'noerrors', + 'mhchem', + ], + } }, startup: { - typeset: false + typeset: false, + pageReady: () => { + console.log('page is ready'); + return MathJax.startup.defaultPageReady(); + }, }, options: { renderActions: { @@ -21,13 +23,12 @@ window.MathJax = { checkLoading: [] }, ignoreHtmlClass: 'tex2jax_ignore', - processHtmlClass: 'tex2jax_process' + processHtmlClass: 'tex2jax_process', }, loader: { load: [ '[tex]/noerrors', '[tex]/mhchem', - '[tex]/require', ] } }; From a7b5a7efe1b4b1aa01a2a7cc0d39f54010074bcd Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sun, 15 Nov 2020 20:57:07 +0100 Subject: [PATCH 3/4] Improve ts hooks to allow for asynchronous actions * reviewer ts hooks may now return a promise, which are then waited upon with Promise.all * this does not break old ts hooks, as Promise.all allows for non-Promises in its array --- qt/aqt/data/web/js/reviewer.ts | 65 +++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index d58b0a55e..1db7d4dfa 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -11,10 +11,14 @@ var aFade = 0; var onUpdateHook; var onShownHook; -function _runHook(arr) { +function _runHook(arr: () => Promise[]): Promise { + var promises = []; + for (var i = 0; i < arr.length; i++) { - arr[i](); + promises.push(arr[i]()); } + + return Promise.all(promises); } function _updateQA(html, fadeTime, onupdate, onshown) { @@ -32,35 +36,38 @@ function _updateQA(html, fadeTime, onupdate, onshown) { onUpdateHook = [onupdate]; onShownHook = [onshown]; - // fade out current text 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, "
") - ); - } - _runHook(onUpdateHook); - // @ts-ignore - MathJax.startup.promise - // render mathjax - // @ts-ignore - .then(MathJax.typesetPromise) - // and reveal when processing is done - .then(function () { - qa.fadeTo(fadeTime, 1, function () { - _runHook(onShownHook); - _updatingQA = false; - }); - }); - }); + // fade out current text + qa.fadeOut(fadeTime).promise() + // update text + .then(() => { + 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, "
") + ); + } + }) + .then(() => _runHook(onUpdateHook)) + // @ts-ignore wait for mathjax to ready + .then(() => 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) { From 435ecc3ae88d9ba755539ef8a58fb400f1dfade0 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sun, 15 Nov 2020 21:21:04 +0100 Subject: [PATCH 4/4] Satisfy formatter --- qt/aqt/data/web/js/mathjax.js | 54 +++++++++++++++------------------- qt/aqt/data/web/js/reviewer.ts | 19 ++++++------ 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/qt/aqt/data/web/js/mathjax.js b/qt/aqt/data/web/js/mathjax.js index 6cae47bdb..2074174f3 100644 --- a/qt/aqt/data/web/js/mathjax.js +++ b/qt/aqt/data/web/js/mathjax.js @@ -1,34 +1,28 @@ window.MathJax = { - tex: { - displayMath: [["\\[", "\\]"]], - processRefs: false, - processEnvironments: false, - packages: { - '[+]': [ - 'noerrors', - 'mhchem', - ], - } - }, - startup: { - typeset: false, - pageReady: () => { - console.log('page is ready'); - return MathJax.startup.defaultPageReady(); + tex: { + displayMath: [["\\[", "\\]"]], + processRefs: false, + processEnvironments: false, + packages: { + "[+]": ["noerrors", "mhchem"], + }, }, - }, - options: { - renderActions: { - addMenu: [], - checkLoading: [] + startup: { + typeset: false, + pageReady: () => { + console.log("page is ready"); + 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', - ] - } }; diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index 1db7d4dfa..2dec1632c 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -39,13 +39,13 @@ function _updateQA(html, fadeTime, onupdate, onshown) { var qa = $("#qa"); // fade out current text - qa.fadeOut(fadeTime).promise() + qa.fadeOut(fadeTime) + .promise() // update text .then(() => { try { - qa.html(html) - } - catch (err) { + qa.html(html); + } catch (err) { qa.html( ( `Invalid HTML on card: ${String(err).substring(0, 2000)}\n` + @@ -55,19 +55,20 @@ function _updateQA(html, fadeTime, onupdate, onshown) { } }) .then(() => _runHook(onUpdateHook)) - // @ts-ignore wait for mathjax to ready - .then(() => MathJax.startup.promise - .then(() => { + .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); + .then(() => (_updatingQA = false)); } function _showQuestion(q, bodyclass) {