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