2017-06-09 13:45:59 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki DiffPage init.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2017-06-09 13:45:59 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2019-02-20 20:23:43 +00:00
|
|
|
/* eslint-disable no-jquery/no-global-selector */
|
2019-01-08 17:00:09 +00:00
|
|
|
|
2017-06-09 13:45:59 +00:00
|
|
|
( function () {
|
2021-05-05 17:06:37 +00:00
|
|
|
var reviewModeButtonSelect, lastDiff,
|
2018-04-30 17:21:28 +00:00
|
|
|
$wikitextDiffContainer, $wikitextDiffHeader, $wikitextDiffBody,
|
2018-03-04 14:45:33 +00:00
|
|
|
$visualDiffContainer = $( '<div>' ),
|
|
|
|
$visualDiff = $( '<div>' ),
|
|
|
|
progress = new OO.ui.ProgressBarWidget( { classes: [ 've-init-mw-diffPage-loading' ] } ),
|
2022-12-12 19:49:26 +00:00
|
|
|
originalUrl = new URL( location.href ),
|
|
|
|
initMode = originalUrl.searchParams.get( 'diffmode' ) || mw.user.options.get( 'visualeditor-diffmode-historical' ) || 'source',
|
2017-06-09 13:45:59 +00:00
|
|
|
conf = mw.config.get( 'wgVisualEditorConfig' ),
|
2023-05-02 05:17:10 +00:00
|
|
|
pluginModules = conf.pluginModules.filter( mw.loader.getState ),
|
|
|
|
diffTypeSwitch;
|
2017-06-09 13:45:59 +00:00
|
|
|
|
2022-01-06 16:47:35 +00:00
|
|
|
if ( initMode !== 'visual' ) {
|
2018-02-22 17:48:37 +00:00
|
|
|
// Enforce a valid mode, to avoid visual glitches in button-selection.
|
2022-01-06 16:47:35 +00:00
|
|
|
initMode = 'source';
|
2018-02-22 17:48:37 +00:00
|
|
|
}
|
2023-05-02 05:17:10 +00:00
|
|
|
var mode = initMode;
|
2018-02-22 17:48:37 +00:00
|
|
|
|
2018-03-04 14:45:33 +00:00
|
|
|
$visualDiffContainer.append(
|
|
|
|
progress.$element.addClass( 'oo-ui-element-hidden' ),
|
|
|
|
$visualDiff
|
|
|
|
);
|
|
|
|
|
2017-06-09 13:45:59 +00:00
|
|
|
function onReviewModeButtonSelectSelect( item ) {
|
2021-05-05 17:06:37 +00:00
|
|
|
var oldPageName, newPageName;
|
2017-07-27 12:26:55 +00:00
|
|
|
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'ComparePages' ) {
|
|
|
|
oldPageName = newPageName = mw.config.get( 'wgRelevantPageName' );
|
|
|
|
} else {
|
2023-04-13 01:42:18 +00:00
|
|
|
var params = new URLSearchParams( location.search );
|
|
|
|
oldPageName = params.get( 'page1' );
|
|
|
|
newPageName = params.get( 'page2' );
|
2017-07-27 12:26:55 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 05:17:10 +00:00
|
|
|
mode = item.getData();
|
2021-05-05 17:06:37 +00:00
|
|
|
var isVisual = mode === 'visual';
|
2018-03-04 14:45:33 +00:00
|
|
|
|
2023-06-26 11:19:08 +00:00
|
|
|
$visualDiffContainer.toggleClass( 'oo-ui-element-hidden', !isVisual );
|
|
|
|
$wikitextDiffBody.toggleClass( 'oo-ui-element-hidden', isVisual );
|
2023-05-02 05:17:10 +00:00
|
|
|
|
|
|
|
if ( typeof diffTypeSwitch !== 'undefined' ) {
|
|
|
|
diffTypeSwitch.setDisabled( isVisual );
|
|
|
|
if ( !isVisual ) {
|
|
|
|
if ( diffTypeSwitch.getValue() ) {
|
2023-06-26 11:19:08 +00:00
|
|
|
$wikitextDiffBody.closest( 'tr:not(.inline-diff-row)' ).addClass( 'oo-ui-element-hidden' );
|
2023-05-02 05:17:10 +00:00
|
|
|
} else {
|
2023-06-26 11:19:08 +00:00
|
|
|
$wikitextDiffBody.closest( 'tr.inline-diff-row' ).addClass( 'oo-ui-element-hidden' );
|
2023-05-02 05:17:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 16:47:35 +00:00
|
|
|
var $revSlider = $( '.mw-revslider-container' );
|
2018-03-04 14:45:33 +00:00
|
|
|
$revSlider.toggleClass( 've-init-mw-diffPage-revSlider-visual', isVisual );
|
2018-04-30 17:21:28 +00:00
|
|
|
if ( isVisual ) {
|
|
|
|
// Highlight the headers using the same styles as the diff, to better indicate
|
|
|
|
// the meaning of headers when not using two-column diff.
|
|
|
|
$wikitextDiffHeader.find( '#mw-diff-otitle1' ).attr( 'data-diff-action', 'remove' );
|
|
|
|
$wikitextDiffHeader.find( '#mw-diff-ntitle1' ).attr( 'data-diff-action', 'insert' );
|
|
|
|
} else {
|
|
|
|
$wikitextDiffHeader.find( '#mw-diff-otitle1' ).removeAttr( 'data-diff-action' );
|
|
|
|
$wikitextDiffHeader.find( '#mw-diff-ntitle1' ).removeAttr( 'data-diff-action' );
|
|
|
|
}
|
2017-06-09 13:45:59 +00:00
|
|
|
|
2022-01-06 16:47:35 +00:00
|
|
|
var oldId = mw.config.get( 'wgDiffOldId' );
|
|
|
|
var newId = mw.config.get( 'wgDiffNewId' );
|
2018-03-04 14:45:33 +00:00
|
|
|
if ( isVisual && !(
|
|
|
|
lastDiff && lastDiff.oldId === oldId && lastDiff.newId === newId &&
|
|
|
|
lastDiff.oldPageName === oldPageName && lastDiff.newPageName === newPageName
|
|
|
|
) ) {
|
|
|
|
$visualDiff.empty();
|
|
|
|
progress.$element.removeClass( 'oo-ui-element-hidden' );
|
2017-06-09 13:45:59 +00:00
|
|
|
// TODO: Load a smaller subset of VE for computing the visual diff
|
2022-01-19 16:01:44 +00:00
|
|
|
var modulePromise = mw.loader.using( [ 'ext.visualEditor.articleTarget', 'ext.visualEditor.mwmeta' ].concat( pluginModules ) );
|
2017-07-13 13:48:32 +00:00
|
|
|
mw.libs.ve.diffLoader.getVisualDiffGeneratorPromise( oldId, newId, modulePromise, oldPageName, newPageName ).then( function ( visualDiffGenerator ) {
|
2019-07-22 20:33:15 +00:00
|
|
|
// This class is loaded via modulePromise above
|
|
|
|
// eslint-disable-next-line no-undef
|
2021-05-05 17:06:37 +00:00
|
|
|
var diffElement = new ve.ui.DiffElement( visualDiffGenerator(), { classes: [ 've-init-mw-diffPage-diff' ] } );
|
2021-10-14 16:26:03 +00:00
|
|
|
diffElement.$document.addClass( 'mw-parser-output content' );
|
2017-06-09 13:45:59 +00:00
|
|
|
|
2022-01-19 01:26:14 +00:00
|
|
|
mw.libs.ve.fixFragmentLinks( diffElement.$document[ 0 ], mw.Title.newFromText( newPageName ), 'mw-diffpage-visualdiff-' );
|
|
|
|
|
2018-03-04 14:45:33 +00:00
|
|
|
progress.$element.addClass( 'oo-ui-element-hidden' );
|
|
|
|
$visualDiff.append( diffElement.$element );
|
|
|
|
lastDiff = {
|
|
|
|
oldId: oldId,
|
|
|
|
newId: newId,
|
|
|
|
oldPageName: oldPageName,
|
|
|
|
newPageName: newPageName
|
|
|
|
};
|
2017-06-09 13:45:59 +00:00
|
|
|
|
|
|
|
diffElement.positionDescriptions();
|
2021-02-23 00:21:55 +00:00
|
|
|
}, function ( code, data ) {
|
|
|
|
mw.notify( new mw.Api().getErrorMessage( data ), { type: 'error' } );
|
|
|
|
reviewModeButtonSelect.selectItemByData( 'source' );
|
2022-11-30 01:23:45 +00:00
|
|
|
} ).catch( function ( error ) {
|
|
|
|
mw.notify( error.message, { type: 'error' } );
|
|
|
|
reviewModeButtonSelect.selectItemByData( 'source' );
|
|
|
|
throw error;
|
2017-06-09 13:45:59 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 16:48:02 +00:00
|
|
|
function onReviewModeButtonSelectChoose( item ) {
|
2023-05-02 05:17:10 +00:00
|
|
|
mode = item.getData();
|
2022-01-06 16:48:02 +00:00
|
|
|
if ( mode !== mw.user.options.get( 'visualeditor-diffmode-historical' ) ) {
|
|
|
|
mw.user.options.set( 'visualeditor-diffmode-historical', mode );
|
|
|
|
// Same as ve.init.target.getLocalApi()
|
|
|
|
new mw.Api().saveOption( 'visualeditor-diffmode-historical', mode );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 13:45:59 +00:00
|
|
|
mw.hook( 'wikipage.diff' ).add( function () {
|
2023-06-10 14:44:18 +00:00
|
|
|
if ( mw.config.get( 'wgDiffOldId' ) === false || mw.config.get( 'wgDiffNewId' ) === false ) {
|
|
|
|
// Don't offer visual diffs for "fake" diffs where the revision to compare to is not given,
|
|
|
|
// e.g. when viewing a "diff" of page creation (T338388)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-30 17:21:28 +00:00
|
|
|
$wikitextDiffContainer = $( 'table.diff[data-mw="interface"]' );
|
2018-05-08 19:57:59 +00:00
|
|
|
$wikitextDiffHeader = $wikitextDiffContainer.find( 'tr.diff-title' )
|
|
|
|
.add( $wikitextDiffContainer.find( 'td.diff-multi, td.diff-notice' ).parent() );
|
2018-04-30 17:21:28 +00:00
|
|
|
$wikitextDiffBody = $wikitextDiffContainer.find( 'tr' ).not( $wikitextDiffHeader );
|
|
|
|
$wikitextDiffContainer.after( $visualDiffContainer );
|
2018-03-04 15:01:26 +00:00
|
|
|
|
2017-06-09 13:45:59 +00:00
|
|
|
// The PHP widget was a ButtonGroupWidget, so replace with a
|
|
|
|
// ButtonSelectWidget instead of infusing.
|
|
|
|
reviewModeButtonSelect = new OO.ui.ButtonSelectWidget( {
|
|
|
|
items: [
|
|
|
|
new OO.ui.ButtonOptionWidget( { data: 'visual', icon: 'eye', label: mw.msg( 'visualeditor-savedialog-review-visual' ) } ),
|
|
|
|
new OO.ui.ButtonOptionWidget( { data: 'source', icon: 'wikiText', label: mw.msg( 'visualeditor-savedialog-review-wikitext' ) } )
|
|
|
|
]
|
|
|
|
} );
|
2022-01-06 16:48:02 +00:00
|
|
|
// Choose is only emitted when the user interacts with the widget, whereas
|
|
|
|
// select is emitted even when the mode is set programmatically (e.g. on load)
|
2017-06-09 13:45:59 +00:00
|
|
|
reviewModeButtonSelect.on( 'select', onReviewModeButtonSelectSelect );
|
2022-01-06 16:48:02 +00:00
|
|
|
reviewModeButtonSelect.on( 'choose', onReviewModeButtonSelectChoose );
|
2017-06-09 13:45:59 +00:00
|
|
|
$( '.ve-init-mw-diffPage-diffMode' ).empty().append( reviewModeButtonSelect.$element );
|
2022-01-06 16:47:35 +00:00
|
|
|
reviewModeButtonSelect.selectItemByData( initMode );
|
2017-06-09 13:45:59 +00:00
|
|
|
} );
|
2023-05-02 05:17:10 +00:00
|
|
|
|
|
|
|
mw.hook( 'wikipage.diff.wikitextBodyUpdate' ).add( function ( $wikitextBody ) {
|
|
|
|
$wikitextDiffBody = $wikitextBody;
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.hook( 'wikipage.diff.diffTypeSwitch' ).add( function ( inlineToggleSwitch ) {
|
|
|
|
diffTypeSwitch = inlineToggleSwitch;
|
|
|
|
diffTypeSwitch.setDisabled( mode === 'visual' );
|
|
|
|
} );
|
2017-06-09 13:45:59 +00:00
|
|
|
}() );
|