8 * @param {jQuery.Event} e
10 function doLivePreview( e
) {
11 var isDiff
, api
, parseRequest
, diffRequest
, postData
, copySelectors
, section
, summary
,
12 $wikiPreview
, $wikiDiff
, $editform
, $textbox
, $copyElements
, $spinner
, $errorBox
;
14 isDiff
= ( e
.target
.name
=== 'wpDiff' );
15 $wikiPreview
= $( '#wikiPreview' );
16 $wikiDiff
= $( '#wikiDiff' );
17 $editform
= $( '#editform' );
18 $textbox
= $editform
.find( '#wpTextbox1' );
20 summary
= OO
.ui
.infuse( $( '#wpSummaryWidget' ) );
22 $spinner
= $( '.mw-spinner-preview' );
23 $errorBox
= $( '.errorbox' );
24 section
= $editform
.find( '[name="wpSection"]' ).val();
26 if ( $textbox
.length
=== 0 ) {
29 // Show changes for a new section is not yet supported
30 if ( isDiff
&& section
=== 'new' ) {
35 // Remove any previously displayed errors
37 // Show #wikiPreview if it's hidden to be able to scroll to it
38 // (if it is hidden, it's also empty, so nothing changes in the rendering)
41 // Jump to where the preview will appear
42 $wikiPreview
[ 0 ].scrollIntoView();
57 $copyElements
= $( copySelectors
.join( ',' ) );
59 // Not shown during normal preview, to be removed if present
60 $( '.mw-newarticletext' ).remove();
62 if ( $spinner
.length
=== 0 ) {
63 $spinner
= $.createSpinner( {
67 .addClass( 'mw-spinner-preview' )
68 .css( 'margin-top', '1em' );
69 $wikiPreview
.before( $spinner
);
74 // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden
75 // (e.g. empty #catlinks)
76 $copyElements
.animate( { opacity
: 0.4 }, 'fast' );
82 title
: mw
.config
.get( 'wgPageName' ),
83 summary
: summary
.getValue(),
90 if ( postData
.summary
) {
91 parseRequest
= api
.post( postData
);
94 diffRequest
= api
.post( {
98 titles
: mw
.config
.get( 'wgPageName' ),
99 rvdifftotext
: $textbox
.textSelection( 'getContents' ),
100 rvdifftotextpst
: true,
102 rvsection
: section
=== '' ? undefined : section
,
103 uselang
: mw
.config
.get( 'wgUserLanguage' )
106 // Wait for the summary before showing the diff so the page doesn't jump twice
107 $.when( diffRequest
, parseRequest
).done( function ( response
) {
110 diffHtml
= response
[ 0 ].query
.pages
[ 0 ]
111 .revisions
[ 0 ].diff
.body
;
112 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
113 mw
.hook( 'wikipage.diff' ).fire( $wikiDiff
.find( 'table.diff' ) );
115 // "result.blah is undefined" error, ignore
123 $.extend( postData
, {
124 prop
: 'text|indicators|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
125 text
: $textbox
.textSelection( 'getContents' ),
128 sectionpreview
: section
!== '',
129 disableeditsection
: true,
130 useskin
: mw
.config
.get( 'skin' ),
131 uselang
: mw
.config
.get( 'wgUserLanguage' )
133 if ( section
=== 'new' ) {
134 postData
.section
= 'new';
135 postData
.sectiontitle
= postData
.summary
;
138 parseRequest
= api
.post( postData
);
139 parseRequest
.done( function ( response
) {
140 var newList
, $displaytitle
, $content
, $parent
, $list
;
141 if ( response
.parse
.jsconfigvars
) {
142 mw
.config
.set( response
.parse
.jsconfigvars
);
144 if ( response
.parse
.modules
) {
145 mw
.loader
.load( response
.parse
.modules
.concat(
146 response
.parse
.modulescripts
,
147 response
.parse
.modulestyles
152 // eslint-disable-next-line no-restricted-properties
153 $.each( response
.parse
.indicators
, function ( name
, indicator
) {
156 .addClass( 'mw-indicator' )
157 .attr( 'id', mw
.util
.escapeIdForAttribute( 'mw-indicator-' + name
) )
160 // Add a whitespace between the <div>s because
161 // they get displayed with display: inline-block
162 document
.createTextNode( '\n' )
165 $( '.mw-indicators' ).empty().append( newList
);
167 if ( response
.parse
.displaytitle
) {
168 $displaytitle
= $( $.parseHTML( response
.parse
.displaytitle
) );
169 $( '#firstHeading' ).msg(
170 mw
.config
.get( 'wgEditMessage', 'editing' ),
173 document
.title
= mw
.msg(
176 mw
.config
.get( 'wgEditMessage', 'editing' ),
181 if ( response
.parse
.categorieshtml
) {
182 $content
= $( $.parseHTML( response
.parse
.categorieshtml
) );
183 mw
.hook( 'wikipage.categories' ).fire( $content
);
184 $( '.catlinks[data-mw="interface"]' ).replaceWith( $content
);
186 if ( response
.parse
.templates
) {
187 newList
= response
.parse
.templates
.map( function ( template
) {
191 href
: mw
.util
.getUrl( template
.title
),
192 'class': ( template
.exists
? '' : 'new' )
194 .text( template
.title
)
198 $editform
.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList
).appendTo( '.templatesUsed' );
200 if ( response
.parse
.limitreporthtml
) {
201 $( '.limitreport' ).html( response
.parse
.limitreporthtml
);
203 if ( response
.parse
.langlinks
&& mw
.config
.get( 'skin' ) === 'vector' ) {
204 newList
= response
.parse
.langlinks
.map( function ( langlink
) {
205 var bcp47
= mw
.language
.bcp47( langlink
.lang
);
207 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
211 title
: langlink
.title
+ ' - ' + langlink
.langname
,
215 .text( langlink
.autonym
)
218 $list
= $( '#p-lang ul' );
219 $parent
= $list
.parent();
220 $list
.detach().empty().append( newList
).prependTo( $parent
);
223 if ( response
.parse
.text
) {
224 $content
= $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' );
227 .html( response
.parse
.text
);
229 mw
.hook( 'wikipage.content' ).fire( $content
);
232 $wikiPreview
.append( $content
);
238 $.when( parseRequest
, diffRequest
).done( function ( parseResp
) {
239 var parse
= parseResp
&& parseResp
[ 0 ].parse
,
240 isSubject
= ( section
=== 'new' ),
241 summaryMsg
= isSubject
? 'subject-preview' : 'summary-preview',
242 $summaryPreview
= $editform
.find( '.mw-summary-preview' ).empty();
243 if ( parse
&& parse
.parsedsummary
) {
244 $summaryPreview
.append(
245 mw
.message( summaryMsg
).parse(),
247 $( '<span>' ).addClass( 'comment' ).html(
248 // There is no equivalent to rawParams
249 mw
.message( 'parentheses' ).escaped()
250 // .replace() use $ as start of a pattern.
251 // $$ is the pattern for '$'.
252 // The inner .replace() duplicates any $ and
253 // the outer .replace() simplifies the $$.
254 .replace( '$1', parse
.parsedsummary
.replace( /\$/g, '$$$$' ) )
258 mw
.hook( 'wikipage.editform' ).fire( $editform
);
259 } ).always( function () {
261 $copyElements
.animate( {
264 } ).fail( function ( code
, result
) {
265 // This just shows the error for whatever request failed first
266 var errorMsg
= 'API error: ' + code
;
267 if ( code
=== 'http' ) {
268 errorMsg
= 'HTTP error: ';
269 if ( result
.exception
) {
270 errorMsg
+= result
.exception
;
272 errorMsg
+= result
.textStatus
;
275 $errorBox
= $( '<div>' )
276 .addClass( 'errorbox' )
277 .html( '<strong>' + mw
.message( 'previewerrortext' ).escaped() + '</strong><br>' )
278 .append( document
.createTextNode( errorMsg
) );
280 $wikiPreview
.hide().before( $errorBox
);
285 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
286 // the scripts or styles without reloading the page.
287 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
291 // The following elements can change in a preview but are not output
292 // by the server when they're empty until the preview response.
293 // TODO: Make the server output these always (in a hidden state), so we don't
294 // have to fish and (hopefully) put them in the right place (since skins
295 // can change where they are output).
297 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
298 $( '.portal:last' ).after(
303 'aria-labelledby': 'p-lang-label'
305 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
306 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
310 if ( !$( '.mw-summary-preview' ).length
) {
311 $( '#wpSummaryWidget' ).after(
312 $( '<div>' ).addClass( 'mw-summary-preview' )
316 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
317 $( '#wikiPreview' ).after(
320 .attr( 'id', 'wikiDiff' )
321 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
322 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
326 // This should be moved down to '#editform', but is kept on the body for now
327 // because the LiquidThreads extension is re-using this module with only half
328 // the EditPage (doesn't include #editform presumably, T57463).
329 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);