]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - resources/src/mediawiki.action/mediawiki.action.edit.stash.js
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / resources / src / mediawiki.action / mediawiki.action.edit.stash.js
1 /*!
2  * Scripts for pre-emptive edit preparing on action=edit
3  */
4 /* eslint-disable no-use-before-define */
5 ( function ( mw, $ ) {
6         if ( !mw.config.get( 'wgAjaxEditStash' ) ) {
7                 return;
8         }
9
10         $( function () {
11                 var idleTimeout = 3000,
12                         api = new mw.Api(),
13                         timer,
14                         stashReq,
15                         lastText,
16                         lastSummary,
17                         lastTextHash,
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(),
25                         lastPriority = 0,
26                         PRIORITY_LOW = 1,
27                         PRIORITY_HIGH = 2;
28
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' ) {
32                         return;
33                 }
34
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() {
39                         var req, params,
40                                 textChanged = isTextChanged(),
41                                 priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
42
43                         if ( stashReq ) {
44                                 if ( lastPriority > priority ) {
45                                         // Stash request for summary change should wait on pending text change stash
46                                         stashReq.then( checkStash );
47                                         return;
48                                 }
49                                 stashReq.abort();
50                         }
51
52                         // Update the "last" tracking variables
53                         lastSummary = $summary.textSelection( 'getContents' );
54                         lastPriority = priority;
55                         if ( textChanged ) {
56                                 lastText = $text.textSelection( 'getContents' );
57                                 // Reset hash
58                                 lastTextHash = null;
59                         }
60
61                         params = {
62                                 formatversion: 2,
63                                 action: 'stashedit',
64                                 title: mw.config.get( 'wgPageName' ),
65                                 section: section,
66                                 sectiontitle: '',
67                                 summary: lastSummary,
68                                 contentmodel: model,
69                                 contentformat: format,
70                                 baserevid: revId
71                         };
72                         if ( lastTextHash ) {
73                                 params.stashedtexthash = lastTextHash;
74                         } else {
75                                 params.text = lastText;
76                         }
77
78                         req = api.postWithToken( 'csrf', params );
79                         stashReq = req;
80                         req.then( function ( data ) {
81                                 if ( req === stashReq ) {
82                                         stashReq = null;
83                                 }
84                                 if ( data.stashedit && data.stashedit.texthash ) {
85                                         lastTextHash = data.stashedit.texthash;
86                                 } else {
87                                         // Request failed or text hash expired;
88                                         // include the text in a future stash request.
89                                         lastTextHash = null;
90                                 }
91                         } );
92                 }
93
94                 // Whether the body text content changed since the last stashEdit()
95                 function isTextChanged() {
96                         return lastText !== $text.textSelection( 'getContents' );
97                 }
98
99                 // Whether the edit summary has changed since the last stashEdit()
100                 function isSummaryChanged() {
101                         return lastSummary !== $summary.textSelection( 'getContents' );
102                 }
103
104                 // Check whether text or summary have changed and call stashEdit()
105                 function checkStash() {
106                         if ( !isTextChanged() && !isSummaryChanged() ) {
107                                 return;
108                         }
109
110                         stashEdit();
111                 }
112
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 ) ) {
121                                 return;
122                         }
123
124                         clearTimeout( timer );
125                         timer = setTimeout( checkStash, idleTimeout );
126                 }
127
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.
131                         idleTimeout = 1000;
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.
134                         checkStash();
135                 }
136
137                 function onTextFocus() {
138                         // User returned to the text field... reset stash rate to default
139                         idleTimeout = 3000;
140                 }
141
142                 $text.on( {
143                         keyup: onKeyUp,
144                         focus: onTextFocus,
145                         change: checkStash
146                 } );
147                 $summary.on( {
148                         keyup: onKeyUp,
149                         focus: onSummaryFocus,
150                         focusout: checkStash
151                 } );
152
153                 if (
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
160                 ) {
161                         checkStash();
162                 }
163         } );
164 }( mediaWiki, jQuery ) );