User:ערן/mathjaxdialog.js

Source: Wikipedia, the free encyclopedia.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* 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=$('<div>');
		var mathInput=$('<input>',{dir:'ltr'}).appendTo(mathEditor);
		var mathPreview=$('<div>').appendTo(mathEditor);
		mathEditor.dialog({title: 'Equation editor', buttons: {
			'Insert': function(){
				$("#wpTextbox1").textSelection('encapsulateSelection', {replace: true, peri: '<math>'+mathInput.val()+'</math>'});
				$(this).dialog('close');
			},
			'Help':function(){window.open(mw.util.getUrl('Help:Math'))},
			'Cancel': function(){ $(this).dialog('close');}
		} });
		mathInput.keyup(function(){
			mathPreview.html('<span dir="ltr" class="tex">$ '+$(this).val()+' $</span>');
			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>(.*)<\/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 }
				}
			}
		} );
})

})