MediaWiki:Gadget-veAutocorrect.js

// 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>

	});
});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.
Kembali kehalaman sebelumnya