Merge "user: Allow "CAS update failed" exceptions to be normalised"
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.edit.stash.js
1 /*!
2 * Scripts for pre-emptive edit preparing on action=edit
3 */
4
5 ( function () {
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 // Whether the body text content changed since the last stashEdit()
36 function isTextChanged() {
37 return lastText !== $text.textSelection( 'getContents' );
38 }
39
40 // Whether the edit summary has changed since the last stashEdit()
41 function isSummaryChanged() {
42 return lastSummary !== $summary.textSelection( 'getContents' );
43 }
44
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() {
49 var req, params,
50 textChanged = isTextChanged(),
51 priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
52
53 if ( stashReq ) {
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 );
58 return;
59 }
60 stashReq.abort();
61 }
62
63 // Update the "last" tracking variables
64 lastSummary = $summary.textSelection( 'getContents' );
65 lastPriority = priority;
66 if ( textChanged ) {
67 lastText = $text.textSelection( 'getContents' );
68 // Reset hash
69 lastTextHash = null;
70 }
71
72 params = {
73 formatversion: 2,
74 action: 'stashedit',
75 title: mw.config.get( 'wgPageName' ),
76 section: section,
77 sectiontitle: '',
78 summary: lastSummary,
79 contentmodel: model,
80 contentformat: format,
81 baserevid: revId
82 };
83 if ( lastTextHash ) {
84 params.stashedtexthash = lastTextHash;
85 } else {
86 params.text = lastText;
87 }
88
89 req = api.postWithToken( 'csrf', params );
90 stashReq = req;
91 req.then( function ( data ) {
92 if ( req === stashReq ) {
93 stashReq = null;
94 }
95 if ( data.stashedit && data.stashedit.texthash ) {
96 lastTextHash = data.stashedit.texthash;
97 } else {
98 // Request failed or text hash expired;
99 // include the text in a future stash request.
100 lastTextHash = null;
101 }
102 } );
103 }
104
105 // Check whether text or summary have changed and call stashEdit()
106 function checkStash() {
107 if ( !isTextChanged() && !isSummaryChanged() ) {
108 return;
109 }
110
111 stashEdit();
112 }
113
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 ) ) {
122 return;
123 }
124
125 clearTimeout( timer );
126 timer = setTimeout( checkStash, idleTimeout );
127 }
128
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.
132 idleTimeout = 1000;
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.
135 checkStash();
136 }
137
138 function onTextFocus() {
139 // User returned to the text field... reset stash rate to default
140 idleTimeout = 3000;
141 }
142
143 $text.on( {
144 keyup: onKeyUp,
145 focus: onTextFocus,
146 change: checkStash
147 } );
148 $summary.on( {
149 keyup: onKeyUp,
150 focus: onSummaryFocus,
151 focusout: checkStash
152 } );
153
154 if (
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
161 ) {
162 checkStash();
163 }
164 } );
165 }() );