Debian Codegraph @

resources/src/mediawiki.action/mediawiki.action.edit.preview.js

raw symbols SCIP
1 /*!
2 * Live edit preview.
3 */
4 ( function () {
5 'use strict';
6
7 const parsedMessages = require( './mediawiki.action.edit.preview.parsedMessages.json' );
8
9 /**
10 * @ignore
11 * @param {jQuery.Event} e
12 */
13 function doLivePreview( e ) {
14 const promise = require( 'mediawiki.page.preview' ).doPreview( {
15 showDiff: e.target.name === 'wpDiff',
16 isLivePreview: true,
17 previewHeader: mw.message( 'preview' ).escaped(),
18 previewNote: parsedMessages.previewnote,
19 createSpinner: true
20 } );
21
22 if ( !promise ) {
23 // Something has gone wrong, so submit the form the normal way.
24 return;
25 }
26
27 e.preventDefault();
28 }
29
30 $( () => {
31 let selector;
32
33 // Enable only live diff on user .js/.css pages, as there's no sensible way of
34 // "previewing" the scripts or styles without reloading the page.
35 if ( $( '#mw-userjsyoucanpreview, #mw-usercssyoucanpreview, #mw-userjspreview, #mw-usercsspreview' ).length ) {
36 selector = '#wpDiff';
37 } else {
38 selector = '#wpPreview, #wpDiff';
39 }
40
41 // The following elements can change in a preview but are not output
42 // by the server when they're empty until the preview response.
43 // TODO: Make the server output these always (in a hidden state), so we don't
44 // have to fish and (hopefully) put them in the right place (since skins
45 // can change where they are output).
46 // FIXME: This is prone to breaking any time Vector's HTML for portals change.
47
48 if ( !document.getElementById( 'p-lang' ) && document.getElementById( 'p-tb' ) && ( mw.config.get( 'skin' ) === 'vector' || mw.config.get( 'skin' ) === 'vector-2022' ) ) {
49 $( '.portal' ).last().after(
50 $( '<nav>' )
51 .attr( {
52 class: 'mw-portlet mw-portlet-lang vector-menu vector-menu-portal portal',
53 id: 'p-lang',
54 role: 'navigation',
55 'aria-labelledby': 'p-lang-label'
56 } )
57 .append(
58 $( '<label>' )
59 .attr( {
60 id: 'p-lang-label',
61 class: 'vector-menu-heading'
62 } )
63 .append(
64 $( '<span>' )
65 .addClass( 'vector-menu-heading-label' )
66 .text( mw.msg( 'otherlanguages' ) )
67 )
68 )
69 .append(
70 $( '<div>' )
71 .addClass( 'body vector-menu-content' )
72 .append( $( '<ul>' ).addClass( 'vector-menu-content-list' ) )
73 )
74 );
75 }
76
77 if ( !$( '.mw-summary-preview' ).length ) {
78 $( '#wpSummaryWidget' ).after(
79 $( '<div>' ).addClass( 'mw-summary-preview' )
80 );
81 }
82
83 // This should be moved down to '#editform', but is kept on the body for now
84 // because the LiquidThreads extension is re-using this module with only half
85 // the EditPage (doesn't include #editform presumably, T57463).
86 $( document.body ).on( 'click', selector, doLivePreview );
87 } );
88
89 }() );