User:ערן/mathjaxdialog.js

/* Math editor based on mathJax */

if ($.inArray(mw.config.get('wgAction'),['edit','submit'])!=-1) mw.loader.using(['ext.wikiEditor'],function(){

function mathDialog(){

mw.loader.using(['jquery.ui','ext.math.mathjax.enabler'],function(){

var mathEditor=$('

');

var mathInput=$('',{dir:'ltr'}).appendTo(mathEditor);

var mathPreview=$('

').appendTo(mathEditor);

mathEditor.dialog({title: 'Equation editor', buttons: {

'Insert': function(){

$("#wpTextbox1").textSelection('encapsulateSelection', {replace: true, peri: ''+mathInput.val()+''});

$(this).dialog('close');

},

'Help':function(){window.open(mw.util.getUrl('Help:Math'))},

'Cancel': function(){ $(this).dialog('close');}

} });

mathInput.keyup(function(){

mathPreview.html('$ '+$(this).val()+' $');

MathJax.Hub.Queue( ["Typeset", MathJax.Hub, mathPreview.get(0)])

});

var selection = $("#wpTextbox1").textSelection( 'getSelection' );

//insert the selected text into the equation editor

var mathRgx=/(.*)<\/math>/;

if(selection && mathRgx.test(selection)){

mathInput.val(mathRgx.exec(selection)[1]);

mathInput.keyup();

}

});

}

$(function(){

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {

'section': 'advanced',

'group': 'insert',

'tools': {

'formula': {

label: 'Math (LaTeX)',

type: 'button',

icon: '//upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Nuvola_apps_edu_mathematics_blue-p.svg/22px-Nuvola_apps_edu_mathematics_blue-p.svg.png',

action: {type: 'callback', execute: mathDialog }

}

}

} );

})

})