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