Jump to content

User:John Vandenberg/switch editor.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by John Vandenberg (talk | contribs) at 02:12, 20 July 2013 (simplify & perf). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// This script adds a 'source' checkbox which, when pressed, causes the 'diff' button to load the Source Editor, with a diff.
 
// It could break with changes to VE. 
// Use at your own risk, and pester the VE team to add this feature.

mw.hook( 've.saveDialog.stateChanged' ).add( function() {
	if ($('#ve-init-mw-viewPageTarget-saveDialog-source').length==0) {
		$("label.ve-init-mw-viewPageTarget-saveDialog-watchList-label").after('<input type="checkbox" name="sourceDiff" id="ve-init-mw-viewPageTarget-saveDialog-source"><label class="ve-init-mw-viewPageTarget-saveDialog-watchList-source">Source</label>');

		ve.init.mw.targets[0].onSaveDialogReviewButtonClick = function () {
			var source = $( '#ve-init-mw-viewPageTarget-saveDialog-source' ).prop( 'checked' );

			if (!source)
				this.swapSaveDialog( 'review' );
			else {
				var doc = this.surface.getModel().getDocument();
				doc = ve.dm.converter.getDomFromData( doc.getFullData(), doc.getStore(), doc.getInternalList() );

				var saveOptions = this.getSaveOptions();
				minorChecked = '';
				watchChecked = '';
				 if ( saveOptions.minor ) {
					minorChecked = 'checked="checked" ';
				}
				if ( saveOptions.watch ) {
					watchChecked = 'checked="checked" ';
				}

				$("body").append('<form id="editform" name="editform" method="post" action="/w/index.php?title='+wgPageName+'&amp;action=submit" enctype="multipart/form-data"><textarea name=wpTextbox1 id=wpTextbox1></textarea><input id="wpDiff" name="wpDiff" type="submit" tabindex="7" value="Show changes"/><input name="wpSummary" value="'+mw.html.escape(saveOptions.summary)+'"/><input name="wpMinoredit" value="1" '+ minorChecked + ' type="checkbox"/><input name="wpWatchthis" value="1" ' + watchChecked + ' type="checkbox"/><input name="wpSection"/><input name="wpAutoSummary"/><input name="wpStarttime"/><input name="wpEdittime"/><input name="oldid" value="0"/><input name="model" value="wikitext"/><input name="format" value="text/x-wiki"/><input name="wpAntispam" value=""/><input name="baseRevId" value="0"/><input name="altBaseRevId" value="0"/><input name="undidRev" value="0"/><input name="wpScrolltop" value="0"/><input name="undidRev" value="0"/><input type="hidden" value="' + ve.init.mw.targets[0].editToken + '" name="wpEditToken" /></form>' );

				this.serializing = false;

				this.serialize( doc, 
					function( content ) {
						window.onbeforeunload = function (){};
						$("textarea#wpTextbox1").val(content).parent().children("#wpDiff").click(); 
					}
				);
			};
		};

		ve.init.mw.targets[0].saveDialogReviewButton.bindings.click[0].callback = ve.init.mw.targets[0].onSaveDialogReviewButtonClick;
	}
});