/*! * Live edit preview. */ ( function ( mw, $ ) { /** * @ignore * @param {jQuery.Event} e */ function doLivePreview( e ) { var isDiff, api, parseRequest, diffRequest, postData, copySelectors, section, summary, $wikiPreview, $wikiDiff, $editform, $textbox, $copyElements, $spinner, $errorBox; isDiff = ( e.target.name === 'wpDiff' ); $wikiPreview = $( '#wikiPreview' ); $wikiDiff = $( '#wikiDiff' ); $editform = $( '#editform' ); $textbox = $editform.find( '#wpTextbox1' ); summary = OO.ui.infuse( $( '#wpSummaryWidget' ) ); $spinner = $( '.mw-spinner-preview' ); $errorBox = $( '.errorbox' ); section = $editform.find( '[name="wpSection"]' ).val(); if ( $textbox.length === 0 ) { return; } // Show changes for a new section is not yet supported if ( isDiff && section === 'new' ) { return; } e.preventDefault(); // Remove any previously displayed errors $errorBox.remove(); // Show #wikiPreview if it's hidden to be able to scroll to it // (if it is hidden, it's also empty, so nothing changes in the rendering) $wikiPreview.show(); // Jump to where the preview will appear $wikiPreview[ 0 ].scrollIntoView(); copySelectors = [ // Main '.mw-indicators', '#firstHeading', '#wikiPreview', '#wikiDiff', '#catlinks', '#p-lang', // Editing-related '.templatesUsed', '.limitreport', '.mw-summary-preview' ]; $copyElements = $( copySelectors.join( ',' ) ); // Not shown during normal preview, to be removed if present $( '.mw-newarticletext' ).remove(); if ( $spinner.length === 0 ) { $spinner = $.createSpinner( { size: 'large', type: 'block' } ) .addClass( 'mw-spinner-preview' ) .css( 'margin-top', '1em' ); $wikiPreview.before( $spinner ); } else { $spinner.show(); } // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden // (e.g. empty #catlinks) $copyElements.animate( { opacity: 0.4 }, 'fast' ); api = new mw.Api(); postData = { formatversion: 2, action: 'parse', title: mw.config.get( 'wgPageName' ), summary: summary.getValue(), prop: '' }; if ( isDiff ) { $wikiPreview.hide(); if ( postData.summary ) { parseRequest = api.post( postData ); } diffRequest = api.post( { formatversion: 2, action: 'query', prop: 'revisions', titles: mw.config.get( 'wgPageName' ), rvdifftotext: $textbox.textSelection( 'getContents' ), rvdifftotextpst: true, rvprop: '', rvsection: section === '' ? undefined : section, uselang: mw.config.get( 'wgUserLanguage' ) } ); // Wait for the summary before showing the diff so the page doesn't jump twice $.when( diffRequest, parseRequest ).done( function ( response ) { var diffHtml; try { diffHtml = response[ 0 ].query.pages[ 0 ] .revisions[ 0 ].diff.body; $wikiDiff.find( 'table.diff tbody' ).html( diffHtml ); mw.hook( 'wikipage.diff' ).fire( $wikiDiff.find( 'table.diff' ) ); } catch ( err ) { // "result.blah is undefined" error, ignore mw.log.warn( err ); } $wikiDiff.show(); } ); } else { $wikiDiff.hide(); $.extend( postData, { prop: 'text|indicators|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml', text: $textbox.textSelection( 'getContents' ), pst: true, preview: true, sectionpreview: section !== '', disableeditsection: true, useskin: mw.config.get( 'skin' ), uselang: mw.config.get( 'wgUserLanguage' ) } ); if ( section === 'new' ) { postData.section = 'new'; postData.sectiontitle = postData.summary; } parseRequest = api.post( postData ); parseRequest.done( function ( response ) { var newList, $displaytitle, $content, $parent, $list; if ( response.parse.jsconfigvars ) { mw.config.set( response.parse.jsconfigvars ); } if ( response.parse.modules ) { mw.loader.load( response.parse.modules.concat( response.parse.modulescripts, response.parse.modulestyles ) ); } newList = []; $.each( response.parse.indicators, function ( name, indicator ) { newList.push( $( '