| 1 |
( () => { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
const histList = [ '' ]; |
| 15 |
let histPos = 0, |
| 16 |
question, |
| 17 |
input, |
| 18 |
output, |
| 19 |
$spinner, |
| 20 |
sessionContent = null, |
| 21 |
sessionKey = null, |
| 22 |
pending = false, |
| 23 |
clearNextRequest = false; |
| 24 |
|
| 25 |
function refocus() { |
| 26 |
|
| 27 |
input.blur(); |
| 28 |
input.focus(); |
| 29 |
} |
| 30 |
|
| 31 |
function initConsole() { |
| 32 |
input = document.getElementById( 'mw-scribunto-input' ); |
| 33 |
output = document.getElementById( 'mw-scribunto-output' ); |
| 34 |
$spinner = $.createSpinner( { size: 'small', type: 'block' } ); |
| 35 |
|
| 36 |
recalculateInputHeight(); |
| 37 |
println( mw.msg( 'scribunto-console-intro' ), 'mw-scribunto-message' ); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
function inputKeydown( e ) { |
| 46 |
if ( e.shiftKey && e.keyCode === 13 ) { |
| 47 |
|
| 48 |
|
| 49 |
} else if ( e.keyCode === 13 ) { |
| 50 |
|
| 51 |
|
| 52 |
go(); |
| 53 |
} else if ( e.keyCode === 38 ) { |
| 54 |
|
| 55 |
|
| 56 |
if ( e.ctrlKey || caretInFirstLine( input ) ) { |
| 57 |
hist( 'up' ); |
| 58 |
} |
| 59 |
} else if ( e.keyCode === 40 ) { |
| 60 |
|
| 61 |
|
| 62 |
if ( e.ctrlKey || caretInLastLine( input ) ) { |
| 63 |
hist( 'down' ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
setTimeout( recalculateInputHeight, 0 ); |
| 68 |
} |
| 69 |
|
| 70 |
function inputFocus() { |
| 71 |
if ( sessionContent === null ) { |
| 72 |
|
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if ( clearNextRequest ) { |
| 77 |
|
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
if ( getContent() !== sessionContent ) { |
| 82 |
printClearBar( 'scribunto-console-cleared' ); |
| 83 |
clearNextRequest = true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
function caretInFirstLine( textbox ) { |
| 88 |
|
| 89 |
if ( textbox.selectionStart === undefined ) { |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
const firstLineBreak = textbox.value.indexOf( '\n' ); |
| 94 |
|
| 95 |
return ( ( firstLineBreak === -1 ) || ( textbox.selectionStart <= firstLineBreak ) ); |
| 96 |
} |
| 97 |
|
| 98 |
function caretInLastLine( textbox ) { |
| 99 |
|
| 100 |
if ( textbox.selectionEnd === undefined ) { |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
const lastLineBreak = textbox.value.lastIndexOf( '\n' ); |
| 105 |
|
| 106 |
return ( textbox.selectionEnd > lastLineBreak ); |
| 107 |
} |
| 108 |
|
| 109 |
function recalculateInputHeight() { |
| 110 |
const rows = input.value.split( /\n/ ).length + |
| 111 |
|
| 112 |
1 + |
| 113 |
|
| 114 |
( window.opera ? 1 : 0 ); |
| 115 |
|
| 116 |
|
| 117 |
if ( input.rows !== rows ) { |
| 118 |
input.rows = rows; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
function println( s, type ) { |
| 123 |
if ( ( s = String( s ) ) ) { |
| 124 |
const newdiv = document.createElement( 'div' ); |
| 125 |
newdiv.appendChild( document.createTextNode( s ) ); |
| 126 |
newdiv.className = type; |
| 127 |
output.appendChild( newdiv ); |
| 128 |
return newdiv; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
function printClearBar( msg ) { |
| 133 |
$( '<div>' ) |
| 134 |
.attr( 'class', 'mw-scribunto-clear' ) |
| 135 |
.text( mw.msg( msg ) ) |
| 136 |
.appendTo( output ); |
| 137 |
} |
| 138 |
|
| 139 |
function hist( direction ) { |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
const L = histList.length; |
| 146 |
|
| 147 |
if ( L === 1 ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
if ( direction === 'up' ) { |
| 152 |
if ( histPos === L - 1 ) { |
| 153 |
|
| 154 |
histList[ histPos ] = input.value; |
| 155 |
} |
| 156 |
|
| 157 |
if ( histPos > 0 ) { |
| 158 |
histPos--; |
| 159 |
|
| 160 |
|
| 161 |
setTimeout( |
| 162 |
() => { |
| 163 |
input.value = ''; |
| 164 |
input.value = histList[ histPos ]; |
| 165 |
const caretPos = input.value.length; |
| 166 |
if ( input.setSelectionRange ) { |
| 167 |
input.setSelectionRange( caretPos, caretPos ); |
| 168 |
} |
| 169 |
}, |
| 170 |
0 |
| 171 |
); |
| 172 |
} |
| 173 |
} else { |
| 174 |
|
| 175 |
if ( histPos < L - 1 ) { |
| 176 |
histPos++; |
| 177 |
input.value = histList[ histPos ]; |
| 178 |
} else if ( histPos === L - 1 ) { |
| 179 |
|
| 180 |
if ( input.value ) { |
| 181 |
histList[ histPos ] = input.value; |
| 182 |
++histPos; |
| 183 |
input.value = ''; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
function printQuestion( q ) { |
| 190 |
println( q, 'mw-scribunto-input' ); |
| 191 |
} |
| 192 |
|
| 193 |
function printError( er ) { |
| 194 |
if ( er.name ) { |
| 195 |
|
| 196 |
const lineNumberString = ( er.lineNumber !== undefined ) ? ( ' on line ' + er.lineNumber + ': ' ) : ': '; |
| 197 |
|
| 198 |
println( er.name + lineNumberString + er.message, 'mw-scribunto-error' ); |
| 199 |
} else { |
| 200 |
|
| 201 |
println( er, 'mw-scribunto-error' ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
function setPending() { |
| 206 |
pending = true; |
| 207 |
input.readOnly = true; |
| 208 |
$spinner.insertBefore( input ); |
| 209 |
} |
| 210 |
|
| 211 |
function clearPending() { |
| 212 |
$spinner.remove(); |
| 213 |
pending = false; |
| 214 |
input.readOnly = false; |
| 215 |
} |
| 216 |
|
| 217 |
function go() { |
| 218 |
if ( pending ) { |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
question = input.value; |
| 226 |
|
| 227 |
if ( question === '' ) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
histList[ histList.length - 1 ] = question; |
| 232 |
histList[ histList.length ] = ''; |
| 233 |
histPos = histList.length - 1; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
input.value = ''; |
| 238 |
|
| 239 |
setTimeout( () => { |
| 240 |
input.value = ''; |
| 241 |
}, 0 ); |
| 242 |
|
| 243 |
recalculateInputHeight(); |
| 244 |
printQuestion( question ); |
| 245 |
|
| 246 |
const params = { |
| 247 |
action: 'scribunto-console', |
| 248 |
title: mw.config.get( 'wgPageName' ), |
| 249 |
question: question |
| 250 |
}; |
| 251 |
|
| 252 |
const content = getContent(); |
| 253 |
let sentContent = false; |
| 254 |
|
| 255 |
if ( !sessionKey || sessionContent !== content ) { |
| 256 |
params.clear = true; |
| 257 |
params.content = content; |
| 258 |
sentContent = true; |
| 259 |
} |
| 260 |
if ( sessionKey ) { |
| 261 |
params.session = sessionKey; |
| 262 |
} |
| 263 |
if ( clearNextRequest ) { |
| 264 |
params.clear = true; |
| 265 |
clearNextRequest = false; |
| 266 |
} |
| 267 |
|
| 268 |
const api = new mw.Api(); |
| 269 |
setPending(); |
| 270 |
|
| 271 |
api.postWithToken( 'csrf', params ) |
| 272 |
.done( ( result ) => { |
| 273 |
if ( result.sessionIsNew === '' && !sentContent ) { |
| 274 |
|
| 275 |
printClearBar( 'scribunto-console-cleared-session-lost' ); |
| 276 |
sessionContent = null; |
| 277 |
clearPending(); |
| 278 |
input.value = params.question; |
| 279 |
go(); |
| 280 |
return; |
| 281 |
} |
| 282 |
sessionKey = result.session; |
| 283 |
sessionContent = content; |
| 284 |
if ( result.type === 'error' ) { |
| 285 |
$( '<div>' ).addClass( 'mw-scribunto-error' ).html( result.html ).appendTo( output ); |
| 286 |
} else { |
| 287 |
if ( result.print !== '' ) { |
| 288 |
println( result.print, 'mw-scribunto-print' ); |
| 289 |
} |
| 290 |
if ( result.return !== '' ) { |
| 291 |
println( result.return, 'mw-scribunto-normalOutput' ); |
| 292 |
} |
| 293 |
} |
| 294 |
clearPending(); |
| 295 |
setTimeout( refocus, 0 ); |
| 296 |
} ) |
| 297 |
.fail( ( code, result ) => { |
| 298 |
if ( result.error && result.error.info ) { |
| 299 |
printError( result.error.info ); |
| 300 |
} else if ( result.exception ) { |
| 301 |
printError( 'Error sending API request: ' + result.exception ); |
| 302 |
} else { |
| 303 |
mw.log( result ); |
| 304 |
printError( 'error' ); |
| 305 |
} |
| 306 |
clearPending(); |
| 307 |
setTimeout( refocus, 0 ); |
| 308 |
} ); |
| 309 |
} |
| 310 |
|
| 311 |
function getContent() { |
| 312 |
const $textarea = $( '#wpTextbox1' ), |
| 313 |
context = $textarea.data( 'wikiEditor-context' ); |
| 314 |
|
| 315 |
if ( context === undefined || context.codeEditor === undefined ) { |
| 316 |
return $textarea.val(); |
| 317 |
} else { |
| 318 |
return $textarea.textSelection( 'getContents' ); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
function onClearClick() { |
| 323 |
$( '#mw-scribunto-output' ).empty(); |
| 324 |
clearNextRequest = true; |
| 325 |
refocus(); |
| 326 |
} |
| 327 |
|
| 328 |
function initEditPage() { |
| 329 |
let $console = $( '#mw-scribunto-console' ); |
| 330 |
if ( !$console.length ) { |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
const $wpTextbox1 = $( '#wpTextbox1' ); |
| 335 |
if ( !$wpTextbox1.length || !$wpTextbox1.prop( 'readonly' ) ) { |
| 336 |
return; |
| 337 |
} |
| 338 |
|
| 339 |
$console = $( '<div>' ).attr( { id: 'mw-scribunto-console' } ); |
| 340 |
$wpTextbox1.after( $console ); |
| 341 |
} |
| 342 |
|
| 343 |
$( '<fieldset>' ) |
| 344 |
.attr( 'class', 'mw-scribunto-console-fieldset' ) |
| 345 |
.append( $( '<legend>' ).text( mw.msg( 'scribunto-console-title' ) ) ) |
| 346 |
.append( $( '<div id="mw-scribunto-output"></div>' ) ) |
| 347 |
.append( |
| 348 |
$( '<div>' ).append( |
| 349 |
$( '<textarea>' ) |
| 350 |
.attr( { |
| 351 |
id: 'mw-scribunto-input', |
| 352 |
class: 'mw-scribunto-input', |
| 353 |
wrap: 'off', |
| 354 |
rows: 1, |
| 355 |
dir: 'ltr', |
| 356 |
lang: 'en' |
| 357 |
} ) |
| 358 |
.on( 'keydown', inputKeydown ) |
| 359 |
.on( 'focus', inputFocus ) |
| 360 |
) |
| 361 |
) |
| 362 |
.append( |
| 363 |
$( '<div>' ).append( |
| 364 |
$( '<input>' ) |
| 365 |
.attr( { |
| 366 |
type: 'button', |
| 367 |
value: mw.msg( 'scribunto-console-clear' ) |
| 368 |
} ) |
| 369 |
.on( 'click', onClearClick ) |
| 370 |
) |
| 371 |
) |
| 372 |
.wrap( '<form>' ) |
| 373 |
.appendTo( $console ); |
| 374 |
|
| 375 |
initConsole(); |
| 376 |
} |
| 377 |
|
| 378 |
$( () => { |
| 379 |
const action = mw.config.get( 'wgAction' ); |
| 380 |
if ( action === 'edit' || action === 'submit' || action === 'editredlink' ) { |
| 381 |
initEditPage(); |
| 382 |
} |
| 383 |
} ); |
| 384 |
|
| 385 |
} )(); |