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

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

/*
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 <https://en.wikinews.org/wiki/User:Gryllida/Tasks>

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

*/ 

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 {{tl|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 ([[phab:T54542#555387]])
			if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {
				$.when(
					mw.loader.using( 'ext.wikiEditor' ), $.ready
				).then( customizeToolbar );
			}
		} );
	}
	// == WikiEditor codes end ==

});