MediaWiki:Markup formatting.js

// When Ctrl/Cmd + 0 is pressed when the page editor is focused... // ... align the "=" in the selected text or entire editor if no text is selected. $('#wpTextbox1').keydown(function(ev) {   // Check if the user pressed Ctrl/Cmd and 0 (48 is the code for the 0 key)    if ((ev.ctrlKey || ev.metaKey) && ev.keyCode == 48)    {        // Get the selected text or all text        var selected_text = (window.getSelection.toString) ? window.getSelection.toString : this.value;        var cleaned_text  = ;        var pos           = 0;        var max_pos       = 0;        var diff          = 0;        var j             = 0;        var space         = ;

// Reduce the spacing between each parameter and the "=" to a single space. // Put each line in an array var lines        = selected_text.replace(/(\n\|\s*[^\s]*)\s*\=/g, '$1 \=').split('\n');

// Loop through each line to find the furthest out "=". for (var i = 0; i < lines.length; i++) {           pos = (lines[i].indexOf('|') == 0) ? lines[i].indexOf('=') : -1; if (pos > max_pos) max_pos = pos; }

// Loop through each line again for (i = 0; i < lines.length; i++) {           // Get the number of spaces to add pos    = (lines[i].indexOf('|') == 0) ? lines[i].indexOf('=') : -1; diff   = max_pos - pos; space  = ''; // Add the spaces for (j = 0; j < diff; j++) space += ' '; // Append the reformatted line into a new string for the reformatted text cleaned_text += (lines[i].indexOf('|') == 0) ? lines[i].replace('=', space+'=') : lines[i]; if (i != lines.length - 1) cleaned_text += '\n'; // add a line break, unless this is the last line }

// Replace the old text with the new text this.value = this.value.replace(selected_text, cleaned_text); } });