1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
|
( function () {
const api = new mw.Api();
const util = require( 'mediawiki.util' );
/**
* Show the edit summary.
*
* @private
* @param {jQuery} $formNode
* @param {Object} response
*/
function showEditSummary( $formNode, response ) {
const $summaryPreview = $formNode.find( '.mw-summary-preview' ).empty();
const parse = response.parse;
if ( !parse || !parse.parsedsummary ) {
return;
}
$summaryPreview.append(
mw.message( 'summary-preview' ).parse(),
' ',
$( '<span>' ).addClass( 'comment' ).html( parenthesesWrap( parse.parsedsummary ) )
);
}
/**
* Wrap a string in parentheses.
*
* @private
* @param {string} str
* @return {string}
*/
function parenthesesWrap( str ) {
if ( str === '' ) {
return str;
}
// There is no equivalent to rawParams
return mw.message( 'parentheses' ).escaped()
// Specify a function as the replacement,
// so that "$" characters in str are not interpreted.
.replace( '$1', () => str );
}
/**
* Show status indicators.
*
* @private
* @param {Array} indicators
*/
function showIndicators( indicators ) {
// eslint-disable-next-line no-jquery/no-map-util
indicators = $.map( indicators, ( indicator, name ) => $( '<div>' )
.addClass( 'mw-indicator' )
.attr( 'id', mw.util.escapeIdForAttribute( 'mw-indicator-' + name ) )
.html( indicator )
.get( 0 ) );
if ( indicators.length ) {
mw.hook( 'wikipage.indicators' ).fire( $( indicators ) );
}
// Add whitespace between the <div>s because
// they get displayed with display: inline-block
const newList = [];
indicators.forEach( ( indicator ) => {
newList.push( indicator, document.createTextNode( '\n' ) );
} );
$( '.mw-indicators' ).empty().append( newList );
}
/**
* Show the templates used.
*
* The formatting here repeats what is done in includes/TemplatesOnThisPageFormatter.php
*
* @private
* @param {Array} templates List of template titles.
*/
function showTemplates( templates ) {
// The .templatesUsed div can be empty, if no templates are in use.
// In that case, we have to create the required structure.
const $parent = $( '.templatesUsed' );
// Find or add the explanation text (the toggler for collapsing).
let $explanation = $parent.find( '.mw-templatesUsedExplanation p' );
if ( $explanation.length === 0 ) {
$explanation = $( '<p>' );
$parent.append( $( '<div>' )
.addClass( 'mw-templatesUsedExplanation' )
.append( $explanation ) );
}
// Find or add the list. The makeCollapsible() method is called on this
// in resources/src/mediawiki.action/mediawiki.action.edit.collapsibleFooter.js
let $list = $parent.find( 'ul' );
if ( $list.length === 0 ) {
$list = $( '<ul>' ).addClass( [ 'mw-editfooter-list', 'mw-collapsible', 'mw-made-collapsible' ] );
$parent.append( $list );
}
if ( templates.length === 0 ) {
$explanation.msg( 'templatesusedpreview', 0 );
$list.empty();
return;
}
// Fetch info about all templates, batched because API is limited to 50 at a time.
$parent.addClass( 'mw-preview-loading-elements-loading' );
const batchSize = 50;
const requests = [];
for ( let batch = 0; batch < templates.length; batch += batchSize ) {
// Build a list of template names for this batch.
const titles = templates
.slice( batch, batch + batchSize )
.map( ( template ) => template.title );
requests.push( api.post( {
action: 'query',
format: 'json',
formatversion: 2,
titles: titles,
prop: 'info',
// @todo Do we need inlinkcontext here?
inprop: 'linkclasses|protection',
intestactions: 'edit'
} ) );
}
$.when( ...requests ).done( function () {
const templatesAllInfo = [];
// For the first batch, empty the list in preparation for either adding new items or not needing to.
for ( let r = 0; r < arguments.length; r++ ) {
// Response is either the whole argument, or the 0th element of it.
const response = arguments[ r ][ 0 ] || arguments[ r ];
const templatesInfo = ( response.query && response.query.pages ) || [];
templatesInfo.forEach( ( ti ) => {
templatesAllInfo.push( {
title: mw.Title.newFromText( ti.title ),
apiData: ti
} );
} );
}
// Sort alphabetically.
templatesAllInfo.sort( ( t1, t2 ) => {
// Compare titles with the same rules of Title::compare() in PHP.
if ( t1.title.getNamespaceId() !== t2.title.getNamespaceId() ) {
return t1.title.getNamespaceId() - t2.title.getNamespaceId();
} else {
return t1.title.getMain() === t2.title.getMain() ?
0 :
t1.title.getMain() < t2.title.getMain() ? -1 : 1;
}
} );
// Add new template list, and update the list header.
const $listNew = $( '<ul>' );
addItemToTemplateListPromise( $listNew, templatesAllInfo, 0 )
.then( () => {
$list.html( $listNew.html() );
} );
$explanation.msg( 'templatesusedpreview', templatesAllInfo.length );
} ).always( () => {
$parent.removeClass( 'mw-preview-loading-elements-loading' );
} );
}
/**
* Recursive function to add a template link to the list of templates in use.
* This is useful because addItemToTemplateList() might need to make extra API requests to fetch
* messages, but we don't want to send parallel requests for these (because they're often the
* for the same messages).
*
* @private
* @param {jQuery} $list The `<ul>` to add the item to.
* @param {Object} templatesInfo All templates' info, sorted by namespace and title.
* @param {number} templateIndex The current item in templatesInfo (0-indexed).
* @return {jQuery.Promise}
*/
function addItemToTemplateListPromise( $list, templatesInfo, templateIndex ) {
return addItemToTemplateList( $list, templatesInfo[ templateIndex ] ).then( () => {
if ( templatesInfo[ templateIndex + 1 ] !== undefined ) {
return addItemToTemplateListPromise( $list, templatesInfo, templateIndex + 1 );
}
} );
}
/**
* Create list item with relevant links for the given template, and add it to the $list.
*
* @private
* @param {jQuery} $list The `<ul>` to add the item to.
* @param {Object} template Template info with which to construct the `<li>`.
* @return {jQuery.Promise}
*/
function addItemToTemplateList( $list, template ) {
const editable = template.apiData.ns >= 0;
const canEdit = editable && template.apiData.actions.edit !== undefined;
const linkClasses = template.apiData.linkclasses || [];
if ( template.apiData.missing !== undefined && template.apiData.known === undefined ) {
linkClasses.push( 'new' );
}
const $baseLink = $( '<a>' )
// Additional CSS classes (e.g. link colors) used for links to this template.
// The following classes might be used here:
// * new
// * mw-redirect
// * any added by the GetLinkColours hook
.addClass( linkClasses );
const $link = $baseLink.clone()
.attr( 'href', template.title.getUrl() )
.text( template.title.getPrefixedText() );
if ( editable ) {
const $editLink = $baseLink.clone()
.attr( 'href', template.title.getUrl( { action: 'edit' } ) )
.append( mw.msg( canEdit ? 'editlink' : 'viewsourcelink' ) );
const wordSep = mw.message( 'word-separator' ).escaped();
return getRestrictionsText( template.apiData.protection || [] )
.then( ( restrictionsList ) => {
// restrictionsList is a comma-separated parentheses-wrapped localized list of restriction level names.
const editLinkParens = parenthesesWrap( $editLink[ 0 ].outerHTML );
const $li = $( '<li>' ).append( $link, wordSep, editLinkParens, wordSep, restrictionsList );
$list.append( $li );
} );
} else {
$list.append( $( '<li>' ).append( $link ) );
return $.Deferred().resolve( '' );
}
}
/**
* Get a localized string listing the restriction levels for a template.
*
* This should match the logic from TemplatesOnThisPageFormatter::getRestrictionsText().
*
* @private
* @param {Array} restrictions Set of protection info objects from the inprop=protection API.
* @return {jQuery.Promise}
*/
function getRestrictionsText( restrictions ) {
let msg = '';
if ( !restrictions ) {
return $.Deferred().resolve( msg );
}
// Record other restriction levels, in case it's protected for others.
const restrictionLevels = [];
restrictions.forEach( ( r ) => {
if ( r.type !== 'edit' ) {
return;
}
if ( r.level === 'sysop' ) {
msg = mw.msg( 'template-protected' );
} else if ( r.level === 'autoconfirmed' ) {
msg = mw.msg( 'template-semiprotected' );
} else {
restrictionLevels.push( r.level );
}
} );
// If sysop or autoconfirmed, use that.
if ( msg !== '' ) {
return $.Deferred().resolve( msg );
}
// Otherwise, if the edit restriction isn't one of the backwards-compatible ones,
// use the (possibly custom) restriction-level-* messages.
const msgs = [];
restrictionLevels.forEach( ( level ) => {
msgs.push( 'restriction-level-' + level );
} );
if ( msgs.length === 0 ) {
return $.Deferred().resolve( '' );
}
// Custom restriction levels don't have their messages loaded, so we have to do that.
return api.loadMessagesIfMissing( msgs ).then( () => {
const localizedMessages = msgs.map(
// Messages that can be used here include:
// * restriction-level-sysop
// * restriction-level-autoconfirmed
( m ) => mw.message( m ).parse()
);
// There's no commaList in JS, so just join with commas (doesn't handle the last item).
return parenthesesWrap( localizedMessages.join( mw.msg( 'comma-separator' ) ) );
} );
}
/**
* Show the language links (Vector-specific).
* TODO: Doesn't work in vector-2022 (maybe it doesn't need to?)
*
* @private
* @param {Array} langLinks
*/
function showLanguageLinks( langLinks ) {
const newList = langLinks.map( ( langLink ) => {
const bcp47 = mw.language.bcp47( langLink.lang );
// eslint-disable-next-line mediawiki/class-doc
return $( '<li>' )
.addClass( 'interlanguage-link interwiki-' + langLink.lang )
.append( $( '<a>' )
.attr( {
href: langLink.url,
title: langLink.title + ' - ' + langLink.langname,
lang: bcp47,
hreflang: bcp47
} )
.text( langLink.autonym )
);
} );
const $list = $( '#p-lang ul' ),
$parent = $list.parent();
$list.detach().empty().append( newList ).prependTo( $parent );
}
/**
* Parse preview response and show a warning at the top of the preview.
*
* @private
* @param {Object} config
* @param {Object} response
*/
function showPreviewNotes( config, response ) {
const arrow = $( document.body ).css( 'direction' ) === 'rtl' ? '←' : '→';
const $previewHeader = $( '<div>' )
.addClass( 'previewnote' )
.append( $( '<h2>' )
.attr( 'id', 'mw-previewheader' )
// TemplateSandbox will insert an HTML string here.
.append( config.previewHeader )
);
const warningContentElement = $( '<div>' )
.append(
// TemplateSandbox will insert a jQuery here.
config.previewNote,
' ',
$( '<span>' )
.addClass( 'mw-continue-editing' )
.append( $( '<a>' )
.attr( 'href', '#' + config.$formNode.attr( 'id' ) )
.text( arrow + ' ' + mw.msg( 'continue-editing' ) )
),
response.parse.parsewarningshtml.map( ( warning ) => $( '<p>' ).append( warning ) )
)[ 0 ];
const warningMessageElement = util.messageBox(
warningContentElement,
'warning'
);
$previewHeader.append( warningMessageElement );
config.$previewNode.prepend( $previewHeader );
}
/**
* Show an error message in place of a preview.
*
* @private
* @param {Object} config
* @param {jQuery} $message
*/
function showError( config, $message ) {
const errorContentElement = $( '<div>' )
.append(
$( '<strong>' ).text( mw.msg( 'previewerrortext' ) ),
$message
)[ 0 ];
const errorMessageElement = util.messageBox( errorContentElement, 'error' );
errorMessageElement.classList.add( 'mw-page-preview-error' );
config.$previewNode.hide().before( errorMessageElement );
if ( config.$diffNode ) {
config.$diffNode.hide();
}
}
/**
* Update the various bits of the page based on the response.
*
* @private
* @param {Object} config
* @param {Object} response
*/
function handleParseResponse( config, response ) {
let $content;
// Js config variables and modules.
if ( response.parse.jsconfigvars ) {
mw.config.set( response.parse.jsconfigvars );
}
if ( response.parse.modules ) {
mw.loader.load( response.parse.modules.concat(
response.parse.modulestyles
) );
}
// Indicators.
showIndicators( response.parse.indicators );
// Display title.
if ( response.parse.displaytitle ) {
$( '#firstHeadingTitle' ).html( response.parse.displaytitle );
}
// Categories.
if ( response.parse.categorieshtml ) {
$content = $( $.parseHTML( response.parse.categorieshtml ) );
mw.hook( 'wikipage.categories' ).fire( $content );
$( '.catlinks[data-mw="interface"]' ).replaceWith( $content );
}
// Table of contents.
if ( response.parse.sections ) {
/**
* Fired when dynamic changes have been made to the table of contents.
*
* @event ~'wikipage.tableOfContents'
* @memberof Hooks
* @param {Object[]} sections Metadata about each section, as returned by
* [API:Parse]{@link https://www.mediawiki.org/wiki/Special:MyLanguage/API:Parsing_wikitext}.
*/
mw.hook( 'wikipage.tableOfContents' ).fire(
response.parse.hidetoc ? [] : response.parse.sections
);
}
// Templates.
if ( response.parse.templates ) {
showTemplates( response.parse.templates );
}
// Limit report.
if ( response.parse.limitreporthtml ) {
$( '.limitreport' ).html( response.parse.limitreporthtml )
.find( '.mw-collapsible' ).makeCollapsible();
}
// Language links.
if ( response.parse.langlinks && mw.config.get( 'skin' ) === 'vector' ) {
showLanguageLinks( response.parse.langlinks );
}
if ( !response.parse.text ) {
return;
}
// Remove any previous preview
config.$previewNode.children( '.mw-parser-output' ).remove();
// Remove preview note, if present (added by Live Preview, etc.).
config.$previewNode.find( '.previewnote' ).remove();
if ( config.isLivePreview ) {
showPreviewNotes( config, response );
}
$content = $( $.parseHTML( response.parse.text ) );
config.$previewNode.append( $content ).show();
mw.hook( 'wikipage.content' ).fire( $content );
}
/**
* Get the unresolved promise of the preview request.
*
* @private
* @param {Object} config
* @param {string|number} section
* @return {jQuery.Promise}
*/
function getParseRequest( config, section ) {
const params = {
formatversion: 2,
action: 'parse',
summary: config.summary,
prop: ''
};
params[ config.titleParam ] = config.title;
if ( !config.showDiff ) {
params[ config.textParam ] = config.$textareaNode.textSelection( 'getContents' );
Object.assign( params, {
prop: 'text|indicators|displaytitle|modules|jsconfigvars|categorieshtml|sections|templates|langlinks|limitreporthtml|parsewarningshtml',
pst: true,
preview: true,
sectionpreview: section !== '',
disableeditsection: true,
useskin: mw.config.get( 'skin' ),
uselang: mw.config.get( 'wgUserLanguage' )
} );
if ( mw.config.get( 'wgUserVariant' ) ) {
params.variant = mw.config.get( 'wgUserVariant' );
}
}
if ( section === 'new' ) {
params.section = 'new';
params.sectiontitle = params.summary;
delete params.summary;
}
Object.assign( params, config.parseParams );
return api.post( params, { headers: { 'Promise-Non-Write-API-Action': 'true' } } );
}
/**
* Get the required <table> structure for displaying diffs.
*
* @return {jQuery}
*/
function getDiffTable() {
return $( '<table>' ).addClass( 'diff' ).append(
$( '<col>' ).addClass( 'diff-marker' ),
$( '<col>' ).addClass( 'diff-content' ),
$( '<col>' ).addClass( 'diff-marker' ),
$( '<col>' ).addClass( 'diff-content' ),
$( '<thead>' ).append(
$( '<tr>' ).addClass( 'diff-title' ).append(
$( '<td>' )
.attr( 'colspan', 2 )
.addClass( 'diff-otitle diff-side-deleted' )
.text( mw.msg( 'currentrev' ) ),
$( '<td>' )
.attr( 'colspan', 2 )
.addClass( 'diff-ntitle diff-side-added' )
.text( mw.msg( 'yourtext' ) )
)
),
$( '<tbody>' )
);
}
/**
* Show the diff from the response.
*
* @private
* @param {Object} config
* @param {Object} response
*/
function handleDiffResponse( config, response ) {
const $table = getDiffTable();
config.$diffNode
.hide()
.empty()
.append( $table );
const diff = response.compare.bodies;
if ( diff.main ) {
$table.find( 'tbody' ).html( diff.main );
mw.hook( 'wikipage.diff' ).fire( $table );
} else {
// The diff is empty.
const $tableCell = $( '<td>' )
.attr( 'colspan', 4 )
.addClass( 'diff-notice' )
.append(
$( '<div>' )
.addClass( 'mw-diff-empty' )
.text( mw.msg( 'diff-empty' ) )
);
$table.find( 'tbody' )
.empty()
.append(
$( '<tr>' ).append( $tableCell )
);
}
config.$diffNode.show();
}
/**
* Get the unresolved promise of the diff request.
*
* @private
* @param {Object} config
* @param {string|number} section
* @param {boolean} pageExists
* @return {jQuery.Promise}
*/
function getDiffRequest( config, section, pageExists ) {
let contents = config.$textareaNode.textSelection( 'getContents' ),
sectionTitle = config.summary;
if ( section === 'new' ) {
// T293930: Hack to show live diff for new section creation.
// We concatenate the section heading with the edit box text and pass it to
// the diff API as the full input text. This is roughly what the server-side
// does when difference is requested for section edit.
// The heading is always prepended, we do not bother with editing old rev
// at this point (`?action=edit&oldid=xxx§ion=new`) -- which will require
// mid-text insertion of the section -- because creation of new section is only
// possible on latest revision.
// The section heading text is unconditionally wrapped in <h2> heading and
// ends with double newlines, except when it's empty. This is for parity with the
// server-side rendering of the same case.
sectionTitle = sectionTitle === '' ? '' : '== ' + sectionTitle + ' ==\n\n';
// Prepend section heading to section text.
contents = sectionTitle + contents;
}
const params = {
action: 'compare',
fromtitle: config.title,
totitle: config.title,
toslots: 'main',
// Remove trailing whitespace for consistency with EditPage diffs.
// TODO trimEnd() when we can use that.
'totext-main': contents.replace( /\s+$/, '' ),
'tocontentmodel-main': mw.config.get( 'wgPageContentModel' ),
topst: true,
slots: 'main',
uselang: mw.config.get( 'wgUserLanguage' )
};
if ( mw.config.get( 'wgUserVariant' ) ) {
params.variant = mw.config.get( 'wgUserVariant' );
}
if ( section ) {
params[ 'tosection-main' ] = section;
}
if ( !pageExists ) {
params.fromslots = 'main';
params[ 'fromcontentmodel-main' ] = mw.config.get( 'wgPageContentModel' );
params[ 'fromtext-main' ] = '';
}
return api.post( params );
}
/**
* Get the selectors of elements that should be grayed out while the preview is being generated.
*
* @memberof module:mediawiki.page.preview
* @return {string[]}
* @stable
*/
function getLoadingSelectors() {
return [
// Main
'.mw-indicators',
'#firstHeading',
'#wikiPreview',
'#wikiDiff',
'#catlinks',
'#p-lang',
// Editing-related
'.templatesUsed',
'.limitreport',
'.mw-summary-preview',
'.hiddencats'
];
}
/**
* Fetch and display a preview of the current editing area.
*
* @memberof module:mediawiki.page.preview
* @param {Object} config Configuration options.
* @param {jQuery} [config.$previewNode=$( '#wikiPreview' )] Where the preview should be displayed.
* @param {jQuery} [config.$diffNode=$( '#wikiDiff' )] Where diffs should be displayed (if showDiff is set).
* @param {jQuery} [config.$formNode=$( '#editform' )] The form node.
* @param {jQuery} [config.$textareaNode=$( '#wpTextbox1' )] The edit form's textarea.
* @param {jQuery} [config.$spinnerNode=$( '.mw-spinner-preview' )] The loading indicator. This will
* be shown/hidden accordingly while waiting for the XMLHttpRequest to complete.
* Ignored if it doesn't exist in the document and `createSpinner` is false.
* @param {string} [config.summary=null] The edit summary. If no value is given, the summary is
* fetched from `$( '#wpSummaryWidget' )`.
* @param {boolean} [config.showDiff=false] Shows a diff in the preview area instead of the content.
* @param {boolean} [config.isLivePreview=false] Instructs the module to replicate the
* server-side preview as much as possible. Specifically:
* - Before initiating the preview, some alerts and error messages at the top of the page will
* be removed, and the browser will scroll to the preview.
* - After finishing the preview, a reminder that it's only a preview, or an error message in
* case a request has failed, will be shown at the top of the preview.
* @param {Node|Node[]|jQuery|string} [config.previewHeader=null] Content of `<h2>` element at
* the top of the preview notes. Required if `isLivePreview` is true.
* @param {Node|Node[]|jQuery|string} [config.previewNote=null] Main text of the first preview
* note. Required if `isLivePreview` is true.
* @param {string} [config.title=mw.config.get( 'wgPageName' )] The title of the page being previewed.
* @param {string} [config.titleParam='title'] Name of the parse API parameter to pass `title` to.
* @param {string} [config.textParam='text'] Name of the parse API parameter to pass the content
* of `$textareaNode` to. Ignored if `showDiff` is true.
* @param {Object} [config.parseParams=null] Additional parse API parameters. This can override
* any parameter set by the module.
* @param {module:mediawiki.page.preview~responseHandler} [config.responseHandler=null] Callback
* to run right after the API responses are received. This allows the config and response
* objects to be modified before the preview is shown.
* @param {boolean} [config.createSpinner=false] Creates `$spinnerNode` and inserts it before
* `$previewNode` if one doesn't already exist and the module `jquery.spinner` is loaded.
* @param {string[]} [config.loadingSelectors=getLoadingSelectors()] An array of query selectors
* (i.e. '#catlinks') that should be grayed out while the preview is being generated.
* @return {jQuery.Promise|undefined} jQuery.Promise or `undefined` if no `$textareaNode` was provided in the config.
* @fires Hooks~'wikipage.categories'
* @fires Hooks~'wikipage.content'
* @fires Hooks~'wikipage.diff'
* @fires Hooks~'wikipage.indicators'
* @fires Hooks~'wikipage.tableOfContents'
* @stable
*/
function doPreview( config ) {
config = Object.assign( {
$previewNode: $( '#wikiPreview' ),
$diffNode: $( '#wikiDiff' ),
$formNode: $( '#editform' ),
$textareaNode: $( '#wpTextbox1' ),
$spinnerNode: $( '.mw-spinner-preview' ),
summary: null,
showDiff: false,
isLivePreview: false,
previewHeader: null,
previewNote: null,
title: mw.config.get( 'wgPageName' ),
titleParam: 'title',
textParam: 'text',
parseParams: null,
responseHandler: null,
createSpinner: false,
loadingSelectors: getLoadingSelectors()
}, config );
const section = config.$formNode.find( '[name="wpSection"]' ).val();
if ( !config.$textareaNode || config.$textareaNode.length === 0 ) {
return;
}
// Fetch edit summary, if not already given.
if ( !config.summary ) {
const $summaryWidget = $( '#wpSummaryWidget' );
if ( $summaryWidget.length ) {
config.summary = OO.ui.infuse( $summaryWidget ).getValue();
}
}
if ( config.isLivePreview ) {
// Not shown during normal preview, to be removed if present
$( '.mw-newarticletext, .mw-page-preview-error' ).remove();
// Show #wikiPreview if it's hidden to be able to scroll to it.
// (If it is hidden, it's also empty, so nothing changes in the rendering.)
config.$previewNode.show();
// Jump to where the preview will appear
config.$previewNode[ 0 ].scrollIntoView();
}
// Show or create the spinner if possible.
if ( config.$spinnerNode && config.$spinnerNode.length ) {
config.$spinnerNode.show();
} else if ( config.createSpinner ) {
if ( mw.loader.getState( 'jquery.spinner' ) === 'ready' ) {
config.$spinnerNode = $.createSpinner( {
size: 'large',
type: 'block'
} )
.addClass( 'mw-spinner-preview' )
.insertBefore( config.$previewNode );
} else {
mw.log.warn( 'createSpinner requires the module jquery.spinner' );
}
}
// Gray out the 'copy elements' while we wait for a response.
const $loadingElements = $( config.loadingSelectors.join( ',' ) );
$loadingElements.addClass( [ 'mw-preview-loading-elements', 'mw-preview-loading-elements-loading' ] );
// Acquire a temporary user username before previewing or diffing, so that signatures and
// user-related magic words display the temp user instead of IP user in the preview. (T331397)
const tempUserNamePromise = mw.user.acquireTempUserName();
let diffRequest;
const parseRequest = tempUserNamePromise.then( () => getParseRequest( config, section ) );
if ( config.showDiff ) {
config.$previewNode.hide();
// Add the diff node if it doesn't exist (directly after the preview node).
if ( config.$diffNode.length === 0 && config.$previewNode.length > 0 ) {
const rtlDir = $( '#wpTextbox1' ).attr( 'dir' ) === 'rtl';
const alignStart = rtlDir ? 'right' : 'left';
config.$diffNode = $( '<div>' )
.attr( 'id', 'wikiDiff' )
// The following classes are used here:
// * diff-editfont-monospace
// * diff-editfont-sans-serif
// * diff-editfont-serif
.addClass( 'diff-editfont-' + mw.user.options.get( 'editfont' ) )
// The following classes are used here:
// * diff-contentalign-left
// * diff-contentalign-right
.addClass( 'diff-contentalign-' + alignStart );
config.$previewNode.after( config.$diffNode );
}
// Hide the table of contents, in case it was previously shown after previewing.
mw.hook( 'wikipage.tableOfContents' ).fire( [] );
// The compare API returns an error if the title doesn't exist and fromtext is not
// specified. So we have to account for the possibility that the page was created or
// deleted after the user started editing. Luckily the parse API returns pageid so we
// can wait for that.
// TODO: Show "Warning: This page was deleted after you started editing!"?
diffRequest = parseRequest.then( ( parseResponse ) => getDiffRequest( config, section, parseResponse.parse.pageid !== 0 ) );
} else if ( config.$diffNode ) {
config.$diffNode.hide();
}
return $.when( parseRequest, diffRequest )
.done( ( parseResponse, diffResponse ) => {
if ( config.responseHandler ) {
/**
* @callback module:mediawiki.page.preview~responseHandler
* @param {Object} config Options for live preview API
* @param {Object} parseResponse Parse API response
* @param {Object} [diffResponse] Compare API response
*/
if ( config.showDiff ) {
config.responseHandler( config, parseResponse[ 0 ], diffResponse[ 0 ] );
} else {
config.responseHandler( config, parseResponse[ 0 ] );
}
}
showEditSummary( config.$formNode, parseResponse[ 0 ] );
if ( config.showDiff ) {
handleDiffResponse( config, diffResponse[ 0 ] );
} else {
handleParseResponse( config, parseResponse[ 0 ] );
}
mw.hook( 'wikipage.editform' ).fire( config.$formNode );
} )
.fail( ( _code, result ) => {
if ( config.isLivePreview ) {
// This just shows the error for whatever request failed first
showError( config, api.getErrorMessage( result ) );
}
} )
.always( () => {
if ( config.$spinnerNode && config.$spinnerNode.length ) {
config.$spinnerNode.hide();
}
$loadingElements.removeClass( 'mw-preview-loading-elements-loading' );
} );
}
/**
* Fetch and display a preview of the current editing area.
*
* @example
* var preview = require( 'mediawiki.page.preview' );
* preview.doPreview();
*
* @exports mediawiki.page.preview
*/
module.exports = {
doPreview: doPreview,
getLoadingSelectors: getLoadingSelectors
};
}() );
|