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
105 // Wait for the summary before showing the diff so the page doesn't jump twice
106 $.when( diffRequest
, parseRequest
).done( function ( response
) {
109 diffHtml
= response
[ 0 ].query
.pages
[ 0 ]
110 .revisions
[ 0 ].diff
.body
;
111 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
112 mw
.hook( 'wikipage.diff' ).fire( $wikiDiff
.find( 'table.diff' ) );
114 // "result.blah is undefined" error, ignore
122 $.extend( postData
, {
123 prop
: 'text|indicators|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
124 text
: $textbox
.textSelection( 'getContents' ),
127 sectionpreview
: section
!== '',
128 disableeditsection
: true,
129 useskin
: mw
.config
.get( 'skin' ),
130 uselang
: mw
.config
.get( 'wgUserLanguage' )
132 if ( section
=== 'new' ) {
133 postData
.section
= 'new';
134 postData
.sectiontitle
= postData
.summary
;
137 parseRequest
= api
.post( postData
);
138 parseRequest
.done( function ( response
) {
139 var li
, newList
, $displaytitle
, $content
, $parent
, $list
;
140 if ( response
.parse
.jsconfigvars
) {
141 mw
.config
.set( response
.parse
.jsconfigvars
);
143 if ( response
.parse
.modules
) {
144 mw
.loader
.load( response
.parse
.modules
.concat(
145 response
.parse
.modulescripts
,
146 response
.parse
.modulestyles
151 $.each( response
.parse
.indicators
, function ( name
, indicator
) {
154 .addClass( 'mw-indicator' )
155 .attr( 'id', mw
.util
.escapeIdForAttribute( 'mw-indicator-' + name
) )
158 // Add a whitespace between the <div>s because
159 // they get displayed with display: inline-block
160 document
.createTextNode( '\n' )
163 $( '.mw-indicators' ).empty().append( newList
);
165 if ( response
.parse
.displaytitle
) {
166 $displaytitle
= $( $.parseHTML( response
.parse
.displaytitle
) );
167 $( '#firstHeading' ).msg(
168 mw
.config
.get( 'wgEditMessage', 'editing' ),
171 document
.title
= mw
.msg(
174 mw
.config
.get( 'wgEditMessage', 'editing' ),
179 if ( response
.parse
.categorieshtml
) {
180 $content
= $( $.parseHTML( response
.parse
.categorieshtml
) );
181 mw
.hook( 'wikipage.categories' ).fire( $content
);
182 $( '.catlinks[data-mw="interface"]' ).replaceWith( $content
);
184 if ( response
.parse
.templates
) {
186 $.each( response
.parse
.templates
, function ( i
, template
) {
190 href
: mw
.util
.getUrl( template
.title
),
191 'class': ( template
.exists
? '' : 'new' )
193 .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' ) {
205 $.each( response
.parse
.langlinks
, function ( i
, langlink
) {
206 var bcp47
= mw
.language
.bcp47( langlink
.lang
);
208 .addClass( 'interlanguage-link interwiki-' + langlink
.lang
)
212 title
: langlink
.title
+ ' - ' + langlink
.langname
,
216 .text( langlink
.autonym
)
220 $list
= $( '#p-lang ul' );
221 $parent
= $list
.parent();
222 $list
.detach().empty().append( newList
).prependTo( $parent
);
225 if ( response
.parse
.text
) {
226 $content
= $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' );
229 .html( response
.parse
.text
);
231 mw
.hook( 'wikipage.content' ).fire( $content
);
234 $wikiPreview
.append( $content
);
240 $.when( parseRequest
, diffRequest
).done( function ( parseResp
) {
241 var parse
= parseResp
&& parseResp
[ 0 ].parse
,
242 isSubject
= ( section
=== 'new' ),
243 summaryMsg
= isSubject
? 'subject-preview' : 'summary-preview',
244 $summaryPreview
= $editform
.find( '.mw-summary-preview' ).empty();
245 if ( parse
&& parse
.parsedsummary
) {
246 $summaryPreview
.append(
247 mw
.message( summaryMsg
).parse(),
249 $( '<span>' ).addClass( 'comment' ).html(
250 // There is no equivalent to rawParams
251 mw
.message( 'parentheses' ).escaped()
252 // .replace() use $ as start of a pattern.
253 // $$ is the pattern for '$'.
254 // The inner .replace() duplicates any $ and
255 // the outer .replace() simplifies the $$.
256 .replace( '$1', parse
.parsedsummary
.replace( /\$/g, '$$$$' ) )
260 mw
.hook( 'wikipage.editform' ).fire( $editform
);
261 } ).always( function () {
263 $copyElements
.animate( {
266 } ).fail( function ( code
, result
) {
267 // This just shows the error for whatever request failed first
268 var errorMsg
= 'API error: ' + code
;
269 if ( code
=== 'http' ) {
270 errorMsg
= 'HTTP error: ';
271 if ( result
.exception
) {
272 errorMsg
+= result
.exception
;
274 errorMsg
+= result
.textStatus
;
277 $errorBox
= $( '<div>' )
278 .addClass( 'errorbox' )
279 .html( '<strong>' + mw
.message( 'previewerrortext' ).escaped() + '</strong><br>' )
280 .append( document
.createTextNode( errorMsg
) );
282 $wikiPreview
.hide().before( $errorBox
);
287 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
288 // the scripts or styles without reloading the page.
289 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
293 // The following elements can change in a preview but are not output
294 // by the server when they're empty until the preview response.
295 // TODO: Make the server output these always (in a hidden state), so we don't
296 // have to fish and (hopefully) put them in the right place (since skins
297 // can change where they are output).
299 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
300 $( '.portal:last' ).after(
305 'aria-labelledby': 'p-lang-label'
307 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
308 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
312 if ( !$( '.mw-summary-preview' ).length
) {
313 $( '#wpSummaryWidget' ).after(
314 $( '<div>' ).addClass( 'mw-summary-preview' )
318 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
319 $( '#wikiPreview' ).after(
322 .attr( 'id', 'wikiDiff' )
323 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
324 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
328 // This should be moved down to '#editform', but is kept on the body for now
329 // because the LiquidThreads extension is re-using this module with only half
330 // the EditPage (doesn't include #editform presumably, T57463).
331 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);
334 }( mediaWiki
, jQuery
) );