User:John Vandenberg/switch editor.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:John Vandenberg/switch editor. |
// 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.
// This hook is fired when the first 'Save' is loaded.
// There are better hooks to load the editor switcher earlier, but
// This is the most reliable hook at present. However this hook is fired
// frequently, so the code needs to be further optimised.
// The save dialog is built from a template, and we dont want to be invoked
// before the save dialog has been properly instantiated.
// TODO: document the other hooks and related problems in more detail, and
// raise bugs if appropriate.
mw.hook( 've.saveDialog.stateChanged' ).add( function() {
// If the editor switch UI hasnt been added, add the UI and associated hooks.
if ($('#ve-init-mw-viewPageTarget-saveDialog-source').length==0) {
// Add a checkbox and label. This UI overlaps the character count, and needs to be revised.
$("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 () {
// Detect if the new checkbox has been ticked.
var source = $( '#ve-init-mw-viewPageTarget-saveDialog-source' ).prop( 'checked' );
if (!source)
this.swapSaveDialog( 'review' );
else {
// Build a 'document' from the VE, ready to be serialised.
var doc = this.surface.getModel().getDocument();
doc = ve.dm.converter.getDomFromData( doc.getFullData(), doc.getStore(), doc.getInternalList() );
// Copy VE save form buttons into SE form
var saveOptions = this.getSaveOptions();
minorChecked = '';
watchChecked = '';
if ( saveOptions.minor ) {
minorChecked = 'checked="checked" ';
}
if ( saveOptions.watch ) {
watchChecked = 'checked="checked" ';
}
// The serialize function checks this variable to avoid multiple ajax requests
this.serializing = false;
this.serialize( doc,
function( content ) {
// Remove form if it already exists
if ($('#editform').length!=0) {
$('#editform').remove();
}
// Inject a source editor form, populated with values.
// It isnt hidden yet, as it shouldn't be too visually annoying
// and keeping it visible may help with debugging if the op fails.
$("body").append('<form id="editform" name="editform" method="post" action="/w/index.php?title='+wgPageName+'&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 disables the "unsaved data" warning
window.onbeforeunload = function (){};
// add wikitext to form and click the diff button
$("textarea#wpTextbox1").val(content).parent().children("#wpDiff").click();
}
);
};
};
// This changes the button click binding.
// It is probably fragile.
ve.init.mw.targets[0].saveDialogReviewButton.bindings.click[0].callback = ve.init.mw.targets[0].onSaveDialogReviewButtonClick;
}
});