3af82226f19446f04673b95dfafac2e492c924f8
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 // FIXME: Use CSS transition
77 // eslint-disable-next-line no-jquery/no-animate
78 $copyElements
.animate( { opacity
: 0.4 }, 'fast' );
84 title
: mw
.config
.get( 'wgPageName' ),
85 summary
: summary
.getValue(),
92 if ( postData
.summary
) {
93 parseRequest
= api
.post( postData
);
96 diffRequest
= api
.post( {
100 titles
: mw
.config
.get( 'wgPageName' ),
101 rvdifftotext
: $textbox
.textSelection( 'getContents' ),
102 rvdifftotextpst
: true,
104 rvsection
: section
=== '' ? undefined : section
,
105 uselang
: mw
.config
.get( 'wgUserLanguage' )
108 // Wait for the summary before showing the diff so the page doesn't jump twice
109 $.when( diffRequest
, parseRequest
).done( function ( response
) {
112 diffHtml
= response
[ 0 ].query
.pages
[ 0 ]
113 .revisions
[ 0 ].diff
.body
;
114 $wikiDiff
.find( 'table.diff tbody' ).html( diffHtml
);
115 mw
.hook( 'wikipage.diff' ).fire( $wikiDiff
.find( 'table.diff' ) );
117 // "result.blah is undefined" error, ignore
125 $.extend( postData
, {
126 prop
: 'text|indicators|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
127 text
: $textbox
.textSelection( 'getContents' ),
130 sectionpreview
: section
!== '',
131 disableeditsection
: true,
132 useskin
: mw
.config
.get( 'skin' ),
133 uselang
: mw
.config
.get( 'wgUserLanguage' )
135 if ( section
=== 'new' ) {
136 postData
.section
= 'new';
137 postData
.sectiontitle
= postData
.summary
;
140 parseRequest
= api
.post( postData
);
141 parseRequest
.done( function ( response
) {
142 var newList
, $displaytitle
, $content
, $parent
, $list
;
143 if ( response
.parse
.jsconfigvars
) {
144 mw
.config
.set( response
.parse
.jsconfigvars
);
146 if ( response
.parse
.modules
) {
147 mw
.loader
.load( response
.parse
.modules
.concat(
148 response
.parse
.modulestyles
153 // eslint-disable-next-line no-jquery/no-each-util
154 $.each( response
.parse
.indicators
, function ( name
, indicator
) {
157 .addClass( 'mw-indicator' )
158 .attr( 'id', mw
.util
.escapeIdForAttribute( 'mw-indicator-' + name
) )
161 // Add a whitespace between the <div>s because
162 // they get displayed with display: inline-block
163 document
.createTextNode( '\n' )
166 $( '.mw-indicators' ).empty().append( newList
);
168 if ( response
.parse
.displaytitle
) {
169 $displaytitle
= $( $.parseHTML( response
.parse
.displaytitle
) );
170 $( '#firstHeading' ).msg(
171 mw
.config
.get( 'wgEditMessage', 'editing' ),
174 document
.title
= mw
.msg(
177 mw
.config
.get( 'wgEditMessage', 'editing' ),
182 if ( response
.parse
.categorieshtml
) {
183 $content
= $( $.parseHTML( response
.parse
.categorieshtml
) );
184 mw
.hook( 'wikipage.categories' ).fire( $content
);
185 $( '.catlinks[data-mw="interface"]' ).replaceWith( $content
);
187 if ( response
.parse
.templates
) {
188 newList
= response
.parse
.templates
.map( function ( template
) {
192 href
: mw
.util
.getUrl( template
.title
),
193 class: ( template
.exists
? '' : 'new' )
195 .text( template
.title
)
199 $editform
.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList
).appendTo( '.templatesUsed' );
201 if ( response
.parse
.limitreporthtml
) {
202 $( '.limitreport' ).html( response
.parse
.limitreporthtml
);
204 if ( response
.parse
.langlinks
&& mw
.config
.get( 'skin' ) === 'vector' ) {
205 newList
= response
.parse
.langlinks
.map( function ( 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
)
219 $list
= $( '#p-lang ul' );
220 $parent
= $list
.parent();
221 $list
.detach().empty().append( newList
).prependTo( $parent
);
224 if ( response
.parse
.text
) {
225 $content
= $wikiPreview
.children( '.mw-content-ltr,.mw-content-rtl' );
228 .html( response
.parse
.text
);
230 mw
.hook( 'wikipage.content' ).fire( $content
);
233 $wikiPreview
.append( $content
);
239 $.when( parseRequest
, diffRequest
).done( function ( parseResp
) {
240 var parse
= parseResp
&& parseResp
[ 0 ].parse
,
241 isSubject
= ( section
=== 'new' ),
242 summaryMsg
= isSubject
? 'subject-preview' : 'summary-preview',
243 $summaryPreview
= $editform
.find( '.mw-summary-preview' ).empty();
244 if ( parse
&& parse
.parsedsummary
) {
245 $summaryPreview
.append(
246 mw
.message( summaryMsg
).parse(),
248 $( '<span>' ).addClass( 'comment' ).html(
249 // There is no equivalent to rawParams
250 mw
.message( 'parentheses' ).escaped()
251 // .replace() use $ as start of a pattern.
252 // $$ is the pattern for '$'.
253 // The inner .replace() duplicates any $ and
254 // the outer .replace() simplifies the $$.
255 .replace( '$1', parse
.parsedsummary
.replace( /\$/g, '$$$$' ) )
259 mw
.hook( 'wikipage.editform' ).fire( $editform
);
260 } ).always( function () {
262 // FIXME: Use CSS transition
263 // eslint-disable-next-line no-jquery/no-animate
264 $copyElements
.animate( {
267 } ).fail( function ( code
, result
) {
268 // This just shows the error for whatever request failed first
269 var errorMsg
= 'API error: ' + code
;
270 if ( code
=== 'http' ) {
271 errorMsg
= 'HTTP error: ';
272 if ( result
.exception
) {
273 errorMsg
+= result
.exception
;
275 errorMsg
+= result
.textStatus
;
278 $errorBox
= $( '<div>' )
279 .addClass( 'errorbox' )
280 .html( '<strong>' + mw
.message( 'previewerrortext' ).escaped() + '</strong><br>' )
281 .append( document
.createTextNode( errorMsg
) );
283 $wikiPreview
.hide().before( $errorBox
);
288 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
289 // the scripts or styles without reloading the page.
290 if ( $( '#mw-userjsyoucanpreview' ).length
|| $( '#mw-usercssyoucanpreview' ).length
) {
294 // The following elements can change in a preview but are not output
295 // by the server when they're empty until the preview response.
296 // TODO: Make the server output these always (in a hidden state), so we don't
297 // have to fish and (hopefully) put them in the right place (since skins
298 // can change where they are output).
300 if ( !document
.getElementById( 'p-lang' ) && document
.getElementById( 'p-tb' ) && mw
.config
.get( 'skin' ) === 'vector' ) {
301 $( '.portal' ).last().after(
306 'aria-labelledby': 'p-lang-label'
308 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw
.msg( 'otherlanguages' ) ) )
309 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
313 if ( !$( '.mw-summary-preview' ).length
) {
314 $( '#wpSummaryWidget' ).after(
315 $( '<div>' ).addClass( 'mw-summary-preview' )
319 if ( !document
.getElementById( 'wikiDiff' ) && document
.getElementById( 'wikiPreview' ) ) {
320 $( '#wikiPreview' ).after(
323 .attr( 'id', 'wikiDiff' )
324 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
325 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
329 // This should be moved down to '#editform', but is kept on the body for now
330 // because the LiquidThreads extension is re-using this module with only half
331 // the EditPage (doesn't include #editform presumably, T57463).
332 $( document
.body
).on( 'click', '#wpPreview, #wpDiff', doLivePreview
);