Diferencia entre revisiones de «MediaWiki:Gadget-Versalitas.js»

De EIFA - Estudios Interdisciplinares de las Fuentes Avilistas
Línea 1: Línea 1:
function makeUnlinkYearsTool() {
function makeMyTool() {
// Function to modify wikitext
//Create and register command
function unlinkYears( wikitext ) {
var myTemplate = [ {
return wikitext.replace( /\[\[(\d+)\]\]/g, '$1' )
type: 'mwTransclusionBlock',
.replace( /\[\[\d+\|(.*?)\]\]/g, '$1' );
attributes: {
}
mw: {
 
parts: [ {
// Create and register command
template: {
function UnlinkYearsCommand() {
target: {
UnlinkYearsCommand.parent.call( this, 'unlinkYears' );
href: 'Template:Versales',
}
wt: 'MyTemplate'
OO.inheritClass( UnlinkYearsCommand, ve.ui.Command );
},
 
params: {
UnlinkYearsCommand.prototype.execute = function ( surface ) {
1: {
var surfaceModel, fragment, wikitext, data = [], onCompleteText;
wt: 'my parameter'
// 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;
}, {
};
type: '/mwTransclusionBlock'
} ];


ve.ui.commandRegistry.register( new UnlinkYearsCommand() );
ve.ui.commandRegistry.register(
new ve.ui.Command( 'mycommand', 'content', 'insert', {
args: [ myTemplate, false, true ],
supportedSelections: [ 'linear' ]
} )
);


// Create, register and insert tool
//Create and register wikitext command
function UnlinkYearsTool() {
if ( ve.ui.wikitextCommandRegistry ) {
UnlinkYearsTool.parent.apply( this, arguments );
ve.ui.wikitextCommandRegistry.register(
new ve.ui.Command( 'mycommand', 'mwWikitext', 'wrapSelection', {
args: [ '{{Versales|', '}}', 'my parameter' ],
supportedSelections: [ 'linear' ]
} )
);
}
}
OO.inheritClass( UnlinkYearsTool, ve.ui.Tool );


UnlinkYearsTool.static.name = 'unlinkYears';
//Create and register tool
UnlinkYearsTool.static.group = 'textStyle';
function MyTool() {
UnlinkYearsTool.static.title = 'Unlink years';
MyTool.parent.apply( this, arguments );
UnlinkYearsTool.static.icon = 'noWikiText';
}
UnlinkYearsTool.static.commandName = 'unlinkYears';
OO.inheritClass( MyTool, ve.ui.MWTransclusionDialogTool );
UnlinkYearsTool.static.deactivateOnSelect = false;
 
UnlinkYearsTool.prototype.onUpdateState = function () {
UnlinkYearsTool.parent.prototype.onUpdateState.apply( this, arguments );
this.setActive( false );
};


ve.ui.toolFactory.register( UnlinkYearsTool );
MyTool.static.name = 'mytool';
MyTool.static.group = 'insert';
MyTool.static.title = 'My tool';
MyTool.static.commandName = 'mycommand';
ve.ui.toolFactory.register( MyTool );


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


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

Revisión del 14:32 22 ene 2022

function makeMyTool() {
	//Create and register command
	var myTemplate = [ {
		type: 'mwTransclusionBlock',
		attributes: {
			mw: {
				parts: [ {
					template: {
						target: {
							href: 'Template:Versales',
							wt: 'MyTemplate'
						},
						params: {
							1: {
								wt: 'my parameter'
							}
						}
					}
				} ]
			}
		}
	}, {
		type: '/mwTransclusionBlock'
	} ];

	ve.ui.commandRegistry.register(
		new ve.ui.Command( 'mycommand', 'content', 'insert', {
			args: [ myTemplate, false, true ],
			supportedSelections: [ 'linear' ]
		} )
	);

	//Create and register wikitext command
	if ( ve.ui.wikitextCommandRegistry ) {
		ve.ui.wikitextCommandRegistry.register(
			new ve.ui.Command( 'mycommand', 'mwWikitext', 'wrapSelection', {
				args: [ '{{Versales|', '}}', 'my parameter' ],
				supportedSelections: [ 'linear' ]
			} )
		);
	}

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

	MyTool.static.name = 'mytool';
	MyTool.static.group = 'insert';
	MyTool.static.title = 'My tool';
	MyTool.static.commandName = 'mycommand';
	ve.ui.toolFactory.register( MyTool );

}

// Initialize
mw.hook( 've.loadModules' ).add( function( addPlugin ) {
	addPlugin( makeMyTool );
} );