Menu.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __values = (this && this.__values) || function(o) {
  19. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  20. if (m) return m.call(o);
  21. if (o && typeof o.length === "number") return {
  22. next: function () {
  23. if (o && i >= o.length) o = void 0;
  24. return { value: o && o[i++], done: !o };
  25. }
  26. };
  27. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  28. };
  29. var __importDefault = (this && this.__importDefault) || function (mod) {
  30. return (mod && mod.__esModule) ? mod : { "default": mod };
  31. };
  32. Object.defineProperty(exports, "__esModule", { value: true });
  33. exports.Menu = void 0;
  34. var mathjax_js_1 = require("../../mathjax.js");
  35. var MathItem_js_1 = require("../../core/MathItem.js");
  36. var global_js_1 = require("../../components/global.js");
  37. var Options_js_1 = require("../../util/Options.js");
  38. var MJContextMenu_js_1 = require("./MJContextMenu.js");
  39. var MmlVisitor_js_1 = require("./MmlVisitor.js");
  40. var SelectableInfo_js_1 = require("./SelectableInfo.js");
  41. var info_js_1 = require("mj-context-menu/js/info.js");
  42. var parse_js_1 = require("mj-context-menu/js/parse.js");
  43. var item_rule_js_1 = require("mj-context-menu/js/item_rule.js");
  44. var css_util_js_1 = require("mj-context-menu/js/css_util.js");
  45. var sre_js_1 = __importDefault(require("../../a11y/sre.js"));
  46. var MathJax = global_js_1.MathJax;
  47. var isMac = (typeof window !== 'undefined' &&
  48. window.navigator && window.navigator.platform.substr(0, 3) === 'Mac');
  49. var Menu = (function () {
  50. function Menu(document, options) {
  51. if (options === void 0) { options = {}; }
  52. var _this = this;
  53. this.settings = null;
  54. this.defaultSettings = null;
  55. this.menu = null;
  56. this.MmlVisitor = new MmlVisitor_js_1.MmlVisitor();
  57. this.jax = {
  58. CHTML: null,
  59. SVG: null
  60. };
  61. this.rerenderStart = MathItem_js_1.STATE.LAST;
  62. this.about = new info_js_1.Info('<b style="font-size:120%;">MathJax</b> v' + mathjax_js_1.mathjax.version, function () {
  63. var lines = [];
  64. lines.push('Input Jax: ' + _this.document.inputJax.map(function (jax) { return jax.name; }).join(', '));
  65. lines.push('Output Jax: ' + _this.document.outputJax.name);
  66. lines.push('Document Type: ' + _this.document.kind);
  67. return lines.join('<br/>');
  68. }, '<a href="https://www.mathjax.org">www.mathjax.org</a>');
  69. this.help = new info_js_1.Info('<b>MathJax Help</b>', function () {
  70. return [
  71. '<p><b>MathJax</b> is a JavaScript library that allows page',
  72. ' authors to include mathematics within their web pages.',
  73. ' As a reader, you don\'t need to do anything to make that happen.</p>',
  74. '<p><b>Browsers</b>: MathJax works with all modern browsers including',
  75. ' Edge, Firefox, Chrome, Safari, Opera, and most mobile browsers.</p>',
  76. '<p><b>Math Menu</b>: MathJax adds a contextual menu to equations.',
  77. ' Right-click or CTRL-click on any mathematics to access the menu.</p>',
  78. '<div style="margin-left: 1em;">',
  79. '<p><b>Show Math As:</b> These options allow you to view the formula\'s',
  80. ' source markup (as MathML or in its original format).</p>',
  81. '<p><b>Copy to Clipboard:</b> These options copy the formula\'s source markup,',
  82. ' as MathML or in its original format, to the clipboard',
  83. ' (in browsers that support that).</p>',
  84. '<p><b>Math Settings:</b> These give you control over features of MathJax,',
  85. ' such the size of the mathematics, and the mechanism used',
  86. ' to display equations.</p>',
  87. '<p><b>Accessibility</b>: MathJax can work with screen',
  88. ' readers to make mathematics accessible to the visually impaired.',
  89. ' Turn on the explorer to enable generation of speech strings',
  90. ' and the ability to investigate expressions interactively.</p>',
  91. '<p><b>Language</b>: This menu lets you select the language used by MathJax',
  92. ' for its menus and warning messages. (Not yet implemented in version 3.)</p>',
  93. '</div>',
  94. '<p><b>Math Zoom</b>: If you are having difficulty reading an',
  95. ' equation, MathJax can enlarge it to help you see it better, or',
  96. ' you can scall all the math on the page to make it larger.',
  97. ' Turn these features on in the <b>Math Settings</b> menu.</p>',
  98. '<p><b>Preferences</b>: MathJax uses your browser\'s localStorage database',
  99. ' to save the preferences set via this menu locally in your browser. These',
  100. ' are not used to track you, and are not transferred or used remotely by',
  101. ' MathJax in any way.</p>'
  102. ].join('\n');
  103. }, '<a href="https://www.mathjax.org">www.mathjax.org</a>');
  104. this.mathmlCode = new SelectableInfo_js_1.SelectableInfo('MathJax MathML Expression', function () {
  105. if (!_this.menu.mathItem)
  106. return '';
  107. var text = _this.toMML(_this.menu.mathItem);
  108. return '<pre>' + _this.formatSource(text) + '</pre>';
  109. }, '');
  110. this.originalText = new SelectableInfo_js_1.SelectableInfo('MathJax Original Source', function () {
  111. if (!_this.menu.mathItem)
  112. return '';
  113. var text = _this.menu.mathItem.math;
  114. return '<pre style="font-size:125%; margin:0">' + _this.formatSource(text) + '</pre>';
  115. }, '');
  116. this.annotationText = new SelectableInfo_js_1.SelectableInfo('MathJax Annotation Text', function () {
  117. if (!_this.menu.mathItem)
  118. return '';
  119. var text = _this.menu.annotation;
  120. return '<pre style="font-size:125%; margin:0">' + _this.formatSource(text) + '</pre>';
  121. }, '');
  122. this.zoomBox = new info_js_1.Info('MathJax Zoomed Expression', function () {
  123. if (!_this.menu.mathItem)
  124. return '';
  125. var element = _this.menu.mathItem.typesetRoot.cloneNode(true);
  126. element.style.margin = '0';
  127. var scale = 1.25 * parseFloat(_this.settings.zscale);
  128. return '<div style="font-size: ' + scale + '%">' + element.outerHTML + '</div>';
  129. }, '');
  130. this.document = document;
  131. this.options = (0, Options_js_1.userOptions)((0, Options_js_1.defaultOptions)({}, this.constructor.OPTIONS), options);
  132. this.initSettings();
  133. this.mergeUserSettings();
  134. this.initMenu();
  135. this.applySettings();
  136. }
  137. Object.defineProperty(Menu.prototype, "isLoading", {
  138. get: function () {
  139. return Menu.loading > 0;
  140. },
  141. enumerable: false,
  142. configurable: true
  143. });
  144. Object.defineProperty(Menu.prototype, "loadingPromise", {
  145. get: function () {
  146. if (!this.isLoading) {
  147. return Promise.resolve();
  148. }
  149. if (!Menu._loadingPromise) {
  150. Menu._loadingPromise = new Promise(function (ok, failed) {
  151. Menu._loadingOK = ok;
  152. Menu._loadingFailed = failed;
  153. });
  154. }
  155. return Menu._loadingPromise;
  156. },
  157. enumerable: false,
  158. configurable: true
  159. });
  160. Menu.prototype.initSettings = function () {
  161. this.settings = this.options.settings;
  162. this.jax = this.options.jax;
  163. var jax = this.document.outputJax;
  164. this.jax[jax.name] = jax;
  165. this.settings.renderer = jax.name;
  166. if (MathJax._.a11y && MathJax._.a11y.explorer) {
  167. Object.assign(this.settings, this.document.options.a11y);
  168. }
  169. this.settings.scale = jax.options.scale;
  170. this.defaultSettings = Object.assign({}, this.settings);
  171. };
  172. Menu.prototype.initMenu = function () {
  173. var _this = this;
  174. var parser = new parse_js_1.Parser([['contextMenu', MJContextMenu_js_1.MJContextMenu.fromJson.bind(MJContextMenu_js_1.MJContextMenu)]]);
  175. this.menu = parser.parse({
  176. type: 'contextMenu',
  177. id: 'MathJax_Menu',
  178. pool: [
  179. this.variable('texHints'),
  180. this.variable('semantics'),
  181. this.variable('zoom'),
  182. this.variable('zscale'),
  183. this.variable('renderer', function (jax) { return _this.setRenderer(jax); }),
  184. this.variable('alt'),
  185. this.variable('cmd'),
  186. this.variable('ctrl'),
  187. this.variable('shift'),
  188. this.variable('scale', function (scale) { return _this.setScale(scale); }),
  189. this.variable('explorer', function (explore) { return _this.setExplorer(explore); }),
  190. this.a11yVar('highlight'),
  191. this.a11yVar('backgroundColor'),
  192. this.a11yVar('backgroundOpacity'),
  193. this.a11yVar('foregroundColor'),
  194. this.a11yVar('foregroundOpacity'),
  195. this.a11yVar('speech'),
  196. this.a11yVar('subtitles'),
  197. this.a11yVar('braille'),
  198. this.a11yVar('viewBraille'),
  199. this.a11yVar('locale', function (value) { return sre_js_1.default.setupEngine({ locale: value }); }),
  200. this.a11yVar('speechRules', function (value) {
  201. var _a = __read(value.split('-'), 2), domain = _a[0], style = _a[1];
  202. _this.document.options.sre.domain = domain;
  203. _this.document.options.sre.style = style;
  204. }),
  205. this.a11yVar('magnification'),
  206. this.a11yVar('magnify'),
  207. this.a11yVar('treeColoring'),
  208. this.a11yVar('infoType'),
  209. this.a11yVar('infoRole'),
  210. this.a11yVar('infoPrefix'),
  211. this.variable('autocollapse'),
  212. this.variable('collapsible', function (collapse) { return _this.setCollapsible(collapse); }),
  213. this.variable('inTabOrder', function (tab) { return _this.setTabOrder(tab); }),
  214. this.variable('assistiveMml', function (mml) { return _this.setAssistiveMml(mml); })
  215. ],
  216. items: [
  217. this.submenu('Show', 'Show Math As', [
  218. this.command('MathMLcode', 'MathML Code', function () { return _this.mathmlCode.post(); }),
  219. this.command('Original', 'Original Form', function () { return _this.originalText.post(); }),
  220. this.submenu('Annotation', 'Annotation')
  221. ]),
  222. this.submenu('Copy', 'Copy to Clipboard', [
  223. this.command('MathMLcode', 'MathML Code', function () { return _this.copyMathML(); }),
  224. this.command('Original', 'Original Form', function () { return _this.copyOriginal(); }),
  225. this.submenu('Annotation', 'Annotation')
  226. ]),
  227. this.rule(),
  228. this.submenu('Settings', 'Math Settings', [
  229. this.submenu('Renderer', 'Math Renderer', this.radioGroup('renderer', [['CHTML'], ['SVG']])),
  230. this.rule(),
  231. this.submenu('ZoomTrigger', 'Zoom Trigger', [
  232. this.command('ZoomNow', 'Zoom Once Now', function () { return _this.zoom(null, '', _this.menu.mathItem); }),
  233. this.rule(),
  234. this.radioGroup('zoom', [
  235. ['Click'], ['DoubleClick', 'Double-Click'], ['NoZoom', 'No Zoom']
  236. ]),
  237. this.rule(),
  238. this.label('TriggerRequires', 'Trigger Requires:'),
  239. this.checkbox((isMac ? 'Option' : 'Alt'), (isMac ? 'Option' : 'Alt'), 'alt'),
  240. this.checkbox('Command', 'Command', 'cmd', { hidden: !isMac }),
  241. this.checkbox('Control', 'Control', 'ctrl', { hiddne: isMac }),
  242. this.checkbox('Shift', 'Shift', 'shift')
  243. ]),
  244. this.submenu('ZoomFactor', 'Zoom Factor', this.radioGroup('zscale', [
  245. ['150%'], ['175%'], ['200%'], ['250%'], ['300%'], ['400%']
  246. ])),
  247. this.rule(),
  248. this.command('Scale', 'Scale All Math...', function () { return _this.scaleAllMath(); }),
  249. this.rule(),
  250. this.checkbox('texHints', 'Add TeX hints to MathML', 'texHints'),
  251. this.checkbox('semantics', 'Add original as annotation', 'semantics'),
  252. this.rule(),
  253. this.command('Reset', 'Reset to defaults', function () { return _this.resetDefaults(); })
  254. ]),
  255. this.submenu('Accessibility', 'Accessibility', [
  256. this.checkbox('Activate', 'Activate', 'explorer'),
  257. this.submenu('Speech', 'Speech', [
  258. this.checkbox('Speech', 'Speech Output', 'speech'),
  259. this.checkbox('Subtitles', 'Speech Subtitles', 'subtitles'),
  260. this.checkbox('Braille', 'Braille Output', 'braille'),
  261. this.checkbox('View Braille', 'Braille Subtitles', 'viewBraille'),
  262. this.rule(),
  263. this.submenu('A11yLanguage', 'Language'),
  264. this.rule(),
  265. this.submenu('Mathspeak', 'Mathspeak Rules', this.radioGroup('speechRules', [
  266. ['mathspeak-default', 'Verbose'],
  267. ['mathspeak-brief', 'Brief'],
  268. ['mathspeak-sbrief', 'Superbrief']
  269. ])),
  270. this.submenu('Clearspeak', 'Clearspeak Rules', this.radioGroup('speechRules', [
  271. ['clearspeak-default', 'Auto']
  272. ])),
  273. this.submenu('ChromeVox', 'ChromeVox Rules', this.radioGroup('speechRules', [
  274. ['chromevox-default', 'Standard'],
  275. ['chromevox-alternative', 'Alternative']
  276. ]))
  277. ]),
  278. this.submenu('Highlight', 'Highlight', [
  279. this.submenu('Background', 'Background', this.radioGroup('backgroundColor', [
  280. ['Blue'], ['Red'], ['Green'], ['Yellow'], ['Cyan'], ['Magenta'], ['White'], ['Black']
  281. ])),
  282. { 'type': 'slider',
  283. 'variable': 'backgroundOpacity',
  284. 'content': ' '
  285. },
  286. this.submenu('Foreground', 'Foreground', this.radioGroup('foregroundColor', [
  287. ['Black'], ['White'], ['Magenta'], ['Cyan'], ['Yellow'], ['Green'], ['Red'], ['Blue']
  288. ])),
  289. { 'type': 'slider',
  290. 'variable': 'foregroundOpacity',
  291. 'content': ' '
  292. },
  293. this.rule(),
  294. this.radioGroup('highlight', [
  295. ['None'], ['Hover'], ['Flame']
  296. ]),
  297. this.rule(),
  298. this.checkbox('TreeColoring', 'Tree Coloring', 'treeColoring')
  299. ]),
  300. this.submenu('Magnification', 'Magnification', [
  301. this.radioGroup('magnification', [
  302. ['None'], ['Keyboard'], ['Mouse']
  303. ]),
  304. this.rule(),
  305. this.radioGroup('magnify', [
  306. ['200%'], ['300%'], ['400%'], ['500%']
  307. ])
  308. ]),
  309. this.submenu('Semantic Info', 'Semantic Info', [
  310. this.checkbox('Type', 'Type', 'infoType'),
  311. this.checkbox('Role', 'Role', 'infoRole'),
  312. this.checkbox('Prefix', 'Prefix', 'infoPrefix')
  313. ], true),
  314. this.rule(),
  315. this.checkbox('Collapsible', 'Collapsible Math', 'collapsible'),
  316. this.checkbox('AutoCollapse', 'Auto Collapse', 'autocollapse', { disabled: true }),
  317. this.rule(),
  318. this.checkbox('InTabOrder', 'Include in Tab Order', 'inTabOrder'),
  319. this.checkbox('AssistiveMml', 'Include Hidden MathML', 'assistiveMml')
  320. ]),
  321. this.submenu('Language', 'Language'),
  322. this.rule(),
  323. this.command('About', 'About MathJax', function () { return _this.about.post(); }),
  324. this.command('Help', 'MathJax Help', function () { return _this.help.post(); })
  325. ]
  326. });
  327. var menu = this.menu;
  328. this.about.attachMenu(menu);
  329. this.help.attachMenu(menu);
  330. this.originalText.attachMenu(menu);
  331. this.annotationText.attachMenu(menu);
  332. this.mathmlCode.attachMenu(menu);
  333. this.zoomBox.attachMenu(menu);
  334. this.checkLoadableItems();
  335. this.enableExplorerItems(this.settings.explorer);
  336. menu.showAnnotation = this.annotationText;
  337. menu.copyAnnotation = this.copyAnnotation.bind(this);
  338. menu.annotationTypes = this.options.annotationTypes;
  339. css_util_js_1.CssStyles.addInfoStyles(this.document.document);
  340. css_util_js_1.CssStyles.addMenuStyles(this.document.document);
  341. };
  342. Menu.prototype.checkLoadableItems = function () {
  343. var e_1, _a;
  344. if (MathJax && MathJax._ && MathJax.loader && MathJax.startup) {
  345. if (this.settings.collapsible && (!MathJax._.a11y || !MathJax._.a11y.complexity)) {
  346. this.loadA11y('complexity');
  347. }
  348. if (this.settings.explorer && (!MathJax._.a11y || !MathJax._.a11y.explorer)) {
  349. this.loadA11y('explorer');
  350. }
  351. if (this.settings.assistiveMml && (!MathJax._.a11y || !MathJax._.a11y['assistive-mml'])) {
  352. this.loadA11y('assistive-mml');
  353. }
  354. }
  355. else {
  356. var menu = this.menu;
  357. try {
  358. for (var _b = __values(Object.keys(this.jax)), _c = _b.next(); !_c.done; _c = _b.next()) {
  359. var name_1 = _c.value;
  360. if (!this.jax[name_1]) {
  361. menu.findID('Settings', 'Renderer', name_1).disable();
  362. }
  363. }
  364. }
  365. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  366. finally {
  367. try {
  368. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  369. }
  370. finally { if (e_1) throw e_1.error; }
  371. }
  372. menu.findID('Accessibility', 'Activate').disable();
  373. menu.findID('Accessibility', 'AutoCollapse').disable();
  374. menu.findID('Accessibility', 'Collapsible').disable();
  375. }
  376. };
  377. Menu.prototype.enableExplorerItems = function (enable) {
  378. var e_2, _a;
  379. var menu = this.menu.findID('Accessibility', 'Activate').menu;
  380. try {
  381. for (var _b = __values(menu.items.slice(1)), _c = _b.next(); !_c.done; _c = _b.next()) {
  382. var item = _c.value;
  383. if (item instanceof item_rule_js_1.Rule)
  384. break;
  385. enable ? item.enable() : item.disable();
  386. }
  387. }
  388. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  389. finally {
  390. try {
  391. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  392. }
  393. finally { if (e_2) throw e_2.error; }
  394. }
  395. };
  396. Menu.prototype.mergeUserSettings = function () {
  397. try {
  398. var settings = localStorage.getItem(Menu.MENU_STORAGE);
  399. if (!settings)
  400. return;
  401. Object.assign(this.settings, JSON.parse(settings));
  402. this.setA11y(this.settings);
  403. }
  404. catch (err) {
  405. console.log('MathJax localStorage error: ' + err.message);
  406. }
  407. };
  408. Menu.prototype.saveUserSettings = function () {
  409. var e_3, _a;
  410. var settings = {};
  411. try {
  412. for (var _b = __values(Object.keys(this.settings)), _c = _b.next(); !_c.done; _c = _b.next()) {
  413. var name_2 = _c.value;
  414. if (this.settings[name_2] !== this.defaultSettings[name_2]) {
  415. settings[name_2] = this.settings[name_2];
  416. }
  417. }
  418. }
  419. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  420. finally {
  421. try {
  422. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  423. }
  424. finally { if (e_3) throw e_3.error; }
  425. }
  426. try {
  427. if (Object.keys(settings).length) {
  428. localStorage.setItem(Menu.MENU_STORAGE, JSON.stringify(settings));
  429. }
  430. else {
  431. localStorage.removeItem(Menu.MENU_STORAGE);
  432. }
  433. }
  434. catch (err) {
  435. console.log('MathJax localStorage error: ' + err.message);
  436. }
  437. };
  438. Menu.prototype.setA11y = function (options) {
  439. if (MathJax._.a11y && MathJax._.a11y.explorer) {
  440. MathJax._.a11y.explorer_ts.setA11yOptions(this.document, options);
  441. }
  442. };
  443. Menu.prototype.getA11y = function (option) {
  444. if (MathJax._.a11y && MathJax._.a11y.explorer) {
  445. if (this.document.options.a11y[option] !== undefined) {
  446. return this.document.options.a11y[option];
  447. }
  448. return this.document.options.sre[option];
  449. }
  450. };
  451. Menu.prototype.applySettings = function () {
  452. this.setTabOrder(this.settings.inTabOrder);
  453. this.document.options.enableAssistiveMml = this.settings.assistiveMml;
  454. this.document.outputJax.options.scale = parseFloat(this.settings.scale);
  455. if (this.settings.renderer !== this.defaultSettings.renderer) {
  456. this.setRenderer(this.settings.renderer);
  457. }
  458. };
  459. Menu.prototype.setScale = function (scale) {
  460. this.document.outputJax.options.scale = parseFloat(scale);
  461. this.document.rerender();
  462. };
  463. Menu.prototype.setRenderer = function (jax) {
  464. var _this = this;
  465. if (this.jax[jax]) {
  466. this.setOutputJax(jax);
  467. }
  468. else {
  469. var name_3 = jax.toLowerCase();
  470. this.loadComponent('output/' + name_3, function () {
  471. var startup = MathJax.startup;
  472. if (name_3 in startup.constructors) {
  473. startup.useOutput(name_3, true);
  474. startup.output = startup.getOutputJax();
  475. _this.jax[jax] = startup.output;
  476. _this.setOutputJax(jax);
  477. }
  478. });
  479. }
  480. };
  481. Menu.prototype.setOutputJax = function (jax) {
  482. this.jax[jax].setAdaptor(this.document.adaptor);
  483. this.document.outputJax = this.jax[jax];
  484. this.rerender();
  485. };
  486. Menu.prototype.setTabOrder = function (tab) {
  487. this.menu.store.inTaborder(tab);
  488. };
  489. Menu.prototype.setAssistiveMml = function (mml) {
  490. this.document.options.enableAssistiveMml = mml;
  491. if (!mml || (MathJax._.a11y && MathJax._.a11y['assistive-mml'])) {
  492. this.rerender();
  493. }
  494. else {
  495. this.loadA11y('assistive-mml');
  496. }
  497. };
  498. Menu.prototype.setExplorer = function (explore) {
  499. this.enableExplorerItems(explore);
  500. this.document.options.enableExplorer = explore;
  501. if (!explore || (MathJax._.a11y && MathJax._.a11y.explorer)) {
  502. this.rerender(this.settings.collapsible ? MathItem_js_1.STATE.RERENDER : MathItem_js_1.STATE.COMPILED);
  503. }
  504. else {
  505. this.loadA11y('explorer');
  506. }
  507. };
  508. Menu.prototype.setCollapsible = function (collapse) {
  509. this.document.options.enableComplexity = collapse;
  510. if (!collapse || (MathJax._.a11y && MathJax._.a11y.complexity)) {
  511. this.rerender(MathItem_js_1.STATE.COMPILED);
  512. }
  513. else {
  514. this.loadA11y('complexity');
  515. }
  516. };
  517. Menu.prototype.scaleAllMath = function () {
  518. var scale = (parseFloat(this.settings.scale) * 100).toFixed(1).replace(/.0$/, '');
  519. var percent = prompt('Scale all mathematics (compared to surrounding text) by', scale + '%');
  520. if (percent) {
  521. if (percent.match(/^\s*\d+(\.\d*)?\s*%?\s*$/)) {
  522. var scale_1 = parseFloat(percent) / 100;
  523. if (scale_1) {
  524. this.menu.pool.lookup('scale').setValue(String(scale_1));
  525. }
  526. else {
  527. alert('The scale should not be zero');
  528. }
  529. }
  530. else {
  531. alert('The scale should be a percentage (e.g., 120%)');
  532. }
  533. }
  534. };
  535. Menu.prototype.resetDefaults = function () {
  536. var e_4, _a;
  537. Menu.loading++;
  538. var pool = this.menu.pool;
  539. var settings = this.defaultSettings;
  540. try {
  541. for (var _b = __values(Object.keys(this.settings)), _c = _b.next(); !_c.done; _c = _b.next()) {
  542. var name_4 = _c.value;
  543. var variable = pool.lookup(name_4);
  544. if (variable) {
  545. variable.setValue(settings[name_4]);
  546. var item = variable.items[0];
  547. if (item) {
  548. item.executeCallbacks_();
  549. }
  550. }
  551. else {
  552. this.settings[name_4] = settings[name_4];
  553. }
  554. }
  555. }
  556. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  557. finally {
  558. try {
  559. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  560. }
  561. finally { if (e_4) throw e_4.error; }
  562. }
  563. Menu.loading--;
  564. this.rerender(MathItem_js_1.STATE.COMPILED);
  565. };
  566. Menu.prototype.checkComponent = function (name) {
  567. var promise = Menu.loadingPromises.get(name);
  568. if (promise) {
  569. mathjax_js_1.mathjax.retryAfter(promise);
  570. }
  571. };
  572. Menu.prototype.loadComponent = function (name, callback) {
  573. if (Menu.loadingPromises.has(name))
  574. return;
  575. var loader = MathJax.loader;
  576. if (!loader)
  577. return;
  578. Menu.loading++;
  579. var promise = loader.load(name).then(function () {
  580. Menu.loading--;
  581. Menu.loadingPromises.delete(name);
  582. callback();
  583. if (Menu.loading === 0 && Menu._loadingPromise) {
  584. Menu._loadingPromise = null;
  585. Menu._loadingOK();
  586. }
  587. }).catch(function (err) {
  588. if (Menu._loadingPromise) {
  589. Menu._loadingPromise = null;
  590. Menu._loadingFailed(err);
  591. }
  592. else {
  593. console.log(err);
  594. }
  595. });
  596. Menu.loadingPromises.set(name, promise);
  597. };
  598. Menu.prototype.loadA11y = function (component) {
  599. var _this = this;
  600. var noEnrich = !MathItem_js_1.STATE.ENRICHED;
  601. this.loadComponent('a11y/' + component, function () {
  602. var startup = MathJax.startup;
  603. mathjax_js_1.mathjax.handlers.unregister(startup.handler);
  604. startup.handler = startup.getHandler();
  605. mathjax_js_1.mathjax.handlers.register(startup.handler);
  606. var document = _this.document;
  607. _this.document = startup.document = startup.getDocument();
  608. _this.document.menu = _this;
  609. _this.document.outputJax.reset();
  610. _this.transferMathList(document);
  611. _this.document.processed = document.processed;
  612. if (!Menu._loadingPromise) {
  613. _this.document.outputJax.reset();
  614. _this.rerender(component === 'complexity' || noEnrich ? MathItem_js_1.STATE.COMPILED : MathItem_js_1.STATE.TYPESET);
  615. }
  616. });
  617. };
  618. Menu.prototype.transferMathList = function (document) {
  619. var e_5, _a;
  620. var MathItem = this.document.options.MathItem;
  621. try {
  622. for (var _b = __values(document.math), _c = _b.next(); !_c.done; _c = _b.next()) {
  623. var item = _c.value;
  624. var math = new MathItem();
  625. Object.assign(math, item);
  626. this.document.math.push(math);
  627. }
  628. }
  629. catch (e_5_1) { e_5 = { error: e_5_1 }; }
  630. finally {
  631. try {
  632. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  633. }
  634. finally { if (e_5) throw e_5.error; }
  635. }
  636. };
  637. Menu.prototype.formatSource = function (text) {
  638. return text.trim().replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  639. };
  640. Menu.prototype.toMML = function (math) {
  641. return this.MmlVisitor.visitTree(math.root, math, {
  642. texHints: this.settings.texHints,
  643. semantics: (this.settings.semantics && math.inputJax.name !== 'MathML')
  644. });
  645. };
  646. Menu.prototype.zoom = function (event, type, math) {
  647. if (!event || this.isZoomEvent(event, type)) {
  648. this.menu.mathItem = math;
  649. if (event) {
  650. this.menu.post(event);
  651. }
  652. this.zoomBox.post();
  653. }
  654. };
  655. Menu.prototype.isZoomEvent = function (event, zoom) {
  656. return (this.settings.zoom === zoom &&
  657. (!this.settings.alt || event.altKey) &&
  658. (!this.settings.ctrl || event.ctrlKey) &&
  659. (!this.settings.cmd || event.metaKey) &&
  660. (!this.settings.shift || event.shiftKey));
  661. };
  662. Menu.prototype.rerender = function (start) {
  663. if (start === void 0) { start = MathItem_js_1.STATE.TYPESET; }
  664. this.rerenderStart = Math.min(start, this.rerenderStart);
  665. if (!Menu.loading) {
  666. if (this.rerenderStart <= MathItem_js_1.STATE.COMPILED) {
  667. this.document.reset({ inputJax: [] });
  668. }
  669. this.document.rerender(this.rerenderStart);
  670. this.rerenderStart = MathItem_js_1.STATE.LAST;
  671. }
  672. };
  673. Menu.prototype.copyMathML = function () {
  674. this.copyToClipboard(this.toMML(this.menu.mathItem));
  675. };
  676. Menu.prototype.copyOriginal = function () {
  677. this.copyToClipboard(this.menu.mathItem.math.trim());
  678. };
  679. Menu.prototype.copyAnnotation = function () {
  680. this.copyToClipboard(this.menu.annotation.trim());
  681. };
  682. Menu.prototype.copyToClipboard = function (text) {
  683. var input = document.createElement('textarea');
  684. input.value = text;
  685. input.setAttribute('readonly', '');
  686. input.style.cssText = 'height: 1px; width: 1px; padding: 1px; position: absolute; left: -10px';
  687. document.body.appendChild(input);
  688. input.select();
  689. try {
  690. document.execCommand('copy');
  691. }
  692. catch (error) {
  693. alert('Can\'t copy to clipboard: ' + error.message);
  694. }
  695. document.body.removeChild(input);
  696. };
  697. Menu.prototype.addMenu = function (math) {
  698. var _this = this;
  699. var element = math.typesetRoot;
  700. element.addEventListener('contextmenu', function () { return _this.menu.mathItem = math; }, true);
  701. element.addEventListener('keydown', function () { return _this.menu.mathItem = math; }, true);
  702. element.addEventListener('click', function (event) { return _this.zoom(event, 'Click', math); }, true);
  703. element.addEventListener('dblclick', function (event) { return _this.zoom(event, 'DoubleClick', math); }, true);
  704. this.menu.store.insert(element);
  705. };
  706. Menu.prototype.clear = function () {
  707. this.menu.store.clear();
  708. };
  709. Menu.prototype.variable = function (name, action) {
  710. var _this = this;
  711. return {
  712. name: name,
  713. getter: function () { return _this.settings[name]; },
  714. setter: function (value) {
  715. _this.settings[name] = value;
  716. action && action(value);
  717. _this.saveUserSettings();
  718. }
  719. };
  720. };
  721. Menu.prototype.a11yVar = function (name, action) {
  722. var _this = this;
  723. return {
  724. name: name,
  725. getter: function () { return _this.getA11y(name); },
  726. setter: function (value) {
  727. _this.settings[name] = value;
  728. var options = {};
  729. options[name] = value;
  730. _this.setA11y(options);
  731. action && action(value);
  732. _this.saveUserSettings();
  733. }
  734. };
  735. };
  736. Menu.prototype.submenu = function (id, content, entries, disabled) {
  737. var e_6, _a;
  738. if (entries === void 0) { entries = []; }
  739. if (disabled === void 0) { disabled = false; }
  740. var items = [];
  741. try {
  742. for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
  743. var entry = entries_1_1.value;
  744. if (Array.isArray(entry)) {
  745. items = items.concat(entry);
  746. }
  747. else {
  748. items.push(entry);
  749. }
  750. }
  751. }
  752. catch (e_6_1) { e_6 = { error: e_6_1 }; }
  753. finally {
  754. try {
  755. if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
  756. }
  757. finally { if (e_6) throw e_6.error; }
  758. }
  759. return { type: 'submenu', id: id, content: content, menu: { items: items }, disabled: (items.length === 0) || disabled };
  760. };
  761. Menu.prototype.command = function (id, content, action, other) {
  762. if (other === void 0) { other = {}; }
  763. return Object.assign({ type: 'command', id: id, content: content, action: action }, other);
  764. };
  765. Menu.prototype.checkbox = function (id, content, variable, other) {
  766. if (other === void 0) { other = {}; }
  767. return Object.assign({ type: 'checkbox', id: id, content: content, variable: variable }, other);
  768. };
  769. Menu.prototype.radioGroup = function (variable, radios) {
  770. var _this = this;
  771. return radios.map(function (def) { return _this.radio(def[0], def[1] || def[0], variable); });
  772. };
  773. Menu.prototype.radio = function (id, content, variable, other) {
  774. if (other === void 0) { other = {}; }
  775. return Object.assign({ type: 'radio', id: id, content: content, variable: variable }, other);
  776. };
  777. Menu.prototype.label = function (id, content) {
  778. return { type: 'label', id: id, content: content };
  779. };
  780. Menu.prototype.rule = function () {
  781. return { type: 'rule' };
  782. };
  783. Menu.MENU_STORAGE = 'MathJax-Menu-Settings';
  784. Menu.OPTIONS = {
  785. settings: {
  786. texHints: true,
  787. semantics: false,
  788. zoom: 'NoZoom',
  789. zscale: '200%',
  790. renderer: 'CHTML',
  791. alt: false,
  792. cmd: false,
  793. ctrl: false,
  794. shift: false,
  795. scale: 1,
  796. autocollapse: false,
  797. collapsible: false,
  798. inTabOrder: true,
  799. assistiveMml: true,
  800. explorer: false
  801. },
  802. jax: {
  803. CHTML: null,
  804. SVG: null
  805. },
  806. annotationTypes: (0, Options_js_1.expandable)({
  807. TeX: ['TeX', 'LaTeX', 'application/x-tex'],
  808. StarMath: ['StarMath 5.0'],
  809. Maple: ['Maple'],
  810. ContentMathML: ['MathML-Content', 'application/mathml-content+xml'],
  811. OpenMath: ['OpenMath']
  812. })
  813. };
  814. Menu.loading = 0;
  815. Menu.loadingPromises = new Map();
  816. Menu._loadingPromise = null;
  817. Menu._loadingOK = null;
  818. Menu._loadingFailed = null;
  819. return Menu;
  820. }());
  821. exports.Menu = Menu;
  822. //# sourceMappingURL=Menu.js.map