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

From TermiKnowledge
Jump to navigation Jump to search
Line 35: Line 35:
},
},
{type:'/paragraph'},
{type:'/paragraph'},
'aaaa',
{type:'paragraph'},
'a',
{type:'/paragraph'}
{type:'/paragraph'}
];
];

Revision as of 01:32, 16 December 2021

function makeMyTool() {
	//Create and register command
	
	var commandName = 'mycommand';
	var title = 'Insert Section';
	
	var url = mw.util.getUrl();
	var page = mw.config.get('wgPageName');
	var namespace = mw.config.get('wgCanonicalNamespace');
	
	var baseLink = url.substr(0, url.length - page.length);
	var pageName = url.substr(url.length - page.length + (namespace.length > 0 ? (namespace.length + 1) : 0));
	
	var template = [
		{type:'paragraph'},
		{
			type: 'mwTransclusionBlock',
			attributes: {
				mw: {
					parts: [ {
						template: {
							i:0,
							target: {
								href: "./Term:Normative",
								wt: "Term:Normative"
							}
						},
						params: {}
					} ]
				}
			}
		},
		{
			type: '/mwTransclusionBlock'
		},
		{type:'/paragraph'},
		{type:'paragraph'},
		'a',
		{type:'/paragraph'}
	];
	
	var sections = [
		'Normative',
		'Research',
		'Press',
		'Comments'
	]
	
	var api = new mw.Api();
	/*
	sections.forEach(function(e){
		template.push({
			type: 'paragraph'
		});
		template.push({
			type: 'mwTransclusionBlock',
			attributes: {
				mw: {
					parts: [ {
						template: {
							i:0,
							target: {
								href: "./"+e+":"+pageName,
								wt: e+":"+pageName
							}
						},
						params: {}
					} ]
				}
			}
		});
		template.push({
			type: '/mwTransclusionBlock'
		});
		template.push({
			type: '/paragraph'
		});
	});
	*/
	
	var InsertSectionsCommand = function(name) {
		InsertSectionsCommand.super.call( this, name );
		this.subcommand = new ve.ui.Command( commandName, 'content', 'insert', {
			args: [ template, false, true ],
			supportedSelections: [ 'linear' ]
		});
	}
	OO.inheritClass( InsertSectionsCommand, ve.ui.Command );
	
	InsertSectionsCommand.prototype.execute = function(surface, args, source) {
		sections.forEach(function(section){
			api.create(section+":"+pageName, {}, 'test');
		});
		
		var ret = this.subcommand.execute(surface, args, source);
		
		return ret;
	}
	
	ve.ui.commandRegistry.register(new InsertSectionsCommand(commandName));

	//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 = title;
	MyTool.static.commandName = commandName;
	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();
			} );
	} );
} );