| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
$( () => { |
| 7 |
const PRIORITY_LOW = 1; |
| 8 |
const PRIORITY_HIGH = 2; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
const $form = $( '#editform' ); |
| 15 |
const section = $form.find( '[name=wpSection]' ).val(); |
| 16 |
if ( !$form.length || section === 'new' ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
let lastText, lastSummary, lastTextHash; |
| 21 |
let lastPriority = 0; |
| 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(); |
| 27 |
|
| 28 |
|
| 29 |
function isTextChanged() { |
| 30 |
return lastText !== $text.textSelection( 'getContents' ); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
function isSummaryChanged() { |
| 35 |
return lastSummary !== $summary.textSelection( 'getContents' ); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
const api = new mw.Api(); |
| 42 |
let stashReq; |
| 43 |
function stashEdit() { |
| 44 |
const textChanged = isTextChanged(); |
| 45 |
const priority = textChanged ? PRIORITY_HIGH : PRIORITY_LOW; |
| 46 |
|
| 47 |
if ( stashReq ) { |
| 48 |
if ( lastPriority > priority ) { |
| 49 |
|
| 50 |
stashReq.then( checkStash ); |
| 51 |
return; |
| 52 |
} |
| 53 |
stashReq.abort(); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
lastSummary = $summary.textSelection( 'getContents' ); |
| 58 |
lastPriority = priority; |
| 59 |
if ( textChanged ) { |
| 60 |
lastText = $text.textSelection( 'getContents' ); |
| 61 |
|
| 62 |
lastTextHash = null; |
| 63 |
} |
| 64 |
|
| 65 |
const params = { |
| 66 |
formatversion: 2, |
| 67 |
action: 'stashedit', |
| 68 |
title: mw.config.get( 'wgPageName' ), |
| 69 |
section: section, |
| 70 |
sectiontitle: '', |
| 71 |
summary: lastSummary, |
| 72 |
contentmodel: model, |
| 73 |
contentformat: format, |
| 74 |
baserevid: revId |
| 75 |
}; |
| 76 |
if ( lastTextHash ) { |
| 77 |
params.stashedtexthash = lastTextHash; |
| 78 |
} else { |
| 79 |
params.text = lastText; |
| 80 |
} |
| 81 |
|
| 82 |
const req = api.postWithToken( 'csrf', params ); |
| 83 |
stashReq = req; |
| 84 |
req.then( ( data ) => { |
| 85 |
if ( req === stashReq ) { |
| 86 |
stashReq = null; |
| 87 |
} |
| 88 |
if ( data.stashedit && data.stashedit.texthash ) { |
| 89 |
lastTextHash = data.stashedit.texthash; |
| 90 |
} else { |
| 91 |
|
| 92 |
|
| 93 |
lastTextHash = null; |
| 94 |
} |
| 95 |
} ); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
function checkStash() { |
| 100 |
if ( !isTextChanged() && !isSummaryChanged() ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
stashEdit(); |
| 105 |
} |
| 106 |
|
| 107 |
let idleTimeout = 3000; |
| 108 |
let timer; |
| 109 |
function onKeyUp( e ) { |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if ( ( e.which >= 33 && e.which <= 40 ) || ( e.which >= 16 && e.which <= 18 ) ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
clearTimeout( timer ); |
| 121 |
timer = setTimeout( checkStash, idleTimeout ); |
| 122 |
} |
| 123 |
|
| 124 |
function onSummaryFocus() { |
| 125 |
|
| 126 |
|
| 127 |
idleTimeout = 1000; |
| 128 |
|
| 129 |
|
| 130 |
checkStash(); |
| 131 |
} |
| 132 |
|
| 133 |
function onTextFocus() { |
| 134 |
|
| 135 |
idleTimeout = 3000; |
| 136 |
} |
| 137 |
|
| 138 |
$text.on( { |
| 139 |
keyup: onKeyUp, |
| 140 |
focus: onTextFocus, |
| 141 |
change: checkStash |
| 142 |
} ); |
| 143 |
$summary.on( { |
| 144 |
keyup: onKeyUp, |
| 145 |
focus: onSummaryFocus, |
| 146 |
focusout: checkStash |
| 147 |
} ); |
| 148 |
|
| 149 |
if ( |
| 150 |
|
| 151 |
|
| 152 |
mw.util.getParamValue( 'undo' ) !== null || |
| 153 |
|
| 154 |
|
| 155 |
[ 'preview', 'diff' ].indexOf( $form.find( '#mw-edit-mode' ).val() ) > -1 |
| 156 |
) { |
| 157 |
checkStash(); |
| 158 |
} |
| 159 |
} ); |