Improved click handler
authorAlexander Ebert <ebert@woltlab.com>
Wed, 21 Jan 2015 23:23:06 +0000 (00:23 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 21 Jan 2015 23:23:06 +0000 (00:23 +0100)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js

index ec8117b516412dc4dd5f15e94fafe12c8c198232..fa3286086755305784cc81293c46b6dfa7254647 100644 (file)
@@ -123,16 +123,44 @@ RedactorPlugins.wmonkeypatch = function() {
                                }
                        }).bind(this);
                        
+                       var $editorPadding = null;
                        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 && $range.collapsed) {
                                                var $current = $range.startContainer;
+                                               
+                                               // this can occur if click occurs within the editor padding
+                                               var $offsets = this.$editor.offset();
+                                               if ($editorPadding === null) {
+                                                       $editorPadding = {
+                                                               left: this.$editor.cssAsNumber('padding-left'),
+                                                               top: this.$editor.cssAsNumber('padding-top')
+                                                       };
+                                               }
+                                               
+                                               if (event.pageY <= $offsets.top + $editorPadding.top) {
+                                                       var $firstChild = this.$editor[0].children[0];
+                                                       if ($firstChild.tagName !== 'BLOCKQUOTE' && ($firstChild.tagName !== 'DIV' || !/\bcodeBox\b/.test($firstChild.className))) {
+                                                               return;
+                                                       }
+                                               }
+                                               else {
+                                                       if (event.pageX <= $offsets.left + $editorPadding.left) {
+                                                               return;
+                                                       }
+                                                       else {
+                                                               if (event.pageX > $offsets.left + this.$editor.width()) {
+                                                                       return;
+                                                               }
+                                                       }
+                                               }
+                                               
                                                while ($current !== null && $current !== this.$editor[0]) {
                                                        if ($current.nodeType === Node.ELEMENT_NODE) {
                                                                if ($current.tagName === 'BLOCKQUOTE' || ($current.tagName === 'DIV' && /\bcodeBox\b/.test($current.className))) {
-                                                                       var $offset = $(element).offset();
+                                                                       var $offset = $($current).offset();
                                                                        if (event.pageY <= $offset.top) {
                                                                                $setCaretBeforeOrAfter($current, true);
                                                                        }