Merge "Revert "ApiSandbox: Display params as JSON on request page""
[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 /* 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 action: 'stashedit',
63 title: mw.config.get( 'wgPageName' ),
64 section: section,
65 sectiontitle: '',
66 summary: lastSummary,
67 contentmodel: model,
68 contentformat: format,
69 baserevid: revId
70 };
71 if ( lastTextHash ) {
72 params.stashedtexthash = lastTextHash;
73 } else {
74 params.text = lastText;
75 }
76
77 req = api.postWithToken( 'csrf', params );
78 stashReq = req;
79 req.then( function ( data ) {
80 if ( req === stashReq ) {
81 stashReq = null;
82 }
83 if ( data.stashedit && data.stashedit.texthash ) {
84 lastTextHash = data.stashedit.texthash;
85 } else {
86 // Request failed or text hash expired;
87 // include the text in a future stash request.
88 lastTextHash = null;
89 }
90 } );
91 }
92
93 // Whether the body text content changed since the last stashEdit()
94 function isTextChanged() {
95 return lastText !== $text.textSelection( 'getContents' );
96 }
97
98 // Whether the edit summary has changed since the last stashEdit()
99 function isSummaryChanged() {
100 return lastSummary !== $summary.textSelection( 'getContents' );
101 }
102
103 // Check whether text or summary have changed and call stashEdit()
104 function checkStash() {
105 if ( !isTextChanged() && !isSummaryChanged() ) {
106 return;
107 }
108
109 stashEdit();
110 }
111
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 ) ) {
120 return;
121 }
122
123 clearTimeout( timer );
124 timer = setTimeout( checkStash, idleTimeout );
125 }
126
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.
130 idleTimeout = 1000;
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.
133 checkStash();
134 }
135
136 function onTextFocus() {
137 // User returned to the text field... reset stash rate to default
138 idleTimeout = 3000;
139 }
140
141 $text.on( {
142 keyup: onKeyUp,
143 focus: onTextFocus,
144 change: checkStash
145 } );
146 $summary.on( {
147 keyup: onKeyUp,
148 focus: onSummaryFocus,
149 focusout: checkStash
150 } );
151
152 if (
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
159 ) {
160 checkStash();
161 }
162 } );
163 }( mediaWiki, jQuery ) );