From 2db09000c0d0d61bf666382a973bb1282161ff4d Mon Sep 17 00:00:00 2001 From: Matt Russell Date: Mon, 27 Jun 2016 18:14:30 +1000 Subject: [PATCH] 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 --- .../mediawiki.action.edit.stash.js | 142 +++++++++--------- 1 file changed, 68 insertions(+), 74 deletions(-) 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 ) ); -- 2.20.1