Perform edit stashing for ?undo and ?oldid reverts
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 8 Jun 2016 19:14:13 +0000 (12:14 -0700)
committerOri.livneh <ori@wikimedia.org>
Sat, 11 Jun 2016 02:48:48 +0000 (02:48 +0000)
Previously, these cases would not send stash requests.

Bug: T136678
Change-Id: I02d760f3c59fb09aeafc0aca429cee835f3e0b6e

resources/src/mediawiki.action/mediawiki.action.edit.stash.js

index 20bfa0e..d354fc2 100644 (file)
@@ -8,46 +8,52 @@
                        pending = null,
                        $form = $( '#editform' ),
                        $text = $form.find( '#wpTextbox1' ),
+                       $summary = $form.find( '#wpSummary' ),
                        data = {},
                        timer = null;
 
-               function stashEdit( token ) {
-                       data = $form.serializeObject();
+               // 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() {
+                       if ( pending ) {
+                               pending.abort();
+                       }
 
-                       pending = api.post( {
-                               action: 'stashedit',
-                               token: token,
-                               title: mw.config.get( 'wgPageName' ),
-                               section: data.wpSection,
-                               sectiontitle: '',
-                               text: data.wpTextbox1,
-                               contentmodel: data.model,
-                               contentformat: data.format,
-                               baserevid: data.parentRevId
+                       api.getToken( 'csrf' ).then( function ( token ) {
+                               data = $form.serializeObject();
+
+                               pending = api.post( {
+                                       action: 'stashedit',
+                                       token: token,
+                                       title: mw.config.get( 'wgPageName' ),
+                                       section: data.wpSection,
+                                       sectiontitle: '',
+                                       text: data.wpTextbox1,
+                                       contentmodel: data.model,
+                                       contentformat: data.format,
+                                       baserevid: data.parentRevId
+                               } );
                        } );
                }
 
-               /* Has the edit body text changed since the last stashEdit() call? */
+               // Check if edit body text changed since the last stashEdit() call or if no edit
+               // stash calls have yet been made
                function isChanged() {
                        // Normalize line endings to CRLF, like $.fn.serializeObject does.
                        var newText = $text.val().replace( /\r?\n/g, '\r\n' );
                        return newText !== data.wpTextbox1;
                }
 
-               function onEditChanged() {
+               function onTextChanged() {
                        if ( !isChanged() ) {
                                return;
                        }
 
-                       // If a request is in progress, abort it; its payload is stale.
-                       if ( pending ) {
-                               pending.abort();
-                       }
-
-                       api.getToken( 'csrf' ).then( stashEdit );
+                       stashEdit();
                }
 
-               function onKeyPress( e ) {
+               function onTextKeyPress( e ) {
                        // Ignore keystrokes that don't modify text, like cursor movements.
                        // See <http://stackoverflow.com/q/2284844>.
                        if ( e.which === 0 ) {
                                pending.abort();
                        }
 
-                       timer = setTimeout( onEditChanged, idleTimeout );
+                       timer = setTimeout( onTextChanged, idleTimeout );
+               }
+
+               function onFormLoaded() {
+                       // Reverts may involve use (undo) links; stash as they review the diff.
+                       // Since the form has a pre-filled summary, stash the edit immediately.
+                       if ( mw.util.getParamValue( 'undo' ) !== null ) {
+                               stashEdit();
+                       }
                }
 
                // We don't attempt to stash new section edits because in such cases
@@ -70,7 +84,9 @@
                        return;
                }
 
-               $text.on( { change: onEditChanged, keypress: onKeyPress } );
+               $text.on( { change: onTextChanged, keypress: onTextKeyPress } );
+               $summary.on( { focus: onTextChanged } );
+               onFormLoaded();
 
        } );
 }( mediaWiki, jQuery ) );