Improved inserting of quotes
authorAlexander Ebert <ebert@woltlab.com>
Mon, 29 Dec 2014 13:54:29 +0000 (14:54 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 29 Dec 2014 13:54:29 +0000 (14:54 +0100)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js
wcfsetup/install/files/js/WCF.Message.js

index 0ed9cf7e67e3df2846d0770b85eb215e98e8e05b..2a87d6e61cb4c128cea342ccbf8b29298cb730f9 100644 (file)
@@ -599,6 +599,32 @@ RedactorPlugins.wutil = function() {
                        this.wutil.saveSelection();
                },
                
+               /**
+                * Inserting block-level elements into other blocks or inline elements can mess up the entire DOM,
+                * this method tries to find the best insert location nearby.
+                */
+               adjustSelectionForBlockElement: function() {
+                       if (document.activeElement !== this.$editor[0]) {
+                               this.wutil.restoreSelection();
+                       }
+                       
+                       if (getSelection().getRangeAt(0).collapsed) {
+                               var $startContainer = getSelection().getRangeAt(0).startContainer;
+                               if ($startContainer.nodeType === Node.TEXT_NODE && $startContainer.textContent === '\u200b' && $startContainer.parentElement.tagName === 'P' && $startContainer.parentElement.parentElement === this.$editor[0]) {
+                                       // caret position is fine
+                                       
+                                       return;
+                               }
+                               else {
+                                       // walk tree up until we find a direct children of the editor and place the caret afterwards
+                                       var $insertAfter = $($startContainer).parentsUntil(this.$editor[0]).last();
+                                       var $p = $('<p><br></p>').insertAfter($insertAfter);
+                                       
+                                       this.caret.setEnd($p);
+                               }
+                       }
+               },
+               
                /**
                 * Returns true if current selection is just a caret or false if selection spans content.
                 * 
index 0500ebbdbd365007d4643c16d05c192a4873e14a..8454828b308e2080e7a2123022a47b000bec80b9 100644 (file)
@@ -834,7 +834,7 @@ WCF.Message.QuickReply = Class.extend({
                        }
                        
                        if ($insertQuote) {
-                               this._messageField.redactor('wutil.selectionEndOfEditor');
+                               this._messageField.redactor('wutil.adjustSelectionForBlockElement');
                                this._messageField.redactor('wbbcode.insertQuoteBBCode', data.quote.username, data.quote.link, data.quote.text, data.quote.text);
                                
                                // scroll to editor