MediaWiki:RegexpButton.js

From Wikisource
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/***
*Regular expressions 
*Author: ThomasV, Pathoschild
*Note : this tool uses the javascript syntax: use $ (and not \) to call a group
*Tutoriel : http://www.regular-expressions.info/tutorial.html
****/

/* create form */
function custom() {
 
	/* if already open */
	if(document.getElementById('regexform')) customremove()
	else {	
               var editbox = document.getElementById('wpTextbox1');
		/* container */
		var regexform = document.createElement('div');
		regexform.setAttribute('id','regexform');
		editbox.parentNode.insertBefore(regexform,editbox.parentNode.firstChild);
 		/* form tag */
		var formform = document.createElement('form');
		formform.setAttribute('id','regexformform');
		//formform.setAttribute('onSubmit','customgo(); return false;');
		regexform.appendChild(formform);

		// add input boxes
	        var newinput = document.createElement('input');
	        newinput.setAttribute('id','formsearch');
	        var newlabel = document.createElement('label');
	        newlabel.setAttribute('for','formsearch');
		newlabel.appendChild(document.createTextNode("Repl. "));

		formform.appendChild(newlabel);
		formform.appendChild(newinput);

		var newinput = document.createElement('input');
		newinput.setAttribute('id','formreplace');
		var newlabel = document.createElement('label');
		newlabel.setAttribute('for','formreplace');
		newlabel.appendChild(document.createTextNode(' par '));

		formform.appendChild(newlabel);
		formform.appendChild(newinput);

		// go! link
		var go_button = document.createElement('input');
		go_button.setAttribute('type',"button");
		go_button.setAttribute('onclick',"customgo();");
		go_button.setAttribute('title',"go!");
		go_button.setAttribute('value',"go!");
		formform.appendChild(go_button);

	}
}
 
 
 
/* run patterns */
function customgo() {
	/* get search and replace strings */

        var msearch = document.getElementById('formsearch').value;
	msearch = msearch.replace(/\\n/g, '\n');
 
        var mreplace = document.getElementById('formreplace').value;
	mreplace = mreplace.replace(/\\n/g, '\n');
 
	/* convert input to regex */
 
	// without delimiters
	if(!msearch.match(/^\s*\/[\s\S]*\/[a-z]*\s*$/i)) {
		var search_re = new RegExp(msearch,'g');
	}
	// with delimiters
	else {
		// break into parts
		var regpattern = msearch.replace(/^\s*\/([\s\S]*)\/[a-z]*\s*$/i,'$1');
		var regmodifiers = msearch.replace(/^\s*\/[\s\S]*\/([a-z]*)\s*$/,'$1');
		// filter invalid flags
		regmodifiers = regmodifiers.replace(/[^gim]/ig,'');
		var search_re = new RegExp(regpattern, regmodifiers);
	}
 
	/* perform */
	var editbox = document.getElementById('wpTextbox1');
	editbox.value = editbox.value.replace(search_re,mreplace);
}
 
/* remove form */
function customremove() {
	var regexform = document.getElementById('regexform');
	regexform.parentNode.removeChild(regexform);
	patterncount = -1;
}

function addButtonToWikiEditorToolbar( b ){
	var tools = {};
	tools[ b.imageId ] = {
		label: b.speedTip,
		type: 'button',
		icon: b.imageFile,
		action: {
			type: 'callback',
			execute: b.onClick
		}
	};
	$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
		section: 'proofreadpage-tools',
		group: 'other',
		tools: tools
	} );
	$( '[rel="' + b.imageId + '"]' ).width( 22 );
}

function addButtonToClassicToolbar( b ){
	mw.toolbar.addButton( {
		imageFile: b.imageFile,
		speedTip: b.speedTip,
		imageId: b.imageId
	} );
	$( '#' + b.imageId ).off( 'click' ).click( function () {
		b.onClick();
		return false;
	} ).width( 23 );
}

function customizeToolbar()
{
	if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) === -1) {
		return; // load only in edit mode
	}
	var modules, add, img;
	// This can be the string "0" if the user disabled the preference ([[bugzilla:52542#c3]])
	if( mw.user.options.get( 'usebetatoolbar' ) == 1 ){
		modules = [ 'ext.wikiEditor' ];
                if (mw.config.get('wgCanonicalNamespace') == 'Page') {
                        modules.push('ext.proofreadpage.page.edit');
                }
		img = '//upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Toolbaricon_RegEx.svg/22px-Toolbaricon_RegEx.svg.png';
		add = addButtonToWikiEditorToolbar;
	} else if ( mw.user.options.get( 'showtoolbar' ) == 1 ){
		modules = 'mediawiki.toolbar';
		img = '//upload.wikimedia.org/wikipedia/commons/a/a0/Button_references_alt.png';
		add = addButtonToClassicToolbar;
	} else {
		return;
	}
	$.when(
		mw.loader.using( modules ),
		$.ready
	).then( function(){
		add( {
			imageFile: img,
			speedTip: 'regexp',
			imageId: 'wsRegexp',
			onClick: custom
		} );
	} );
}

mw.loader.using( 'user.options' ).done( customizeToolbar );