/*! * Live edit preview. */ ( function ( mw, $ ) { /** * @ignore * @param {jQuery.Event} e */ function doLivePreview( e ) { var isDiff, api, request, postData, copySelectors, section, $wikiPreview, $wikiDiff, $editform, $textbox, $summary, $copyElements, $spinner, $errorBox; isDiff = ( e.target.name === 'wpDiff' ); $wikiPreview = $( '#wikiPreview' ); $wikiDiff = $( '#wikiDiff' ); $editform = $( '#editform' ); $textbox = $editform.find( '#wpTextbox1' ); $summary = $editform.find( '#wpSummary' ); $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 '#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(); $spinner = $.createSpinner( { size: 'large', type: 'block' } ); $wikiPreview.before( $spinner ); $spinner.css( { marginTop: $spinner.height() } ); // 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 = { action: 'parse', uselang: mw.config.get( 'wgUserLanguage' ), title: mw.config.get( 'wgPageName' ), text: $textbox.textSelection( 'getContents' ), summary: $summary.textSelection( 'getContents' ) }; if ( section !== '' ) { postData.sectionpreview = ''; if ( section === 'new' ) { postData.section = section; postData.sectiontitle = postData.summary; } } if ( isDiff ) { $wikiPreview.hide(); // First PST the input, then diff it postData.onlypst = ''; request = api.post( postData ); request.done( function ( response ) { var postData; postData = { action: 'query', indexpageids: '', prop: 'revisions', titles: mw.config.get( 'wgPageName' ), rvdifftotext: response.parse.text['*'], rvprop: '' }; if ( section !== '' ) { postData.rvsection = section; } return api.post( postData ).done( function ( result2 ) { try { var diffHtml = result2.query.pages[result2.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, { pst: '', preview: '', prop: 'text|displaytitle|modules|categorieshtml|templates|langlinks|limitreporthtml', disableeditsection: true } ); request = api.post( postData ); request.done( function ( response ) { var li, newList, $displaytitle, $content, $parent, $list; if ( response.parse.modules ) { mw.loader.load( response.parse.modules.concat( response.parse.modulescripts, response.parse.modulestyles, response.parse.modulemessages ) ); } if ( response.parse.displaytitle ) { $displaytitle = $( $.parseHTML( response.parse.displaytitle ) ); $( '#firstHeading' ).msg( mw.config.get( 'wgEditMessage', 'editing' ), $displaytitle ); document.title = mw.msg( 'pagetitle', mw.msg( mw.config.get( 'wgEditMessage', 'editing' ), $displaytitle.text() ) ); } if ( response.parse.categorieshtml ) { $( '#catlinks' ).replaceWith( response.parse.categorieshtml['*'] ); } if ( response.parse.templates ) { newList = []; $.each( response.parse.templates, function ( i, template ) { li = $( '