User:Gryllida/js/addInstantSaveToCodeEditor-0.1.js

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

/*
Author : Svetlana Tkachenko svetlana@members.fsf.org
This file is a part of addInstantSaveToCodeEditor.
Licence: GPLv3+
Description: adds 'Instant Save' button to CodeEditor and WikiEditor editors. 
Version: 0.1
ChangeLog:
- Version 0.1 (2018-02-24) - initial release

Description:
Adds 'Instant Save' button to CodeEditor and WikiEditor editors.

This button is located at the right of the 'Save Changes', 'Show Preview', 'Show changes' buttons. 
When clicked, says 'Saving...', saves currently typed text using a MediaWiki API query 
without reloading the page, and goes back to 'Instant Save' button text. While saving
the edit summary is modified to append " ([[User:Gryllida/js/addSourcesUi.js|assisted]])" at the end.

*/ 

// 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-0.1.js|assisted]])'
			}).done(function (data){
				// Success; Update button text
				button.setLabel( 'Instant Save');
			});
		});
		$('#wpDiffWidget').after(button.$element);
		
		
	}
});

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