From 60f450a773f5d8d17d8192514152c71b5bd556b7 Mon Sep 17 00:00:00 2001 From: bhsd <2545473905@qq.com> Date: Fri, 29 Nov 2024 21:42:37 +0800 Subject: [PATCH] mediaWikiLang: remove used hook handlers Every time when `mediaWikiLang()` is called, a new handler is registered for the hook `ext.CodeMirror.ready`. This causes unexpected errors, so the used handler needs to be removed before registering a new handler. Bug: T380840 Change-Id: I81bed58ec37a508e53a93895c7e20de358a658e0 --- resources/codemirror.mediawiki.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/resources/codemirror.mediawiki.js b/resources/codemirror.mediawiki.js index ce3c4b2a..63d09797 100644 --- a/resources/codemirror.mediawiki.js +++ b/resources/codemirror.mediawiki.js @@ -1374,6 +1374,8 @@ class CodeMirrorModeMediaWiki { } } +let handler; + /** * Gets a LanguageSupport instance for the MediaWiki mode. * @@ -1401,7 +1403,10 @@ const mediaWikiLang = ( config = { bidiIsolation: false }, mwConfig = null ) => // Register MW-specific Extensions into CodeMirror preferences. Whether they are enabled // or not is determined by the user's preferences and wiki configuration. - mw.hook( 'ext.CodeMirror.ready' ).add( ( $textarea, cm ) => { + if ( handler ) { + mw.hook( 'ext.CodeMirror.ready' ).remove( handler ); + } + handler = ( $textarea, cm ) => { if ( config.templateFolding !== false ) { cm.preferences.registerExtension( 'templateFolding', templateFoldingExtension, cm.view ); } @@ -1411,7 +1416,8 @@ const mediaWikiLang = ( config = { bidiIsolation: false }, mwConfig = null ) => if ( config.bidiIsolation ) { cm.preferences.registerExtension( 'bidiIsolation', bidiIsolationExtension, cm.view ); } - } ); + }; + mw.hook( 'ext.CodeMirror.ready' ).add( handler ); return new LanguageSupport( lang, langExtension ); };