From 541741d763675febdfe1dc27c4610e9fcbe0d337 Mon Sep 17 00:00:00 2001 From: MusikAnimal Date: Tue, 3 Dec 2024 18:11:06 -0500 Subject: [PATCH] Hooks: ensure GetPreferences hook and 2017 editor respect beta feature For users of the 2003 editor (or if WikiEditor is not available), the user is supposed to still be able to toggle CodeMirror on and off using the visible preference in the new 'Syntax highlighting' section of Special:Preferences. This patch fixes that, restoring use of standalone CodeMirror for those who have the beta feature on but WikiEditor off. This patch also fixes a bug that prevented 2017 editor users from getting CM6 via the beta feature. Bug: T376735 Change-Id: I69562ee1f936050301900ee80ab1aa8eaf92ec0d --- includes/Hooks.php | 17 ++++++++++++----- resources/ve-cm/ve.ui.CodeMirror.init.js | 5 ++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/includes/Hooks.php b/includes/Hooks.php index d025350a..723718f7 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -164,10 +164,17 @@ class Hooks implements * @todo Remove check for cm6enable flag after migration is complete */ private function shouldUseV6( OutputPage $out ): bool { - return $this->useV6 || $out->getRequest()->getRawVal( 'cm6enable' ) || ( - ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) && - BetaFeatures::isFeatureEnabled( $out->getUser(), 'codemirror-beta-feature-enable' ) - ); + return $this->useV6 || $out->getRequest()->getBool( 'cm6enable' ) || + $this->isBetaFeatureEnabled( $out->getUser() ); + } + + /** + * @param User $user + * @return bool + */ + private function isBetaFeatureEnabled( User $user ): bool { + return ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) && + BetaFeatures::isFeatureEnabled( $user, 'codemirror-beta-feature-enable' ); } /** @@ -194,7 +201,7 @@ class Hooks implements * @return bool|void True or no return value to continue or false to abort */ public function onGetPreferences( $user, &$defaultPreferences ) { - if ( !$this->useV6 ) { + if ( !$this->useV6 && !$this->isBetaFeatureEnabled( $user ) ) { $defaultPreferences['usecodemirror'] = [ 'type' => 'api', ]; diff --git a/resources/ve-cm/ve.ui.CodeMirror.init.js b/resources/ve-cm/ve.ui.CodeMirror.init.js index 0e6fd479..cff23cde 100644 --- a/resources/ve-cm/ve.ui.CodeMirror.init.js +++ b/resources/ve-cm/ve.ui.CodeMirror.init.js @@ -7,9 +7,12 @@ require( '../ext.CodeMirror.data.js' ); // TODO: Change the PluginModules in extension.json when fully migrated to v6 and drop this. mw.hook( 've.loadModules' ).add( ( addPlugin ) => { const urlParams = new URLSearchParams( window.location.search ); + const shouldUseV6 = mw.config.get( 'extCodeMirrorConfig' ).useV6 || + urlParams.get( 'cm6enable' ) || + mw.user.options.get( 'codemirror-beta-feature-enable' ) === '1'; // VE would wait for plugin callbacks to resolve before initialisation. - if ( mw.config.get( 'extCodeMirrorConfig' ).useV6 || urlParams.get( 'cm6enable' ) ) { + if ( shouldUseV6 ) { addPlugin( () => mw.loader.using( 'ext.CodeMirror.v6.visualEditor' ) ); } else { addPlugin( () => mw.loader.using( 'ext.CodeMirror.visualEditor' ) );