Improved editor focus if quotes are present
authorAlexander Ebert <ebert@woltlab.com>
Mon, 27 Oct 2014 11:57:20 +0000 (12:57 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 27 Oct 2014 11:57:32 +0000 (12:57 +0100)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js
wcfsetup/install/files/js/WCF.Message.js

index 97c4017f70dec042829e6b1cfac34c76359bf556..13c028485037b283d7bb1d872be97e7f30dbb909 100644 (file)
@@ -22,6 +22,9 @@ RedactorPlugins.wbbcode = function() {
                                var $content = $.trim(this.wutil.getOption('woltlab.originalValue'));
                                if ($content.length) {
                                        this.wutil.replaceText($content);
+                                       
+                                       // ensure that the caret is not within a quote tag
+                                       this.wutil.selectionEndOfEditor();
                                }
                                
                                delete this.opts.woltlab.originalValue;
@@ -1259,29 +1262,41 @@ RedactorPlugins.wbbcode = function() {
                 * @param       string          plainText
                 */
                insertQuoteBBCode: function(author, link, html, plainText) {
-                       var $bbcode = '[quote]';
+                       var $openTag = '[quote]';
+                       var $closingTag = '[/quote]';
+                       
                        if (author) {
                                if (link) {
-                                       $bbcode = "[quote='" + author + "','" + link + "']";
+                                       $openTag = "[quote='" + author + "','" + link + "']";
                                }
                                else {
-                                       $bbcode = "[quote='" + author + "']";
+                                       $openTag = "[quote='" + author + "']";
                                }
                        }
                        
-                       if (plainText) $bbcode += plainText;
-                       $bbcode += '[/quote]';
-                       
                        if (this.wutil.inWysiwygMode()) {
-                               $bbcode = this.wbbcode.convertToHtml($bbcode);
-                               this.insert.html($bbcode, false);
+                               var $innerHTML = (plainText) ? this.wbbcode.convertToHtml(plainText) : '';
+                               var $id = WCF.getUUID();
+                               var $html = this.wbbcode.convertToHtml($openTag + $id + $closingTag);
+                               $html = $html.replace($id, $innerHTML);
+                               
+                               // assign a unique id in order to recognize the inserted quote
+                               $html = $html.replace(/(<p>)?<blockquote/, '$1<blockquote id="' + $id + '"');
+                               
+                               this.insert.html($html, false);
+                               
+                               var $quote = this.$editor.find('#' + $id);
+                               if ($quote.length) {
+                                       $quote.removeAttr('id');
+                                       this.wutil.setCaretAfter($quote[0]);
+                               }
                                
                                this.wbbcode._observeQuotes();
                                
                                this.$toolbar.find('a.re-__wcf_quote').addClass('redactor-button-disabled');
                        }
                        else {
-                               this.wutil.insertAtCaret($bbcode);
+                               this.wutil.insertAtCaret($openTag + plainText + $closingTag);
                        }
                },
                
index d1653333268d6cc90f79e999fe3cc15d582809cb..0780038e9b4f618d579e32bb462da20d9721a013 100644 (file)
@@ -347,7 +347,17 @@ RedactorPlugins.wutil = function() {
                 * Sets the selection after the last direct children of the editor.
                 */
                selectionEndOfEditor: function() {
-                       this.selectionEnd(this.$editor.children(':last')[0]);
+                       var $lastChild = this.$editor.children(':last')[0];
+                       if ($lastChild.tagName === 'P') {
+                               // sometimes the last <p> is just empty, causing the method to fail
+                               if ($lastChild.innerHTML === '') {
+                                       $lastChild = $($lastChild).replaceWith($(this.opts.emptyHtml));
+                                       this.caret.setEnd($lastChild[0]);
+                               }
+                       }
+                       else {
+                               this.wutil.setCaretAfter($lastChild);
+                       }
                },
                
                /**
index 1f697eaf9ebc16796708b8bcd1abcfed0497ecf6..835afc36637579e857d07f7e87374e4758078687 100644 (file)
@@ -896,6 +896,7 @@ WCF.Message.QuickReply = Class.extend({
                        
                        this._messageField.redactor('focus.setEnd');
                        this._messageField.redactor('wutil.insertDynamic', $html, data.returnValues.template);
+                       this._messageField.redactor('wutil.selectionEndOfEditor');
                }
                else {
                        this._messageField.val(data.returnValues.template);