User:Gryllida/js/fact-check-in-source-editor.js

/* Author : Svetlana Tkachenko svetlana@members.fsf.org Licence: GPLv3+ Description: [beta] splits selected text into separate sentences, one per line, numbered list Version: 0.1 TODO: [ ] test it See also 

technical details of customizing the toolbar snippets of code for common additions to the toolbar
 * http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization
 * http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization/Library



mw.loader.using(['mediawiki.api'], function {	"use strict";	var splitBySentences = function(context){		var selection = context.$textarea.textSelection( 'getSelection' );		// Split the selection, prepare remarks		//sentencesArray = [];		var sentencesArray = selection.split('.');		var result = '';		var arrayLength = sentencesArray.length;		for (var i = 0; i < arrayLength; i++) {			var sentenceItem = sentencesArray[i];			sentenceItem = sentenceItem.trim;			result = result + '#' + sentenceItem + '.\r\n#:\r\n';		}		$( '#wpTextbox1' ).focus;        $.wikiEditor.modules.toolbar.fn.doAction( $( '#wpTextbox1' ).data( 'wikiEditor-context' ), {				'type': 'replace', 'options': { 'peri': result, }           },            $( '#wpTextbox1' ) );	};	// == WikiEditor codes ==	var customizeToolbar = function {		// add buttons for W and 		$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', { 'section': 'main', 'group': 'insert', 'tools': { 'splitBySentences': { label: 'One sentence per line', type: 'button', icon: '//upload.wikimedia.org/wikipedia/commons/thumb/b/b3/Circle-icons-stop.svg/22px-Circle-icons-stop.svg.png', 'action': { 'type': 'callback', execute: function(context){ splitBySentences(context); }					},				}			}		} );	};	/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */	if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {		mw.loader.using( 'user.options' ).then( function { // This can be the string "0" if the user disabled the preference (T54542) if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) { $.when(					mw.loader.using( 'ext.wikiEditor' ), $.ready				).then( customizeToolbar ); }		} );	}	// == WikiEditor codes end ==

});