Thành viên:Mifield/common/CodeEditor/main.js

Chú ý: Sau khi lưu thay đổi trang, bạn phải xóa bộ nhớ đệm của trình duyệt để nhìn thấy các thay đổi. Google Chrome, Firefox, Internet ExplorerSafari: Giữ phím ⇧ Shift và nhấn nút Reload/Tải lại trên thanh công cụ của trình duyệt. Để biết chi tiết và hướng dẫn cho các trình duyệt khác, xem Trợ giúp:Xóa bộ nhớ đệm.

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

( function () {
	
const _a_ = performance.now();

for ( var i = 0; i <= 1e8; i++ ) Math.random();

const _b_ = performance.now();

const negligible = ( _b_ - _a_ ) * 1.25; // milliseconds

const viewport = window.visualViewport;

const searchInput = document.getElementById( "searchInput" );

function _show ( string ) {
	searchInput.value = /* Date.now() + ": " + */ String( string ) + " // " + searchInput.value;
}

_show( negligible );

function getConfig ( ) {
	const username = mw.config.get( "wgUserName" );
	const userpage = "User:" + username;

	const titles = [
		[ "config", "CodeEditor"  ],
		[ "config", "codeEditor"  ],
		[ "config", "code-editor" ],
		[ "config", "codeeditor"  ],
		[ "config", "Codeeditor"  ],
		[ "config", "CODEEDITOR"  ],
	].map( function ( path ) {
		return path.toSpliced(
			0, 0, userpage
		).join( "/" ) + ".js";
	} );
	_show( titles );
}

function enhanceCodeEditor ( context ) {
	
	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;

		const config = getConfig();

		editor.setOptions( {

			/* editor options */

//?			selectionStyle:              "line", "text",
//			highlightActiveLine:         true,                 // default: true
//			highlightSelectedWord:       true,                 // default: true
//			readOnly:                    false,                // default: false
			// cursorStyle options:      "ace", "slim", "smooth", "wide"
			cursorStyle:                 [ "smooth", "slim" ], // default: "ace"
			// mergeUndoDeltas options:  true, false, "always"
//			mergeUndoDeltas:             true,
//			behavioursEnabled:           true,
//			wrapBehavioursEnabled:       true,                 // controlled by toolbar toggle
			// this is needed if editor is inside scrollable page
			autoScrollEditorIntoView:    true,                 // default: false
			// copy/cut the full line if selection is empty
			copyWithEmptySelection:      true,                 // default: false 
//			useSoftTabs:                 false,                // default: false
			navigateWithinSoftTabs:      false,                // default: false
//			enableMultiselect:           true,                 // default: true
//			enableAutoIndent:            true,
//			enableKeyboardAccessibility: true,


			/* extension-defined editor options */

			// the following options require ext-language_tools.js
//			enableBasicAutocompletion:   true,                 // wiki default: true
			enableLiveAutocompletion:    true,                 // wiki default: false
//			enableSnippets:              true,                 // wiki default: true
			// the following option requires ext-emmet.js and the emmet library 
//			enableEmmet:                 true,
			// the following option requires ext-elastic_tabstops_lite.js
//			useElasticTabstops:          true,


			/* renderer options */

//			hScrollBarAlwaysVisible:     false,                // default: false
			vScrollBarAlwaysVisible:     true,                 // default: false
//?			highlightGutterLine:         true,
			animatedScroll:              true,                 // default: false
//			showInvisibles:              true,                 // controlled by toolbar toggle
//			showPrintMargin:             true,
//			printMarginColumn:           80,                   // default: 80
			// printMargin: shortcut for showPrintMargin and printMarginColumn
			// printMargin options:      false, <number>
//			printMargin:                 80,
			fadeFoldWidgets:             true,                 // default: false
//			showFoldWidgets:             true,                 // default: true
//			showLineNumbers:             true,                 // default: true
//			showGutter:                  true,                 // default: true
//			displayIndentGuides:         true,                 // default: true
//			highlightIndentGuides:       true,
			// fontSize options:         <number>, "<font-size>"
			fontSize:                    14,                      // default: 12(px)
			// fontFamily options:       "<font-family>"
//			fontFamily:                  "",
			// resize editor based on the contents of the editor
			// until the number of lines reaches maxLines
			// maxLines, minLines:       <number>
			maxLines:                    20,
//			minLines:                    0,
			// number of page sizes to scroll after document end
			// scrollPastEnd options:    <number>, true, false
			// (typical values are 0, 0.5, and 1)
			scrollPastEnd:               0.5,                     // default: 0
			fixedWidthGutter:            true,                    // default: false
			theme:                       "ace/theme/dawn",        // default: "ace/theme/textmate"
//			customScrollbar:             true,                    // default: false
			hasCssTransforms:            true,                    // default: false?
			// maxPixelHeight:           <number>
			maxPixelHeight:              viewport.height,
			useSvgGutterIcons:           true,                    // default: ?


			/* mouseHandler options */

			// scrollSpeed:              <number>
//			scrollSpeed:                 0,
			// dragDelay:                <number>
//			dragDelay:                   0,
//			dragEnabled:                 true,                    // default: true
			// focusTimout:              <number>
//			focusTimout:                 0,
//			tooltipFollowsMouse:         true,


			/* sesion options */

//			firstLineNumber:             1,                       // default: 1
//			overwrite:                   false,                   // default: false
			// newLineMode options:      "auto", "unix", "windows"
//			newLineMode:                 "unix",
//			useWorker:                   true,
//			useSoftTabs:                 false,
//			indentedSoftWrap:            true,
//			navigateWithinSoftTabs:      false,
			// tabSize:                  <number>
//			tabSize:                     4,
			// wrap options:             "off", "free", "printmargin", <boolean>, <number>
//			wrap:                        "printmargin",
			// wrapMethod options:       "code", "text", "auto"
//			wrapMethod:                  "auto",
			// foldStyle options:        "markbegin", "markbeginend", "manual"
			foldStyle:                   "markbeginend",           // default: markbegin?
			// mode:                     path to a mode, such as "ace/mode/text"
//			mod.	e:                        "ace/mode/text",

		} );

		setTimeout( resizeTextArea, negligible );

	}
}

var check = 3;
// presume no editor exists if this many checks have been performed
var interval;

function checkCodeEditorExistence ( _ ) {
	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" ) && // <context>
			_.wikiEditor.instances[ 0 ].data( "wikiEditor-context" ).codeEditor &&
			_.wikiEditor.instances[ 0 ].data( "wikiEditor-context" ).codeEditor.setOptions
		) {
			enhanceCodeEditor( _.wikiEditor.instances[ 0 ].data( "wikiEditor-context" ) );
			check = false;
		}
	} else clearInterval( interval );
}

function startCheckingCodeEditorExistence () {
	interval = setInterval( checkCodeEditorExistence, negligible, $ );
}

$.when( [
	mw.loader.using( [
		"ext.codeEditor",
		"jquery.codeEditor",
		"mediawiki.page.ready",
		"mediawiki.Title",
	] ),
	$( document ).ready(),
] ).then( startCheckingCodeEditorExistence );

} )();