Use ES6 features, replace jQuery bind() with on()

Bug: T334853
Change-Id: I33ce4c07613a53ef394b4addee0e2b1b6c58455c
This commit is contained in:
Dringsim 2024-04-20 03:04:19 +08:00
parent dfdf5cb54a
commit a978a00d00
No known key found for this signature in database
GPG key ID: 7C5B1DFB8E73902C
4 changed files with 36 additions and 46 deletions

View file

@ -1,7 +1,7 @@
{
"root": true,
"extends": [
"wikimedia",
"wikimedia/client-es6",
"wikimedia/mediawiki",
"wikimedia/jquery"
],

View file

@ -1,6 +1,7 @@
'use strict';
/* eslint-env node, es6 */
module.exports = function ( grunt ) {
var conf = grunt.file.readJSON( 'extension.json' );
const conf = grunt.file.readJSON( 'extension.json' );
grunt.loadNpmTasks( 'grunt-banana-checker' );
grunt.loadNpmTasks( 'grunt-eslint' );

View file

@ -1,4 +1,4 @@
( function () {
( () => {
/**
* Debug console
@ -11,8 +11,8 @@
* * Collapsible backtrace display
*/
var histList = [ '' ],
histPos = 0,
const histList = [ '' ];
let histPos = 0,
question,
input,
output,
@ -85,33 +85,29 @@
}
function caretInFirstLine( textbox ) {
var firstLineBreak;
// IE doesn't support selectionStart/selectionEnd
if ( textbox.selectionStart === undefined ) {
return true;
}
firstLineBreak = textbox.value.indexOf( '\n' );
const firstLineBreak = textbox.value.indexOf( '\n' );
return ( ( firstLineBreak === -1 ) || ( textbox.selectionStart <= firstLineBreak ) );
}
function caretInLastLine( textbox ) {
var lastLineBreak;
// IE doesn't support selectionStart/selectionEnd
if ( textbox.selectionEnd === undefined ) {
return true;
}
lastLineBreak = textbox.value.lastIndexOf( '\n' );
const lastLineBreak = textbox.value.lastIndexOf( '\n' );
return ( textbox.selectionEnd > lastLineBreak );
}
function recalculateInputHeight() {
var rows = input.value.split( /\n/ ).length +
const rows = input.value.split( /\n/ ).length +
// prevent scrollbar flickering in Mozilla
1 +
// leave room for scrollbar in Opera
@ -124,9 +120,8 @@
}
function println( s, type ) {
var newdiv;
if ( ( s = String( s ) ) ) {
newdiv = document.createElement( 'div' );
const newdiv = document.createElement( 'div' );
newdiv.appendChild( document.createTextNode( s ) );
newdiv.className = type;
output.appendChild( newdiv );
@ -147,7 +142,7 @@
// (last item in histList) and should be reachable by pressing
// down again.
var L = histList.length;
const L = histList.length;
if ( L === 1 ) {
return;
@ -165,10 +160,9 @@
// Set to nothing first for the same reason
setTimeout(
function () {
var caretPos;
input.value = '';
input.value = histList[ histPos ];
caretPos = input.value.length;
const caretPos = input.value.length;
if ( input.setSelectionRange ) {
input.setSelectionRange( caretPos, caretPos );
}
@ -197,11 +191,9 @@
}
function printError( er ) {
var lineNumberString;
if ( er.name ) {
// lineNumberString should not be '', to avoid a very wacky bug in IE 6.
lineNumberString = ( er.lineNumber !== undefined ) ? ( ' on line ' + er.lineNumber + ': ' ) : ': ';
const lineNumberString = ( er.lineNumber !== undefined ) ? ( ' on line ' + er.lineNumber + ': ' ) : ': ';
// Because IE doesn't have error.toString.
println( er.name + lineNumberString + er.message, 'mw-scribunto-error' );
} else {
@ -223,8 +215,6 @@
}
function go() {
var params, api, content, sentContent;
if ( pending ) {
// If there is an XHR request pending, don't send another one
// We set readOnly on the textarea to give a UI indication, this is
@ -253,14 +243,14 @@
recalculateInputHeight();
printQuestion( question );
params = {
const params = {
action: 'scribunto-console',
title: mw.config.get( 'wgPageName' ),
question: question
};
content = getContent();
sentContent = false;
const content = getContent();
let sentContent = false;
if ( !sessionKey || sessionContent !== content ) {
params.clear = true;
@ -275,7 +265,7 @@
clearNextRequest = false;
}
api = new mw.Api();
const api = new mw.Api();
setPending();
api.postWithToken( 'csrf', params )
@ -319,7 +309,7 @@
}
function getContent() {
var $textarea = $( '#wpTextbox1' ),
const $textarea = $( '#wpTextbox1' ),
context = $textarea.data( 'wikiEditor-context' );
if ( context === undefined || context.codeEditor === undefined ) {
@ -336,13 +326,12 @@
}
function initEditPage() {
var $wpTextbox1,
$console = $( '#mw-scribunto-console' );
let $console = $( '#mw-scribunto-console' );
if ( !$console.length ) {
// There is no console in the DOM; on read-only (protected) pages,
// we need to add it here, because the hook does not insert
// it server-side.
$wpTextbox1 = $( '#wpTextbox1' );
const $wpTextbox1 = $( '#wpTextbox1' );
if ( !$wpTextbox1.length || !$wpTextbox1.prop( 'readonly' ) ) {
return;
}
@ -366,8 +355,8 @@
dir: 'ltr',
lang: 'en'
} )
.bind( 'keydown', inputKeydown )
.bind( 'focus', inputFocus )
.on( 'keydown', inputKeydown )
.on( 'focus', inputFocus )
)
)
.append(
@ -377,7 +366,7 @@
type: 'button',
value: mw.msg( 'scribunto-console-clear' )
} )
.bind( 'click', onClearClick )
.on( 'click', onClearClick )
)
)
.wrap( '<form>' )
@ -386,11 +375,11 @@
initConsole();
}
$( function () {
var action = mw.config.get( 'wgAction' );
$( () => {
const action = mw.config.get( 'wgAction' );
if ( action === 'edit' || action === 'submit' || action === 'editredlink' ) {
initEditPage();
}
} );
}() );
} )();

View file

@ -1,18 +1,18 @@
( function () {
( () => {
mw.hook( 'wikipage.content' ).add( function () {
var regex = /^mw-scribunto-error-(\w+)/,
popup;
mw.hook( 'wikipage.content' ).add( () => {
const regex = /^mw-scribunto-error-(\w+)/;
let popup;
$( '.scribunto-error' ).each( function ( index, span ) {
var matches = regex.exec( span.id );
$( '.scribunto-error' ).each( ( index, span ) => {
const matches = regex.exec( span.id );
if ( matches === null ) {
mw.log( 'mw.scribunto.errors: regex mismatch!' );
return;
}
var $span = $( span );
$span.on( 'click', function () {
var error = mw.config.get( 'ScribuntoErrors-' + matches[ 1 ] );
const $span = $( span );
$span.on( 'click', () => {
const error = mw.config.get( 'ScribuntoErrors-' + matches[ 1 ] );
if ( typeof error !== 'string' ) {
mw.log( 'mw.scribunto.errors: error ' + matches[ 1 ] + ' not found.' );
return;
@ -35,4 +35,4 @@
} );
} );
}() );
} )();