User:Nardog/SymmetricalChanges.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(function symmetricalChanges() {
	if (!document.getElementById('p-tb')) return;
	let callback = $diff => {
		let $rows = $diff
			.find('.diff-deletedline:not(.diff-empty) ~ .diff-addedline:not(.diff-empty)')
			.parent();
		if (!$rows.length) return;
		$rows.addClass('symmetricalchange');
		$rows.prev().has('.diff-context').addClass('symmetricalchange-context')
			.prev().has('.diff-context').addClass('symmetricalchange-context');
		$rows.next().has('.diff-context').addClass('symmetricalchange-context')
			.next().has('.diff-context').addClass('symmetricalchange-context');
		if (document.getElementById('t-symmetricalchanges')) return;
		mw.loader.using('mediawiki.util', () => {
			mw.util.addPortletLink('p-tb', '#', 'Toggle symmetrical changes', 't-symmetricalchanges').firstElementChild.addEventListener('click', e => {
				e.preventDefault();
				document.body.classList.toggle('symmetricalchanges-enabled');
			});
		});
	};
	mw.hook('wikipage.diff').add(callback);
	mw.hook('ve.activationComplete').add(() => {
		let setDiffAndReview = ve.ui.MWSaveDialog.prototype.setDiffAndReview;
		ve.ui.MWSaveDialog.prototype.setDiffAndReview = function (promise) {
			setDiffAndReview.apply(this, arguments);
			promise.then(() => {
				callback(this.$reviewWikitextDiff);
			});
		};
	});
});