MediaWiki:Gadget-veAutocorrect.js

Z Wikipedii, wolnej encyklopedii

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
// Adapted from: https://de.wikipedia.org/w/index.php?title=Benutzer:Schnark/js/veAutocorrect.js&oldid=162304816

mw.loader.using('ext.visualEditor.desktopArticleTarget.init').done(function () {
	mw.libs.ve.addPlugin(function () {

//Dokumentation unter [[Benutzer:Schnark/js/veAutocorrect]] <nowiki>
/*global mediaWiki, OO, ve*/
(function (mw) {
"use strict";

var VISUAL = 1, SOURCE = 2, deps;

function initAutoCorrect (lang, wiki) {
	var autoCorrectCommandCount = 0, quotes;

	//like ve.ui.Sequence, with the difference that for regular expressions
	//of the form /foo(bar)/ only the parentheses is used as Range, not the whole expression
	function ReSequence () {
		ReSequence.parent.apply(this, arguments);
	}
	OO.inheritClass(ReSequence, ve.ui.Sequence);
	ReSequence.prototype.match = function (data, offset, plaintext) {
		var execResult;
		if (this.data instanceof RegExp) {
			execResult = this.data.exec(plaintext);
			return execResult && new ve.Range(offset - execResult[1].length, offset);
		}
		return ReSequence.parent.prototype.match.apply(this, arguments);
	};

	//when the user enters "from" change it to "to"
	//from can be a string, a regular expression of the form /foo(bar)/ or an array of data
	//to can be a string or an array of data
	//strip is the number of chars to strip
	//mode defaults to both VISUAL and SOURCE
	function autoCorrectFromTo (from, to, strip, mode) {
		//get a unique name, we use it for both the command and the sequence
		var name = 'schnarkAutoCorrectCommand-' + (autoCorrectCommandCount++), seq;
		//create and register the command
		ve.ui.commandRegistry.register(
			//1st true: annotate, 2nd true: collapse to end
			new ve.ui.Command(name, 'content', 'insert', {args: [to, true, true]})
		);
		//create and register the sequence
		seq = new ReSequence(/*sequence*/ name, /*command*/ name, from, strip);
		if (!mode || mode === VISUAL) {
			ve.ui.sequenceRegistry.register(seq);
		}
		if ((!mode || mode === SOURCE) && ve.ui.wikitextSequenceRegistry) {
			ve.ui.wikitextSequenceRegistry.register(seq);
		}
	}

	//define what should be autocorrected
	quotes = {
		de: ['„', '“'],
		en: ['“', '”'],
		pl: ['„', '”']
	};

	// disable wikitext warning for italics
	ve.ui.sequenceRegistry.register(
		new ve.ui.Sequence( 'wikitextItalic', 'doNothing', '\'\'' )
	);

	//(?:^|=\s*"[^"]*") - anchor at the start of the line or a quoted attribute
	//[^=]* - skip chars that aren't equal signs
	//=\s*(?:[^" ]|"[^"]*") - equal sign either not followed by quote mark or with complete quoted attribute
	//depending on the content language
	if (quotes.hasOwnProperty(lang)) {
		// straight quotation marks "" to localized quotation marks
		autoCorrectFromTo(/(?:^|[( \n])(")$/, quotes[lang][0], 1, VISUAL);
		autoCorrectFromTo(/(?:^|=\s*"[^"]*")[^=]*(?:=\s*(?:[^" ]|"[^"]*")[^=]*)*(?:^|[( \n])(")$/, quotes[lang][0], 1, SOURCE);
		autoCorrectFromTo(/[^\d( \n](")$/, quotes[lang][1], 1, VISUAL);
		autoCorrectFromTo(/(?:^|=\s*"[^"]*")[^=]*(?:=\s*(?:[^" ]|"[^"]*")[^=]*)*[^\d( \n=](")$/, quotes[lang][1], 1, SOURCE);
		// double apostrophes '' to localized quotation marks
		autoCorrectFromTo(/(?:^|[( \n])('')$/, quotes[lang][0], 2, VISUAL);
		autoCorrectFromTo(/[^\d( \n]('')$/, quotes[lang][1], 2, VISUAL);
	}
}

deps = ['ext.visualEditor.core'];
if (mw.libs.ve.isWikitextAvailable) {
	deps.push('ext.visualEditor.mwwikitext');
}

mw.loader.using(deps).done(function () {
	initAutoCorrect(mw.config.get('wgContentLanguage'), mw.config.get('wgDBname'));
	mw.hook('userjs.script-ready.veAutocorrect').fire();
});

})(mediaWiki);
//</nowiki>

	});
});