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' );
63 title: mw.config.get( 'wgPageName' ),
68 contentformat: format,
72 params.stashedtexthash = lastTextHash;
74 params.text = lastText;
77 req = api.postWithToken( 'csrf', params );
79 req.then( function ( data ) {
80 if ( req === stashReq ) {
83 if ( data.stashedit && data.stashedit.texthash ) {
84 lastTextHash = data.stashedit.texthash;
86 // Request failed or text hash expired;
87 // include the text in a future stash request.
93 // Whether the body text content changed since the last stashEdit()
94 function isTextChanged() {
95 return lastText !== $text.textSelection( 'getContents' );
98 // Whether the edit summary has changed since the last stashEdit()
99 function isSummaryChanged() {
100 return lastSummary !== $summary.textSelection( 'getContents' );
103 // Check whether text or summary have changed and call stashEdit()
104 function checkStash() {
105 if ( !isTextChanged() && !isSummaryChanged() ) {
112 function onKeyUp( e ) {
113 // Ignore keystrokes that don't modify text, like cursor movements.
114 // See <http://www.javascripter.net/faq/keycodes.htm> and
115 // <http://www.quirksmode.org/js/keys.html>. We don't have to be exhaustive,
116 // because the cost of misfiring is low.
117 // * Key code 33-40: Page Up/Down, End, Home, arrow keys.
118 // * Key code 16-18: Shift, Ctrl, Alt.
119 if ( ( e.which >= 33 && e.which <= 40 ) || ( e.which >= 16 && e.which <= 18 ) ) {
123 clearTimeout( timer );
124 timer = setTimeout( checkStash, idleTimeout );
127 function onSummaryFocus() {
128 // Summary typing is usually near the end of the workflow and involves less pausing.
129 // Re-stash more frequently in hopes of capturing the final summary before submission.
131 // Stash now since the text is likely the final version. The re-stashes based on the
132 // summary are targeted at caching edit checks that need the final summary.
136 function onTextFocus() {
137 // User returned to the text field... reset stash rate to default
148 focus: onSummaryFocus,
153 // Reverts may involve use (undo) links; stash as they review the diff.
154 // Since the form has a pre-filled summary, stash the edit immediately.
155 mw.util.getParamValue( 'undo' ) !== null ||
156 // Pressing "show changes" and "preview" also signify that the user will
157 // probably save the page soon
158 $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
163 }( mediaWiki, jQuery ) );