2 * Pre-emptive page parsing edits.
4 * See also PageEditStash in PHP.
7 const PRIORITY_LOW = 1;
8 const PRIORITY_HIGH = 2;
10 // Do not attempt to stash "new section" edits, because for those cases the ParserOutput
11 // varies on the edit summary field, and thus pre-parsing the page whilst that field is
12 // being typed in would be counter-productive. (The field is re-purposed for the new
13 // section's heading.)
14 const $form = $( '#editform' );
15 const section = $form.find( '[name=wpSection]' ).val();
16 if ( !$form.length || section === 'new' ) {
20 let lastText, lastSummary, lastTextHash;
22 const $text = $form.find( '#wpTextbox1' );
23 const $summary = $form.find( '#wpSummary' );
24 const model = $form.find( '[name=model]' ).val();
25 const format = $form.find( '[name=format]' ).val();
26 const revId = $form.find( '[name=parentRevId]' ).val();
28 // Whether the body text content changed since the last stashEdit()
29 function isTextChanged() {
30 return lastText !== $text.textSelection( 'getContents' );
33 // Whether the edit summary has changed since the last stashEdit()
34 function isSummaryChanged() {
35 return lastSummary !== $summary.textSelection( 'getContents' );
38 // Send a request to stash the edit to the API.
39 // If a request is in progress, abort it since its payload is stale and the API
40 // may limit concurrent stash parses.
41 const api = new mw.Api();
43 function stashEdit() {
44 const textChanged = isTextChanged();
45 const priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW;
48 if ( lastPriority > priority ) {
49 // Stash request for summary change should wait on pending text change stash
50 stashReq.then( checkStash );
56 // Update the "last" tracking variables
57 lastSummary = $summary.textSelection( 'getContents' );
58 lastPriority = priority;
60 lastText = $text.textSelection( 'getContents' );
68 title: mw.config.get( 'wgPageName' ),
73 contentformat: format,
77 params.stashedtexthash = lastTextHash;
79 params.text = lastText;
82 const req = api.postWithToken( 'csrf', params );
84 req.then( ( data ) => {
85 if ( req === stashReq ) {
88 if ( data.stashedit && data.stashedit.texthash ) {
89 lastTextHash = data.stashedit.texthash;
91 // Request failed or text hash expired;
92 // include the text in a future stash request.
98 // Check whether text or summary have changed and call stashEdit()
99 function checkStash() {
100 if ( !isTextChanged() && !isSummaryChanged() ) {
107 let idleTimeout = 3000;
109 function onKeyUp( e ) {
110 // Ignore keystrokes that don't modify text, like cursor movements.
111 // See <http://www.javascripter.net/faq/keycodes.htm> and
112 // <http://www.quirksmode.org/js/keys.html>. We don't have to be exhaustive,
113 // because the cost of misfiring is low.
114 // * Key code 33-40: Page Up/Down, End, Home, arrow keys.
115 // * Key code 16-18: Shift, Ctrl, Alt.
116 if ( ( e.which >= 33 && e.which <= 40 ) || ( e.which >= 16 && e.which <= 18 ) ) {
120 clearTimeout( timer );
121 timer = setTimeout( checkStash, idleTimeout );
124 function onSummaryFocus() {
125 // Summary typing is usually near the end of the workflow and involves less pausing.
126 // Re-stash more frequently in hopes of capturing the final summary before submission.
128 // Stash now since the text is likely the final version. The re-stashes based on the
129 // summary are targeted at caching edit checks that need the final summary.
133 function onTextFocus() {
134 // User returned to the text field... reset stash rate to default
145 focus: onSummaryFocus,
150 // Reverts may involve use (undo) links; stash as they review the diff.
151 // Since the form has a pre-filled summary, stash the edit immediately.
152 mw.util.getParamValue( 'undo' ) !== null ||
153 // Pressing "show changes" and "preview" also signify that the user will
154 // probably save the page soon
155 [ 'preview', 'diff' ].indexOf( $form.find( '#mw-edit-mode' ).val() ) > -1