2 * Scripts for pre-emptive edit preparing on action=edit
6 var idleTimeout
= 3000,
9 $form
= $( '#editform' ),
10 $text
= $form
.find( '#wpTextbox1' ),
14 function stashEdit( token
) {
15 data
= $form
.serializeObject();
20 title
: mw
.config
.get( 'wgPageName' ),
21 section
: data
.wpSection
,
23 text
: data
.wpTextbox1
,
24 contentmodel
: data
.model
,
25 contentformat
: data
.format
,
26 baserevid
: data
.parentRevId
30 /* Has the edit body text changed since the last stashEdit() call? */
31 function isChanged() {
32 // Normalize line endings to CRLF, like $.fn.serializeObject does.
33 var newText
= $text
.val().replace( /\r?\n/g, '\r\n' );
34 return newText
!== data
.wpTextbox1
;
37 function onEditChanged() {
42 // If a request is in progress, abort it; its payload is stale.
47 api
.getToken( 'edit' ).then( stashEdit
);
50 function onKeyPress( e
) {
51 // Ignore keystrokes that don't modify text, like cursor movements.
52 // See <http://stackoverflow.com/q/2284844>.
53 if ( e
.which
=== 0 ) {
57 clearTimeout( timer
);
63 timer
= setTimeout( onEditChanged
, idleTimeout
);
66 // We don't attempt to stash new section edits because in such cases
67 // the parser output varies on the edit summary (since it determines
68 // the new section's name).
69 if ( $form
.find( 'input[name=wpSection]' ).val() === 'new' ) {
73 $text
.on( { change
: onEditChanged
, keypress
: onKeyPress
} );
76 }( mediaWiki
, jQuery
) );