MediaWiki:Gadget-Versalitas.js

De EIFA - Estudios Interdisciplinares de las Fuentes Avilistas

Nota: Despues de publicar, quizás necesite actualizar la caché de su navegador para ver los cambios.

  • Firefox/Safari: Mantenga presionada la tecla Shift mientras pulsa el botón Actualizar, o presiona Ctrl+F5 o Ctrl+R (⌘+R en Mac)
  • Google Chrome: presione Ctrl+Shift+R (⌘+Mayús+R en Mac)
  • Internet Explorer/Edge: mantenga presionada Ctrl mientras pulsa Actualizar, o presione Ctrl+F5
  • Opera: Presiona Ctrl+F5.
function makeUnlinkYearsTool() {
	// Function to modify wikitext
	function unlinkYears( wikitext ) {
		return wikitext.replace( /\[\[(\d+)\]\]/g, '$1' )
			.replace( /\[\[\d+\|(.*?)\]\]/g, '$1' );
	}

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

	UnlinkYearsCommand.prototype.execute = function ( surface ) {
		var surfaceModel, fragment, wikitext, data = [], onCompleteText;
		// Get fragment to work on
		surfaceModel = surface.getModel();
		fragment = surfaceModel.getFragment();
		if ( fragment.getSelection().isCollapsed() ) {
			surfaceModel.setLinearSelection(
				new ve.Range( 0, surfaceModel.getDocument().data.getLength() )
			);
			fragment = surfaceModel.getFragment();
			onCompleteText = true;
		}
		// Visual mode
		if ( ve.init.target.getSurface().getMode() !== 'source' ) {
			fragment.annotateContent(
				'clear',
				fragment.getAnnotations( true ).filter( function ( annotation ) {
					return annotation.getType() === 'link/mwInternal' &&
						/^\d+$/.test( annotation.getAttribute( 'normalizedTitle' ) );
				} )
			);
			return true;
		}
		// Source mode
		wikitext = fragment.getText( true ).replace( /^\n/, '' ).replace( /\n\n/g, '\n' );
		wikitext = unlinkYears( wikitext );
		wikitext.split( '' ).forEach( function ( c ) {
			if ( c === '\n' ) {
				data.push( { type: '/paragraph' } );
				data.push( { type: 'paragraph' } );
			} else {
				data.push( c );
			}
		} );
		if ( onCompleteText ) {
			fragment.insertContent( wikitext );
		} else {
			fragment.insertContent( data );
		}
		if ( onCompleteText ) {
			fragment.collapseToStart().select();
		}
		return true;
	};

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

	// Create, register and insert tool
	function UnlinkYearsTool() {
		UnlinkYearsTool.parent.apply( this, arguments );
	}
	OO.inheritClass( UnlinkYearsTool, ve.ui.Tool );

	UnlinkYearsTool.static.name = 'unlinkYears';
	UnlinkYearsTool.static.group = 'textStyle';
	UnlinkYearsTool.static.title = 'Unlink years';
	UnlinkYearsTool.static.icon = 'noWikiText';
	UnlinkYearsTool.static.commandName = 'unlinkYears';
	UnlinkYearsTool.static.autoAddToCatchall = false;
	UnlinkYearsTool.static.deactivateOnSelect = false;

	UnlinkYearsTool.prototype.onUpdateState = function () {
		UnlinkYearsTool.parent.prototype.onUpdateState.apply( this, arguments );
		this.setActive( false );
	};

	ve.ui.toolFactory.register( UnlinkYearsTool );

	ve.init.mw.DesktopArticleTarget.static.actionGroups[ 1 ].include.push( 'unlinkYears' );
}

// Initialize
mw.loader.using( 'ext.visualEditor.desktopArticleTarget.init' ).then( function () {
	mw.libs.ve.addPlugin( function () {
		return mw.loader.using( [ 'ext.visualEditor.core' ] )
			.then( function () {
				makeUnlinkYearsTool();
			} );
	} );
} );