MediaWiki:Diacritice-ve.js

De la Wikipedia, enciclopedia liberă

Notă: După salvare, trebuie să treceți peste cache-ul browser-ului pentru a vedea modificările.

  • Mozilla, Google Chrome, Safari, Konqueror: țineți apăsat Shift în timp ce apăsați butonul Reload (sau apăsați Ctrl-Shift-R).
  • Internet Explorer: apăsați Ctrl-F5.
  • Opera: apăsați F5.
mw.hook('ve.saveDialog.stateChanged').add(function () {
    replaceAllTextDiacritics(ve.init.target.getSurface());
});

function replaceAllTextDiacritics(surface) {
	if (surface.getMode() === 'source') {
		var surfaceModel = surface.getModel();
		var documentModel = surfaceModel.getDocument();
		//get all document fragment
		var fragment = surfaceModel.getLinearFragment(documentModel.getDocumentRange(), true);
		var wikitext = ve.dm.sourceConverter.getSourceTextFromDataRange(fragment.getData());
		wikitext = sanitizeWikitext( wikitext );
		fragment.insertContent( ve.dm.sourceConverter.getDataFromSourceText(wikitext) );
	} else {
		replaceLetter("ş", "ș");
		replaceLetter("Ş", "Ș");
		replaceLetter("ţ", "ț");
		replaceLetter("Ţ", "Ț");
	}
	return true;
}

function replaceLetter(old, replace) {
	var surface = ve.init.target.getSurface();
    var surfaceModel = surface.getModel();
    var documentModel = surfaceModel.getDocument();
    var query = new RegExp(old, "g");
    var fragments = [];
    if (!documentModel.getInternalList().getListNode())
    	return;
    var ranges = documentModel.findText(query, true, true);
    var i, l;
    for (i = 0, l = ranges.length; i < l; i++) {
        fragment = surfaceModel.getLinearFragment(ranges[i], true, true);
        annotations = fragment.getAnnotations(true);
        if (annotations.getLength() > 0)
        	annotations = annotations.getAnnotationsByName('meta/language');
        if (annotations.getLength() === 0)
        	fragments.push(fragment);
    }
    for (i = 0, l = fragments.length; i < l; i++) {
        replaceOne(fragments[i], query, replace);
    }
}

function replaceOne(fragment, query, replace) {
    if (query instanceof RegExp) {
        fragment.insertContent(
            fragment.getText().replace(query, replace),
            true);
    } else {
        fragment.insertContent(replace, true);
    }
}

function makeDiacriticsTool() {

	// Create and register command
	ve.ui.DiacriticsCommand = function DiacriticsCommand() {
		ve.ui.DiacriticsCommand.parent.call( this, 'DiacriticsCommand' );
	};
	OO.inheritClass( ve.ui.DiacriticsCommand, ve.ui.Command );

	ve.ui.DiacriticsCommand.prototype.execute = replaceAllTextDiacritics;

	ve.ui.commandRegistry.register( new ve.ui.DiacriticsCommand() );

	ve.ui.sequenceRegistry.register(
		new ve.ui.Sequence(
			'DiacrticisCommand', // Sequence name
			'DiacriticsCommand', // Command name
			new RegExp("[ŢţŞş]"), // Text to detect
			0, // Number of characters to delete after the sequence is matched
			false, // setSelection
			false, // delayed
			true // checkOnPaste
		)
	);
	
}

mw.loader.using( 'ext.visualEditor.desktopArticleTarget.init' ).then( function () {
	mw.libs.ve.addPlugin( function () {
		return mw.loader.using( [ 'ext.visualEditor.core', 'ext.visualEditor.mwwikitext', 'ext.visualEditor.mwtransclusion' ] )
			.then( function () {
				makeDiacriticsTool();
			} );
	} );
} );

//TODO: in order to replace the template elements, I need to find a relevant hook and call the following:
// $('.ve-ui-mwParameterPage-field > .oo-ui-textInputWidget > textarea')[i].val(sanitizeWikitext($('.ve-ui-mwParameterPage-field > .oo-ui-textInputWidget > textarea')[i].val()))

//TODO: skip sensitive templates

//TODO: galleries - the editor is similar to templates, so we can just ignore it for now; wa are at feature-parity with the normal editor