Jump to content

Topic on Talk:VisualEditor/Gadgets/Add a tool

How to wrap selection in a simple template?

3
Varlin (talkcontribs)

Hi, following the examples given, I have been able to insert an all-defined template at cursor position, but unable to do something as simple as to wrap selection in a (mono-argument) template. I.e :

  1. I select "some text"
  2. I trigger the template
  3. I get "{{MyTemplate|some text}}"

I guess it is really simple but I spend hours trying to figure out how to do that... Any help would be highly appreciated !

EDIT : I precise I know how to do it using getText(), but it returns the plain text selection and so it removes the links and wikitext formatting.

Whatamidoing (WMF) (talkcontribs)

I don't know how to solve your problem, sorry.

Is it always the same template? I think this can be done with quotation marks, in the VisualEditor/Special characters tool, and (if your wiki only needs one template to work like this), I wonder if it might be faster and easier to pretend that {{MyTemplate| and }} were just strange quotation marks.

Varlin (talkcontribs)

In case it helps, here is the code I used to achieve what I wanted.

This one defineds a "CrossedOut" button that adds a simple template {{CrossedOut|MyText}} that put "strikethroug" formatting (with a custom infotip) to the selection.

ve.ui.CrossedOutCommand = function VeUiCrossedOutCommand() {
	ve.ui.CrossedOutCommand.super.call(	this, 'CrossedOut' );
};
OO.inheritClass( ve.ui.CrossedOutCommand, ve.ui.Command );

ve.ui.CrossedOutCommand.prototype.execute = function ( surface ) {
	var model = surface.getModel(),
		doc = model.getDocument(),
		range = model.getSelection().getRange(),
		docRange = doc.shallowCloneFromRange( range );

	ve.init.target.getWikitextFragment( docRange, false ).done( function ( wikitext ) {
		model.getFragment().insertContent([
			{
				type: 'mwTransclusionInline',
				attributes: {
					mw: {
						parts: [{
							template: {
								target: {
									href: 'Template:CrossedOut',
									wt: 'CrossedOut'
								},
								params: {
									1: {
										wt: wikitext
									}
								}
							}
						}]
					}
				}
			} , {
				type: '/mwTransclusionInline'
			} ] );
	} );
};

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

ve.ui.CrossedOutTool = function VeUiCrossedOutTool() {
	ve.ui.CrossedOutTool.super.apply( this, arguments );
};
OO.inheritClass( ve.ui.CrossedOutTool, ve.ui.Tool );
ve.ui.CrossedOutTool.static.name = 'CrossedOut';
ve.ui.CrossedOutTool.static.icon = 'strikethrough';
ve.ui.CrossedOutTool.static.title = OO.ui.deferMsg('CrossedOut');
ve.ui.CrossedOutTool.static.commandName = 'CrossedOut';
ve.ui.toolFactory.register( ve.ui.CrossedOutTool );