jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/344912 )

Change subject: Fix CodeMirror state when initializing
......................................................................


Fix CodeMirror state when initializing

Before CodeMirror was enabled every time (ignoring user settings)
since switchCodeMirror was called in initialisation and some others bugs...

Change-Id: I418c64000e05dbfac62f5bb2327cfe7ed74efb17
---
M resources/ext.CodeMirror.js
1 file changed, 34 insertions(+), 21 deletions(-)

Approvals:
  jenkins-bot: Verified
  Kaldari: Looks good to me, approved



diff --git a/resources/ext.CodeMirror.js b/resources/ext.CodeMirror.js
index 7c56ac6..fcd9311 100644
--- a/resources/ext.CodeMirror.js
+++ b/resources/ext.CodeMirror.js
@@ -264,7 +264,7 @@
                                }
                        );
                        // eslint-disable-next-line no-use-before-define
-                       switchCodeMirror( $( '#wpTextbox1' ).data( 
'wikiEditor-context' ) );
+                       updateToolbarButton( $( '#wpTextbox1' ).data( 
'wikiEditor-context' ) );
                }
        }
 
@@ -294,6 +294,8 @@
         * @param {boolean} prefValue True, if CodeMirror should be enabled by 
default, otherwise false.
         */
        function setCodeEditorPreference( prefValue ) {
+               useCodeMirror = prefValue; // Save state for function 
updateToolbarIcon()
+
                if ( mw.user.isAnon() ) { // Skip it for anon users
                        return;
                }
@@ -302,37 +304,51 @@
        }
 
        /**
+        * Updates CodeMirror button on the toolbar according to the current 
state (on/off)
+        *
+        * @param {Object} [wikiEditor] WikiEditor, if present
+        */
+       function updateToolbarButton( wikiEditor ) {
+               var label;
+
+               if ( useCodeMirror ) {
+                       label = mw.msg( 'codemirror-enable-label' );
+               } else {
+                       label = mw.msg( 'codemirror-disable-label' );
+               }
+
+               if ( wikiEditor ) {
+                       wikiEditor.modules.toolbar.$toolbar.find( 
'a.tool[rel=CodeMirror]' )
+                               .toggleClass( 'tool-codemirror-on', 
!!useCodeMirror )
+                               .toggleClass( 'tool-codemirror-off', 
!useCodeMirror )
+                               .attr( 'title', label );
+               } else {
+                       $( '#mw-editbutton-codemirror' )
+                               .toggleClass( 'mw-editbutton-codemirror-on', 
!!useCodeMirror )
+                               .toggleClass( 'mw-editbutton-codemirror-off', 
!useCodeMirror )
+                               .attr( 'title', label );
+               }
+       }
+
+       /**
         * Enables or disables CodeMirror
         *
         * @param {Object} [wikiEditor] WikiEditor, if present
         */
        function switchCodeMirror( wikiEditor ) {
-               var label;
-
                if ( codeMirror ) {
                        setCodeEditorPreference( false );
                        codeMirror.save();
                        codeMirror.toTextArea();
                        codeMirror = null;
                        $.fn.textSelection = origTextSelection;
-                       label = mw.msg( 'codemirror-enable-label' );
+
                } else {
                        // eslint-disable-next-line no-use-before-define
                        enableCodeMirror( !!wikiEditor );
-                       label = mw.msg( 'codemirror-disable-label' );
                        setCodeEditorPreference( true );
                }
-               if ( wikiEditor ) {
-                       wikiEditor.modules.toolbar.$toolbar.find( 
'a.tool[rel=CodeMirror]' )
-                               .toggleClass( 'tool-codemirror-on', 
!!codeMirror )
-                               .toggleClass( 'tool-codemirror-off', 
!codeMirror )
-                               .attr( 'title', label );
-               } else {
-                       $( '#mw-editbutton-codemirror' )
-                               .toggleClass( 'mw-editbutton-codemirror-on', 
!!codeMirror )
-                               .toggleClass( 'mw-editbutton-codemirror-off', 
!codeMirror )
-                               .attr( 'title', label );
-               }
+               updateToolbarButton( wikiEditor );
        }
 
        /**
@@ -399,11 +415,8 @@
                                                        return false;
                                                }
                                        } );
-                                       $( function () {
-                                               $( '#mw-editbutton-codemirror' )
-                                                       // Classes used here: 
mw-editbutton-codemirror-on, mw-editbutton-codemirror-off
-                                                       .addClass( 
'mw-editbutton-codemirror-' + ( useCodeMirror ? 'on' : 'off' ) );
-                                       } );
+                                       // We don't know when button will be 
added, wait until the document is ready for update it
+                                       $( document ).ready( function () { 
updateToolbarButton(); } );
                                } );
                        }
                } );

-- 
To view, visit https://gerrit.wikimedia.org/r/344912
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I418c64000e05dbfac62f5bb2327cfe7ed74efb17
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/CodeMirror
Gerrit-Branch: master
Gerrit-Owner: Pastakhov <[email protected]>
Gerrit-Reviewer: Esanders <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to