2 * Scripts for pre-emptive edit preparing on action=edit
5 if ( !mw
.config
.get( 'wgAjaxEditStash' ) ) {
10 var idleTimeout
= 3000,
13 $form
= $( '#editform' ),
14 $text
= $form
.find( '#wpTextbox1' ),
15 $summary
= $form
.find( '#wpSummary' ),
19 // Send a request to stash the edit to the API.
20 // If a request is in progress, abort it since its payload is stale and the API
21 // may limit concurrent stash parses.
22 function stashEdit() {
27 api
.getToken( 'csrf' ).then( function ( token
) {
28 data
= $form
.serializeObject();
33 title
: mw
.config
.get( 'wgPageName' ),
34 section
: data
.wpSection
,
36 text
: data
.wpTextbox1
,
37 summary
: data
.wpSummary
,
38 contentmodel
: data
.model
,
39 contentformat
: data
.format
,
40 baserevid
: data
.parentRevId
45 // Check if edit body text changed since the last stashEdit() call or if no edit
46 // stash calls have yet been made
47 function isChanged() {
48 // Normalize line endings to CRLF, like $.fn.serializeObject does.
49 var newText
= $text
.val().replace( /\r?\n/g, '\r\n' );
50 return newText
!== data
.wpTextbox1
;
53 function onEditorIdle() {
61 function onTextKeyUp( e
) {
62 // Ignore keystrokes that don't modify text, like cursor movements.
63 // See <http://www.javascripter.net/faq/keycodes.htm> and
64 // <http://www.quirksmode.org/js/keys.html>. We don't have to be
65 // exhaustive, because the cost of misfiring is low.
66 if ( ( e
.which
>= 33 && e
.which
<= 40 ) || ( e
.which
>= 16 && e
.which
<= 18 ) ) {
70 clearTimeout( timer
);
71 timer
= setTimeout( onEditorIdle
, idleTimeout
);
74 function onFormLoaded() {
76 // Reverts may involve use (undo) links; stash as they review the diff.
77 // Since the form has a pre-filled summary, stash the edit immediately.
78 mw
.util
.getParamValue( 'undo' ) !== null
79 // Pressing "show changes" and "preview" also signify that the user will
80 // probably save the page soon
81 || $.inArray( $form
.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
87 // We don't attempt to stash new section edits because in such cases
88 // the parser output varies on the edit summary (since it determines
89 // the new section's name).
90 if ( $form
.find( 'input[name=wpSection]' ).val() === 'new' ) {
94 $text
.on( { change
: onEditorIdle
, keyup
: onTextKeyUp
} );
95 $summary
.on( { focus
: onEditorIdle
} );
99 }( mediaWiki
, jQuery
) );