Editing.js – MediaWiki

From Bohemia Interactive Community
Jump to navigation Jump to search
m (same)
m (Some JS formatting)
Line 4: Line 4:
  *****************************************************************************************/
  *****************************************************************************************/


(function() { // BEGIN WRAPPER
(function() { // BEGIN WRAPPER


/*****************************************************************************************
/*****************************************************************************************
* Variables required by all functions below
* Variables required by all functions below
*/
*/
 
var editingTextbox = document.getElementById("wpTextbox1");
var editingTextbox = document.getElementById("wpTextbox1");
 
/*****************************************************************************************/
/*****************************************************************************************
/*****************************************************************************************
* Add checkbox toggle of allowance to use TAB key in editing textarea to insert TAB chars
* Add checkbox toggle of allowance to use TAB key in editing textarea to insert TAB chars
* Maintainer: [[User:Fred Gandt]]
* Maintainer: [[User:Fred Gandt]]
*/
*/
function allowTabs() {
function allowTabs() {
var ecb = document.getElementsByClassName("editCheckboxes")[0],
var ecb = document.getElementsByClassName("editCheckboxes")[0],
cb = document.createElement("input"),
cb = document.createElement("input"),
cb_lbl = document.createElement("label"),
cb_lbl = document.createElement("label"),
tabsAllowed = function(evt) {
tabsAllowed = function(evt) {
if (evt.keyCode == 9) {
if (evt.keyCode == 9) {
evt.preventDefault();
evt.preventDefault();
var ss = editingTextbox.selectionStart;
var ss = editingTextbox.selectionStart;
editingTextbox.value = editingTextbox.value.substr(0, ss) + String.fromCharCode(evt.keyCode) + editingTextbox.value.substr(editingTextbox.selectionEnd);
editingTextbox.value = editingTextbox.value.substr(0, ss) + String.fromCharCode(evt.keyCode) + editingTextbox.value.substr(editingTextbox.selectionEnd);
editingTextbox.setSelectionRange(ss + 1, ss + 1);
editingTextbox.setSelectionRange(ss + 1, ss + 1);
}
}
}
initTabsAllowed = function() {
}
editingTextbox.addEventListener("keydown", tabsAllowed, false);
 
editingTextbox.form.action += "&allowTabs";
initTabsAllowed = function() {
};
editingTextbox.addEventListener("keydown", tabsAllowed, false);
cb.setAttribute("id", "allowTabs");
editingTextbox.form.action += "&allowTabs";
cb.setAttribute("type", "checkbox");
};
cb.setAttribute("style", "margin-left: 3em;");
cb.setAttribute("id", "allowTabs");
cb_lbl.setAttribute("for", "allowTabs");
cb.setAttribute("type", "checkbox");
cb_lbl.setAttribute("title", "Allows insertion of Tab characters when and where the Tab key is pressed");
cb.setAttribute("style", "margin-left: 3em");
cb_lbl.innerHTML = " Allow tabulations usage";
cb_lbl.setAttribute("for", "allowTabs");
cb.addEventListener("change", function() {
cb_lbl.setAttribute("title", "Allows insertion of Tab characters when and where the Tab key is pressed");
if (this.checked) {
cb_lbl.innerHTML = " Allow tabulations usage";
initTabsAllowed();
cb.addEventListener("change", function() {
} else {
if (this.checked) {
editingTextbox.removeEventListener("keydown", tabsAllowed, false);
editingTextbox.form.action = editingTextbox.form.action.replace("&allowTabs", "");
}
}, false);
if (~window.location.search.indexOf("&allowTabs")) {
cb.checked = true;
initTabsAllowed();
initTabsAllowed();
} else {
editingTextbox.removeEventListener("keydown", tabsAllowed, false);
editingTextbox.form.action = editingTextbox.form.action.replace("&allowTabs", "");
}
}
ecb.appendChild(cb);
}, false);
ecb.appendChild(cb_lbl);
 
if (~window.location.search.indexOf("&allowTabs")) {
cb.checked = true;
initTabsAllowed();
}
}
 
/*****************************************************************************************/
ecb.appendChild(cb);
ecb.appendChild(cb_lbl);
}
 
/*****************************************************************************************
/*****************************************************************************************
* Prevents page body scrolling when scrolling of the editing textarea reaches the top or bottom.
* Prevents page body scrolling when scrolling of the editing textarea reaches the top or bottom.
* Maintainer: [[User:Fred Gandt]]
* Maintainer: [[User:Fred Gandt]]
*/
*/
function holdStill() {
function holdStill() {
editingTextbox.addEventListener("mousewheel", function(evt) {
editingTextbox.addEventListener("mousewheel", function(evt) {
evt.preventDefault();
evt.preventDefault();
editingTextbox.scrollTop -= evt.wheelDelta / 3;
editingTextbox.scrollTop -= evt.wheelDelta / 3;
}, false);
}, false);
}
}
 
/*****************************************************************************************/
/*****************************************************************************************
/*****************************************************************************************
* Add a checkbox to preview darkmode (?useskin=darkvector)
* Add a checkbox to preview darkmode (?useskin=darkvector)
* Maintainer: [[User:Lou Montana]]
* Maintainer: [[User:Lou Montana]]
*/
*/
function darkModePreview() {
function darkModePreview() {
var ecb = document.getElementsByClassName("editCheckboxes")[0],
var ecb = document.getElementsByClassName("editCheckboxes")[0],
cb = document.createElement("input"),
cb = document.createElement("input"),
Line 88: Line 81:
cb.setAttribute("id", "previewDarkMode");
cb.setAttribute("id", "previewDarkMode");
cb.setAttribute("type", "checkbox");
cb.setAttribute("type", "checkbox");
cb.setAttribute("style", "margin-left: 3em;");
cb.setAttribute("style", "margin-left: 3em");
label.setAttribute("for", "previewDarkMode");
label.setAttribute("for", "previewDarkMode");
label.setAttribute("title", "Preview Dark Mode render (next preview)");
label.setAttribute("title", "Preview Dark Mode render (next preview)");
Line 109: Line 102:
ecb.appendChild(label);
ecb.appendChild(label);
}
}
 
/*****************************************************************************************/
/*****************************************************************************************
* Next ***
*/
// code code code
/*****************************************************************************************/
/*****************************************************************************************
/*****************************************************************************************
* Call the required functions from those above
* Call the required functions from those above
*/
*/
 
allowTabs();
allowTabs();
darkModePreview();
darkModePreview();
 
/*****************************************************************************************/
}()); // END WRAPPER
}()); // END WRAPPER

Revision as of 00:46, 10 June 2024

/*****************************************************************************************
 * JavaScript here will be loaded by MediaWiki:Common.js when "editing mode" is detected.
 *
 *****************************************************************************************/

(function() { // BEGIN WRAPPER

	/*****************************************************************************************
	 * Variables required by all functions below
	 */

	var editingTextbox = document.getElementById("wpTextbox1");

	/*****************************************************************************************
	 * Add checkbox toggle of allowance to use TAB key in editing textarea to insert TAB chars
	 * Maintainer: [[User:Fred Gandt]]
	 */
	function allowTabs() {
		var ecb = document.getElementsByClassName("editCheckboxes")[0],
			cb = document.createElement("input"),
			cb_lbl = document.createElement("label"),
			tabsAllowed = function(evt) {
				if (evt.keyCode == 9) {
					evt.preventDefault();
					var ss = editingTextbox.selectionStart;
					editingTextbox.value = editingTextbox.value.substr(0, ss) + String.fromCharCode(evt.keyCode) + editingTextbox.value.substr(editingTextbox.selectionEnd);
					editingTextbox.setSelectionRange(ss + 1, ss + 1);
				}
			}

		initTabsAllowed = function() {
			editingTextbox.addEventListener("keydown", tabsAllowed, false);
			editingTextbox.form.action += "&allowTabs";
		};
		cb.setAttribute("id", "allowTabs");
		cb.setAttribute("type", "checkbox");
		cb.setAttribute("style", "margin-left: 3em");
		cb_lbl.setAttribute("for", "allowTabs");
		cb_lbl.setAttribute("title", "Allows insertion of Tab characters when and where the Tab key is pressed");
		cb_lbl.innerHTML = " Allow tabulations usage";
		cb.addEventListener("change", function() {
			if (this.checked) {
				initTabsAllowed();
			} else {
				editingTextbox.removeEventListener("keydown", tabsAllowed, false);
				editingTextbox.form.action = editingTextbox.form.action.replace("&allowTabs", "");
			}
		}, false);

		if (~window.location.search.indexOf("&allowTabs")) {
			cb.checked = true;
			initTabsAllowed();
		}

		ecb.appendChild(cb);
		ecb.appendChild(cb_lbl);
	}

	/*****************************************************************************************
	 * Prevents page body scrolling when scrolling of the editing textarea reaches the top or bottom.
	 * Maintainer: [[User:Fred Gandt]]
	 */
	function holdStill() {
		editingTextbox.addEventListener("mousewheel", function(evt) {
			evt.preventDefault();
			editingTextbox.scrollTop -= evt.wheelDelta / 3;
		}, false);
	}

	/*****************************************************************************************
	 * Add a checkbox to preview darkmode (?useskin=darkvector)
	 * Maintainer: [[User:Lou Montana]]
	 */
	function darkModePreview() {
		var ecb = document.getElementsByClassName("editCheckboxes")[0],
			cb = document.createElement("input"),
			label = document.createElement("label"),
			actionDarkAdd = "&useskin=darkvector";
			actionLightAdd = "&useskin=vector";

		cb.setAttribute("id", "previewDarkMode");
		cb.setAttribute("type", "checkbox");
		cb.setAttribute("style", "margin-left: 3em");
		label.setAttribute("for", "previewDarkMode");
		label.setAttribute("title", "Preview Dark Mode render (next preview)");
		label.innerHTML = " Preview Dark Mode";
		cb.addEventListener("change", function() {
			if (this.checked) {
				editingTextbox.form.action = editingTextbox.form.action.replace(actionLightAdd, "")
				editingTextbox.form.action += actionDarkAdd;
			} else {
				editingTextbox.form.action = editingTextbox.form.action.replace(actionDarkAdd, "");
				editingTextbox.form.action += actionLightAdd;
			}
		}, false);

		cb.checked = document.getElementsByTagName("body")[0].classList.contains("skin-darkvector");
		let event = new Event("change");
		cb.dispatchEvent(event);

		ecb.appendChild(cb);
		ecb.appendChild(label);
	}

	/*****************************************************************************************
	 * Call the required functions from those above
	 */

	allowTabs();
	darkModePreview();

}()); // END WRAPPER