* @param {jQuery.Event} e
*/
function doLivePreview( e ) {
- var isDiff, api, request, postData, copySelectors, section,
+ var isDiff, api, parseRequest, diffRequest, postData, copySelectors, section,
$wikiPreview, $wikiDiff, $editform, $textbox, $summary, $copyElements, $spinner, $errorBox;
isDiff = ( e.target.name === 'wpDiff' );
api = new mw.Api();
postData = {
action: 'parse',
- uselang: mw.config.get( 'wgUserLanguage' ),
title: mw.config.get( 'wgPageName' ),
- text: $textbox.textSelection( 'getContents' ),
summary: $summary.textSelection( 'getContents' ),
- sectionpreview: section !== ''
+ prop: ''
};
- if ( section === 'new' ) {
- postData.section = 'new';
- postData.sectiontitle = postData.summary;
- }
-
if ( isDiff ) {
$wikiPreview.hide();
- // First PST the input, then diff it
- postData.onlypst = true;
- request = api.post( postData );
- request.done( function ( response ) {
- api.post( {
- action: 'query',
- indexpageids: true,
- prop: 'revisions',
- titles: mw.config.get( 'wgPageName' ),
- rvdifftotext: response.parse.text[ '*' ],
- rvprop: [],
- rvsection: section === '' ? undefined : section
- } ).done( function ( response ) {
- try {
- var diffHtml = response.query.pages[ response.query.pageids[ 0 ] ]
- .revisions[ 0 ].diff[ '*' ];
- $wikiDiff.find( 'table.diff tbody' ).html( diffHtml );
- } catch ( e ) {
- // "result.blah is undefined" error, ignore
- mw.log.warn( e );
- }
- $wikiDiff.show();
- } );
+ if ( postData.summary ) {
+ parseRequest = api.post( postData );
+ }
+
+ diffRequest = api.post( {
+ action: 'query',
+ indexpageids: true,
+ prop: 'revisions',
+ titles: mw.config.get( 'wgPageName' ),
+ rvdifftotext: $textbox.textSelection( 'getContents' ),
+ rvdifftotextpst: true,
+ rvprop: '',
+ rvsection: section === '' ? undefined : section
+ } );
+
+ // Wait for the summary before showing the diff so the page doesn't jump twice
+ $.when( diffRequest, parseRequest ).done( function ( response ) {
+ var diffHtml,
+ query = response[ 0 ].query;
+ try {
+ diffHtml = query.pages[ query.pageids[ 0 ] ]
+ .revisions[ 0 ].diff[ '*' ];
+ $wikiDiff.find( 'table.diff tbody' ).html( diffHtml );
+ } catch ( e ) {
+ // "result.blah is undefined" error, ignore
+ mw.log.warn( e );
+ }
+ $wikiDiff.show();
} );
} else {
$wikiDiff.hide();
+
$.extend( postData, {
+ prop: 'text|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
+ text: $textbox.textSelection( 'getContents' ),
pst: true,
preview: true,
- prop: 'text|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
- disableeditsection: true
+ sectionpreview: section !== '',
+ disableeditsection: true,
+ uselang: mw.config.get( 'wgUserLanguage' )
} );
- request = api.post( postData );
- request.done( function ( response ) {
+ if ( section === 'new' ) {
+ postData.section = 'new';
+ postData.sectiontitle = postData.summary;
+ }
+
+ parseRequest = api.post( postData );
+ parseRequest.done( function ( response ) {
var li, newList, $displaytitle, $content, $parent, $list;
if ( response.parse.jsconfigvars ) {
mw.config.set( response.parse.jsconfigvars );
$wikiPreview.append( $content );
$wikiPreview.show();
-
}
} );
}
- request.done( function ( response ) {
- var isSubject = ( section === 'new' ),
+ $.when( parseRequest, diffRequest ).done( function ( parseResp ) {
+ var parse = parseResp && parseResp[ 0 ].parse,
+ isSubject = ( section === 'new' ),
summaryMsg = isSubject ? 'subject-preview' : 'summary-preview',
$summaryPreview = $editform.find( '.mw-summary-preview' ).empty();
- if ( response.parse.parsedsummary && response.parse.parsedsummary[ '*' ] !== '' ) {
+ if ( parse && parse.parsedsummary && parse.parsedsummary[ '*' ] !== '' ) {
$summaryPreview.append(
mw.message( summaryMsg ).parse(),
' ',
$( '<span>' ).addClass( 'comment' ).html(
// There is no equivalent to rawParams
mw.message( 'parentheses' ).escaped()
- .replace( '$1', response.parse.parsedsummary[ '*' ] )
+ .replace( '$1', parse.parsedsummary[ '*' ] )
)
);
}
mw.hook( 'wikipage.editform' ).fire( $editform );
- } );
- request.always( function () {
+ } ).always( function () {
$spinner.hide();
$copyElements.animate( {
opacity: 1
}, 'fast' );
- } );
- request.fail( function ( code, result ) {
- var errorMsg = 'API error: ' + code;
+ } ).fail( function ( code, result ) {
+ // This just shows the error for whatever request failed first
+ var errorMsg = 'API error: ' + code;
if ( code === 'http' ) {
errorMsg = 'HTTP error: ';
if ( result.exception ) {