User:Thundercraft5/global.js/CodeMirror.js

/* jshint esversion: 6, forin: true, immed: true, indent: 4, latedef: true, newcap: true, noarg: true, undef: true, undef: true, unused: true, browser: true, jquery: true, onevar: true, eqeqeq: true, multistr: true, maxerr: 999999, -W082, -W084

function main { // CodeMirror Customization $.extend(CodeMirror.prototype, {		setOptions(options) {			Object.entries(options).forEach(([k, v]) => { this.setOption(k, v); });			return this;		},	}); WikiEditorCodeMirror.setOptions({		extraKeys: {			End: "goLineRight",			Home: "goLineLeft",			"Shift-Tab": cm => {				cm.execCommand("indentLess");				cm.execCommand("indentLess");			},			"Tab": cm => {				cm.execCommand("indent");				cm.execCommand("indent");			},		},		indentWithTabs: true,		autofocus: true,		showCursorWhenSelecting: true,		lineNumbers: true,	}); // Bracket matching Set ( => {		const DEFAULT_BRACKETS = "[]{}''\"\"\`\`";		CodeMirror.defineOption("autoCloseBrackets", false, (cm, val, old) => { var wasOn = old && old !== CodeMirror.Init; if (val && !wasOn) cm.addKeyMap(buildKeymap(typeof val === "string" ? val : DEFAULT_BRACKETS)); else if (!val && wasOn) cm.removeKeyMap("autoCloseBrackets"); });		const getChars = (line, ch) => {			if (!line) line = this.getCursor.line;			var docLine = this.doc.getLine(line);			if (!ch) return docLine;			else {				var pos = Math.max(ch, 0);				return docLine.substr(pos, 1);			}		};		function buildKeymap(pairs) {			var map = { name: "autoCloseBrackets" };			function loopCallback(left, right) {				function maybeOverwrite(cm) {					var cur = cm.getCursor, ahead = cm.getRange(cur, CodeMirror.Pos(cur.line, cur.ch + 1));					if (ahead !== right || right === "'") return CodeMirror.Pass;					else cm.execCommand("goCharRight");				}				map["'" + left + "'"] = cm => {					if (left === right && maybeOverwrite(cm) !== CodeMirror.Pass) return;					var cur = cm.getCursor, ahead = CodeMirror.Pos(cur.line, cur.ch + 1);					cm.replaceSelection( left !== '(' && (getChars(cur.line, cur.ch).match(/^[a-z]$/i) || left === "'" && getChars(cur.line, cur.ch-1).match(/^[a-z]$/i))							? left							: left + right, 						{head: ahead, anchor: ahead}					); cm.execCommand("goCharRight"); };				if (left !== right) map["'" + right + "'"] = maybeOverwrite; }			for (var i = 0; i < pairs.length; i += 2) loopCallback(pairs.charAt(i), pairs.charAt(i + 1)); return map; }		const FRONT_BRACKETS = "({[\"\`\'\`"; // jshint ignore:line		const REAR_BRACKETS = "]})\"\`\'\`"; // jshint ignore:line var doneChars = {}; var nextChar; var len; var pos; this.setOptions({ autoCloseBrackets: true }); this.on('beforeChange', cm => {			var line = cm.doc.getLine(cm.getCursor.line);			pos = Math.max(cm.getCursor.ch + 1, 0);			nextChar = line.substr(pos, 1) || null;			len = line.length;		}); this.on('change', (cm, changes) => {			var char = changes.removed[0];			if (changes.origin === "+delete" && FRONT_BRACKETS.includes(char) && changes.removed.length === 1 && changes.text[0] === "" && changes.text.length === 1 && !doneChars[char] ) {				if ((nextChar && REAR_BRACKETS.includes(nextChar)) || pos === len) {					doneChars[char] = true;					this.execCommand("goCharRight");					this.execCommand("delCharBefore");				}			} else {				doneChars = {};			}		}); }); }

/* global mw, CodeMirror, WikiEditorCodeMirror */ mw.loader.using(['ext.fandom.wikiEditor.js']).then( => {	let inter = setInterval( => { if (!window.WikiEditorCodeMirror) return; else clearInterval(inter), main.call(WikiEditorCodeMirror); }, 5); }).catch(console.warn);