2 * Scripts for pre-emptive edit preparing on action=edit
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 // We don't attempt to stash new section edits because in such cases the parser output
30 // varies on the edit summary (since it determines the new section's name).
31 if ( !$form
.length
|| section
=== 'new' ) {
35 // Whether the body text content changed since the last stashEdit()
36 function isTextChanged() {
37 return lastText
!== $text
.textSelection( 'getContents' );
40 // Whether the edit summary has changed since the last stashEdit()
41 function isSummaryChanged() {
42 return lastSummary
!== $summary
.textSelection( 'getContents' );
45 // Send a request to stash the edit to the API.
46 // If a request is in progress, abort it since its payload is stale and the API
47 // may limit concurrent stash parses.
48 function stashEdit() {
50 textChanged
= isTextChanged(),
51 priority
= textChanged
? PRIORITY_HIGH
: PRIORITY_LOW
;
54 if ( lastPriority
> priority
) {
55 // Stash request for summary change should wait on pending text change stash
56 // eslint-disable-next-line no-use-before-define
57 stashReq
.then( checkStash
);
63 // Update the "last" tracking variables
64 lastSummary
= $summary
.textSelection( 'getContents' );
65 lastPriority
= priority
;
67 lastText
= $text
.textSelection( 'getContents' );
75 title
: mw
.config
.get( 'wgPageName' ),
80 contentformat
: format
,
84 params
.stashedtexthash
= lastTextHash
;
86 params
.text
= lastText
;
89 req
= api
.postWithToken( 'csrf', params
);
91 req
.then( function ( data
) {
92 if ( req
=== stashReq
) {
95 if ( data
.stashedit
&& data
.stashedit
.texthash
) {
96 lastTextHash
= data
.stashedit
.texthash
;
98 // Request failed or text hash expired;
99 // include the text in a future stash request.
105 // Check whether text or summary have changed and call stashEdit()
106 function checkStash() {
107 if ( !isTextChanged() && !isSummaryChanged() ) {
114 function onKeyUp( e
) {
115 // Ignore keystrokes that don't modify text, like cursor movements.
116 // See <http://www.javascripter.net/faq/keycodes.htm> and
117 // <http://www.quirksmode.org/js/keys.html>. We don't have to be exhaustive,
118 // because the cost of misfiring is low.
119 // * Key code 33-40: Page Up/Down, End, Home, arrow keys.
120 // * Key code 16-18: Shift, Ctrl, Alt.
121 if ( ( e
.which
>= 33 && e
.which
<= 40 ) || ( e
.which
>= 16 && e
.which
<= 18 ) ) {
125 clearTimeout( timer
);
126 timer
= setTimeout( checkStash
, idleTimeout
);
129 function onSummaryFocus() {
130 // Summary typing is usually near the end of the workflow and involves less pausing.
131 // Re-stash more frequently in hopes of capturing the final summary before submission.
133 // Stash now since the text is likely the final version. The re-stashes based on the
134 // summary are targeted at caching edit checks that need the final summary.
138 function onTextFocus() {
139 // User returned to the text field... reset stash rate to default
150 focus
: onSummaryFocus
,
155 // Reverts may involve use (undo) links; stash as they review the diff.
156 // Since the form has a pre-filled summary, stash the edit immediately.
157 mw
.util
.getParamValue( 'undo' ) !== null ||
158 // Pressing "show changes" and "preview" also signify that the user will
159 // probably save the page soon
160 [ 'preview', 'diff' ].indexOf( $form
.find( '#mw-edit-mode' ).val() ) > -1