MediaWiki:Gadget-wikificator.js: различия между версиями

Материал из Каталог переславских телепередач
Перейти к навигации Перейти к поиску
Строка 147: Строка 147:
 
}
 
}
  
$.when(
+
if ( mw.config.get( 'wgServerName' ) === 'ru.wikipedia.org' ) {
mw.loader.using( [ 'mediawiki.util', 'user.options' ] ),
+
registerWikificatorTool();
$.getScript( 'https://ru.wikipedia.org/w/index.php?title=MediaWiki:Gadget-registerTool.js&action=raw&ctype=text/javascript' )
+
} else {
).done( registerWikificatorTool );
+
$.when(
 +
mw.loader.using( [ 'mediawiki.util', 'user.options' ] ),
 +
$.getScript( 'https://tv.museumpereslavl.ru/w/index.php?title=MediaWiki:Gadget-registerTool.js&action=raw&ctype=text/javascript' )
 +
).done( registerWikificatorTool );
 +
}
  
 
$( '#editform' ).on( 'keydown', function ( e ) {
 
$( '#editform' ).on( 'keydown', function ( e ) {

Версия 23:54, 6 января 2023

// <nowiki>
mw.loader.using( 'jquery.client', function () {

var clientProfile = $.client.profile();
var hotkey = clientProfile.platform === 'mac' ? 'Ctrl+Shift+W' : 'Ctrl+Alt+W';
var strings = {
	name: 'Викификатор',
	tooltip: 'Викификатор — автоматический обработчик текста (' + hotkey + ')',
	fullText: 'Викификатор обработает ВЕСЬ текст на этой странице. Продолжить?',
	talkPage: 'Викификатор не обрабатывает страницы обсуждения целиком.\n\nВыделите ваше сообщение — обработано будет только оно.'
};

// Function takes an input or text as an argument. If it is absent, it uses $( '#wpTextbox1' )
// as an input.
window.Wikify = function ( inputOrText ) {
	'use strict';

	// FUNCTIONS

	function r( r1, r2 ) {
		txt = txt.replace( r1, r2 );
	}

	function processText() {
		var i;

		r( / +(\n|\r)/g, '$1' ); // spaces at EOL
		txt = '\n' + txt + '\n';

		r( /«|»|“|”|„/g, '"' ); // temp

		// Hyphens and en dashes to pretty dashes
		r( /–/g, '-' ); // &ndash; -> hyphen
		r( /(\s)-{1,3} /g, '$1— ' ); // hyphen -> &mdash;
		r( /(\d)--(\d)/g, '$1—$2' ); // -> &mdash;

		// "" → «»
		for ( i = 1; i <= 2; i++ ) {
			r( /([\s\x02!|#'"\/([{;+\-])"([^"]*)([^\s"([{|])"([^a-zа-яё])/ig, '$1«$2$3»$4' ); // "
		}
		while ( /«[^»]*«/.test( txt ) ) {
			r( /«([^»]*)«([^»]*)»/g, '«$1„$2“' );
		}

		r( 'ёё' , '</div>\n<hr class=newtheme>\n<div class=newtheme>' );
		r( 'ёёё' , '<hr class=newtheme>\n' );

		txt = txt.substr( 1, txt.length - 2 ); // compensation for "txt = '\n' + txt + '\n';"
	}

	function processAllText() {
		txt = $input ? $input.textSelection( 'getContents' ) : text;
		processText();
		if ( $input ) {
			r( /^[\n\r]+/, '' );
			
			$input.textSelection( 'setContents', txt );
			if ( caretPosition ) {
				$input.textSelection( 'setSelection', {
					start: caretPosition[0] > txt.length ? txt.length : caretPosition[0]
				} );
			}
		} else {
			text = txt;
		}
	}

	// MAIN CODE
	
	// Check what's in the first parameter
	var text;
	var isInput;
	var $input;
	if ( typeof inputOrText === 'string' ) {
		text = inputOrText;
	} else {
		isInput = (
			inputOrText &&
			(
				( inputOrText.nodeType && inputOrText.value !== undefined ) || // node
				( inputOrText.prop && inputOrText.prop( 'nodeType' ) ) // jQuery object
			)
		);
		$input = $( isInput ? inputOrText : '#wpTextbox1' );
	}

	var txt = '';
	var hidden = [];
	var winScroll = document.documentElement.scrollTop;
	var caretPosition;
	if ( $input ) {
		$input.focus();
		
		caretPosition = $input.textSelection( 'getCaretPosition', { startAndEnd: true } );
		if ( caretPosition ) {
			var $CodeMirrorVscrollbar = $( '.CodeMirror-vscrollbar' );
			var textScroll = ( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input )
				.scrollTop();
			if ( caretPosition[0] === caretPosition[1] ) {
				processAllText();
			} else {
				txt = $input.textSelection( 'getSelection' );
				processText();
				// replaceSelection doesn't work with MediaWiki 1.30 in case this gadget is loaded
				// from other wiki
				$input.textSelection( 'encapsulateSelection', {
					replace: true,
					peri: txt
				} );
				// In CodeMirror, the selection isn't preserved, so we do it explicitly
				$input.textSelection( 'setSelection', {
					start: caretPosition[0],
					end: caretPosition[0] + txt.length
				} );
			}
			( $CodeMirrorVscrollbar.length ? $CodeMirrorVscrollbar : $input )
				.scrollTop( textScroll );
		// If something went wrong
		} else if ( confirm( strings.fullText ) ) {
			processAllText();
		}
	} else {
		processAllText();
		return text;
	}

	// scroll back, for 2017 wikitext editor, IE, Opera
	document.documentElement.scrollTop = winScroll;
};

function registerWikificatorTool() {
	registerTool( {
		name: 'wikificator',
		position: 100,
		title: strings.name,
		label: strings.tooltip,
		callback: Wikify,
		classic: {
			icon: '//upload.wikimedia.org/wikipedia/commons/0/06/Wikify-toolbutton.png',
		},
		visual: {
			icon: '//upload.wikimedia.org/wikipedia/commons/thumb/4/41/Wikificator_VE_icon.svg/20px-Wikificator_VE_icon.svg.png',
			modes: [ 'source' ],
			addRightAway: true,
		},
	} );
}

if ( mw.config.get( 'wgServerName' ) === 'ru.wikipedia.org' ) {
	registerWikificatorTool();
} else {
	$.when(
		mw.loader.using( [ 'mediawiki.util', 'user.options' ] ),
		$.getScript( 'https://tv.museumpereslavl.ru/w/index.php?title=MediaWiki:Gadget-registerTool.js&action=raw&ctype=text/javascript' )
	).done( registerWikificatorTool );
}

$( '#editform' ).on( 'keydown', function ( e ) {
	// Ctrl+Alt+W on Windows, Ctrl+Shift+W on Mac
	if (
		e.ctrlKey &&
		!e.metaKey &&
		(clientProfile.platform === 'mac' ? e.shiftKey && !e.altKey : !e.shiftKey && e.altKey) &&
		e.keyCode === 87
	) {
		Wikify();
	}
} );

}() );
// </nowiki>