User:Dixtosa/shortcuts.js

From Wiktionary, the free dictionary
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 Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

Ctrl-X and Ctrl-S on WikiMedia!


// Descriptions   : A geek's shortcuts Ctrl+S saves and Ctrl+X deletes current line
// [[Category:Wiktionary scripts|s]]

function CtrlX(textarea)
{
	var lines = textarea.val().split("\n");
	var sz = 0;
	var s = document.getElementById("wpTextbox1").selectionStart;
	var e = document.getElementById("wpTextbox1").selectionEnd;

	if (e != s) return false;

	var newval = "";
	var newpos = -1;
	for (var i = 0; i < lines.length; i++)
	{
		if (sz <= s && s <= sz + lines[i].length)
			newpos = sz;
		else
			newval += lines[i] + "\n";
		sz += lines[i].length + 1;
	}
	textarea.val(newval);
	document.getElementById("wpTextbox1").selectionStart = newpos;
	document.getElementById("wpTextbox1").selectionEnd = newpos;
	textarea.focus();
	return true;
}

function CtrlS()
{
	$("#wpSave").click();
	return true;
}

if (mw.config.values.wgAction == "edit")
{
	$("#wpTextbox1").keydown( function(event){
		var performed = 0;
		if (event.ctrlKey && event.which == 88) performed |= CtrlX($(this));
		if (event.ctrlKey && event.which == 83) performed |= CtrlS();
		if (performed === 1) {
			event.preventDefault();
			return false;
		}
		return true;
	});
}