From e75ca66a667e12cb2fe06741b24780839ecfa731 Mon Sep 17 00:00:00 2001 From: Func Date: Sat, 7 Dec 2024 09:28:02 +0800 Subject: [PATCH] ve.ui.CodeMirror.v6: Use plugin callback to load the actual module TODO: Change the PluginModules in extension.json when fully migrated to v6 and drop this hack. Bug: T374072 Change-Id: I5f06af67928c491db330d9761084d8740cfc2487 --- resources/ve-cm/ve.ui.CodeMirror.init.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/resources/ve-cm/ve.ui.CodeMirror.init.js b/resources/ve-cm/ve.ui.CodeMirror.init.js index eb41aa36..0e6fd479 100644 --- a/resources/ve-cm/ve.ui.CodeMirror.init.js +++ b/resources/ve-cm/ve.ui.CodeMirror.init.js @@ -1,9 +1,17 @@ require( '../ext.CodeMirror.data.js' ); -const urlParams = new URLSearchParams( window.location.search ); +// This is hacky: mw.hook ensures that new handlers would be fired immediately if they are +// added after the event. We are using this feature to load the actual module via a callback +// when this module is loaded by VE. +// +// 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 ); -if ( mw.config.get( 'extCodeMirrorConfig' ).useV6 || urlParams.get( 'cm6enable' ) ) { - mw.loader.load( 'ext.CodeMirror.v6.visualEditor' ); -} else { - mw.loader.load( 'ext.CodeMirror.visualEditor' ); -} + // VE would wait for plugin callbacks to resolve before initialisation. + if ( mw.config.get( 'extCodeMirrorConfig' ).useV6 || urlParams.get( 'cm6enable' ) ) { + addPlugin( () => mw.loader.using( 'ext.CodeMirror.v6.visualEditor' ) ); + } else { + addPlugin( () => mw.loader.using( 'ext.CodeMirror.visualEditor' ) ); + } +} );