Difference between revisions of "MediaWiki:Gadget-Section creator.js"

From TermiKnowledge
Jump to navigation Jump to search
Line 1: Line 1:
function makeMyTool() {
function makeMyTool() {
var template = [
//Create and register command
{
var myTemplate = [ {
type: 'mwTransclusionBlock',
type: 'mwTransclusionBlock',
attributes: { mw: { parts: [
attributes: {
{
mw: {
template: {
parts: [ {
target: { href: 'Template:SectionBlock', wt: 'SectionBlock'},
template: {
params: { 1: { wt: 'name' }, 2: { wt: 'category' } }
target: {
}
href: 'Template:MyTemplate',
wt: 'MyTemplate'
},
params: {
1: {
wt: 'my parameter'
}
}
}
} ]
}
}
] } }
}
},{ type: '/mwTransclusionBlock' } ];
}, {
type: '/mwTransclusionBlock'
var commandName = 'termi_section_creator';
} ];
 
ve.ui.commandRegistry.register(
ve.ui.commandRegistry.register(
new ve.ui.Command(commandName, 'content', 'insert', {args: [ template, false, true], supportedSelections: ['linear'] } )
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: [ '{{MyTemplate|', '}}', 'my parameter' ],
supportedSelections: [ 'linear' ]
} )
);
}
 
//Create and register tool
function MyTool() {
function MyTool() {
MyTool.parent.apply( this, arguments );
MyTool.parent.apply( this, arguments );
}
}
OO.inheritClass( MyTool, ve.ui.MWTransclusionDialogTool );
OO.inheritClass( MyTool, ve.ui.MWTransclusionDialogTool );


Line 28: Line 50:
MyTool.static.group = 'insert';
MyTool.static.group = 'insert';
MyTool.static.title = 'My tool';
MyTool.static.title = 'My tool';
MyTool.static.commandName = commandName;
MyTool.static.commandName = 'mycommand';
ve.ui.toolFactory.register( MyTool );
ve.ui.toolFactory.register( MyTool );
 
console.log('Section creator installed');
}
}


//Initialize
mw.hook( 've.loadModules' ).add( function( addPlugin ) {
mw.hook( 've.loadModules' ).add( function( addPlugin ) {
addPlugin( function() {
addPlugin( function() {
return mw.loader.using( [ 'ext.visualEditor.core', 'ext.visualEditor.mwtransclusion' ] )
return mw.loader.using( [ 'ext.visualEditor.core', 'ext.visualEditor.mwwikitext', 'ext.visualEditor.mwtransclusion' ] )
.then( function() {
.then( function() {
makeMyTool();
makeMyTool();

Revision as of 16:57, 2 December 2021

function makeMyTool() {
	//Create and register command
	var myTemplate = [ {
		type: 'mwTransclusionBlock',
		attributes: {
			mw: {
				parts: [ {
					template: {
						target: {
							href: 'Template:MyTemplate',
							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: [ '{{MyTemplate|', '}}', '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( function() {
		return mw.loader.using( [ 'ext.visualEditor.core', 'ext.visualEditor.mwwikitext', 'ext.visualEditor.mwtransclusion' ] )
			.then( function() {
				makeMyTool();
			} );
	} );
} );