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 // 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 // Send a request to stash the edit to the API.
36 // If a request is in progress, abort it since its payload is stale and the API
37 // may limit concurrent stash parses.
38 function stashEdit() {
40 textChanged = isTextChanged(),
41 priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
44 if ( lastPriority > priority ) {
45 // Stash request for summary change should wait on pending text change stash
46 stashReq.then( checkStash );
52 // Update the "last" tracking variables
53 lastSummary = $summary.textSelection( 'getContents' );
54 lastPriority = priority;
56 lastText = $text.textSelection( 'getContents' );
64 title: mw.config.get( 'wgPageName' ),
69 contentformat: format,
73 params.stashedtexthash = lastTextHash;
75 params.text = lastText;
78 req = api.postWithToken( 'csrf', params );
80 req.then( function ( data ) {
81 if ( req === stashReq ) {
84 if ( data.stashedit && data.stashedit.texthash ) {
85 lastTextHash = data.stashedit.texthash;
87 // Request failed or text hash expired;
88 // include the text in a future stash request.
94 // Whether the body text content changed since the last stashEdit()
95 function isTextChanged() {
96 return lastText !== $text.textSelection( 'getContents' );
99 // Whether the edit summary has changed since the last stashEdit()
100 function isSummaryChanged() {
101 return lastSummary !== $summary.textSelection( 'getContents' );
104 // Check whether text or summary have changed and call stashEdit()
105 function checkStash() {
106 if ( !isTextChanged() && !isSummaryChanged() ) {
113 function onKeyUp( e ) {
114 // Ignore keystrokes that don't modify text, like cursor movements.
115 // See <http://www.javascripter.net/faq/keycodes.htm> and
116 // <http://www.quirksmode.org/js/keys.html>. We don't have to be exhaustive,
117 // because the cost of misfiring is low.
118 // * Key code 33-40: Page Up/Down, End, Home, arrow keys.
119 // * Key code 16-18: Shift, Ctrl, Alt.
120 if ( ( e.which >= 33 && e.which <= 40 ) || ( e.which >= 16 && e.which <= 18 ) ) {
124 clearTimeout( timer );
125 timer = setTimeout( checkStash, idleTimeout );
128 function onSummaryFocus() {
129 // Summary typing is usually near the end of the workflow and involves less pausing.
130 // Re-stash more frequently in hopes of capturing the final summary before submission.
132 // Stash now since the text is likely the final version. The re-stashes based on the
133 // summary are targeted at caching edit checks that need the final summary.
137 function onTextFocus() {
138 // User returned to the text field... reset stash rate to default
149 focus: onSummaryFocus,
154 // Reverts may involve use (undo) links; stash as they review the diff.
155 // Since the form has a pre-filled summary, stash the edit immediately.
156 mw.util.getParamValue( 'undo' ) !== null ||
157 // Pressing "show changes" and "preview" also signify that the user will
158 // probably save the page soon
159 $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
164 }( mediaWiki, jQuery ) );