Improved click handler for clicks on codebox/quote margins
authorAlexander Ebert <ebert@woltlab.com>
Sat, 17 Jan 2015 23:55:12 +0000 (00:55 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 17 Jan 2015 23:55:12 +0000 (00:55 +0100)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js

index e6b04678e71154e7864a1123c868a6390917ebe7..5d23b53442140052ee18ca8191c7824cb1be29f1 100644 (file)
@@ -253,7 +253,7 @@ RedactorPlugins.wbbcode = function() {
                                
                                var $uuid = WCF.getUUID();
                                $cachedCodeListings[$uuid] = {
-                                       codeContent: codeContent.replace(/<li>/g, '').replace(/<\/li>/g, '\n'),
+                                       codeContent: codeContent.replace(/<li>/g, '').replace(/<\/li>/g, '\n').replace(/\n$/, ''),
                                        filename: $filename.replace(/['"]/g, ''),
                                        highlighter: ($highlighter === 'plain' ? '' : $highlighter),
                                        lineNumber: (lineNumber > 1 ? lineNumber : 0)
@@ -663,7 +663,7 @@ RedactorPlugins.wbbcode = function() {
                                        
                                        var $bbcode = '[code' + ($attributes.length ? '=' + $attributes : '') + ']' + listing.codeContent + '[/code]\n';
                                        
-                                       html = html.replace(new RegExp('@@@' + uuid + '@@@', 'g'), $bbcode);
+                                       html = html.replace(new RegExp('@@@' + uuid + '@@@\n?', 'g'), $bbcode);
                                });
                        }
                        
@@ -1297,7 +1297,6 @@ RedactorPlugins.wbbcode = function() {
                 * @return      string
                 */
                _pasteCallback: function(html) {
-                       console.debug(html);
                        // reduce successive <br> by one
                        //html = html.replace(/<br[^>]*>(<br[^>]*>)+/g, '$1');
                        
@@ -1989,6 +1988,7 @@ RedactorPlugins.wbbcode = function() {
                 * Ensures that there is a paragraph in front of each block-level element because you cannot click in between two of them.
                 */
                fixBlockLevelElements: function() {
+                       return;
                        var $addSpacing = (function(referenceElement, target) {
                                var $tagName = 'P';
                                
index 90624c945fecfa95c59a5840b53527fda1fe8e69..89676dafe408f6ca2b6403cc59db9fbddc7b0e1c 100644 (file)
@@ -104,23 +104,21 @@ RedactorPlugins.wmonkeypatch = function() {
                                }
                        }).bind(this));
                        
-                       var $setCaretBeforeOrAfter = (function(event, blockquote) {
-                               var $offset = $(blockquote).offset();
-                               if (event.pageY <= $offset.top) {
-                                       // set caret before
-                                       if (blockquote.previousElementSibling && blockquote.previousElementSibling.tagName === 'P') {
-                                               this.caret.setEnd(blockquote.previousElementSibling);
+                       var $setCaretBeforeOrAfter = (function(element, setBefore) {
+                               if (setBefore) {
+                                       if (element.previousElementSibling && element.previousElementSibling.tagName === 'P') {
+                                               this.caret.setEnd(element.previousElementSibling);
                                        }
                                        else {
-                                               this.wutil.setCaretBefore(blockquote);
+                                               this.wutil.setCaretBefore(element);
                                        }
                                }
                                else {
-                                       if (blockquote.nextElementSibling && blockquote.nextElementSibling.tagName === 'P') {
-                                               this.caret.setEnd(blockquote.nextElementSibling);
+                                       if (element.nextElementSibling && element.nextElementSibling.tagName === 'P') {
+                                               this.caret.setEnd(element.nextElementSibling);
                                        }
                                        else {
-                                               this.wutil.setCaretAfter(blockquote);
+                                               this.wutil.setCaretAfter(element);
                                        }
                                }
                        }).bind(this);
@@ -128,14 +126,21 @@ RedactorPlugins.wmonkeypatch = function() {
                        this.$editor.on('click.wmonkeypatch', (function(event) {
                                if (event.target === this.$editor[0]) {
                                        var $range = (window.getSelection().rangeCount) ? window.getSelection().getRangeAt(0) : null;
-                                       if ($range !== null && $range.collapsed) {
-                                               // clicking on the margin created by <blockquote> will direct the cursor inside the quote
+                                       
+                                       if ($range && $range.collapsed) {
                                                var $current = $range.startContainer;
                                                while ($current !== null && $current !== this.$editor[0]) {
                                                        if ($current.nodeType === Node.ELEMENT_NODE) {
-                                                               if ($current.tagName === 'BLOCKQUOTE') {
-                                                                       $setCaretBeforeOrAfter(event, $current);
+                                                               if ($current.tagName === 'BLOCKQUOTE' || ($current.tagName === 'DIV' && /\bcodeBox\b/.test($current.className))) {
+                                                                       var $offset = $(element).offset();
+                                                                       if (event.pageY <= $offset.top) {
+                                                                               $setCaretBeforeOrAfter($current, true);
+                                                                       }
+                                                                       else {
+                                                                               $setCaretBeforeOrAfter($current, false);
+                                                                       }
                                                                        
+                                                                       // stop processing
                                                                        return false;
                                                                }
                                                        }
@@ -143,6 +148,28 @@ RedactorPlugins.wmonkeypatch = function() {
                                                        $current = $current.parentElement;
                                                }
                                        }
+                                       
+                                       var $elements = this.$editor.children('blockquote, div.codeBox');
+                                       $elements.each(function(index, element) {
+                                               var $element = $(element);
+                                               var $offset = $element.offset();
+                                               
+                                               if (event.pageY <= $offset.top) {
+                                                       $setCaretBeforeOrAfter(element, true);
+                                                       
+                                                       return false;
+                                               }
+                                               else {
+                                                       var $height = $element.outerHeight() + (parseInt($element.css('margin-bottom'), 10) || 0);
+                                                       if ((event.pageY <= $offset.top + $height) || (index + 1) === $elements.length) {
+                                                               $setCaretBeforeOrAfter(element, false);
+                                                               
+                                                               return false;
+                                                       }
+                                               }
+                                       });
+                                       
+                                       return false;
                                }
                        }).bind(this));
                },