113 lines
3.1 KiB
Plaintext
Raw Normal View History

2017-07-06 16:50:38 -04:00
import { withPluginApi } from 'discourse/lib/plugin-api';
import loadScript from 'discourse/lib/load-script';
let initializedMathJax = false;
function initMathJax(opts) {
2017-07-06 16:50:38 -04:00
if (initializedMathJax) { return; }
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 = {
2017-07-06 16:50:38 -04: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: '/plugins/discourse-math/mathjax'
2017-08-02 18:18:43 +09: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;
}
function ensureMathJax(opts){
initMathJax(opts);
2017-07-06 16:50:38 -04:00
return loadScript('/plugins/discourse-math/mathjax/MathJax.1.7.1.js');
}
function decorate(elem, isPreview){
2017-07-06 16:50:38 -04:00
const $elem= $(elem);
2017-07-06 17:15:37 -04:00
if ($elem.data('applied-mathjax')){
return;
}
$elem.data('applied-mathjax', true);
if($elem.hasClass('math')) {
const tag = elem.tagName === "DIV" ? "div" : "span";
const display = tag === "div" ? "; mode=display" : "";
var $mathWrapper = $(`<${tag} style="display: none;"><script type="math/tex${display}"></script></${tag}>`);
var $math = $mathWrapper.children();
$math.html($elem.text());
$elem.after($mathWrapper);
}
else if($elem.hasClass('asciimath')) {
var $mathWrapper = $(`<span style="display: none;"><script type="math/asciimath"></script></span>`);
var $math = $mathWrapper.children();
$math.html($elem.text());
$elem.after($mathWrapper);
}
2017-07-06 16:50:38 -04:00
Em.run.later(this, ()=> {
window.MathJax.Hub.Queue(() => {
// don't bother processing previews removed from DOM
2017-07-06 17:15:37 -04:00
if (elem.parentElement && elem.parentElement.offsetParent !== null) {
2017-07-06 16:50:38 -04:00
window.MathJax.Hub.Typeset($math[0], ()=> {
$elem.remove();
$mathWrapper.show();
});
}
});
}, isPreview ? 200 : 0);
}
function mathjax($elem, opts) {
2017-11-20 15:06:24 +11:00
if (!$elem || !$elem.find) {
return;
}
let mathElems;
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) {
const isPreview = $elem.hasClass('d-editor-preview');
2017-07-06 17:15:37 -04:00
ensureMathJax(opts).then(()=>{
2017-07-06 16:50:38 -04:00
mathElems.each((idx,elem) => decorate(elem, isPreview));
});
}
}
function initializeMath(api, discourse_math_opts) {
api.decorateCooked(function(elem) {mathjax(elem,discourse_math_opts)});
2017-07-06 16:50:38 -04:00
}
export default {
name: "apply-math",
initialize(container) {
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
};
2017-07-06 16:50:38 -04:00
if (siteSettings.discourse_math_enabled) {
withPluginApi('0.5', function(api) {initializeMath(api,discourse_math_opts)});
2017-07-06 16:50:38 -04:00
}
}
};