From f5210867baf5a5d8d646fd8d3f0218ab3610de85 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Wed, 30 Aug 2017 22:53:55 +0100 Subject: [PATCH] Respect beta feature in new wikitext editor Bug: T173480 Change-Id: I2ca5a33f09aefe0c3d31082270869a13cee14fc4 --- CodeMirror.hooks.php | 22 ++++++++++++++----- .../modules/ve-cm/ve.ui.CodeMirror.init.js | 20 +++++++++-------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/CodeMirror.hooks.php b/CodeMirror.hooks.php index ef61fbfb..dd2725e6 100644 --- a/CodeMirror.hooks.php +++ b/CodeMirror.hooks.php @@ -3,7 +3,7 @@ class CodeMirrorHooks { /** - * Checks, if CodeMirror should be loaded on this page or not. + * Checks if CodeMirror is enabled for this user * * @param IContextSource $context The current ContextSource object * @staticvar null|boolean $isEnabled Saves, if CodeMirror should be loaded on this page or not @@ -16,10 +16,9 @@ class CodeMirrorHooks { // Check, if we already checked, if page action is editing, if not, do it now if ( $isEnabled === null ) { if ( !$wgCodeMirrorBetaFeature ) { - $isEnabled = in_array( Action::getActionName( $context ), [ 'edit', 'submit' ] ); + $isEnabled = true; } else { - $isEnabled = in_array( Action::getActionName( $context ), [ 'edit', 'submit' ] ) && - $wgCodeMirrorBetaFeature && + $isEnabled = $wgCodeMirrorBetaFeature && ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) && BetaFeatures::isFeatureEnabled( $context->getUser(), 'codemirror-syntax-highlight' ); @@ -29,6 +28,18 @@ class CodeMirrorHooks { return $isEnabled; } + /** + * Checks if CodeMirror for textarea wikitext editor should be loaded on this page or not. + * + * @param IContextSource $context The current ContextSource object + * @staticvar null|boolean $isEnabled Saves, if CodeMirror should be loaded on this page or not + * @return bool + */ + private static function isCodeMirrorOnPage( IContextSource $context ) { + return in_array( Action::getActionName( $context ), [ 'edit', 'submit' ] ) && + self::isCodeMirrorEnabled( $context ); + } + /** * BeforePageDisplay hook handler * @@ -38,9 +49,10 @@ class CodeMirrorHooks { * @param Skin $skin */ public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) { - if ( self::isCodeMirrorEnabled( $out->getContext() ) ) { + if ( self::isCodeMirrorOnPage( $out->getContext() ) ) { $out->addModules( 'ext.CodeMirror' ); } + $out->addJsConfigVars( 'wgCodeMirrorEnabled', self::isCodeMirrorEnabled( $out->getContext() ) ); } /** diff --git a/resources/modules/ve-cm/ve.ui.CodeMirror.init.js b/resources/modules/ve-cm/ve.ui.CodeMirror.init.js index 4708879d..11a85e4d 100644 --- a/resources/modules/ve-cm/ve.ui.CodeMirror.init.js +++ b/resources/modules/ve-cm/ve.ui.CodeMirror.init.js @@ -1,12 +1,14 @@ ( function ( ve, mw ) { - mw.libs.ve.targetLoader.addPlugin( function () { - var i, target, index; - for ( i in ve.init.mw ) { - target = ve.init.mw[ i ]; - if ( target === ve.init.mw.DesktopArticleTarget ) { - index = target.static.actionGroups[ 1 ].include.indexOf( 'changeDirectionality' ); - target.static.actionGroups[ 1 ].include.splice( index, 0, 'codeMirror' ); + if ( mw.config.get( 'wgCodeMirrorEnabled' ) ) { + mw.libs.ve.targetLoader.addPlugin( function () { + var i, target, index; + for ( i in ve.init.mw ) { + target = ve.init.mw[ i ]; + if ( target === ve.init.mw.DesktopArticleTarget ) { + index = target.static.actionGroups[ 1 ].include.indexOf( 'changeDirectionality' ); + target.static.actionGroups[ 1 ].include.splice( index, 0, 'codeMirror' ); + } } - } - } ); + } ); + } }( ve, mediaWiki ) );