From: Matt Russell Date: Mon, 27 Jun 2016 08:14:30 +0000 (+1000) Subject: Edit stash: Simplify with postWithToken X-Git-Tag: 1.31.0-rc.0~4460^2 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=2db09000c0d0d61bf666382a973bb1282161ff4d;p=lhc%2Fweb%2Fwiklou.git Edit stash: Simplify with postWithToken The original aborting issue in T102863 was fixed in 4a61bb8fb318. Also moved early return to the top and added early return if the form is missing, as well as moving code from the useless `onFormLoaded` function into the main body. Change-Id: Ie05d267f19ac2801754860fca1cec677167cd5d8 --- diff --git a/resources/src/mediawiki.action/mediawiki.action.edit.stash.js b/resources/src/mediawiki.action/mediawiki.action.edit.stash.js index c8d3fad631..7e966eabe1 100644 --- a/resources/src/mediawiki.action/mediawiki.action.edit.stash.js +++ b/resources/src/mediawiki.action/mediawiki.action.edit.stash.js @@ -11,7 +11,7 @@ var idleTimeout = 3000, api = new mw.Api(), timer, - pending, + stashReq, lastText, lastSummary, lastTextHash, @@ -26,64 +26,67 @@ PRIORITY_LOW = 1, PRIORITY_HIGH = 2; + // We don't attempt to stash new section edits because in such cases the parser output + // varies on the edit summary (since it determines the new section's name). + if ( !$form.length || section === 'new' ) { + return; + } + // Send a request to stash the edit to the API. // If a request is in progress, abort it since its payload is stale and the API // may limit concurrent stash parses. function stashEdit() { - api.getToken( 'csrf' ).then( function ( token ) { - var req, params, - textChanged = isTextChanged(), - priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW; - - if ( pending ) { - if ( lastPriority > priority ) { - // Stash request for summary change should wait on pending text change stash - pending.then( checkStash ); - return; - } - pending.abort(); + var req, params, + textChanged = isTextChanged(), + priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW; + + if ( stashReq ) { + if ( lastPriority > priority ) { + // Stash request for summary change should wait on pending text change stash + stashReq.then( checkStash ); + return; } + stashReq.abort(); + } - // Update the "last" tracking variables - lastSummary = $summary.textSelection( 'getContents' ); - lastPriority = priority; - if ( textChanged ) { - lastText = $text.textSelection( 'getContents' ); - // Reset hash - lastTextHash = null; - } + // Update the "last" tracking variables + lastSummary = $summary.textSelection( 'getContents' ); + lastPriority = priority; + if ( textChanged ) { + lastText = $text.textSelection( 'getContents' ); + // Reset hash + lastTextHash = null; + } + + params = { + action: 'stashedit', + title: mw.config.get( 'wgPageName' ), + section: section, + sectiontitle: '', + summary: lastSummary, + contentmodel: model, + contentformat: format, + baserevid: revId + }; + if ( lastTextHash ) { + params.stashedtexthash = lastTextHash; + } else { + params.text = lastText; + } - params = { - action: 'stashedit', - token: token, - title: mw.config.get( 'wgPageName' ), - section: section, - sectiontitle: '', - summary: lastSummary, - contentmodel: model, - contentformat: format, - baserevid: revId - }; - if ( lastTextHash ) { - params.stashedtexthash = lastTextHash; + req = api.postWithToken( 'csrf', params ); + stashReq = req; + req.then( function ( data ) { + if ( req === stashReq ) { + stashReq = null; + } + if ( data.stashedit && data.stashedit.texthash ) { + lastTextHash = data.stashedit.texthash; } else { - params.text = lastText; + // Request failed or text hash expired; + // include the text in a future stash request. + lastTextHash = null; } - - req = api.post( params ); - pending = req; - req.then( function ( data ) { - if ( req === pending ) { - pending = null; - } - if ( data.stashedit && data.stashedit.texthash ) { - lastTextHash = data.stashedit.texthash; - } else { - // Request failed or text hash expired; - // include the text in a future stash request. - lastTextHash = null; - } - } ); } ); } @@ -135,35 +138,26 @@ idleTimeout = 3000; } - function onFormLoaded() { - if ( - // Reverts may involve use (undo) links; stash as they review the diff. - // Since the form has a pre-filled summary, stash the edit immediately. - mw.util.getParamValue( 'undo' ) !== null || - // Pressing "show changes" and "preview" also signify that the user will - // probably save the page soon - $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1 - ) { - checkStash(); - } - } - - // We don't attempt to stash new section edits because in such cases the parser output - // varies on the edit summary (since it determines the new section's name). - if ( $form.find( 'input[name=wpSection]' ).val() === 'new' ) { - return; - } - $text.on( { - change: checkStash, keyup: onKeyUp, - focus: onTextFocus + focus: onTextFocus, + change: checkStash } ); $summary.on( { + keyup: onKeyUp, focus: onSummaryFocus, - focusout: checkStash, - keyup: onKeyUp + focusout: checkStash } ); - onFormLoaded(); + + if ( + // Reverts may involve use (undo) links; stash as they review the diff. + // Since the form has a pre-filled summary, stash the edit immediately. + mw.util.getParamValue( 'undo' ) !== null || + // Pressing "show changes" and "preview" also signify that the user will + // probably save the page soon + $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1 + ) { + checkStash(); + } } ); }( mediaWiki, jQuery ) );