Skip to content

Commit

Permalink
Merge branch 'MDL-79039-master' of https://github.com/NashTechOpenUni…
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Aug 25, 2023
2 parents 928ea71 + 80c9de3 commit 3421925
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 432 deletions.
11 changes: 11 additions & 0 deletions filter/mathjaxloader/amd/build/loader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions filter/mathjaxloader/amd/build/loader.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions filter/mathjaxloader/amd/src/loader.js
@@ -0,0 +1,123 @@
// This file is part of Moodle - http://moodle.org/ //
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Mathjax JS Loader.
*
* @module filter_mathjaxloader
* @copyright 2014 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import {eventTypes} from 'core_filters/events';

/**
* The users current language - this can't be set until MathJax is loaded - so we need to store it.
* @property {string} lang
* @default ''
* @private
*/
let lang = '';

/**
* Used to prevent configuring MathJax twice.
* @property {boolean} configured
* @default false
* @private
*/
let configured = false;

/**
* Called by the filter when it is active on any page.
* This does not load MathJAX yet - it addes the configuration to the head incase it gets loaded later.
* It also subscribes to the filter-content-updated event so MathJax can respond to content loaded by Ajax.
*
* @method configure
* @param {Object} params List of configuration params containing mathjaxconfig (text) and lang
*/
export const configure = (params) => {
// Add a js configuration object to the head.
// See "http://docs.mathjax.org/en/latest/dynamic.html#ajax-mathjax"
let script = document.createElement("script");
script.type = "text/x-mathjax-config";
script[(window.opera ? "innerHTML" : "text")] = params.mathjaxconfig;
document.getElementsByTagName("head")[0].appendChild(script);

// Save the lang config until MathJax is actually loaded.
lang = params.lang;

// Listen for events triggered when new text is added to a page that needs
// processing by a filter.
document.addEventListener(eventTypes.filterContentUpdated, contentUpdated);
};

/**
* Set the correct language for the MathJax menus. Only do this once.
*
* @method setLocale
* @private
*/
const setLocale = () => {
if (!configured) {
if (typeof window.MathJax !== "undefined") {
window.MathJax.Hub.Queue(function() {
window.MathJax.Localization.setLocale(lang);
});
window.MathJax.Hub.Configured();
configured = true;
}
}
};

/**
* Called by the filter when an equation is found while rendering the page.
*
* @method typeset
*/
export const typeset = () => {
if (!configured) {
setLocale();
const elements = document.getElementsByClassName('filter_mathjaxloader_equation');
for (let i = 0; i < elements.length; i++) {
if (typeof window.MathJax !== "undefined") {
window.MathJax.Hub.Queue(["Typeset", window.MathJax.Hub, elements[i]]);
}
}
}
};

/**
* Handle content updated events - typeset the new content.
*
* @method contentUpdated
* @param {CustomEvent} event - Custom event with "nodes" indicating the root of the updated nodes.
*/
export const contentUpdated = (event) => {
if (typeof window.MathJax === "undefined") {
return;
}
const processdelay = window.MathJax.Hub.processSectionDelay;
// Set the process section delay to 0 when updating the formula.
window.MathJax.Hub.processSectionDelay = 0;
// When content is updated never position to hash, it may cause unexpected document scrolling.
window.MathJax.Hub.Config({positionToHash: false});
setLocale();
// The list of HTMLElements in an Array.
event.detail.nodes.forEach((node) => {
const mathjaxElements = node.querySelectorAll('.filter_mathjaxloader_equation');
mathjaxElements.forEach((node) => {
window.MathJax.Hub.Queue(["Typeset", window.MathJax.Hub, node]);
});
});
window.MathJax.Hub.processSectionDelay = processdelay;
};
11 changes: 3 additions & 8 deletions filter/mathjaxloader/filter.php
Expand Up @@ -85,12 +85,7 @@ public function setup($page, $context) {
$lang = $this->map_language_code(current_language());
$url = new moodle_url($url, array('delayStartupUntil' => 'configured'));

$moduleconfig = array(
'name' => 'mathjax',
'fullpath' => $url
);

$page->requires->js_module($moduleconfig);
$page->requires->js($url);

$config = get_config('filter_mathjaxloader', 'mathjaxconfig');
$wwwroot = new moodle_url('/');
Expand All @@ -99,7 +94,7 @@ public function setup($page, $context) {

$params = array('mathjaxconfig' => $config, 'lang' => $lang);

$page->requires->yui_module('moodle-filter_mathjaxloader-loader', 'M.filter_mathjaxloader.configure', array($params));
$page->requires->js_call_amd('filter_mathjaxloader/loader', 'configure', [$params]);
}
}

Expand Down Expand Up @@ -156,7 +151,7 @@ public function filter($text, array $options = array()) {
}

if ($hasdisplayorinline || $hasextra) {
$PAGE->requires->yui_module('moodle-filter_mathjaxloader-loader', 'M.filter_mathjaxloader.typeset');
$PAGE->requires->js_call_amd('filter_mathjaxloader/loader', 'typeset');
return '<span class="filter_mathjaxloader_equation">' . $text . '</span>';
}
return $text;
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 3421925

Please sign in to comment.