Welcome to Wargaming.net Wiki!

Difference between revisions of "User:XXN:na/wikilinker.js"

Jump to: navigation, search
Revision as of 18:05, 4 November 2016Revision as of 10:19, 5 November 2016
Line 84:Line 84:
  
  function processText ( txt ) {  function processText ( txt ) {
? var preparedText = txt+ var preparedText = txt.replace('/,/', ']], [[' );
? .substr( 0, 100 )+
? .replace('/,/', ']], [[' );+
  }  }
  $( '#wpTextbox1' )  $( '#wpTextbox1' )

Revision as of 10:19, 5 November 2016

// Wiki Linker
// Add links for selected text
// based on User:XXN:na/WPwikilinker.js


// Toolbar buttons
var addOldToolbarButton = function() {
	var $toolbar = $( '#gadget-toolbar' );
	if ( !$toolbar.length ) {
		$toolbar = $( '#wikiEditor-ui-toolbar' );
	}
	$( '<div>' )
		.addClass( 'tool tool-button wikiEditor-toolbar-spritedButton' )
		.attr( 'id', 'mw-editbutton-gadget-wikilinker' )
		.attr( 'alt', 'Wiki Linker' )
		.attr( 'title', 'Wiki Linker — add links for selected text' )
		.css( 'background-image', 'url(//upload.wikimedia.org/wikipedia/commons/a/ad/Wikilinker_toolbar.png) ' )
		.appendTo( $toolbar )
		.on( 'click', WikiLinker );
};

var addNewToolbarButton = function() {
	$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
		'section': 'main',
		'group': 'format',
		'tools': {
			'wikilinker': {
				label: 'Wiki Linker — add links for selected text',
				type: 'button',
				icon: '//upload.wikimedia.org/wikipedia/commons/b/bd/Wikilinker.png',
				action: {
					type: 'callback',
					execute: function() {
						WikiLinker();
					}
				}
			}
		}
	} );
};

if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
	mw.loader.using( [ 'user.options', 'jquery.textSelection' ], function () {
		if ( mw.user.options.get( 'usebetatoolbar' ) === 1 ) {
			if ( mw.user.options.get( 'showtoolbar' ) === 1 ) {
				$.when(
					mw.loader.using( 'ext.wikiEditor.toolbar' ),
					$.ready
				).then( addNewToolbarButton );
			}
		} else {
			mw.loader.using( 'mediawiki.action.edit', function() {
				$( addOldToolbarButton );
			} );
		}
	} );
}


function WikiLinker() {
 
	var CantWork = 'Select some text';
 	var $wpTextbox1 = $( '#wpTextbox1' );
 
	var txt = $wpTextbox1.textSelection( 'getSelection' );
	var startEndPos = $( '#wpTextbox1' ).textSelection( 'getCaretPosition', {
			startAndEnd: true
		} ),
		startPos = startEndPos[0],
	endPos = startEndPos[1];

if ( txt === '' ) {
		mw.notify( CantWork );
		return;
	}
	if ( startEndPos[0] !== startPos || startEndPos[1] !== endPos ) {
		$wpTextbox1.textSelection( 'setSelection', {
			start: startPos,
			end: endPos 
		} );
	}

	processText( txt );

	function processText ( txt ) {
		var preparedText = txt.replace('/,/', ']], [[' );
	}
				$( '#wpTextbox1' )
					.textSelection( 'encapsulateSelection', {
						peri: txt,
						replace: true
					} )
					.textSelection( 'scrollToCaretPosition' )
			 		.focus();
}