MediaWiki:Gadget-MonobookToolbarSortSelected.js
Apparence
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.if ( [ 'edit', 'submit' ].includes( mw.config.get( 'wgAction' ) ) ) {
mw.loader.using( [ 'ext.gadget.MonobookToolbar', 'jquery.textSelection' ], function () {
function selectEntireLines( $textbox ) {
var range = $textbox.textSelection( 'getCaretPosition', { startAndEnd: true } );
var start = range[ 0 ];
var end = range[ 1 ];
// Exclude leading/trailing newlines from selection
// This avoids some undesirable effects, namely:
// - This excludes leading/trailing empty lines from the sort
// - This excludes line if caret is on it but with no characters selected
var selText = $textbox.textSelection( 'getSelection' );
var leadingNewlines = selText.match( /^\n+/ );
if ( leadingNewlines ) {
start += leadingNewlines[ 0 ].length;
}
var trailingNewlines = selText.match( /\n+$/ );
if ( trailingNewlines ) {
end -= trailingNewlines[ 0 ].length;
}
// Expand selection to start of first selected line, and end of last selected line
var allText = $textbox[ 0 ].value;
start = allText.lastIndexOf( '\n', start );
if ( start === -1 ) {
start = 0;
} else {
start += 1;
}
end = allText.indexOf( '\n', end );
if ( end === -1 ) {
end = allText.length;
}
$textbox.textSelection( 'setSelection', { start: start, end: end } );
}
function sortSelectedLines( type ) {
var $textbox = $( MonobookToolbar.getCurrentFocused() );
selectEntireLines( $textbox );
var selText = $textbox.textSelection( 'getSelection' );
var lines = selText.split( '\n' );
lines.sort();
if ( type === 1 ) {
lines.reverse();
}
selText = lines.join( '\n' );
$textbox.textSelection( 'replaceSelection', selText );
$textbox.focus();
}
MonobookToolbar.addButton( {
imageFile: '/media/wikipedia/commons/6/6f/Button_arrow_up.PNG',
speedTip: 'Tri croissant',
imageId: 'mw-editbutton-sortascending',
callback: function () {
sortSelectedLines( 0 );
}
} );
MonobookToolbar.addButton( {
imageFile: '/media/wikipedia/commons/2/2b/Button_arrow_down.PNG',
speedTip: 'Tri décroissant',
imageId: 'mw-editbutton-sortdescending',
callback: function () {
sortSelectedLines( 1 );
}
} );
} );
}