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' ] } ),
|
2021-08-14 10:24:39 +00:00
|
|
|
originalUri = new mw.Uri(),
|
2022-01-06 16:47:35 +00:00
|
|
|
initMode = originalUri.query.diffmode || mw.user.options.get( 'visualeditor-diffmode-historical' ) || 'source',
|
2017-06-09 13:45:59 +00:00
|
|
|
conf = mw.config.get( 'wgVisualEditorConfig' ),
|
|
|
|
pluginModules = conf.pluginModules.filter( mw.loader.getState );
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 ) {
|
2022-01-06 16:47:35 +00:00
|
|
|
var uri = new mw.Uri();
|
2017-06-09 13:45:59 +00:00
|
|
|
|
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 {
|
|
|
|
oldPageName = uri.query.page1;
|
|
|
|
newPageName = uri.query.page2;
|
|
|
|
}
|
|
|
|
|
2022-01-06 16:47:35 +00:00
|
|
|
var mode = item.getData();
|
2021-05-05 17:06:37 +00:00
|
|
|
var isVisual = mode === 'visual';
|
2018-03-04 14:45:33 +00:00
|
|
|
|
|
|
|
$visualDiffContainer.toggleClass( 'oo-ui-element-hidden', !isVisual );
|
2018-04-30 17:21:28 +00:00
|
|
|
$wikitextDiffBody.toggleClass( 'oo-ui-element-hidden', isVisual );
|
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
|
2021-05-05 17:06:37 +00:00
|
|
|
var modulePromise = mw.loader.using( [ 'ext.visualEditor.articleTarget' ].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' );
|
2017-06-09 13:45:59 +00:00
|
|
|
} );
|
|
|
|
}
|
2017-08-10 15:24:54 +00:00
|
|
|
|
|
|
|
if ( history.replaceState ) {
|
|
|
|
uri.query.diffmode = mode;
|
2021-08-14 10:24:39 +00:00
|
|
|
history.replaceState( history.state, document.title, uri );
|
2017-08-10 15:24:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 13:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 16:48:02 +00:00
|
|
|
function onReviewModeButtonSelectChoose( item ) {
|
|
|
|
var mode = item.getData();
|
|
|
|
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 () {
|
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
|
|
|
} );
|
|
|
|
}() );
|