1 line
14 KiB
Plaintext
1 line
14 KiB
Plaintext
{"version":3,"file":"ext-mathjax.js","sources":["../../../../src/editor/extensions/ext-mathjax/ext-mathjax.js"],"sourcesContent":["/* globals MathJax */\n/**\n * @file ext-mathjax.js\n *\n * @license MIT\n *\n * @copyright 2013 Jo Segaert\n *\n */\n\nexport default {\n name: 'mathjax',\n async init ({$}) {\n const svgEditor = this;\n // eslint-disable-next-line node/no-unsupported-features/es-syntax\n const {default: strings} = await import(`./locale/${svgEditor.curPrefs.lang}.js`);\n const svgCanvas = svgEditor.canvas;\n\n // Configuration of the MathJax extention.\n\n // This will be added to the head tag before MathJax is loaded.\n const /* mathjaxConfiguration = `<script type=\"text/x-mathjax-config\">\n MathJax.Hub.Config({\n extensions: ['tex2jax.js'],\n jax: ['input/TeX', 'output/SVG'],\n showProcessingMessages: true,\n showMathMenu: false,\n showMathMenuMSIE: false,\n errorSettings: {\n message: ['[Math Processing Error]'],\n style: {color: '#CC0000', 'font-style': 'italic'}\n },\n elements: [],\n tex2jax: {\n ignoreClass: 'tex2jax_ignore2', processClass: 'tex2jax_process2',\n },\n TeX: {\n extensions: ['AMSmath.js', 'AMSsymbols.js', 'noErrors.js', 'noUndefined.js']\n },\n SVG: {\n }\n });\n </script>`, */\n // mathjaxSrc = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js',\n // Had been on https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_SVG.js\n // Obtained Text-AMS-MML_SVG.js from https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.3/config/TeX-AMS-MML_SVG.js\n mathjaxSrcSecure = 'mathjax/MathJax.min.js?config=TeX-AMS-MML_SVG.js',\n {uiStrings} = svgEditor;\n let\n math,\n locationX,\n locationY,\n mathjaxLoaded = false;\n\n // TODO: Implement language support. Move these uiStrings to the locale files and\n // the code to the langReady callback. Also i18nize alert and HTML below\n $.extend(uiStrings, {\n mathjax: {\n embed_svg: 'Save as mathematics',\n embed_mathml: 'Save as figure',\n svg_save_warning: 'The math will be transformed into a figure is ' +\n 'manipulatable like everything else. You will not be able to ' +\n 'manipulate the TeX-code anymore.',\n mathml_save_warning: 'Advised. The math will be saved as a figure.',\n title: 'Mathematics code editor'\n }\n });\n\n /**\n *\n * @returns {void}\n */\n function saveMath () {\n const code = $('#mathjax_code_textarea').val();\n // displaystyle to force MathJax NOT to use the inline style. Because it is\n // less fancy!\n MathJax.Hub.queue.Push(['Text', math, '\\\\displaystyle{' + code + '}']);\n\n /*\n * The MathJax library doesn't want to bloat your webpage so it creates\n * every symbol (glymph) you need only once. These are saved in a `<svg>` on\n * the top of your html document, just under the body tag. Each glymph has\n * its unique id and is saved as a `<path>` in the `<defs>` tag of the `<svg>`\n *\n * Then when the symbols are needed in the rest of your html document they\n * are refferd to by a `<use>` tag.\n * Because of bug 1076 we can't just grab the defs tag on the top and add it\n * to your formula's `<svg>` and copy the lot. So we have to replace each\n * `<use>` tag by its `<path>`.\n */\n MathJax.Hub.queue.Push(\n function () {\n const mathjaxMath = $('.MathJax_SVG');\n const svg = $(mathjaxMath.html());\n svg.find('use').each(function () {\n // TODO: find a less pragmatic and more elegant solution to this.\n const id = $(this).attr('href')\n ? $(this).attr('href').slice(1) // Works in Chrome.\n : $(this).attr('xlink:href').slice(1); // Works in Firefox.\n const glymph = $('#' + id).clone().removeAttr('id');\n const x = $(this).attr('x');\n const y = $(this).attr('y');\n const transform = $(this).attr('transform');\n if (transform && (x || y)) {\n glymph.attr('transform', transform + ' translate(' + x + ',' + y + ')');\n } else if (transform) {\n glymph.attr('transform', transform);\n } else if (x || y) {\n glymph.attr('transform', 'translate(' + x + ',' + y + ')');\n }\n $(this).replaceWith(glymph);\n });\n // Remove the style tag because it interferes with SVG-Edit.\n svg.removeAttr('style');\n svg.attr('xmlns', 'http://www.w3.org/2000/svg');\n svgCanvas.importSvgString($('<div>').append(svg.clone()).html(), true);\n svgCanvas.ungroupSelectedElement();\n // TODO: To undo the adding of the Formula you now have to undo twice.\n // This should only be once!\n svgCanvas.moveSelectedElements(locationX, locationY, true);\n }\n );\n }\n\n const buttons = [{\n id: 'tool_mathjax',\n type: 'mode',\n icon: 'mathjax.png',\n events: {\n async click () {\n // Set the mode.\n svgCanvas.setMode('mathjax');\n\n // Only load Mathjax when needed, we don't want to strain Svg-Edit any more.\n // From this point on it is very probable that it will be needed, so load it.\n if (mathjaxLoaded === false) {\n $(\n '<div id=\"mathjax\">' +\n '<!-- Here is where MathJax creates the math -->' +\n '<div id=\"mathjax_creator\" class=\"tex2jax_process\" style=\"display:none\">' +\n '$${}$$' +\n '</div>' +\n '<div id=\"mathjax_overlay\"></div>' +\n '<div id=\"mathjax_container\">' +\n '<div id=\"tool_mathjax_back\" class=\"toolbar_button\">' +\n '<button id=\"tool_mathjax_save\">OK</button>' +\n '<button id=\"tool_mathjax_cancel\">Cancel</button>' +\n '</div>' +\n '<fieldset>' +\n '<legend id=\"mathjax_legend\">Mathematics Editor</legend>' +\n '<label>' +\n '<span id=\"mathjax_explication\">Please type your mathematics in ' +\n '<a href=\"https://en.wikipedia.org/wiki/Help:' +\n 'Displaying_a_formula\" target=\"_blank\">TeX</a> code.' +\n '</span></label>' +\n '<textarea id=\"mathjax_code_textarea\" spellcheck=\"false\"></textarea>' +\n '</fieldset>' +\n '</div>' +\n '</div>'\n ).insertAfter('#svg_prefs').hide();\n\n // Make the MathEditor draggable.\n $('#mathjax_container').draggable({\n cancel: 'button,fieldset',\n containment: 'window'\n });\n\n // Add functionality and picture to cancel button.\n $('#tool_mathjax_cancel').prepend($.getSvgIcon('cancel', true))\n .on('click touched', function () {\n $('#mathjax').hide();\n });\n\n // Add functionality and picture to the save button.\n $('#tool_mathjax_save').prepend($.getSvgIcon('ok', true))\n .on('click touched', function () {\n saveMath();\n $('#mathjax').hide();\n });\n\n // MathJax preprocessing has to ignore most of the page.\n $('body').addClass('tex2jax_ignore');\n\n try {\n // eslint-disable-next-line node/no-unsupported-features/es-syntax\n await import(mathjaxSrcSecure);\n // When MathJax is loaded get the div where the math will be rendered.\n MathJax.Hub.queue.Push(function () {\n math = MathJax.Hub.getAllJax('#mathjax_creator')[0];\n console.log(math); // eslint-disable-line no-console\n mathjaxLoaded = true;\n console.log('MathJax Loaded'); // eslint-disable-line no-console\n });\n } catch (e) {\n console.log('Failed loading MathJax.'); // eslint-disable-line no-console\n $.alert('Failed loading MathJax. You will not be able to change the mathematics.');\n }\n }\n }\n }\n }];\n\n return {\n name: strings.name,\n svgicons: 'mathjax-icons.xml',\n buttons: strings.buttons.map((button, i) => {\n return Object.assign(buttons[i], button);\n }),\n\n mouseDown () {\n if (svgCanvas.getMode() === 'mathjax') {\n return {started: true};\n }\n return undefined;\n },\n mouseUp (opts) {\n if (svgCanvas.getMode() === 'mathjax') {\n // Get the coordinates from your mouse.\n const zoom = svgCanvas.getZoom();\n // Get the actual coordinate by dividing by the zoom value\n locationX = opts.mouse_x / zoom;\n locationY = opts.mouse_y / zoom;\n\n $('#mathjax').show();\n return {started: false}; // Otherwise the last selected object dissapears.\n }\n return undefined;\n },\n callback () {\n $('<style>').text(\n '#mathjax fieldset{' +\n 'padding: 5px;' +\n 'margin: 5px;' +\n 'border: 1px solid #DDD;' +\n '}' +\n '#mathjax label{' +\n 'display: block;' +\n 'margin: .5em;' +\n '}' +\n '#mathjax legend {' +\n 'max-width:195px;' +\n '}' +\n '#mathjax_overlay {' +\n 'position: absolute;' +\n 'top: 0;' +\n 'left: 0;' +\n 'right: 0;' +\n 'bottom: 0;' +\n 'background-color: black;' +\n 'opacity: 0.6;' +\n 'z-index: 20000;' +\n '}' +\n '#mathjax_container {' +\n 'position: absolute;' +\n 'top: 50px;' +\n 'padding: 10px;' +\n 'background-color: #B0B0B0;' +\n 'border: 1px outset #777;' +\n 'opacity: 1.0;' +\n 'font-family: Verdana, Helvetica, sans-serif;' +\n 'font-size: .8em;' +\n 'z-index: 20001;' +\n '}' +\n '#tool_mathjax_back {' +\n 'margin-left: 1em;' +\n 'overflow: auto;' +\n '}' +\n '#mathjax_legend{' +\n 'font-weight: bold;' +\n 'font-size:1.1em;' +\n '}' +\n '#mathjax_code_textarea {\\\\n' +\n 'margin: 5px .7em;' +\n 'overflow: hidden;' +\n 'width: 416px;' +\n 'display: block;' +\n 'height: 100px;' +\n '}'\n ).appendTo('head');\n\n // Add the MathJax configuration.\n // $(mathjaxConfiguration).appendTo('head');\n }\n };\n }\n};\n"],"names":["name","init","saveMath","code","$","val","MathJax","Hub","queue","Push","math","mathjaxMath","svg","html","find","each","id","this","attr","slice","glymph","clone","removeAttr","x","y","transform","replaceWith","svgCanvas","importSvgString","append","ungroupSelectedElement","moveSelectedElements","locationX","locationY","svgEditor","_this","import","curPrefs","lang","strings","canvas","uiStrings","mathjaxLoaded","extend","mathjax","embed_svg","embed_mathml","svg_save_warning","mathml_save_warning","title","buttons","type","icon","events","click","setMode","insertAfter","hide","draggable","cancel","containment","prepend","getSvgIcon","on","addClass","getAllJax","console","log","alert","svgicons","map","button","i","Object","assign","mouseDown","getMode","started","mouseUp","opts","zoom","getZoom","mouse_x","mouse_y","show","callback","text","appendTo"],"mappings":";;;;;;;;GAUA,MAAe,CACbA,KAAM,UACAC,+HA4DKC,qGAAAA,2BACDC,EAAOC,EAAE,0BAA0BC,MAGzCC,QAAQC,IAAIC,MAAMC,KAAK,CAAC,OAAQC,EAAM,kBAAoBP,EAAO,MAcjEG,QAAQC,IAAIC,MAAMC,MAChB,eACQE,EAAcP,EAAE,gBAChBQ,EAAMR,EAAEO,EAAYE,QAC1BD,EAAIE,KAAK,OAAOC,MAAK,eAEbC,EAAKZ,EAAEa,MAAMC,KAAK,QACpBd,EAAEa,MAAMC,KAAK,QAAQC,MAAM,GAC3Bf,EAAEa,MAAMC,KAAK,cAAcC,MAAM,GAC/BC,EAAShB,EAAE,IAAMY,GAAIK,QAAQC,WAAW,MACxCC,EAAInB,EAAEa,MAAMC,KAAK,KACjBM,EAAIpB,EAAEa,MAAMC,KAAK,KACjBO,EAAYrB,EAAEa,MAAMC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