Adding mathjax extension

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2504 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jo Segaert 2013-04-12 19:25:18 +00:00
parent d9e5b797f4
commit ccbb084bbf
2 changed files with 282 additions and 0 deletions

View File

@ -0,0 +1,271 @@
/*
* ext-mathjax.js
*
* Licensed under the Apache License
*
* Copyright(c) 2013 Jo Segaert
*
*/
svgEditor.addExtension("mathjax", function() {
// Configuration of the MathJax extention.
// This will be added to the head tag before MathJax is loaded.
var mathjaxConfiguration = '<script type="text/x-mathjax-config">\
MathJax.Hub.Config({\
extensions: ["tex2jax.js"],\
jax: ["input/TeX","output/SVG"],\
showProcessingMessages: true,\
showMathMenu: false,\
showMathMenuMSIE: false,\
errorSettings: {\
message: ["[Math Processing Error]"],\
style: {color: "#CC0000", "font-style":"italic"}\
},\
elements: [],\
tex2jax: {\
ignoreClass: "tex2jax_ignore2", processClass: "tex2jax_process2",\
},\
TeX: {\
extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]\
},\
"SVG": {\
}\
});\
</script>',
mathjaxSrc = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js',
mathjaxSrcSecure = 'https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_SVG.js',
math,
locationX,
locationY,
mathjaxLoaded = false,
uiStrings = svgEditor.uiStrings;
// TODO: Implement language support.
$.extend(uiStrings, {
mathjax: {
embed_svg: 'Save as mathematics',
embed_mathml: 'Save as figure',
svg_save_warning: 'The math will be transformed into a figure is manipulatable like everything else. You will not be able to manipulate the TeX-code anymore. ',
mathml_save_warning: 'Advised. The math will be saved as a figure.',
title: 'Mathematics code editor'
}
});
function saveMath() {
var code = $('#mathjax_code_textarea').val();
// displaystyle to force MathJax NOT to use the inline style. Because it is
// less fancy!
MathJax.Hub.queue.Push(['Text', math, '\\displaystyle{' + code + '}']);
/*
* The MathJax library doesn't want to bloat your webpage so it creates
* every symbol (glymph) you need only once. These are saved in a <svg> on
* the top of your html document, just under the body tag. Each glymph has
* its unique id and is saved as a <path> in the <defs> tag of the <svg>
*
* Then when the symbols are needed in the rest of your html document they
* are refferd to by a <use> tag.
* Because of bug 1076 we can't just grab the defs tag on the top and add it
* to your formula's <svg> and copy the lot. So we have to replace each
* <use> tag by it's <path>.
*/
MathJax.Hub.queue.Push(
function() {
var mathjaxMath = $('.MathJax_SVG');
var svg = $(mathjaxMath.html());
svg.find('use').each(function() {
var x, y, transform;
// TODO: find a less pragmatic and more elegant solution to this.
if ($(this).attr('href')) {
var id = $(this).attr('href').slice(1); // Works in Chrome.
} else {
var id = $(this).attr('xlink:href').slice(1); // Works in Firefox.
}
var glymph = $('#' + id).clone().removeAttr('id');
x = $(this).attr('x');
y = $(this).attr('y');
transform = $(this).attr('transform');
if (transform && ( x || y )) {
glymph.attr('transform', transform + ' translate(' + x + ',' + y + ')');
}
else if (transform) {
glymph.attr('transform', transform);
}
else if (x || y) {
glymph.attr('transform', 'translate(' + x + ',' + y + ')');
}
$(this).replaceWith(glymph);
});
// Remove the style tag because it interferes with SVG-Edit.
svg.removeAttr('style');
svg.attr('xmlns', 'http://www.w3.org/2000/svg');
svgCanvas.importSvgString($('<div>').append(svg.clone()).html(), true);
svgCanvas.ungroupSelectedElement();
// TODO: To undo the adding of the Formula you now have to undo twice.
// This should only be once!
svgCanvas.moveSelectedElements(locationX, locationY, true);
}
);
}
return {
name: "MatJax",
svgicons: "extensions/mathjax-icons.xml",
buttons: [{
id: "tool_mathjax",
type: "mode",
title: "Add Mathematics",
events: {
'click': function() {
// Only load Mathjax when needed, we don't want to strain Svg-Edit any more.
// From this point on it is very probable that it will be needed, so load it.
if (mathjaxLoaded === false) {
$('<div id="mathjax">\
<!-- Here is where MathJax creates the math -->\
<div id="mathjax_creator" class="tex2jax_process" style="display:none">\
$${}$$\
</div>\
<div id="mathjax_overlay"></div>\
<div id="mathjax_container">\
<div id="tool_mathjax_back" class="toolbar_button">\
<button id="tool_mathjax_save">OK</button>\
<button id="tool_mathjax_cancel">Cancel</button>\
</div>\
<fieldset>\
<legend id="mathjax_legend">Mathematics Editor</legend>\
<label>\
<span id="mathjax_explication">Please type your mathematics in \
<a href="http://en.wikipedia.org/wiki/Help:Displaying_a_formula" target="_blank">TeX</a> code.</span></label>\
<textarea id="mathjax_code_textarea" spellcheck="false"></textarea>\
</fieldset>\
</div>\
</div>'
).insertAfter('#svg_prefs').hide();
// Make the MathEditor draggable.
$('#mathjax_container').draggable({cancel: 'button,fieldset', containment: 'window'});
// Add functionality and picture to cancel button.
$('#tool_mathjax_cancel').prepend($.getSvgIcon('cancel', true))
.on("click touched", function() {
$('#mathjax').hide();
});
// Add functionality and picture to the save button.
$('#tool_mathjax_save').prepend($.getSvgIcon('ok', true))
.on("click touched", function() {
saveMath();
$('#mathjax').hide();
});
// MathJax preprocessing has to ignore most of the page.
$('body').addClass('tex2jax_ignore');
// Now get (and run) the MathJax Library.
$.getScript(mathjaxSrcSecure)
.done(function(script, textStatus) {
// When MathJax is loaded get the div where the math will be rendered.
MathJax.Hub.queue.Push(function() {
math = MathJax.Hub.getAllJax('#mathjax_creator')[0];
console.log(math);
mathjaxLoaded = true;
console.log('MathJax Loaded');
});
})
// If it fails.
.fail(function() {
console.log("Failed loadeing MathJax.");
$.alert("Failed loading MathJax. You will not be able to change the mathematics.");
});
}
// Set the mode.
svgCanvas.setMode("mathjax");
}
}
}],
mouseDown: function() {
if (svgCanvas.getMode() === "mathjax") {
return {started: true};
}
},
mouseUp: function(opts) {
if (svgCanvas.getMode() === "mathjax") {
// Get the coordinates from your mouse.
var zoom = svgCanvas.getZoom();
// Get the actual coordinate by dividing by the zoom value
locationX = opts.mouse_x / zoom;
locationY = opts.mouse_y / zoom;
$('#mathjax').show();
return {started: false}; // Otherwise the last selected object dissapears.
}
},
callback: function() {
$('<style>').text('\
#mathjax fieldset{\
padding: 5px;\
margin: 5px;\
border: 1px solid #DDD;\
}\
#mathjax label{\
display: block;\
margin: .5em;\
}\
#mathjax legend {\
max-width:195px;\
}\
#mathjax_overlay {\
position: absolute;\
top: 0;\
left: 0;\
right: 0;\
bottom: 0;\
background-color: black;\
opacity: 0.6;\
z-index: 20000;\
}\
\
#mathjax_container {\
position: absolute;\
top: 50px;\
padding: 10px;\
background-color: #B0B0B0;\
border: 1px outset #777;\
opacity: 1.0;\
font-family: Verdana, Helvetica, sans-serif;\
font-size: .8em;\
z-index: 20001;\
}\
\
#tool_mathjax_back {\
margin-left: 1em;\
overflow: auto;\
}\
\
#mathjax_legend{\
font-weight: bold;\
font-size:1.1em;\
}\
\
#mathjax_code_textarea {\\n\
margin: 5px .7em;\
overflow: hidden;\
width: 416px;\
display: block;\
height: 100px;\
}\
').appendTo('head');
// Add the MathJax configuration.
//$(mathjaxConfiguration).appendTo('head');
}
};
});

View File

@ -0,0 +1,11 @@
<svg xmlns="http://www.w3.org/2000/svg">
<g id="tool_mathjax">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 43 43">
<g id="svg_20" stroke="black" fill="black" transform="matrix(0.0510725, 0, 0, -0.0510725, 0, 37.4108)">
<path id="svg_21" stroke-width="10" d="m20.45372,122.639297q0,57 19,104t49,78.000015t67,52t70,31t63.000015,9l3,0q45,0 78,-22t33,-65q0,-90.000015 -111,-118.000015q-49.000015,-13 -134.000015,-14q-37,0 -38,-2q0,-2 -6,-35t-7,-58q0,-46.999996 21,-73.999996t63,-28t93.000015,19t92,65.999996q9,10 12,10q4,0 13,-9t10,-13.999996t-9,-16t-30,-27t-46,-31t-63,-25t-76.000015,-10q-79,0 -122,53t-44,125.999996zm334.000015,185.000015q-6,52 -68,52q-33.000015,0 -61.000015,-14t-45,-34t-29,-41t-16,-36.000015t-5,-19q0,-1 20,-1q113.000015,0 158.000015,24t46,69.000015z"/>
<path id="svg_22" stroke-width="10" d="m489.156433,571.236389q4.949707,29.698486 38.183716,68.589355t82.024414,39.597961q24.748657,0 45.254761,-12.727905t30.40564,-31.819824q29.698486,44.547729 71.417847,44.547729q26.162842,0 45.254761,-15.556335t19.79895,-41.719299q0,-20.506104 -9.899414,-33.234009t-19.091919,-15.556396t-16.263428,-2.82843q-13.435059,0 -21.920288,7.778198t-8.485352,20.506104q0,32.526917 35.355347,44.547729q-7.778198,9.192383 -28.284302,9.192383q-9.192383,0 -13.434937,-1.414246q-26.870117,-11.31366 -39.598022,-46.669006q-42.426392,-156.270599 -42.426392,-182.433502q0,-19.799011 11.313721,-28.284302t24.748657,-8.485291q26.162964,0 51.618896,23.334534t34.648193,57.275635q2.121338,7.071106 4.242676,7.778198t11.313721,1.414185l2.828369,0q10.606567,0 10.606567,-5.65686q0,-0.707092 -1.414185,-7.778137q-11.313721,-40.305115 -43.840576,-71.417786t-75.6604,-31.112732q-49.497559,0 -74.953369,44.547729q-28.991333,-43.840576 -66.468018,-43.840576l-4.242676,0q-34.648193,0 -49.497437,18.384766t-15.556396,38.890869q0,22.62738 13.435059,36.769531t31.819824,14.142151q30.405518,0 30.405518,-29.698486q0,-14.142151 -8.485229,-24.748718t-16.263428,-14.142151t-9.192383,-3.535522l-2.121338,-0.707153q0,-0.707092 4.242676,-2.828369t11.313599,-4.949768t13.435059,-2.121338q25.455811,0 43.840698,31.819824q6.363892,11.313721 16.263428,48.083252t19.79895,76.367523t11.313721,46.669006q3.535522,19.091919 3.535522,27.577209q0,19.79895 -10.606567,28.284241t-24.041626,8.485291q-28.284302,0 -53.033081,-22.627441t-34.648193,-57.982727q-1.414185,-6.363953 -3.535522,-7.071106t-11.313721,-1.414185l-9.899536,0q-4.242554,4.242615 -4.242554,7.778198z"/>
</g>
</svg>
</g>
<g id="svg_eof"/>
</svg>

After

Width:  |  Height:  |  Size: 2.5 KiB