From e08bbdf4f9a344f228dca2123adeb83034435b33 Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Tue, 9 Dec 2014 21:13:44 -0800 Subject: [PATCH] Stash edit when user idles Try to anticipate edit submissions earlier by looking for pauses in keyboard input, on the assumption that users often take a few moments to look over an edit before submitting it to the server. Change-Id: Ib2de3f15b93e5d32c66a03363e98606412d5d53b --- .../mediawiki.action.edit.stash.js | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/resources/src/mediawiki.action/mediawiki.action.edit.stash.js b/resources/src/mediawiki.action/mediawiki.action.edit.stash.js index 9972321e47..9b8a0ceb81 100644 --- a/resources/src/mediawiki.action/mediawiki.action.edit.stash.js +++ b/resources/src/mediawiki.action/mediawiki.action.edit.stash.js @@ -3,10 +3,16 @@ */ ( function ( mw, $ ) { $( function () { - var api = new mw.Api(), pending = null, $form = $( '#editform' ); + var idleTimeout = 5000, + api = new mw.Api(), + pending = null, + $form = $( '#editform' ), + $text = $form.find( '#wpTextbox1' ), + data = {}, + timer = null; function stashEdit( token ) { - var data = $form.serializeObject(); + data = $form.serializeObject(); pending = api.post( { action: 'stashedit', @@ -21,15 +27,42 @@ } ); } + /* Has the edit body text changed since the last stashEdit() call? */ + 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() { - // If a stash request is already in flight, abort it, since its - // payload has just been invalidated by this change. + if ( !isChanged() ) { + return; + } + + // If a request is in progress, abort it; its payload is stale. if ( pending ) { pending.abort(); } + api.getToken( 'edit' ).then( stashEdit ); } + function onKeyPress( e ) { + // Ignore keystrokes that don't modify text, like cursor movements. + // See . + if ( e.which === 0 ) { + return; + } + + clearTimeout( timer ); + + if ( pending ) { + pending.abort(); + } + + timer = setTimeout( onEditChanged, idleTimeout ); + } + // 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). @@ -37,6 +70,7 @@ return; } - $form.find( '#wpTextbox1' ).on( 'change', onEditChanged ); + $text.on( { change: onEditChanged, keypress: onKeyPress } ); + } ); }( mediaWiki, jQuery ) ); -- 2.20.1