discourse-math/assets/javascripts/initializers/discourse-math-mathjax.js.es6

146 lines
3.7 KiB
Plaintext
Raw Normal View History

2018-08-08 11:58:37 +10:00
import { withPluginApi } from "discourse/lib/plugin-api";
import loadScript from "discourse/lib/load-script";
2017-07-06 16:50:38 -04:00
let initializedMathJax = false;
function initMathJax(opts) {
2018-08-08 11:58:37 +10:00
if (initializedMathJax) {
return;
}
2017-07-06 16:50:38 -04:00
var extensions = ["toMathML.js", "Safe.js"];
2017-11-20 15:06:24 +11:00
if (opts.enable_accessibility) {
2017-08-02 18:18:43 +09:00
extensions.push("[a11y]/accessibility-menu.js");
}
2017-08-02 18:18:43 +09:00
var settings = {
2018-08-08 11:58:37 +10:00
jax: ["input/TeX", "input/AsciiMath", "input/MathML", "output/CommonHTML"],
TeX: { extensions: ["AMSmath.js", "AMSsymbols.js", "autoload-all.js"] },
extensions: extensions,
2017-07-06 16:50:38 -04:00
showProcessingMessages: false,
root: Discourse.getURLWithCDN("/plugins/discourse-math/mathjax")
2017-08-02 18:18:43 +09:00
};
2018-08-08 11:58:37 +10:00
if (opts.zoom_on_hover) {
settings.menuSettings = { zoom: "Hover" };
settings.MathEvents = { hover: 750 };
}
window.MathJax = settings;
2017-07-06 16:50:38 -04:00
initializedMathJax = true;
}
2018-08-08 11:58:37 +10:00
function ensureMathJax(opts) {
initMathJax(opts);
2018-08-08 11:58:37 +10:00
return loadScript("/plugins/discourse-math/mathjax/MathJax.2.7.5.js");
2017-07-06 16:50:38 -04:00
}
2018-08-08 11:58:37 +10:00
function decorate(elem, isPreview) {
const $elem = $(elem);
2018-08-08 11:58:37 +10:00
if ($elem.data("applied-mathjax")) {
2017-07-06 17:15:37 -04:00
return;
}
2018-08-08 11:58:37 +10:00
$elem.data("applied-mathjax", true);
let $mathWrapper, $math;
2017-07-06 17:15:37 -04:00
2018-08-08 11:58:37 +10:00
if ($elem.hasClass("math")) {
const tag = elem.tagName === "DIV" ? "div" : "span";
const display = tag === "div" ? "; mode=display" : "";
const displayClass = tag === "div" ? "block-math" : "inline-math";
const type = `math/tex${display}`;
const classList = `math-container ${displayClass} mathjax-math`;
2018-08-08 11:58:37 +10:00
$mathWrapper = $(
`<${tag} class="${classList}" style="display: none;">
<script type="${type}"></script>
</${tag}>`
2018-08-08 11:58:37 +10:00
);
2018-08-08 11:58:37 +10:00
$math = $mathWrapper.children();
$math.text($elem.text());
$elem.after($mathWrapper);
2018-08-08 11:58:37 +10:00
} else if ($elem.hasClass("asciimath")) {
// asciimath is always inline
const classList = `math-container inline-math ascii-math`;
const type = `math/asciimath`;
2018-08-08 11:58:37 +10:00
$mathWrapper = $(
`<span class="${classList}" style="display: none;">
<script type="${type}"></script>
</span>`
2018-08-08 11:58:37 +10:00
);
2018-08-08 11:58:37 +10:00
$math = $mathWrapper.children();
$math.text($elem.text());
$elem.after($mathWrapper);
}
2017-07-06 16:50:38 -04:00
2019-06-14 12:47:22 -04:00
Ember.run.later(
2018-08-08 11:58:37 +10:00
this,
() => {
window.MathJax.Hub.Queue(() => {
// don't bother processing previews removed from DOM
if (elem.parentElement && elem.parentElement.offsetParent !== null) {
window.MathJax.Hub.Typeset($math[0], () => {
$elem.remove();
$mathWrapper.show();
});
}
});
},
isPreview ? 200 : 0
);
2017-07-06 16:50:38 -04:00
}
function mathjax($elem, opts) {
2017-11-20 15:06:24 +11:00
if (!$elem || !$elem.find) {
return;
}
let mathElems;
2018-08-08 11:58:37 +10:00
if (opts.enable_asciimath) {
mathElems = $elem.find(".math, .asciimath");
} else {
mathElems = $elem.find(".math");
}
2017-07-06 16:50:38 -04:00
if (mathElems.length > 0) {
2018-08-08 11:58:37 +10:00
const isPreview = $elem.hasClass("d-editor-preview");
2017-07-06 17:15:37 -04:00
2018-08-08 11:58:37 +10:00
ensureMathJax(opts).then(() => {
mathElems.each((idx, elem) => decorate(elem, isPreview));
2017-07-06 16:50:38 -04:00
});
}
}
function initializeMath(api, discourse_math_opts) {
2019-06-20 10:14:57 -04:00
api.decorateCooked(
function(elem) {
mathjax(elem, discourse_math_opts);
},
{ id: "mathjax" }
);
2017-07-06 16:50:38 -04:00
}
export default {
name: "apply-math-mathjax",
2017-07-06 16:50:38 -04:00
initialize(container) {
2018-08-08 11:58:37 +10:00
const siteSettings = container.lookup("site-settings:main");
let discourse_math_opts = {
zoom_on_hover: siteSettings.discourse_math_zoom_on_hover,
enable_accessibility: siteSettings.discourse_math_enable_accessibility,
enable_asciimath: siteSettings.discourse_math_enable_asciimath
};
2019-06-14 13:28:18 -04:00
if (
siteSettings.discourse_math_enabled &&
siteSettings.discourse_math_provider === "mathjax"
) {
2018-08-08 11:58:37 +10:00
withPluginApi("0.5", function(api) {
initializeMath(api, discourse_math_opts);
});
2017-07-06 16:50:38 -04:00
}
}
};