Added proper support for [code], inline editor partially works now
authorAlexander Ebert <ebert@woltlab.com>
Sun, 30 Mar 2014 22:50:04 +0000 (00:50 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 30 Mar 2014 22:50:04 +0000 (00:50 +0200)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js
wcfsetup/install/files/js/WCF.Message.js

index 7c9365165e9dfa0eb5ad80a66fee8ba2ad3d87ce..93e64da8d5048285a0d9c4e72ce5b2e7d8973163 100644 (file)
@@ -127,7 +127,7 @@ RedactorPlugins.wbbcode = {
                                return $html;
                        }
                        
-                       return $html + '<br>';;
+                       return $html + '<br>';
                });
                this.sync();
        },
@@ -138,6 +138,10 @@ RedactorPlugins.wbbcode = {
        _convertFromHtml: function() {
                var html = this.$source.val();
                
+               // drop line break right before/after a <pre> tag (used by [code]-BBCode)
+               html = html.replace(/<br>\n<pre>\n/g, '');
+               html = html.replace(/<\/pre>\n<br>\n/g, '');
+               
                // drop <br>, they are pointless because the editor already adds a newline after them
                html = html.replace(/<br>/g, '');
                html = html.replace(/&nbsp;/gi," ");
@@ -258,13 +262,26 @@ RedactorPlugins.wbbcode = {
                html = html.replace(/&gt;/g, '>');
                html = html.replace(/&amp;/g, '&');
                
-               // Restore (and )
+               // Restore ( and )
                html = html.replace(/%28/g, '(');
                html = html.replace(/%29/g, ')');
                
                // Restore %20
                html = html.replace(/%20/g, ' ');
                
+               // cache source code tags to preserve leading tabs
+               var $cachedCodes = { };
+               for (var $i = 0, $length = __REDACTOR_SOURCE_BBCODES.length; $i < $length; $i++) {
+                       var $bbcode = __REDACTOR_SOURCE_BBCODES[$i];
+                       
+                       var $regExp = new RegExp('\\[' + $bbcode + '([\\S\\s]+?)\\[\\/' + $bbcode + '\\]', 'gi');
+                       html = html.replace($regExp, function(match) {
+                               var $key = match.hashCode();
+                               $cachedCodes[$key] = match.replace(/\$/g, '$$$$');
+                               return '@@' + $key + '@@';
+                       });
+               }
+               
                // trim leading tabs
                var $tmp = html.split("\n");
                for (var $i = 0, $length = $tmp.length; $i < $length; $i++) {
@@ -272,6 +289,14 @@ RedactorPlugins.wbbcode = {
                }
                html = $tmp.join("\n");
                
+               // insert codes
+               if ($.getLength($cachedCodes)) {
+                       for (var $key in $cachedCodes) {
+                               var $regex = new RegExp('@@' + $key + '@@', 'g');
+                               html = html.replace($regex, $cachedCodes[$key]);
+                       }
+               }
+               
                this.$source.val(html);
        },
        
@@ -304,7 +329,7 @@ RedactorPlugins.wbbcode = {
                for (var $i = 0, $length = __REDACTOR_SOURCE_BBCODES.length; $i < $length; $i++) {
                        var $bbcode = __REDACTOR_SOURCE_BBCODES[$i];
                        
-                       var $regExp = new RegExp('\\[' + $bbcode + '(.+?)\\[\\/' + $bbcode + '\\]', 'gi');
+                       var $regExp = new RegExp('\\[' + $bbcode + '([\\S\\s]+?)\\[\\/' + $bbcode + '\\]', 'gi');
                        data = data.replace($regExp, function(match) {
                                var $key = match.hashCode();
                                $cachedCodes[$key] = match.replace(/\$/g, '$$$$');
@@ -413,6 +438,9 @@ RedactorPlugins.wbbcode = {
                        }
                }
                
+               // preserve leading whitespaces in [code] tags
+               data = data.replace(/\[code\][\S\s]*?\[\/code\]/, '<pre>$&</pre>');
+               
                this.$source.val(data);
        }
 };
index cc1678d9366d7de0015a0fb25a38808088c253cd..807cb949ed3556afaae04c100371171bbf717ebe 100644 (file)
@@ -60,6 +60,10 @@ RedactorPlugins.wmonkeypatch = {
                this.modalTemplatesInit();
        },
        
+       cleanRemoveSpaces: function(html, buffer) {
+               return html;
+       },
+       
        /**
         * Overwrites $.Redactor.inlineRemoveStyle() to drop empty <inline> elements.
         * 
index 2dea0f1d9ae718c14b4d3da46fb5fec19280ba73..5ccbd1ac6f57a2a3fcf6b3d3329e570653ade5b3 100644 (file)
@@ -1291,6 +1291,7 @@ WCF.Message.InlineEditor = Class.extend({
                // hide message options
                this._container[this._activeElementID].find('.messageOptions').addClass('forceHidden');
                
+               // TODO: support redactor here, in particular update the quote manager first
                if ($.browser.ckeditor) {
                        new WCF.PeriodicalExecuter($.proxy(function(pe) {
                                pe.stop();
@@ -1332,9 +1333,8 @@ WCF.Message.InlineEditor = Class.extend({
                var $objectID = $container.data('objectID');
                var $message = '';
                
-               if ($.browser.ckeditor) {
-                       var $ckEditor = $('#' + this._messageEditorIDPrefix + $objectID).ckeditorGet();
-                       $message = $ckEditor.getData();
+               if ($.browser.redactor) {
+                       $message = $('#' + this._messageEditorIDPrefix + $objectID).redactor('getText');
                }
                else {
                        $message = $('#' + this._messageEditorIDPrefix + $objectID).val();
@@ -1365,9 +1365,8 @@ WCF.Message.InlineEditor = Class.extend({
                var $objectID = $container.data('objectID');
                var $message = '';
                
-               if ($.browser.ckeditor) {
-                       var $ckEditor = $('#' + this._messageEditorIDPrefix + $objectID).ckeditorGet();
-                       $message = $ckEditor.getData();
+               if ($.browser.redactor) {
+                       $message = $('#' + this._messageEditorIDPrefix + $objectID).redactor('getText');
                }
                else {
                        $message = $('#' + this._messageEditorIDPrefix + $objectID).val();
@@ -1401,9 +1400,12 @@ WCF.Message.InlineEditor = Class.extend({
                // show unrelated content
                $messageBody.find('.jsInlineEditorHideContent').show();
                
+               /*
+                * TODO: the quote manager does not support redactor yet
                if (this._quoteManager) {
                        this._quoteManager.clearAlternativeCKEditor();
                }
+               */
        },
        
        /**
@@ -1424,9 +1426,8 @@ WCF.Message.InlineEditor = Class.extend({
                this._container[this._activeElementID].find('.messageOptions').removeClass('forceHidden');
                
                // remove editor
-               if ($.browser.ckeditor) {
-                       var $ckEditor = $('#' + this._messageEditorIDPrefix + $container.data('objectID')).ckeditorGet();
-                       $ckEditor.destroy();
+               if ($.browser.redactor) {
+                       $('#' + this._messageEditorIDPrefix + $container.data('objectID')).redactor('destroy');
                }
                
                $content.empty();
@@ -1440,9 +1441,12 @@ WCF.Message.InlineEditor = Class.extend({
                
                this._notification.show();
                
+               /*
+                * TODO: the quote manager does not support redactor yet
                if (this._quoteManager) {
                        this._quoteManager.clearAlternativeCKEditor();
                }
+               */
        },
        
        /**