Jump to content

User:Danski454/codeEditWindowSize.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Danski454 (talk | contribs) at 21:15, 22 August 2019 (test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
window.codeEditorHeight = window.codeEditorHeight || 700;
$(function(){
	var attempts = 0;
	function resize(){
		console.log("resize");
		if ($(".wikiEditor-ui-text div").first().is(".ui-resizable")) {//do we have a code editor?
			$(".wikiEditor-ui-text div").first().css("height", 
				window.codeEditorHeight.toString() + "px");
			//resizing the edit window causes part of the edit window to appear empty
			//calling the resize event fixes this.
			window.dispatchEvent(new Event("resize"));
		}
	}
	function initialLoop(){//we may have to wait for code editor to load
		console.log("initialLoop");
		if ($(".wikiEditor-ui-text div").first().is(".ui-resizable")) {
			resize();
		} else if (attempts < 100) {//try again later
			attempts++;
			window.setTimeout(initialLoop, 50);
		}//after 100 attempts (min 5 seconds), give up
	}
	
	if ((mw.config.get("wgAction") === "edit" || 
		mw.config.get("wgAction") === "submit") //are we in edit mode
		&& ["javascript", "css", "Scribunto"].indexOf(mw.config.get("wgPageContentModel")) !== -1) {//and on a code page?
		$(".tool[rel='codeEditor'] a").first().click(resize);
		initialLoop();
	}
});