2 * Scripts for pre-emptive edit preparing on action=edit
6 var idleTimeout
= 3000,
9 $form
= $( '#editform' ),
10 $text
= $form
.find( '#wpTextbox1' ),
11 $summary
= $form
.find( '#wpSummary' ),
15 // Send a request to stash the edit to the API.
16 // If a request is in progress, abort it since its payload is stale and the API
17 // may limit concurrent stash parses.
18 function stashEdit() {
23 api
.getToken( 'csrf' ).then( function ( token
) {
24 data
= $form
.serializeObject();
29 title
: mw
.config
.get( 'wgPageName' ),
30 section
: data
.wpSection
,
32 text
: data
.wpTextbox1
,
33 summary
: data
.wpSummary
,
34 contentmodel
: data
.model
,
35 contentformat
: data
.format
,
36 baserevid
: data
.parentRevId
41 // Check if edit body text changed since the last stashEdit() call or if no edit
42 // stash calls have yet been made
43 function isChanged() {
44 // Normalize line endings to CRLF, like $.fn.serializeObject does.
45 var newText
= $text
.val().replace( /\r?\n/g, '\r\n' );
46 return newText
!== data
.wpTextbox1
;
49 function onEditorIdle() {
57 function onTextKeyUp( e
) {
58 // Ignore keystrokes that don't modify text, like cursor movements.
59 // See <http://www.javascripter.net/faq/keycodes.htm> and
60 // <http://www.quirksmode.org/js/keys.html>. We don't have to be
61 // exhaustive, because the cost of misfiring is low.
62 if ( ( e
.which
>= 33 && e
.which
<= 40 ) || ( e
.which
>= 16 && e
.which
<= 18 ) ) {
66 clearTimeout( timer
);
67 timer
= setTimeout( onEditorIdle
, idleTimeout
);
70 function onFormLoaded() {
72 // Reverts may involve use (undo) links; stash as they review the diff.
73 // Since the form has a pre-filled summary, stash the edit immediately.
74 mw
.util
.getParamValue( 'undo' ) !== null
75 // Pressing "show changes" and "preview" also signify that the user will
76 // probably save the page soon
77 || $.inArray( $form
.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
83 // We don't attempt to stash new section edits because in such cases
84 // the parser output varies on the edit summary (since it determines
85 // the new section's name).
86 if ( $form
.find( 'input[name=wpSection]' ).val() === 'new' ) {
90 $text
.on( { change
: onEditorIdle
, keyup
: onTextKeyUp
} );
91 $summary
.on( { focus
: onEditorIdle
} );
95 }( mediaWiki
, jQuery
) );