8 * @param {jQuery.Event} e
10 function doLivePreview( e ) {
11 var isDiff, api, parseRequest, diffRequest, postData, copySelectors, section,
12 $wikiPreview, $wikiDiff, $editform, $textbox, $summary, $copyElements, $spinner, $errorBox;
14 isDiff = ( e.target.name === 'wpDiff' );
15 $wikiPreview = $( '#wikiPreview' );
16 $wikiDiff = $( '#wikiDiff' );
17 $editform = $( '#editform' );
18 $textbox = $editform.find( '#wpTextbox1' );
19 $summary = $editform.find( '#wpSummary' );
20 $spinner = $( '.mw-spinner-preview' );
21 $errorBox = $( '.errorbox' );
22 section = $editform.find( '[name="wpSection"]' ).val();
24 if ( $textbox.length === 0 ) {
27 // Show changes for a new section is not yet supported
28 if ( isDiff && section === 'new' ) {
33 // Remove any previously displayed errors
35 // Show #wikiPreview if it's hidden to be able to scroll to it
36 // (if it is hidden, it's also empty, so nothing changes in the rendering)
39 // Jump to where the preview will appear
40 $wikiPreview[ 0 ].scrollIntoView();
55 $copyElements = $( copySelectors.join( ',' ) );
57 // Not shown during normal preview, to be removed if present
58 $( '.mw-newarticletext' ).remove();
60 if ( $spinner.length === 0 ) {
61 $spinner = $.createSpinner( {
65 .addClass( 'mw-spinner-preview' )
66 .css( 'margin-top', '1em' );
67 $wikiPreview.before( $spinner );
72 // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden
73 // (e.g. empty #catlinks)
74 $copyElements.animate( { opacity: 0.4 }, 'fast' );
80 title: mw.config.get( 'wgPageName' ),
81 summary: $summary.textSelection( 'getContents' ),
88 if ( postData.summary ) {
89 parseRequest = api.post( postData );
92 diffRequest = api.post( {
96 titles: mw.config.get( 'wgPageName' ),
97 rvdifftotext: $textbox.textSelection( 'getContents' ),
98 rvdifftotextpst: true,
100 rvsection: section === '' ? undefined : section
103 // Wait for the summary before showing the diff so the page doesn't jump twice
104 $.when( diffRequest, parseRequest ).done( function ( response ) {
107 diffHtml = response[ 0 ].query.pages[ 0 ]
108 .revisions[ 0 ].diff.body;
109 $wikiDiff.find( 'table.diff tbody' ).html( diffHtml );
110 mw.hook( 'wikipage.diff' ).fire( $wikiDiff.find( 'table.diff' ) );
112 // "result.blah is undefined" error, ignore
120 $.extend( postData, {
121 prop: 'text|indicators|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
122 text: $textbox.textSelection( 'getContents' ),
125 sectionpreview: section !== '',
126 disableeditsection: true,
127 uselang: mw.config.get( 'wgUserLanguage' )
129 if ( section === 'new' ) {
130 postData.section = 'new';
131 postData.sectiontitle = postData.summary;
134 parseRequest = api.post( postData );
135 parseRequest.done( function ( response ) {
136 var li, newList, $displaytitle, $content, $parent, $list;
137 if ( response.parse.jsconfigvars ) {
138 mw.config.set( response.parse.jsconfigvars );
140 if ( response.parse.modules ) {
141 mw.loader.load( response.parse.modules.concat(
142 response.parse.modulescripts,
143 response.parse.modulestyles
148 $.each( response.parse.indicators, function ( name, indicator ) {
151 .addClass( 'mw-indicator' )
152 .attr( 'id', mw.util.escapeId( 'mw-indicator-' + name ) )
155 // Add a whitespace between the <div>s because
156 // they get displayed with display: inline-block
157 document.createTextNode( '\n' )
160 $( '.mw-indicators' ).empty().append( newList );
162 if ( response.parse.displaytitle ) {
163 $displaytitle = $( $.parseHTML( response.parse.displaytitle ) );
164 $( '#firstHeading' ).msg(
165 mw.config.get( 'wgEditMessage', 'editing' ),
168 document.title = mw.msg(
171 mw.config.get( 'wgEditMessage', 'editing' ),
176 if ( response.parse.categorieshtml ) {
177 $content = $( $.parseHTML( response.parse.categorieshtml ) );
178 mw.hook( 'wikipage.categories' ).fire( $content );
179 $( '.catlinks[data-mw="interface"]' ).replaceWith( $content );
181 if ( response.parse.templates ) {
183 $.each( response.parse.templates, function ( i, template ) {
187 href: mw.util.getUrl( template.title ),
188 'class': ( template.exists ? '' : 'new' )
190 .text( template.title )
195 $editform.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList ).appendTo( '.templatesUsed' );
197 if ( response.parse.limitreporthtml ) {
198 $( '.limitreport' ).html( response.parse.limitreporthtml );
200 if ( response.parse.langlinks && mw.config.get( 'skin' ) === 'vector' ) {
202 $.each( response.parse.langlinks, function ( i, langlink ) {
204 .addClass( 'interlanguage-link interwiki-' + langlink.lang )
208 title: langlink.title + ' - ' + langlink.langname,
210 hreflang: langlink.lang
212 .text( langlink.autonym )
216 $list = $( '#p-lang ul' );
217 $parent = $list.parent();
218 $list.detach().empty().append( newList ).prependTo( $parent );
221 if ( response.parse.text ) {
222 $content = $wikiPreview.children( '.mw-content-ltr,.mw-content-rtl' );
225 .html( response.parse.text );
227 mw.hook( 'wikipage.content' ).fire( $content );
230 $wikiPreview.append( $content );
236 $.when( parseRequest, diffRequest ).done( function ( parseResp ) {
237 var parse = parseResp && parseResp[ 0 ].parse,
238 isSubject = ( section === 'new' ),
239 summaryMsg = isSubject ? 'subject-preview' : 'summary-preview',
240 $summaryPreview = $editform.find( '.mw-summary-preview' ).empty();
241 if ( parse && parse.parsedsummary ) {
242 $summaryPreview.append(
243 mw.message( summaryMsg ).parse(),
245 $( '<span>' ).addClass( 'comment' ).html(
246 // There is no equivalent to rawParams
247 mw.message( 'parentheses' ).escaped()
248 // .replace() use $ as start of a pattern.
249 // $$ is the pattern for '$'.
250 // The inner .replace() duplicates any $ and
251 // the outer .replace() simplifies the $$.
252 .replace( '$1', parse.parsedsummary.replace( /\$/g, '$$$$' ) )
256 mw.hook( 'wikipage.editform' ).fire( $editform );
257 } ).always( function () {
259 $copyElements.animate( {
262 } ).fail( function ( code, result ) {
263 // This just shows the error for whatever request failed first
264 var errorMsg = 'API error: ' + code;
265 if ( code === 'http' ) {
266 errorMsg = 'HTTP error: ';
267 if ( result.exception ) {
268 errorMsg += result.exception;
270 errorMsg += result.textStatus;
273 $errorBox = $( '<div>' )
274 .addClass( 'errorbox' )
275 .html( '<strong>' + mw.message( 'previewerrortext' ).escaped() + '</strong><br>' )
276 .append( document.createTextNode( errorMsg ) );
278 $wikiPreview.hide().before( $errorBox );
283 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
284 // the scripts or styles without reloading the page.
285 if ( $( '#mw-userjsyoucanpreview' ).length || $( '#mw-usercssyoucanpreview' ).length ) {
289 // The following elements can change in a preview but are not output
290 // by the server when they're empty until the preview response.
291 // TODO: Make the server output these always (in a hidden state), so we don't
292 // have to fish and (hopefully) put them in the right place (since skins
293 // can change where they are output).
295 if ( !document.getElementById( 'p-lang' ) && document.getElementById( 'p-tb' ) && mw.config.get( 'skin' ) === 'vector' ) {
296 $( '.portal:last' ).after(
301 'aria-labelledby': 'p-lang-label'
303 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw.msg( 'otherlanguages' ) ) )
304 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
308 if ( !$( '.mw-summary-preview' ).length ) {
309 $( '#wpSummary' ).after(
310 $( '<div>' ).addClass( 'mw-summary-preview' )
314 if ( !document.getElementById( 'wikiDiff' ) && document.getElementById( 'wikiPreview' ) ) {
315 $( '#wikiPreview' ).after(
318 .attr( 'id', 'wikiDiff' )
319 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
320 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
324 // This should be moved down to '#editform', but is kept on the body for now
325 // because the LiquidThreads extension is re-using this module with only half
326 // the EditPage (doesn't include #editform presumably, T57463).
327 $( document.body ).on( 'click', '#wpPreview, #wpDiff', doLivePreview );
330 }( mediaWiki, jQuery ) );