MediaWiki:Gadget-DeveloperEditorTweaks.js

'use strict'; /*jshint undef:true */ // /*global mw, jQuery */ if ((mw.config.get('wgAction') === 'edit') || (mw.config.get('wgAction') === 'submit')) jQuery(document).ready(function {

var isCoding = { 'javascript': true, 'css': true, 'Scribunto': true }; var wgPageContentModel = mw.config.get('wgPageContentModel');

var wpTextbox1 = jQuery('#wpTextbox1'); if (!wpTextbox1.length) { console.error("det: failed to find WikiEditor entry point"); return; } if (!wpTextbox1.wikiEditor) { console.error("det: WikiEditor not loaded?"); return; }

if (wgPageContentModel !== 'wikitext') { // remove irrelevant buttons // XXX: this should be reported to Bugzilla, really wpTextbox1.wikiEditor('removeFromToolbar', {		'section': 'main',		'group': 'insert'	}); wpTextbox1.wikiEditor('removeFromToolbar', {		'section': 'main',		'group': 'format'	}); wpTextbox1.wikiEditor('removeFromToolbar', {		'section': 'help'	}); wpTextbox1.wikiEditor('removeFromToolbar', {		'section': 'advanced',		'group': 'heading'	}); wpTextbox1.wikiEditor('removeFromToolbar', {		'section': 'advanced',		'group': 'format'	}); wpTextbox1.wikiEditor('removeFromToolbar', {		'section': 'advanced',		'group': 'size'	}); wpTextbox1.wikiEditor('removeFromToolbar', {		'section': 'advanced',		'group': 'insert'	}); }

// workaround 118993 mw.util.addCSS(	".group-codeeditor-tools," +	".codeEditor-ui-toolbar .group-insert," +	".codeEditor-ui-toolbar .group-format," +	".codeEditor-ui-toolbar .tabs span.tab-advanced," +	".codeEditor-ui-toolbar .tabs span.tab-characters," +	".codeEditor-ui-toolbar .tabs span.tab-help," +	".codeEditor-ui-toolbar .sections {" +	"	display: block !important;" +	"}");

if (isCoding[wgPageContentModel]) { wpTextbox1.wikiEditor('addToToolbar', {		'section': 'main',		'groups': {			'codefmt': {				'tools': {					'normws': {						'type': 'button',						'label': 'Normalise whitespace',						'icon': '//upload.wikimedia.org/wikipedia/commons/thumb/0/00/VisualEditor_-_Icon_-_Indent-list-ltr.svg/24px-VisualEditor_-_Icon_-_Indent-list-ltr.svg.png', // XXX						'action': {							'type': 'callback',							'execute': function (context) {								function normws(text, lineStart, lineEnd) {									text = text.replace(lineEnd ? /[ \t]+(\n|$)/g : /[ \t]+(\n)/g, '$1');									var step = 4; // XXX: guess it by inspecting the source?									text = text.replace(lineStart ? /(^|\n)([\t ]+)/g : /(\n)([\t ]+)/g, function (_, nl, indent) { indent = indent.replace(/   /g, '\t').replace(/ +\t/g, '\t').replace(/\t/g, '    '); var steps = Math.floor(indent.length / step); indent = ''; for (var i = 0; i < steps; ++i) indent += '\t'; return nl + indent; });									return text;								}

if (!context.$textarea.textSelection('getSelection')) { if (context.codeEditor) context.codeEditor.selectAll; else context.$textarea[0].select; }

context.$textarea.textSelection('encapsulateSelection', {									replace: true, selectPeri: true,									peri: normws(context.$textarea[0].value, true, true)								}); }						}					}				}			}		}	}); }

if ((wgPageContentModel === 'javascript') || (mw.config.get('wgPageName') === 'MediaWiki:Gadgets-definition')) { var moduleDescriptions = { 'mediawiki.Title': " object", 'mediawiki.Uri' : "  object", 'mediawiki.util' : " ", 'mediawiki.user' : " ", 'moment'        : "Moment.js", 'site'          : "Site-specific scripts: MediaWiki:Common.js and per-skin JS", 'es5-shim'      : "ECMAScript 5 shim for older browsers",

'':""	};

wpTextbox1.wikiEditor('addToToolbar', {		'sections': {			'js': {				'type': 'booklet',				'label': "JavaScript reference",				'pages': {					'modules': {						'layout': 'table',						'label': "ResourceLoader modules",						'headings': [							{ 'text': "Identifier" },							{ 'text': "Description" }						],						'rows': mw.loader.getModuleNames.sort.filter(function (modname) { // they are quite dull modules, but they are there if (/^ext\.geshi\.language\./.test(modname)) return false; if (/^ext\.math\.mathjax\.jax\.output\./.test(modname)) return false; return true; }).map(function (modname) { return { 'id': { 'html': ' ', },								'desc': { 'html': moduleDescriptions[modname] || '(no description) ' }							};						})					}				}			}		}	}); }

if (wgPageContentModel === 'javascript') { var jshintDescriptions = { 'bitwise'                  : "Prohibit bitwise operators", 'camelcase'                : "Mandate   (deprecated)", 'curly'                    : "Mandate curly brackets in control structures", 'eqeqeq'                   : "Prohibit   and  ", 'es3'                      : "Mandate ECMAScript 3 compatibility (deprecated; use  )", 'es5'                      : "Mandate ECMAScript 5 compatibility (deprecated; use  )", 'esversion: n '   : 'Mandate compatibility with an ECMAScript version (3, 5, or 6)', 'forin'                    : "Mandate   in   loops", 'freeze'                   : "Prohibit changing prototypes of built-in objects", 'futurehostile'            : "Warn about identifiers defined in future versions of JavaScript", 'globals'                  : "Disable warnings for these globals", 'immed'                    : "Mandate parentheses around immediately-executed function expressions (deprecated)", 'indent: n '      : "Set tab width (deprecated)", 'latedef'                  : "Prohibit using variables before their declaration", 'newcap'                   : "Mandate capitalisation of constructors (deprecated)", 'noarg'                    : "Prohibit use of   and  ", 'noempty'                  : "Warn about empty blocks (deprecated)", 'nonbsp'                   : "Warn about non-breaking space", 'nonew'                    : "Prohibit constructor usage for side effects", 'plusplus'                 : "Prohibit unary increment and decrement operators", 'quotmark:true'            : "Mandate consistency of quotation marks (deprecated)", 'quotmark:single'          : "Mandate single quotation marks only (deprecated)", 'quotmark:double'          : "Mandata double quotation marks only (deprecated)", 'undef'                    : "Prohibit use of variables not explicitly declared", 'unused'                   : "Warn about unused variables", 'varstmt'                  : "Prohibit   (use   or  )", 'strict'                   : "Mandate strict mode compatibility", 'maxparams: n '   : "Enforce maximum number of formal parameters", 'maxdepth: n '    : "Enforce maximum depth of nested blocks", 'maxstatements'            : "Enforce maximum number of statements per function", 'maxcomplexity: n ': "Enforce maximum cyclomatic complexity", 'maxlen'                   : "Enforce maximum length of a line (deprecated)", 'asi'                      : "Allow semicolon insertion", 'boss'                     : "Allow assignment-expressions", 'debug'                    : "Allow   statements", 'eqnull'                   : "Allow   comparisons", 'esnext'                   : "Allow ES.next syntax (deprecated; use  )", 'evil'                     : "Allow  ", 'expr'                     : "Allow expressions where statements are expected", 'funcscope'                : "Allow using variables outside the block where they are declared", 'globalstrict'             : "Allow global strict mode (deprecated)", 'iterator'                 : "Allow using  ", 'lastsemic'                : "Allow omitting semicolons last statement", 'laxbreak'                 : "Allow lax line breaks (deprecated)", 'laxcomma'                 : "Allow comma-first coding style (deprecated)", 'loopfunc'                 : "Allow making functions within a loop", 'maxerr: n '      : "Set maximum number of warnings", 'moz'                      : "Allow Mozilla extensions", 'multistr'                 : "Allow multi-line string literals (deprecated)", 'notypeof'                 : "Allow lax usage of  ", 'proto'                    : "Allow using  ", 'scripturl'                : "Allow   URLs", 'shadow'                   : "Allow variable shadowing", 'sub'                      : "Allow bracket notation for object member access (deprecated)", 'supernew'                 : "Allow atypical constructor usage", 'validthis'                : "  is valid in this function", 'noyield'                  : "Allow generators without  ",

'browser'                  : "Assume a browser environment", 'devel'                    : "Assume   is available", 'jquery'                   : "Assume jQuery is available", 'nonstandard'              : "Assume non-standard global functions are available", 'worker'                   : "Assume Web Worker globals are available",

/* probably not useful: assume a framework is available 'couch'                : "", 'dojo'                 : "", 'mootools'             : "", 'node'                 : "", 'phantom'              : "", 'prototypejs'          : "", 'rhino'                : "", 'wsh'                  : "", 'yui'                  : "", */	};

wpTextbox1.wikiEditor('addToToolbar', {		'section': 'js',		'pages': {			'jshint': {				'layout': 'table',				'label': "JSHint options",				'headings': [					{ 'text': "Option" },					{ 'text': "Description" }				],				'rows': Object.keys(jshintDescriptions).sort.map(function (option) { return { 'option': { 'html': ' ' },						'description': { 'html': jshintDescriptions[option] }					};				})			}		}	}); }

if (wgPageContentModel === 'Scribunto') { // XXX: move console upwards }

});