Fixed quote insertion and inline error visuals
authorAlexander Ebert <ebert@woltlab.com>
Fri, 12 Sep 2014 18:10:14 +0000 (20:10 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 12 Sep 2014 18:10:14 +0000 (20:10 +0200)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/WCF.Message.js
wcfsetup/install/files/style/redactor.less

index 4680e221e41d1789c95a0ab52d475f505f0b0819..44f037c4b171f8e5223373659167d6ce0d320171 100644 (file)
@@ -8,6 +8,8 @@ if (!RedactorPlugins) var RedactorPlugins = {};
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  */
 RedactorPlugins.wbbcode = {
+       wBlockTest: /^(BLOCKQUOTE|DIV)$/,
+       
        /**
         * Initializes the RedactorPlugins.wbbcode plugin.
         */
@@ -156,6 +158,7 @@ RedactorPlugins.wbbcode = {
                else {
                        this._convertToHtml();
                        this.toggleVisual();
+                       this.fixQuoteContent();
                        this._observeQuotes();
                        
                        this.buttonGet('html').children('i').removeClass('fa-square').addClass('fa-square-o');
@@ -658,74 +661,96 @@ RedactorPlugins.wbbcode = {
                // insert quotes
                if ($.getLength($cachedQuotes)) {
                        // [quote]
-                       var $unquoteString = function(quotedString) {
-                               return quotedString.replace(/^['"]/, '').replace(/['"]$/, '');
-                       };
+                       for (var $key in $cachedQuotes) {
+                               var $regex = new RegExp('@@' + $key + '@@', 'g');
+                               data = data.replace($regex, this.transformQuote($cachedQuotes[$key]));
+                       }
+               }
+               
+               WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'afterConvertToHtml', { data: data });
+               
+               this.$source.val(data);
+       },
+       
+       unquoteString: function(quotedString) {
+               return quotedString.replace(/^['"]/, '').replace(/['"]$/, '');
+       },
+       
+       transformQuote: function(quote) {
+               var self = this;
+               quote = quote.replace(/\[quote([^\]]+)?\]/gi, function(match, attributes, innerContent) {
+                       var $author = '';
+                       var $link = '';
                        
-                       var self = this;
-                       var $transformQuote = function(quote) {
-                               return quote.replace(/\[quote([^\]]+)?\]([\S\s]*)\[\/quote\]?/gi, $.proxy(function(match, attributes, innerContent) {
-                                       var $author = '';
-                                       var $link = '';
+                       if (attributes) {
+                               attributes = attributes.substr(1);
+                               attributes = attributes.split(',');
+                               
+                               switch (attributes.length) {
+                                       case 1:
+                                               $author = attributes[0];
+                                       break;
                                        
-                                       if (attributes) {
-                                               attributes = attributes.substr(1);
-                                               attributes = attributes.split(',');
-                                               
-                                               switch (attributes.length) {
-                                                       case 1:
-                                                               $author = attributes[0];
-                                                       break;
-                                                       
-                                                       case 2:
-                                                               $author = attributes[0];
-                                                               $link = attributes[1];
-                                                       break;
-                                               }
-                                               
-                                               $author = WCF.String.escapeHTML($unquoteString($.trim($author)));
-                                               $link = WCF.String.escapeHTML($unquoteString($.trim($link)));
+                                       case 2:
+                                               $author = attributes[0];
+                                               $link = attributes[1];
+                                       break;
+                               }
+                               
+                               $author = WCF.String.escapeHTML(self.unquoteString($.trim($author)));
+                               $link = WCF.String.escapeHTML(self.unquoteString($.trim($link)));
+                       }
+                       
+                       var $quote = '<blockquote class="quoteBox" cite="' + $link + '" data-author="' + $author + '">'
+                               + '<div class="container containerPadding">'
+                                       + '<header>'
+                                               + '<h3>'
+                                                       + self._buildQuoteHeader($author, $link)
+                                               + '</h3>'
+                                               + '<a class="redactorQuoteEdit"></a>'
+                                       + '</header>'
+                                       + '<div class="redactorQuoteContent">';
+                       
+                       return $quote;
+               });
+               
+               return quote.replace(/\[\/quote\]/gi, '</blockquote>');
+       },
+       
+       /**
+        * Fixes quote content by converting newlines into own paragraphs.
+        */
+       fixQuoteContent: function() {
+               var $count = this.$editor.find('.redactorQuoteContent').length;
+               var self = this;
+               while ($count > 0) {
+                       var $content = this.$editor.find('.redactorQuoteContent:eq(0)').removeClass('redactorQuoteContent');
+                       $content.contents().each(function(index, node) {
+                               if (node.nodeType === Element.ELEMENT_NODE) {
+                                       if (self.opts.rBlockTest.test(this.tagName) || self.wBlockTest.test(this.tagName)) {
+                                               $(node).wrap('<div />');
                                        }
+                               }
+                               else if (node.nodeType === Element.TEXT_NODE) {
+                                       var $node = $(node);
                                        
-                                       var $quote = '<blockquote class="quoteBox" cite="' + $link + '" data-author="' + $author + '">'
-                                               + '<div class="container containerPadding">'
-                                                       + '<header>'
-                                                               + '<h3>'
-                                                                       + self._buildQuoteHeader($author, $link)
-                                                               + '</h3>'
-                                                               + '<a class="redactorQuoteEdit"></a>'
-                                                       + '</header>';
-                                       
-                                       innerContent = $.trim(innerContent);
-                                       var $tmp = '';
-                                       
-                                       if (innerContent.length) {
-                                               var $lines = innerContent.split('\n');
-                                               
-                                               for (var $i = 0; $i < $lines.length; $i++) {
-                                                       $tmp += '<div>' + $lines[$i] + '</div>';
+                                       // split text nodes
+                                       var $parts = node.nodeValue.split(/\n/);
+                                       for (var $i = 0; $i < $parts.length; $i++) {
+                                               var $part = $.trim($parts[$i]);
+                                               if (!$part.length) {
+                                                       $part = self.opts.invisibleSpace;
                                                }
+                                               
+                                               $('<div>' + $part + '</div>').insertAfter($node);
                                        }
-                                       else {
-                                               $tmp = '<div>' + self.opts.invisibleSpace + '</div>';
-                                       }
-                                       
-                                       $quote += $tmp;
-                                       $quote += '</blockquote>';
                                        
-                                       return $quote;
-                               }, this));
-                       };
+                                       $node.remove();
+                               }
+                       });
                        
-                       for (var $key in $cachedQuotes) {
-                               var $regex = new RegExp('@@' + $key + '@@', 'g');
-                               data = data.replace($regex, $transformQuote($cachedQuotes[$key]));
-                       }
+                       $count--;
                }
-               
-               WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'afterConvertToHtml', { data: data });
-               
-               this.$source.val(data);
        },
        
        /**
index 1a45ed3c58f28a89c74386d2998cdd06ca154e62..fd6119cfd51c3cedac9406b81118f9e9c76a7a0c 100644 (file)
@@ -893,6 +893,7 @@ WCF.Message.QuickReply = Class.extend({
                        
                        this._messageField.redactor('selectionEndOfEditor');
                        this._messageField.redactor('insertDynamic', $html, data.returnValues.template);
+                       this._messageField.redactor('fixQuoteContent');
                }
                else {
                        this._messageField.val(data.returnValues.template);
index 411cc1ff4ca4f59a512ea68481c907d28ae263a1..804e20375221a497d8b25eae05cf451bee359042 100644 (file)
@@ -60,7 +60,7 @@
                border-radius: 0;
                box-shadow: none;
                display: block;
-               margin-top: -1px;
+               margin: -1px;
        }
 }