Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/preinit/ve.init.mw.DiffPage.init.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki DiffPage init.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /* eslint-disable no-jquery/no-global-selector */
9
10 ( function () {
11 const $visualDiff = $( '<div>' ),
12 progress = new OO.ui.ProgressBarWidget( { classes: [ 've-init-mw-diffPage-loading' ] } ),
13 originalUrl = new URL( location.href ),
14 conf = mw.config.get( 'wgVisualEditorConfig' ),
15 pluginModules = conf.pluginModules.filter( mw.loader.getState );
16 let reviewModeButtonSelect, lastDiff,
17 $wikitextDiffHeader, $wikitextDiffBody,
18 initMode = originalUrl.searchParams.get( 'diffmode' ) || mw.user.options.get( 'visualeditor-diffmode-historical' ) || 'source',
19 diffTypeSwitch;
20
21 if ( initMode !== 'visual' ) {
22 // Enforce a valid mode, to avoid visual glitches in button-selection.
23 initMode = 'source';
24 }
25 let mode = initMode;
26
27 const $visualDiffContainer = $( '<div>' ).append(
28 progress.$element.addClass( 'oo-ui-element-hidden' ),
29 $visualDiff
30 );
31
32 function onReviewModeButtonSelectSelect( item ) {
33 let oldPageName, newPageName;
34 if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'ComparePages' ) {
35 oldPageName = newPageName = mw.config.get( 'wgRelevantPageName' );
36 } else {
37 const params = new URLSearchParams( location.search );
38 oldPageName = params.get( 'page1' );
39 newPageName = params.get( 'page2' );
40 }
41
42 mode = item.getData();
43 const isVisual = mode === 'visual';
44
45 $visualDiffContainer.toggleClass( 'oo-ui-element-hidden', !isVisual );
46 $wikitextDiffBody.toggleClass( 'oo-ui-element-hidden', isVisual );
47
48 // If inline switch exists
49 if ( typeof diffTypeSwitch !== 'undefined' ) {
50 diffTypeSwitch.setDisabled( isVisual );
51 }
52
53 $( '.mw-revslider-container' ).toggleClass( 've-init-mw-diffPage-revSlider-visual', isVisual );
54 if ( isVisual ) {
55 // Highlight the headers using the same styles as the diff, to better indicate
56 // the meaning of headers when not using two-column diff.
57 $wikitextDiffHeader.find( '#mw-diff-otitle1' ).attr( 'data-diff-action', 'remove' );
58 $wikitextDiffHeader.find( '#mw-diff-ntitle1' ).attr( 'data-diff-action', 'insert' );
59 } else {
60 $wikitextDiffHeader.find( '#mw-diff-otitle1, #mw-diff-ntitle1' )
61 .removeAttr( 'data-diff-action' );
62 }
63
64 const oldId = mw.config.get( 'wgDiffOldId' );
65 const newId = mw.config.get( 'wgDiffNewId' );
66 if ( isVisual && !(
67 lastDiff && lastDiff.oldId === oldId && lastDiff.newId === newId &&
68 lastDiff.oldPageName === oldPageName && lastDiff.newPageName === newPageName
69 ) ) {
70 $visualDiff.empty();
71 progress.$element.removeClass( 'oo-ui-element-hidden' );
72 // TODO: Load a smaller subset of VE for computing the visual diff
73 const modulePromise = mw.loader.using( [ 'ext.visualEditor.articleTarget', 'ext.visualEditor.mwmeta', ...pluginModules ] );
74 mw.libs.ve.diffLoader.getVisualDiffGeneratorPromise( oldId, newId, modulePromise, oldPageName, newPageName ).then( ( visualDiffGenerator ) => {
75 // This class is loaded via modulePromise above
76 // eslint-disable-next-line no-undef
77 const diffElement = new ve.ui.DiffElement( visualDiffGenerator(), { classes: [ 've-init-mw-diffPage-diff' ] } );
78 diffElement.$document.addClass( 'mw-parser-output content' );
79
80 mw.libs.ve.fixFragmentLinks( diffElement.$document[ 0 ], mw.Title.newFromText( newPageName ), 'mw-diffpage-visualdiff-' );
81
82 progress.$element.addClass( 'oo-ui-element-hidden' );
83 $visualDiff.append( diffElement.$element );
84 lastDiff = {
85 oldId: oldId,
86 newId: newId,
87 oldPageName: oldPageName,
88 newPageName: newPageName
89 };
90
91 diffElement.positionDescriptions();
92 }, ( code, data ) => {
93 mw.notify( new mw.Api().getErrorMessage( data ), { type: 'error' } );
94 reviewModeButtonSelect.selectItemByData( 'source' );
95 } ).catch( ( error ) => {
96 mw.notify( error.message, { type: 'error' } );
97 reviewModeButtonSelect.selectItemByData( 'source' );
98 throw error;
99 } );
100 }
101 }
102
103 function onReviewModeButtonSelectChoose( item ) {
104 mode = item.getData();
105 if ( mode !== mw.user.options.get( 'visualeditor-diffmode-historical' ) ) {
106 mw.user.options.set( 'visualeditor-diffmode-historical', mode );
107 // Same as ve.init.target.getLocalApi()
108 new mw.Api().saveOption( 'visualeditor-diffmode-historical', mode );
109 }
110 }
111
112 mw.hook( 'wikipage.diff' ).add( () => {
113 if ( mw.config.get( 'wgDiffOldId' ) === false || mw.config.get( 'wgDiffNewId' ) === false ) {
114 // Don't offer visual diffs for "fake" diffs where the revision to compare to is not given,
115 // e.g. when viewing a "diff" of page creation (T338388)
116 return;
117 }
118 if ( !Object.prototype.hasOwnProperty.call( conf.contentModels, mw.config.get( 'wgPageContentModel' ) ) ) {
119 // Don't offer visual diffs for non-wikitext pages.
120 // TODO: This is wrong if we're comparing revisions of two different pages, or if the content
121 // model of the page has changed between the revisions. The diff needs to give us more
122 // information about the revisions being compared… (T346252)
123 return;
124 }
125
126 const $wikitextDiffContainer = $( 'table.diff[data-mw="interface"]' );
127 $wikitextDiffHeader = $wikitextDiffContainer.find( 'tr.diff-title' )
128 .add( $wikitextDiffContainer.find( 'td.diff-multi, td.diff-notice' ).parent() );
129 $wikitextDiffBody = $wikitextDiffContainer.find( 'tr' ).not( $wikitextDiffHeader );
130 $wikitextDiffContainer.after( $visualDiffContainer );
131
132 // The PHP widget was a ButtonGroupWidget, so replace with a
133 // ButtonSelectWidget instead of infusing.
134 reviewModeButtonSelect = new OO.ui.ButtonSelectWidget( {
135 items: [
136 new OO.ui.ButtonOptionWidget( { data: 'visual', icon: 'eye', label: mw.msg( 'visualeditor-savedialog-review-visual' ) } ),
137 new OO.ui.ButtonOptionWidget( { data: 'source', icon: 'wikiText', label: mw.msg( 'visualeditor-savedialog-review-wikitext' ) } )
138 ]
139 } );
140 // Choose is only emitted when the user interacts with the widget, whereas
141 // select is emitted even when the mode is set programmatically (e.g. on load)
142 reviewModeButtonSelect.on( 'select', onReviewModeButtonSelectSelect );
143 reviewModeButtonSelect.on( 'choose', onReviewModeButtonSelectChoose );
144 $( '.ve-init-mw-diffPage-diffMode' ).empty().append( reviewModeButtonSelect.$element );
145 reviewModeButtonSelect.selectItemByData( mode );
146 } );
147
148 mw.hook( 'wikipage.diff.wikitextBodyUpdate' ).add( ( $wikitextBody ) => {
149 $wikitextDiffBody = $wikitextBody;
150 } );
151
152 mw.hook( 'wikipage.diff.diffTypeSwitch' ).add( ( inlineToggleSwitch ) => {
153 diffTypeSwitch = inlineToggleSwitch;
154 diffTypeSwitch.setDisabled( mode === 'visual' );
155 } );
156 }() );