Reverse removal of Live Preview in r59446, with the following changes:
authorAndrew Garrett <werdna@users.mediawiki.org>
Tue, 9 Feb 2010 07:39:09 +0000 (07:39 +0000)
committerAndrew Garrett <werdna@users.mediawiki.org>
Tue, 9 Feb 2010 07:39:09 +0000 (07:39 +0000)
* Make Live Preview entirely JS-based. It requires no modification of the HTML.
* Replace annoying slideDown with less annoying fadeIn().
* Ensure that elements not supposed to be shown are not, in fact, shown by fadeIn().
* Remove some redundant lines of code.
* Make compatible with the current jQuery usage in MediaWiki.

includes/EditPage.php
includes/Preferences.php
skins/common/preview.js [new file with mode: 0644]

index b0f90fe..e3bd3a4 100644 (file)
@@ -364,7 +364,7 @@ class EditPage {
         * the newly-edited page.
         */
        function edit() {
-               global $wgOut, $wgRequest;
+               global $wgOut, $wgRequest, $wgUser;
                // Allow extensions to modify/prevent this form or submission
                if ( !wfRunHooks( 'AlternateEdit', array( $this ) ) ) {
                        return;
@@ -392,6 +392,11 @@ class EditPage {
                }
 
                $wgOut->addScriptFile( 'edit.js' );
+               
+               if ( $wgUser->getOption( 'uselivepreview', false ) ) {
+                       $wgOut->includeJQuery();
+                       $wgOut->addScriptFile( 'preview.js' );
+               }
 
                $permErrors = $this->getEditPermissionErrors();
                if ( $permErrors ) {
index a701125..2137f9d 100644 (file)
@@ -637,7 +637,7 @@ class Preferences {
        }
 
        static function editingPreferences( $user, &$defaultPreferences ) {
-               global $wgUseExternalEditor;
+               global $wgUseExternalEditor, $wgLivePreview;
 
                ## Editing #####################################
                $defaultPreferences['cols'] =
@@ -739,6 +739,14 @@ class Preferences {
                                        'section' => 'editing/advancedediting',
                                        'label-message' => 'tog-forceeditsummary',
                                );
+               if ( $wgLivePreview ) {
+                       $defaultPreferences['uselivepreview'] =
+                                       array(
+                                               'type' => 'toggle',
+                                               'section' => 'editing/advancedediting',
+                                               'label-message' => 'tog-uselivepreview',
+                                       );
+               }
        }
 
        static function rcPreferences( $user, &$defaultPreferences ) {
diff --git a/skins/common/preview.js b/skins/common/preview.js
new file mode 100644 (file)
index 0000000..4476903
--- /dev/null
@@ -0,0 +1,52 @@
+/**
+ * Live preview script for MediaWiki
+ */
+
+function doLivePreview( e ) {
+       e.preventDefault();
+       var previewText = $j('#wpTextbox1').val();
+
+       var editToken = $j( '[name="wpEditToken"]' ).attr( 'value' );
+       var editTime = $j( '[name="wpEdittime"]' ).attr( 'value' );
+       var startTime = $j( '[name="wpStarttime"]' ).attr( 'value' );
+
+       var postData = { 'action' : 'submit', 'wpTextbox1' : previewText, 'wpPreview' : true,
+               'wpEditToken' : editToken, 'wpEdittime': editTime, 'wpStarttime': startTime, 'title' : wgPageName };
+       
+       // Hide active diff, used templates, old preview if shown
+       var copyElements = ['#wikiPreview', '.templatesUsed', '.hiddencats',
+                                               '#catlinks'];
+
+       $j.each( copyElements, function(k,v) { $j(v).fadeOut(); } );
+       
+       // Display a loading graphic
+       var loadSpinner = $j('<div class="mw-ajax-loader"/>');
+       $j('#wikiPreview').before( loadSpinner );
+       
+       var page = $j('<html/>');
+       page.load( wgScript+'?action=submit',
+                               postData,
+               function() {
+                       
+                       for( var i=0; i<copyElements.length; ++i) {
+                               // For all the specified elements, find the elements in the loaded page
+                               //  and the real page, empty the element in the real page, and fill it
+                               //  with the content of the loaded page
+                               var copyContent = page.find( copyElements[i] ).contents();
+                               $j(copyElements[i]).empty().append( copyContent );
+                               var newClasses = page.find( copyElements[i] ).attr('class');
+                               $j(copyElements[i]).attr( 'class', newClasses );
+                       }
+                       
+                       $j.each( copyElements, function(k,v) {
+                               // Don't belligerently show elements that are supposed to be hidden
+                               $j(v).fadeIn( 'fast', function() { $j(this).css('display', ''); } );
+                       } );
+                       
+                       loadSpinner.remove();
+               } );
+}
+
+$j(document).ready( function() {
+       $j('#wpPreview').click( doLivePreview );
+} );