Use the new 'difftotextpst' option of prop=revisions API in live preview script
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.edit.preview.js
1 /*!
2 * Live edit preview.
3 */
4 ( function ( mw, $ ) {
5
6 /**
7 * @ignore
8 * @param {jQuery.Event} e
9 */
10 function doLivePreview( e ) {
11 var isDiff, api, parseRequest, diffRequest, postData, copySelectors, section,
12 $wikiPreview, $wikiDiff, $editform, $textbox, $summary, $copyElements, $spinner, $errorBox;
13
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();
23
24 if ( $textbox.length === 0 ) {
25 return;
26 }
27 // Show changes for a new section is not yet supported
28 if ( isDiff && section === 'new' ) {
29 return;
30 }
31 e.preventDefault();
32
33 // Remove any previously displayed errors
34 $errorBox.remove();
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)
37 $wikiPreview.show();
38
39 // Jump to where the preview will appear
40 $wikiPreview[ 0 ].scrollIntoView();
41
42 copySelectors = [
43 // Main
44 '#firstHeading',
45 '#wikiPreview',
46 '#wikiDiff',
47 '#catlinks',
48 '#p-lang',
49 // Editing-related
50 '.templatesUsed',
51 '.limitreport',
52 '.mw-summary-preview'
53 ];
54 $copyElements = $( copySelectors.join( ',' ) );
55
56 // Not shown during normal preview, to be removed if present
57 $( '.mw-newarticletext' ).remove();
58
59 if ( $spinner.length === 0 ) {
60 $spinner = $.createSpinner( {
61 size: 'large',
62 type: 'block'
63 } )
64 .addClass( 'mw-spinner-preview' )
65 .css( 'margin-top', '1em' );
66 $wikiPreview.before( $spinner );
67 } else {
68 $spinner.show();
69 }
70
71 // Can't use fadeTo because it calls show(), and we might want to keep some elements hidden
72 // (e.g. empty #catlinks)
73 $copyElements.animate( { opacity: 0.4 }, 'fast' );
74
75 api = new mw.Api();
76 postData = {
77 action: 'parse',
78 title: mw.config.get( 'wgPageName' ),
79 summary: $summary.textSelection( 'getContents' ),
80 prop: ''
81 };
82
83 if ( isDiff ) {
84 $wikiPreview.hide();
85
86 if ( postData.summary ) {
87 parseRequest = api.post( postData );
88 }
89
90 diffRequest = api.post( {
91 action: 'query',
92 indexpageids: true,
93 prop: 'revisions',
94 titles: mw.config.get( 'wgPageName' ),
95 rvdifftotext: $textbox.textSelection( 'getContents' ),
96 rvdifftotextpst: true,
97 rvprop: '',
98 rvsection: section === '' ? undefined : section
99 } );
100
101 // Wait for the summary before showing the diff so the page doesn't jump twice
102 $.when( diffRequest, parseRequest ).done( function ( response ) {
103 var diffHtml,
104 query = response[ 0 ].query;
105 try {
106 diffHtml = query.pages[ query.pageids[ 0 ] ]
107 .revisions[ 0 ].diff[ '*' ];
108 $wikiDiff.find( 'table.diff tbody' ).html( diffHtml );
109 } catch ( e ) {
110 // "result.blah is undefined" error, ignore
111 mw.log.warn( e );
112 }
113 $wikiDiff.show();
114 } );
115 } else {
116 $wikiDiff.hide();
117
118 $.extend( postData, {
119 prop: 'text|displaytitle|modules|jsconfigvars|categorieshtml|templates|langlinks|limitreporthtml',
120 text: $textbox.textSelection( 'getContents' ),
121 pst: true,
122 preview: true,
123 sectionpreview: section !== '',
124 disableeditsection: true,
125 uselang: mw.config.get( 'wgUserLanguage' )
126 } );
127 if ( section === 'new' ) {
128 postData.section = 'new';
129 postData.sectiontitle = postData.summary;
130 }
131
132 parseRequest = api.post( postData );
133 parseRequest.done( function ( response ) {
134 var li, newList, $displaytitle, $content, $parent, $list;
135 if ( response.parse.jsconfigvars ) {
136 mw.config.set( response.parse.jsconfigvars );
137 }
138 if ( response.parse.modules ) {
139 mw.loader.load( response.parse.modules.concat(
140 response.parse.modulescripts,
141 response.parse.modulestyles
142 ) );
143 }
144 if ( response.parse.displaytitle ) {
145 $displaytitle = $( $.parseHTML( response.parse.displaytitle ) );
146 $( '#firstHeading' ).msg(
147 mw.config.get( 'wgEditMessage', 'editing' ),
148 $displaytitle
149 );
150 document.title = mw.msg(
151 'pagetitle',
152 mw.msg(
153 mw.config.get( 'wgEditMessage', 'editing' ),
154 $displaytitle.text()
155 )
156 );
157 }
158 if ( response.parse.categorieshtml ) {
159 $( '#catlinks' ).replaceWith( response.parse.categorieshtml[ '*' ] );
160 }
161 if ( response.parse.templates ) {
162 newList = [];
163 $.each( response.parse.templates, function ( i, template ) {
164 li = $( '<li>' )
165 .append( $( '<a>' )
166 .attr( {
167 href: mw.util.getUrl( template[ '*' ] ),
168 'class': ( template.exists !== undefined ? '' : 'new' )
169 } )
170 .text( template[ '*' ] )
171 );
172 newList.push( li );
173 } );
174
175 $editform.find( '.templatesUsed .mw-editfooter-list' ).detach().empty().append( newList ).appendTo( '.templatesUsed' );
176 }
177 if ( response.parse.limitreporthtml ) {
178 $( '.limitreport' ).html( response.parse.limitreporthtml[ '*' ] );
179 }
180 if ( response.parse.langlinks && mw.config.get( 'skin' ) === 'vector' ) {
181 newList = [];
182 $.each( response.parse.langlinks, function ( i, langlink ) {
183 li = $( '<li>' )
184 .addClass( 'interlanguage-link interwiki-' + langlink.lang )
185 .append( $( '<a>' )
186 .attr( {
187 href: langlink.url,
188 title: langlink[ '*' ] + ' - ' + langlink.langname,
189 lang: langlink.lang,
190 hreflang: langlink.lang
191 } )
192 .text( langlink.autonym )
193 );
194 newList.push( li );
195 } );
196 $list = $( '#p-lang ul' );
197 $parent = $list.parent();
198 $list.detach().empty().append( newList ).prependTo( $parent );
199 }
200
201 if ( response.parse.text[ '*' ] ) {
202 $content = $wikiPreview.children( '.mw-content-ltr,.mw-content-rtl' );
203 $content
204 .detach()
205 .html( response.parse.text[ '*' ] );
206
207 mw.hook( 'wikipage.content' ).fire( $content );
208
209 // Reattach
210 $wikiPreview.append( $content );
211
212 $wikiPreview.show();
213 }
214 } );
215 }
216 $.when( parseRequest, diffRequest ).done( function ( parseResp ) {
217 var parse = parseResp && parseResp[ 0 ].parse,
218 isSubject = ( section === 'new' ),
219 summaryMsg = isSubject ? 'subject-preview' : 'summary-preview',
220 $summaryPreview = $editform.find( '.mw-summary-preview' ).empty();
221 if ( parse && parse.parsedsummary && parse.parsedsummary[ '*' ] !== '' ) {
222 $summaryPreview.append(
223 mw.message( summaryMsg ).parse(),
224 ' ',
225 $( '<span>' ).addClass( 'comment' ).html(
226 // There is no equivalent to rawParams
227 mw.message( 'parentheses' ).escaped()
228 .replace( '$1', parse.parsedsummary[ '*' ] )
229 )
230 );
231 }
232 mw.hook( 'wikipage.editform' ).fire( $editform );
233 } ).always( function () {
234 $spinner.hide();
235 $copyElements.animate( {
236 opacity: 1
237 }, 'fast' );
238 } ).fail( function ( code, result ) {
239 // This just shows the error for whatever request failed first
240 var errorMsg = 'API error: ' + code;
241 if ( code === 'http' ) {
242 errorMsg = 'HTTP error: ';
243 if ( result.exception ) {
244 errorMsg += result.exception;
245 } else {
246 errorMsg += result.textStatus;
247 }
248 }
249 $errorBox = $( '<div>' )
250 .addClass( 'errorbox' )
251 .html( '<strong>' + mw.message( 'previewerrortext' ).escaped() + '</strong><br>' )
252 .append( document.createTextNode( errorMsg ) );
253 $wikiDiff.hide();
254 $wikiPreview.hide().before( $errorBox );
255 } );
256 }
257
258 $( function () {
259 // Do not enable on user .js/.css pages, as there's no sane way of "previewing"
260 // the scripts or styles without reloading the page.
261 if ( $( '#mw-userjsyoucanpreview' ).length || $( '#mw-usercssyoucanpreview' ).length ) {
262 return;
263 }
264
265 // The following elements can change in a preview but are not output
266 // by the server when they're empty until the preview response.
267 // TODO: Make the server output these always (in a hidden state), so we don't
268 // have to fish and (hopefully) put them in the right place (since skins
269 // can change where they are output).
270
271 if ( !document.getElementById( 'p-lang' ) && document.getElementById( 'p-tb' ) && mw.config.get( 'skin' ) === 'vector' ) {
272 $( '.portal:last' ).after(
273 $( '<div>' ).attr( {
274 'class': 'portal',
275 id: 'p-lang',
276 role: 'navigation',
277 title: mw.msg( 'tooltip-p-lang' ),
278 'aria-labelledby': 'p-lang-label'
279 } )
280 .append( $( '<h3>' ).attr( 'id', 'p-lang-label' ).text( mw.msg( 'otherlanguages' ) ) )
281 .append( $( '<div>' ).addClass( 'body' ).append( '<ul>' ) )
282 );
283 }
284
285 if ( !$( '.mw-summary-preview' ).length ) {
286 $( '#wpSummary' ).after(
287 $( '<div>' ).addClass( 'mw-summary-preview' )
288 );
289 }
290
291 if ( !document.getElementById( 'wikiDiff' ) && document.getElementById( 'wikiPreview' ) ) {
292 $( '#wikiPreview' ).after(
293 $( '<div>' )
294 .hide()
295 .attr( 'id', 'wikiDiff' )
296 .html( '<table class="diff"><col class="diff-marker"/><col class="diff-content"/>' +
297 '<col class="diff-marker"/><col class="diff-content"/><tbody/></table>' )
298 );
299 }
300
301 // This should be moved down to '#editform', but is kept on the body for now
302 // because the LiquidThreads extension is re-using this module with only half
303 // the EditPage (doesn't include #editform presumably, bug 55463).
304 $( document.body ).on( 'click', '#wpPreview, #wpDiff', doLivePreview );
305 } );
306
307 }( mediaWiki, jQuery ) );