2 * Scripts for pre-emptive edit preparing on action=edit
5 if ( !mw
.config
.get( 'wgAjaxEditStash' ) ) {
10 var idleTimeout
= 3000,
17 $form
= $( '#editform' ),
18 $text
= $form
.find( '#wpTextbox1' ),
19 $summary
= $form
.find( '#wpSummary' ),
20 section
= $form
.find( '[name=wpSection]' ).val(),
21 model
= $form
.find( '[name=model]' ).val(),
22 format
= $form
.find( '[name=format]' ).val(),
23 revId
= $form
.find( '[name=parentRevId]' ).val(),
28 // Send a request to stash the edit to the API.
29 // If a request is in progress, abort it since its payload is stale and the API
30 // may limit concurrent stash parses.
31 function stashEdit() {
32 api
.getToken( 'csrf' ).then( function ( token
) {
34 textChanged
= isTextChanged(),
35 priority
= textChanged
? PRIORITY_HIGH
: PRIORITY_LOW
;
38 if ( lastPriority
> priority
) {
39 // Stash request for summary change should wait on pending text change stash
40 pending
.then( checkStash
);
46 // Update the "last" tracking variables
47 lastSummary
= $summary
.textSelection( 'getContents' );
48 lastPriority
= priority
;
50 lastText
= $text
.textSelection( 'getContents' );
58 title
: mw
.config
.get( 'wgPageName' ),
63 contentformat
: format
,
67 params
.stashedtexthash
= lastTextHash
;
69 params
.text
= lastText
;
72 req
= api
.post( params
);
74 req
.then( function ( data
) {
75 if ( req
=== pending
) {
78 if ( data
.stashedit
&& data
.stashedit
.texthash
) {
79 lastTextHash
= data
.stashedit
.texthash
;
81 // Request failed or text hash expired;
82 // include the text in a future stash request.
89 // Whether the body text content changed since the last stashEdit()
90 function isTextChanged() {
91 return lastText
!== $text
.textSelection( 'getContents' );
94 // Whether the edit summary has changed since the last stashEdit()
95 function isSummaryChanged() {
96 return lastSummary
!== $summary
.textSelection( 'getContents' );
99 // Check whether text or summary have changed and call stashEdit()
100 function checkStash() {
101 if ( !isTextChanged() && !isSummaryChanged() ) {
108 function onKeyUp( e
) {
109 // Ignore keystrokes that don't modify text, like cursor movements.
110 // See <http://www.javascripter.net/faq/keycodes.htm> and
111 // <http://www.quirksmode.org/js/keys.html>. We don't have to be exhaustive,
112 // because the cost of misfiring is low.
113 // * Key code 33-40: Page Up/Down, End, Home, arrow keys.
114 // * Key code 16-18: Shift, Ctrl, Alt.
115 if ( ( e
.which
>= 33 && e
.which
<= 40 ) || ( e
.which
>= 16 && e
.which
<= 18 ) ) {
119 clearTimeout( timer
);
120 timer
= setTimeout( checkStash
, idleTimeout
);
123 function onSummaryFocus() {
124 // Summary typing is usually near the end of the workflow and involves less pausing.
125 // Re-stash more frequently in hopes of capturing the final summary before submission.
127 // Stash now since the text is likely the final version. The re-stashes based on the
128 // summary are targeted at caching edit checks that need the final summary.
132 function onTextFocus() {
133 // User returned to the text field... reset stash rate to default
137 function onFormLoaded() {
139 // Reverts may involve use (undo) links; stash as they review the diff.
140 // Since the form has a pre-filled summary, stash the edit immediately.
141 mw
.util
.getParamValue( 'undo' ) !== null
142 // Pressing "show changes" and "preview" also signify that the user will
143 // probably save the page soon
144 || $.inArray( $form
.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
150 // We don't attempt to stash new section edits because in such cases the parser output
151 // varies on the edit summary (since it determines the new section's name).
152 if ( $form
.find( 'input[name=wpSection]' ).val() === 'new' ) {
162 focus
: onSummaryFocus
,
163 focusout
: checkStash
,
168 }( mediaWiki
, jQuery
) );