Jump to content

User:Gryllida/js/addInstantSaveToCodeEditor.js

From Wikipedia, the free encyclopedia
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.
/*
Author : Svetlana Tkachenko svetlana@members.fsf.org
This file is a part of addInstantSaveToCodeEditor.
Licence: GPLv3+
Version: 0.1
Release date: 2018-02-26
Description: adds an instant save button to CodeEditor
*/ 

// Check that CodeEditor is loaded
mw.loader.using(['mediawiki.api', 'oojs-ui'], function () {
	if('.wikiEditor-ui'){
		var button = new OO.ui.ButtonWidget( { 
		  label: 'Instant Save' 
		} );
		// Instant save on click
		button.$element.click(function(){
			// Update button text
			button.setLabel( 'Saving...');
			// Get text area contents
			var textbox = $('#wpTextbox1');
			var context = textbox && textbox.data('wikiEditor-context');
			var currentText = context.$textarea.textSelection( 'getContents' );
			// Save the page via AJAX edit api
			var api = new mw.Api();
			api.postWithToken("edit", {
				action: 'edit',
				title: mw.config.get ('wgPageName'),
				text: currentText,
				summary: $('#wpSummary').val() + ' ([[User:Gryllida/js/addInstantSaveToCodeEditor.js|assisted]])'
			}).done(function (data){
				// Success; Update button text
				button.setLabel( 'Instant Save');
			});
		});
		$('#wpDiffWidget').after(button.$element);
		
		
	}
});

//	text: $('#wpTextbox1').text()