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' ),
16 section
= $form
.find( '[name=wpSection]' ).val(),
17 model
= $form
.find( '[name=model]' ).val(),
18 format
= $form
.find( '[name=format]' ).val(),
19 revId
= $form
.find( '[name=parentRevId]' ).val(),
20 lastText
= $text
.textSelection( 'getContents' ),
23 // Send a request to stash the edit to the API.
24 // If a request is in progress, abort it since its payload is stale and the API
25 // may limit concurrent stash parses.
26 function stashEdit() {
31 api
.getToken( 'csrf' ).then( function ( token
) {
32 lastText
= $text
.textSelection( 'getContents' );
37 title
: mw
.config
.get( 'wgPageName' ),
41 summary
: $summary
.textSelection( 'getContents' ),
43 contentformat
: format
,
49 // Check if edit body text changed since the last stashEdit() call or if no edit
50 // stash calls have yet been made
51 function isChanged() {
52 var newText
= $text
.textSelection( 'getContents' );
53 return newText
!== lastText
;
56 function onEditorIdle() {
64 function onTextKeyUp( e
) {
65 // Ignore keystrokes that don't modify text, like cursor movements.
66 // See <http://www.javascripter.net/faq/keycodes.htm> and
67 // <http://www.quirksmode.org/js/keys.html>. We don't have to be
68 // exhaustive, because the cost of misfiring is low.
69 if ( ( e
.which
>= 33 && e
.which
<= 40 ) || ( e
.which
>= 16 && e
.which
<= 18 ) ) {
73 clearTimeout( timer
);
74 timer
= setTimeout( onEditorIdle
, idleTimeout
);
77 function onFormLoaded() {
79 // Reverts may involve use (undo) links; stash as they review the diff.
80 // Since the form has a pre-filled summary, stash the edit immediately.
81 mw
.util
.getParamValue( 'undo' ) !== null
82 // Pressing "show changes" and "preview" also signify that the user will
83 // probably save the page soon
84 || $.inArray( $form
.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
90 // We don't attempt to stash new section edits because in such cases
91 // the parser output varies on the edit summary (since it determines
92 // the new section's name).
93 if ( $form
.find( 'input[name=wpSection]' ).val() === 'new' ) {
97 $text
.on( { change
: onEditorIdle
, keyup
: onTextKeyUp
} );
98 $summary
.on( { focus
: onEditorIdle
} );
102 }( mediaWiki
, jQuery
) );