LivePreview: Let's not potentially mangle #wikiPreview
authorDerk-Jan Hartman <hartman.wiki@gmail.com>
Wed, 25 Mar 2015 23:33:06 +0000 (00:33 +0100)
committerDerk-Jan Hartman <hartman.wiki@gmail.com>
Wed, 25 Mar 2015 23:33:06 +0000 (00:33 +0100)
Sometimes you look at what you have written and realize it can be better...

Bug: T90490
Change-Id: If620e5c672e4632997028ddc9f802a82a83e6924

resources/src/mediawiki.action/mediawiki.action.edit.preview.js

index 1878c89..6f8e8af 100644 (file)
@@ -9,12 +9,13 @@
         */
        function doLivePreview( e ) {
                var isDiff, api, request, postData, copySelectors, section,
-                       $wikiPreview, $wikiDiff, $editform, $copyElements, $spinner;
+                       $wikiPreview, $wikiDiff, $editform, $copyElements, $spinner, $errorBox;
 
                isDiff = ( e.target.name === 'wpDiff' );
                $wikiPreview = $( '#wikiPreview' );
                $wikiDiff = $( '#wikiDiff' );
                $editform = $( '#editform' );
+               $errorBox = $( '.errorbox' );
                section = $editform.find( '[name="wpSection"]' ).val();
 
                // Show changes for a new section is not yet supported
@@ -23,6 +24,8 @@
                }
                e.preventDefault();
 
+               // Remove any previously displayed errors
+               $errorBox.remove();
                // Show #wikiPreview if it's hidden to be able to scroll to it
                // (if it is hidden, it's also empty, so nothing changes in the rendering)
                $wikiPreview.show();
                                        errorMsg += result.textStatus;
                                }
                        }
-                       $wikiPreview.children( '.mw-content-ltr,.mw-content-rtl' )
-                               .empty()
-                               .append( $( '<div>' )
-                                       .addClass( 'errorbox' )
-                                       .html( '<strong>' + mw.message( 'previewerrortext' ).escaped() + '</strong><br>' )
-                                       .append( document.createTextNode( errorMsg ) ) );
-                       $wikiPreview.show();
+                       $errorBox = $( '<div>' )
+                               .addClass( 'errorbox' )
+                               .html( '<strong>' + mw.message( 'previewerrortext' ).escaped() + '</strong><br>' )
+                               .append( document.createTextNode( errorMsg ) );
+                       $wikiDiff.hide();
+                       $wikiPreview.hide().before( $errorBox );
                } );
        }