MediaWiki:Gadget-WikiEditor-TemplateTools.js

Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/** <nowiki>
 * WikiEditor tools for Template editing
 *
 * The WikiEditor-Tools-loader gadget will only load this when editing a
 * page in the Template namespace. Users not editing such pages will never have
 * it loaded.
 *
 * Suggest new help items on the talk page.
 */

( function () {

	'strict';

	var addTemplatesnippets = function ( wikiEditor ) {

		var makeSnippetRule = function ( label, pre, post, peri ) {
			var ret = {
				label: label,
				action: {
					type: 'encapsulate',
					options: {
						pre: pre
					}
				}
			};

			if ( peri ) {
				ret.action.options.periMsg = peri;
			}

			if ( post ) {
				ret.action.options.post = post;
			}
			return ret;
		};

		var snips;
		var title;

		if ( mw.config.get( 'wgTitle' ).match( /\/doc$/ ) ) {

			title = 'Template doc snippets';

			var base = mw.config.get( 'wgTitle' ).replace( /\/doc$/, '' );

			snips = {
				docexample: makeSnippetRule( '{{doc example}}',
					'{{doc example|content=\n<pre>\n', '\n</pre>\n\n}}' ),
				luatemp: makeSnippetRule( '{{lua}}',
					'{{lua|Module:', '}}', base ),
				useststemp: makeSnippetRule( '{{uses TemplateStyles}}',
					'{{uses TemplateStyles|Template:', '/styles.css}}', base ),
				templatedata: makeSnippetRule( 'TemplateData',
					'===Template data===\n{{TemplateDataHeader}}\n<templatedata>\n{}\n</templatedata>\n' )
			};
		} else {
			title = 'Template snippets';

			snips = {
				incnoinc: makeSnippetRule( 'includeonly/noinclude',
					'<includeonly>', '</includeonly><noinclude>\n{{documentation}}\n</noinclude>' ),
				module: makeSnippetRule( 'Invoke module',
					'{{#invoke:', '|function_name}}', mw.config.get( 'wgTitle' ) ),
				ts: makeSnippetRule( 'TemplateStyles',
					'<templatestyles src="', '/styles.css" />', mw.config.get( 'wgTitle' ) ),
				ifinline: makeSnippetRule( '#if parameter',
					'{{#if:{{{', '|}}}|THEN|ELSE}}', 'PARAMETER' )
			};
		}

		wikiEditor( 'addToToolbar', {
			section: 'main',
			groups: {
				list: {
					tools: {
						props: {
							label: title,
							icon: 'puzzle',
							type: 'select',
							list: snips
						}
					}
				}
			}
		} );
	};

	window.wsutils.wikiEditorConfigIncantation( addTemplatesnippets );

}() );
// </nowiki>