| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 56 |
|
| 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 |
|
| 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 |
|
| 76 |
|
| 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 |
|
| 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 |
|
| 115 |
|
| 116 |
return; |
| 117 |
} |
| 118 |
if ( !Object.prototype.hasOwnProperty.call( conf.contentModels, mw.config.get( 'wgPageContentModel' ) ) ) { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
2) |
| 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 |
|
| 133 |
|
| 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 |
|
| 141 |
|
| 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 |
}() ); |