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 $copyElements
.addClass( [ 'mw-preview-copyelements', 'mw-preview-copyelements-loading' ] );
80 title
: mw
.config
.get( 'wgPageName' ),
81 summary
: summary
.getValue(),
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
,
101 uselang
: mw
.config
.get( 'wgUserLanguage' )
104 // Wait for the summary before showing the diff so the page doesn't jump twice
105 $.when( diffRequest
, parseRequest
).done( function ( response
) {
108 diffHtml
= response
[ 0 ].query
.pages
[ 0 ]
109 .revisions
[ 0 ].diff
.body
;
110 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
111 mw
.hook( 'wikipage.diff' ).fire( $wikiDiff
.find( 'table.diff' ) );
113 // "result.blah is undefined" error, ignore
121 $.extend( postData
, {
122 prop
: 'text|indicators|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
123 text
: $textbox
.textSelection( 'getContents' ),
126 sectionpreview
: section
!== '',
127 disableeditsection
: true,
128 useskin
: mw
.config
.get( 'skin' ),
129 uselang
: mw
.config
.get( 'wgUserLanguage' )
131 if ( section
=== 'new' ) {
132 postData
.section
= 'new';
133 postData
.sectiontitle
= postData
.summary
;
136 parseRequest
= api
.post( postData
);
137 parseRequest
.done( function ( response
) {
138 var newList
, $displaytitle
, $content
, $parent
, $list
;
139 if ( response
.parse
.jsconfigvars
) {
140 mw
.config
.set( response
.parse
.jsconfigvars
);
142 if ( response
.parse
.modules
) {
143 mw
.loader
.load( response
.parse
.modules
.concat(
144 response
.parse
.modulestyles
149 // eslint-disable-next-line no-jquery/no-each-util
150 $.each( response
.parse
.indicators
, function ( name
, indicator
) {
153 .addClass( 'mw-indicator' )
154 .attr( 'id', mw
.util
.escapeIdForAttribute( 'mw-indicator-' + name
) )
157 // Add a whitespace between the <div>s because
158 // they get displayed with display: inline-block
159 document
.createTextNode( '\n' )
162 $( '.mw-indicators' ).empty().append( newList
);
164 if ( response
.parse
.displaytitle
) {
165 $displaytitle
= $( $.parseHTML( response
.parse
.displaytitle
) );
166 $( '#firstHeading' ).msg(
167 mw
.config
.get( 'wgEditMessage', 'editing' ),
170 document
.title
= mw
.msg(
173 mw
.config
.get( 'wgEditMessage', 'editing' ),
178 if ( response
.parse
.categorieshtml
) {
179 $content
= $( $.parseHTML( response
.parse
.categorieshtml
) );
180 mw
.hook( 'wikipage.categories' ).fire( $content
);
181 $( '.catlinks[data-mw="interface"]' ).replaceWith( $content
);
183 if ( response
.parse
.templates
) {
184 newList
= response
.parse
.templates
.map( function ( template
) {
188 href
: mw
.util
.getUrl( template
.title
),
189 class: ( template
.exists
? '' : 'new' )
191 .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' ) {
201 newList
= response
.parse
.langlinks
.map( function ( langlink
) {
202 var bcp47
= mw
.language
.bcp47( langlink
.lang
);
204 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
208 title
: langlink
.title
+ ' - ' + langlink
.langname
,
212 .text( langlink
.autonym
)
215 $list
= $( '#p-lang ul' );
216 $parent
= $list
.parent();
217 $list
.detach().empty().append( newList
).prependTo( $parent
);
220 if ( response
.parse
.text
) {
221 $content
= $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' );
224 .html( response
.parse
.text
);
226 mw
.hook( 'wikipage.content' ).fire( $content
);
229 $wikiPreview
.append( $content
);
235 $.when( parseRequest
, diffRequest
).done( function ( parseResp
) {
236 var parse
= parseResp
&& parseResp
[ 0 ].parse
,
237 isSubject
= ( section
=== 'new' ),
238 summaryMsg
= isSubject
? 'subject-preview' : 'summary-preview',
239 $summaryPreview
= $editform
.find( '.mw-summary-preview' ).empty();
240 if ( parse
&& parse
.parsedsummary
) {
241 $summaryPreview
.append(
242 mw
.message( summaryMsg
).parse(),
244 $( '<span>' ).addClass( 'comment' ).html(
245 // There is no equivalent to rawParams
246 mw
.message( 'parentheses' ).escaped()
247 // .replace() use $ as start of a pattern.
248 // $$ is the pattern for '$'.
249 // The inner .replace() duplicates any $ and
250 // the outer .replace() simplifies the $$.
251 .replace( '$1', parse
.parsedsummary
.replace( /\$/g, '$$$$' ) )
255 mw
.hook( 'wikipage.editform' ).fire( $editform
);
256 } ).always( function () {
258 $copyElements
.removeClass( 'mw-preview-copyelements-loading' );
259 } ).fail( function ( code
, result
) {
260 // This just shows the error for whatever request failed first
261 var errorMsg
= 'API error: ' + code
;
262 if ( code
=== 'http' ) {
263 errorMsg
= 'HTTP error: ';
264 if ( result
.exception
) {
265 errorMsg
+= result
.exception
;
267 errorMsg
+= result
.textStatus
;
270 $errorBox
= $( '<div>' )
271 .addClass( 'errorbox' )
272 .html( '<strong>' + mw
.message( 'previewerrortext' ).escaped() + '</strong><br>' )
273 .append( document
.createTextNode( errorMsg
) );
275 $wikiPreview
.hide().before( $errorBox
);
280 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
281 // the scripts or styles without reloading the page.
282 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
286 // The following elements can change in a preview but are not output
287 // by the server when they're empty until the preview response.
288 // TODO: Make the server output these always (in a hidden state), so we don't
289 // have to fish and (hopefully) put them in the right place (since skins
290 // can change where they are output).
292 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
293 $( '.portal' ).last().after(
298 'aria-labelledby': 'p-lang-label'
300 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
301 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
305 if ( !$( '.mw-summary-preview' ).length
) {
306 $( '#wpSummaryWidget' ).after(
307 $( '<div>' ).addClass( 'mw-summary-preview' )
311 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
312 $( '#wikiPreview' ).after(
315 .attr( 'id', 'wikiDiff' )
316 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
317 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
321 // This should be moved down to '#editform', but is kept on the body for now
322 // because the LiquidThreads extension is re-using this module with only half
323 // the EditPage (doesn't include #editform presumably, T57463).
324 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);