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, "root": true,
"extends": [ "extends": [
"wikimedia", "wikimedia/client-es6",
"wikimedia/mediawiki", "wikimedia/mediawiki",
"wikimedia/jquery" "wikimedia/jquery"
], ],

View file

@ -1,6 +1,7 @@
'use strict';
/* eslint-env node, es6 */ /* eslint-env node, es6 */
module.exports = function ( grunt ) { 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-banana-checker' );
grunt.loadNpmTasks( 'grunt-eslint' ); grunt.loadNpmTasks( 'grunt-eslint' );

View file

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

View file

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