User:Mifield/common/CodeEditor/main.js

/* globals $, ace, mw, importStylesheet */ "use strict";

( function {

/* basic constants for deciding whether const wgPageName        = mw.config.get( "wgPageName" ); const wgPageContentModel = mw.config.get( "wgPageContentModel" ); // alert(wgPageContentModel) const isJsPage      = wgPageName.endsWith( ".js" ) || wgPageContentModel === "javascript"; const isCssPage     = wgPageName.endsWith( ".css" ) || wgPageContentModel === "css" || wgPageContentModel === "sanitized-css";
 * the bulk of this script should be run */

/* if there's nothing to do, if ( !isJsPage && !isCssPage ) return;
 * don't waste compute resources */

importStylesheet( "User:Mifield/common/CodeEditor/style.css" );

/* test how fast the device is. algorithm can be improved, const _a_ = performance.now; for ( var i = 0; i <= 1e8; i++ ) Math.random; const _b_ = performance.now; const negligible = ( _b_ - _a_ ) * 1.25; // milliseconds
 * since currently the test doesn't involve the DOM */

/* debug utility; better than using alert */ const searchInput = document.getElementById( "searchInput" ); function _show ( string ) { searchInput.value = /* Date.now + ": " + */ String( string ) + " // " + searchInput.value; } _show( negligible );

/* get config from $.codeEditor.config, if it exists, function getConfig ( _ ) { if ( !_ ) throw new Error( "getConfig was given a bad parameter" ); if ( !_.codeEditor ) _.codeEditor = {}; if ( !_.codeEditor.config ) _.codeEditor.config = {}; return _.codeEditor.config; }
 * and create the object structure if it doesn't */

/* this section handles the removal of notices, which function removeElementAndEmptyParent ( element ) { // as long as $( element ) works, this works fine const $element = $( element ); const $parent = $element.parent; const parent  = $parent[ 0 ]; $element.remove; if ( parent && parent.children && parent.children.length === 0 ) $parent.remove; }
 * doesn't depend on .editor having been initialized */

function removeExtras ( config ) { _show ( "removeExtras called" ); const hideWarnings  = false || ( config.hideScriptWarnings && isJsPage ) || ( config.hideStylesheetWarnings && isCssPage );

/* should apply to a page whether in edit mode or not */ if ( isJsPage &&		( config.hideScriptWarnings || config.hideAllExtras )	) { $( ".mw-userconfigpublic" ).parent.remove; $( ".mw-userconfigdangerous" ).parent.remove; }

if ( isCssPage &&		( config.hideStylesheetWarnings || config.hideAllExtras )	) { $( ".mw-userconfigpublic" ).parent.remove; // $( ".mw-userconfigdangerous" ).parent.remove; }

if ( hideWarnings || config.hideAllExtras ||		config.hideConfidentialityWarning	) { removeElementAndEmptyParent( ".mw-userconfigpublic" ); }

if ( hideWarnings || config.hideAllExtras ||		config.hideMaliciousCodeWarning	) { removeElementAndEmptyParent( ".mw-userconfigdangerous" ); removeElementAndEmptyParent( "#jswarning" ); }

if ( config.hidePurgeCacheTip || config.hideAllExtras ) { removeElementAndEmptyParent( "#clearprefcache" ); }

if ( config.hideScriptDocumentationNotice || config.hideAllExtras ) { removeElementAndEmptyParent( "#mw-script-doc" ); } }

function removeExtrasDispatcher ( config ) { setTimeout( removeExtras, negligible, config ); }

function removeExtrasOnEditPage ( config ) { _show( "removeExtrasOnEditPage called" ); /* should only apply if page is in edit mode */ if ( config.hidePreviewTip || config.hideAllExtras ) { $( "#mw-userjsyoucanpreview" ).remove; }

if ( config.hideCopyrightWarning || config.hideAllExtras ) { $( ".editpage-head-copywarn" ).remove; }

if ( config.hideSpecialChars || config.hideAllExtras ) { $( "#editpage-specialchars" ).remove; } }

function removeExtrasOnEditPageDispatcher ( config ) { setTimeout( removeExtrasOnEditPage, negligible, config ); }

/* this section handles the changes involving const viewport = window.visualViewport;
 * .editor and its associated elements & objects */

function enhanceCodeEditor ( context, config ) { const editor                  = context.codeEditor; const editorElement           = document.getElementsByClassName( "editor" )[ 0 ]; // const editorContainerElement  = editor.container; const $editor                 = $( editorElement ); // const $editorContainer        = $( editorContainerElement ); const $textarea               = context.$textarea;

function resizeTextArea { const height = $editor.height; // _show(height - viewport.height) // $editorContainer.height( height ); $textarea.height( height ); $textarea.parent.height( height ); $textarea.parent.parent.height( height ); }

function onViewportResize ( event ) { editor.setOptions( {			maxPixelHeight: event.target.height,		} );

setTimeout( resizeTextArea, negligible ); // _show(editor.getOption( "minLines" )) // setTimeout( resizeTextArea, negligible ); }

if ( editor ) { viewport.onresize   = onViewportResize;

editor.setOptions( config ); setTimeout( resizeTextArea, negligible );

removeExtrasOnEditPageDispatcher( config ); } }

/* this section does the work of preparing for .editor var check = 3; // presume no editor exists if this many checks have been performed var interval;
 * enhancements by checking if things are ready */

function checkCodeEditorExistence ( _, config ) { if ( check ) { check--; if( _ && _.wikiEditor && _.wikiEditor.instances &&			_.wikiEditor.instances.length && // optional chaining would simplify this!			_.wikiEditor.instances[ 0 ] && // jQuery object			_.wikiEditor.instances[ 0 ][ 0 ] && // HTMLInputElement			_.wikiEditor.instances[ 0 ].data &&			_.wikiEditor.instances[ 0 ].data( "wikiEditor-context" ) && // 			_.wikiEditor.instances[ 0 ].data( "wikiEditor-context" ).codeEditor &&			_.wikiEditor.instances[ 0 ].data( "wikiEditor-context" ).codeEditor.setOptions		) { enhanceCodeEditor(				_.wikiEditor.instances[ 0 ].data( "wikiEditor-context" ),				config			); check = false; }	} else clearInterval( interval ); }

function startCheckingCodeEditorExistence ( config ) { interval = setInterval(		checkCodeEditorExistence,		negligible, // delay		$, config // parameters passed to checkCodeEditorExistence	); }

function onReadyDispatcher { const config = getConfig( $ ); removeExtrasDispatcher( config ); startCheckingCodeEditorExistence( config ); }

/* run things only when ready */ $.when( [	mw.loader.using( [ "ext.codeEditor", "jquery.codeEditor", "mediawiki.page.ready", "mediawiki.Title", // "ext.gadget.charinsert-core" ] ),	$( document ).ready, ] ).then( onReadyDispatcher );

} );