CodeMirrorWikiEditor: properly manage WikiEditor buttons

Fixes an issue introduced with Ib65d47e989 where continually toggling on
and off CodeMirror would duplicate the search button exponentially, even
leading to browser crashes.

Create a new 'codemirror' toolbar group for the prefs button, styling it
just like the search group, and use WikiEditor's removeToToolbar() to
remove it.

Remove now redundant CSS classes.

Bug: T359498
Follow-Up: Ib65d47e98908b6a888d24157a235de4efe95a33b
Change-Id: If8ae9c0b04b2edcb30a5b8cd788214598a492d30
This commit is contained in:
MusikAnimal 2024-11-25 02:27:10 -05:00
parent b61fe12dfe
commit f62a2be966
2 changed files with 25 additions and 8 deletions

View file

@ -56,10 +56,16 @@ class CodeMirrorWikiEditor extends CodeMirror {
* @type {Function|null}
*/
this.realtimePreviewHandler = null;
/**
* The WikiEditor search button, which is usurped to open the CodeMirror search panel.
*
* @type {jQuery|null}
*/
this.$searchBtn = null;
/**
* The old WikiEditor search button, to be restored if CodeMirror is disabled.
*
* @type {jQuery}
* @type {jQuery|null}
*/
this.$oldSearchBtn = null;
}
@ -127,9 +133,10 @@ class CodeMirrorWikiEditor extends CodeMirror {
// Hijack the search button to open the CodeMirror search panel
// instead of the WikiEditor search dialog.
// eslint-disable-next-line no-jquery/no-global-selector
const $searchBtn = $( '.wikiEditor-ui .group-search a' );
this.$oldSearchBtn = $searchBtn.clone( true );
$searchBtn.off( 'click keydown keypress' )
this.$searchBtn = $( '.wikiEditor-ui .group-search .tool' );
this.$oldSearchBtn = this.$searchBtn.clone( true );
this.$searchBtn.find( 'a' )
.off( 'click keydown keypress' )
.on( 'click keydown', ( e ) => {
if ( e.type === 'click' || ( e.type === 'keydown' && e.key === 'Enter' ) ) {
openSearchPanel( this.view );
@ -143,7 +150,7 @@ class CodeMirrorWikiEditor extends CodeMirror {
{
section: 'advanced',
groups: {
search: {
codemirror: {
tools: {
CodeMirrorPreferences: {
type: 'element',
@ -152,7 +159,7 @@ class CodeMirrorWikiEditor extends CodeMirror {
title: mw.msg( 'codemirror-prefs-title' ),
icon: 'settings',
framed: false,
classes: [ 'tool', 'cm-mw-settings' ]
classes: [ 'tool' ]
} );
button.on( 'click',
() => this.preferences.toggle( this.view, true )
@ -285,8 +292,11 @@ class CodeMirrorWikiEditor extends CodeMirror {
if ( this.view ) {
this.setCodeMirrorPreference( false );
this.destroy();
// eslint-disable-next-line no-jquery/no-global-selector
$( '.wikiEditor-ui .group-search a' ).replaceWith( this.$oldSearchBtn );
this.$searchBtn.replaceWith( this.$oldSearchBtn );
this.$textarea.wikiEditor( 'removeFromToolbar', {
section: 'advanced',
group: 'codemirror'
} );
mw.hook( 'ext.CodeMirror.switch' ).fire( false, this.$textarea );
} else {
this.enableCodeMirror();

View file

@ -6,6 +6,13 @@
border: inherit;
}
// Mimics .group-search
.wikiEditor-ui-toolbar .group-codemirror {
float: right;
border-right: 0;
border-left: @border-subtle;
}
.cm-mw-toggle-wikieditor {
.oo-ui-icon-syntax-highlight {
background-color: @color-base;