From 406e9f8e9cd751b6dc3737cc3430cd7f09c0bf28 Mon Sep 17 00:00:00 2001 From: Derk-Jan Hartman Date: Tue, 18 Oct 2016 17:38:31 +0200 Subject: [PATCH] Enable conditional loading of ACE language modes require bypasses Ace's internal loadModule() logic which is capable of on demand loading of Ace modules. Because unloaded modules are not defined, they cannot be required, and because we don't use RL to preload all modes (because it's a lot of bytes), currently only very few of the available language modes were currently available. Also validate language mode names passed to Ace. Bug: T148518 Change-Id: I82d278920695be12aa80a79548abf8b8ce5445fd --- modules/ve-mw/ui/widgets/ve.ui.MWAceEditorWidget.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/ve-mw/ui/widgets/ve.ui.MWAceEditorWidget.js b/modules/ve-mw/ui/widgets/ve.ui.MWAceEditorWidget.js index 187c9d29a2..8cf0d44919 100644 --- a/modules/ve-mw/ui/widgets/ve.ui.MWAceEditorWidget.js +++ b/modules/ve-mw/ui/widgets/ve.ui.MWAceEditorWidget.js @@ -70,8 +70,8 @@ OO.inheritClass( ve.ui.MWAceEditorWidget, ve.ui.WhitespacePreservingTextInputWid */ ve.ui.MWAceEditorWidget.prototype.setup = function () { if ( !this.loadingPromise ) { - this.loadingPromise = mw.loader.getState( 'ext.codeEditor.ace.modes' ) ? - mw.loader.using( 'ext.codeEditor.ace.modes' ) : + this.loadingPromise = mw.loader.getState( 'ext.codeEditor.ace' ) ? + mw.loader.using( 'ext.codeEditor.ace' ) : $.Deferred().reject().promise(); // Resolved promises will run synchronously, so ensure #setupEditor // runs after this.loadingPromise is stored. @@ -327,9 +327,12 @@ ve.ui.MWAceEditorWidget.prototype.togglePrintMargin = function ( visible ) { ve.ui.MWAceEditorWidget.prototype.setLanguage = function ( lang ) { var widget = this; this.loadingPromise.done( function () { - // TODO: Just use ace.require once T127643 is resolved - var require = ace.require || require; - widget.editor.getSession().setMode( 'ace/mode/' + ( require( 'ace/mode/' + lang ) ? lang : 'text' ) ); + ace.config.loadModule( 'ace/ext/modelist', function ( modelist ) { + if ( !modelist || !modelist.modesByName[ lang ] ) { + lang = 'text'; + } + widget.editor.getSession().setMode( 'ace/mode/' + lang ); + } ); } ); return this; };