Improved keydown/keyup handling
authorAlexander Ebert <ebert@woltlab.com>
Sun, 11 Jan 2015 00:37:55 +0000 (01:37 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 11 Jan 2015 00:37:55 +0000 (01:37 +0100)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js

index c45679054e784dd75d3b759f953a74316afd99b0..58b10c5a5f5f7e3e8ccdbc2250c82dbf71fcd7b2 100644 (file)
@@ -1417,7 +1417,7 @@ RedactorPlugins.wbbcode = function() {
                        }
                        
                        this.selection.get();
-                       var $current = $(this.selection.getCurrent());
+                       var current = this.selection.getCurrent();
                        var $parent = this.selection.getParent();
                        $parent = ($parent) ? $($parent) : $parent;
                        var $quote = ($parent) ? $parent.closest('blockquote.quoteBox', this.$editor.get()[0]) : { length: 0 };
@@ -1426,41 +1426,55 @@ RedactorPlugins.wbbcode = function() {
                                // backspace key
                                case $.ui.keyCode.BACKSPACE:
                                        if (this.wutil.isCaret()) {
+                                               var $preventAndSelectQuote = false;
+                                               
                                                if ($quote.length) {
                                                        // check if quote is empty
                                                        var $isEmpty = true;
-                                                       $quote.children('div').each(function() {
-                                                               if ($(this).text().replace(/\u200B/, '').length) {
-                                                                       $isEmpty = false;
-                                                                       return false;
+                                                       for (var $i = 0; $i < $quote[0].children.length; $i++) {
+                                                               var $child = $quote[0].children[$i];
+                                                               if ($child.tagName === 'DIV') {
+                                                                       if ($child.textContent.replace(/\u200b/, '').length) {
+                                                                               $isEmpty = false;
+                                                                               break;
+                                                                       }
                                                                }
-                                                       });
+                                                       }
                                                        
                                                        if ($isEmpty) {
-                                                               // expand selection and prevent delete
-                                                               var $selection = window.getSelection();
-                                                               if ($selection.rangeCount) $selection.removeAllRanges();
-                                                               
-                                                               var $quoteRange = document.createRange();
-                                                               $quoteRange.selectNode($quote[0]);
-                                                               $selection.addRange($quoteRange);
-                                                               
-                                                               data.cancel = true;
+                                                               $preventAndSelectQuote = true;
                                                        }
                                                }
+                                               else if (current.previousElementSibling && current.previousElementSibling.tagName === 'BLOCKQUOTE') {
+                                                       $quote = current.previousElementSibling;
+                                                       $preventAndSelectQuote = true;
+                                               }
+                                               
+                                               if ($preventAndSelectQuote) {
+                                                       // expand selection and prevent delete
+                                                       var $selection = window.getSelection();
+                                                       if ($selection.rangeCount) $selection.removeAllRanges();
+                                                       
+                                                       var $quoteRange = document.createRange();
+                                                       $quoteRange.selectNode($quote[0] || $quote);
+                                                       $selection.addRange($quoteRange);
+                                                       
+                                                       data.cancel = true;
+                                               }
                                        }
                                break;
                                
                                // delete key
                                case $.ui.keyCode.DELETE:
-                                       if (this.wutil.isCaret()) {
-                                               if (this.wutil.isEndOfElement($current[0]) && $current.next('blockquote').length) {
+                                       if (this.wutil.isCaret() && this.wutil.isEndOfElement(current)) {
+                                               var $next = current.nextElementSibling;
+                                               if ($next && $next.tagName === 'BLOCKQUOTE') {
                                                        // expand selection and prevent delete
                                                        var $selection = window.getSelection();
                                                        if ($selection.rangeCount) $selection.removeAllRanges();
                                                        
                                                        var $quoteRange = document.createRange();
-                                                       $quoteRange.selectNode($current.next()[0]);
+                                                       $quoteRange.selectNode($next);
                                                        $selection.addRange($quoteRange);
                                                        
                                                        data.cancel = true;
@@ -1470,6 +1484,7 @@ RedactorPlugins.wbbcode = function() {
                                
                                // arrow down
                                case $.ui.keyCode.DOWN:
+                                       var $current = $(current);
                                        if ($current.next('blockquote').length) {
                                                this.caret.setStart($current.next().children('div:first'));
                                                
@@ -1513,16 +1528,13 @@ RedactorPlugins.wbbcode = function() {
                                                return;
                                        }
                                        
-                                       var $container = $current.closest('div', $quote[0]);
+                                       var $container = $(current).closest('div', $quote[0]);
                                        var $prev = $container.prev();
                                        if ($prev[0].tagName === 'DIV') {
                                                return;
                                        }
                                        else if ($prev[0].tagName === 'BLOCKQUOTE') {
-                                               // TODO
-                                               // set focus to quote text rather than the element itself
                                                return;
-                                               //this.selectionEnd($prev.find('> div > div:last'));
                                        }
                                        
                                        var $previousElement = $quote.prev();
index 8aa23a4ed00d4ded900a67b4c55adb67b70e609f..91a447ac5c573ac968a2a892941a5d3599d2c291 100644 (file)
@@ -33,6 +33,7 @@ RedactorPlugins.wmonkeypatch = function() {
                        this.wmonkeypatch.inline();
                        this.wmonkeypatch.insert();
                        this.wmonkeypatch.keydown();
+                       this.wmonkeypatch.keyup();
                        this.wmonkeypatch.link();
                        this.wmonkeypatch.modal();
                        this.wmonkeypatch.paste();
@@ -651,6 +652,21 @@ RedactorPlugins.wmonkeypatch = function() {
                        }).bind(this);
                },
                
+               /**
+                * Partially overwrites the 'keyup' module.
+                * 
+                *  - prevent divs inside a quote being replace with paragraphs
+                */
+               keyup: function() {
+                       // keyup.replaceToParagraph
+                       var $mpReplaceToParagraph = this.keyup.replaceToParagraph;
+                       this.keyup.replaceToParagraph = (function(clone) {
+                               if (this.keyup.current.tagName !== 'DIV' || this.keyup.current.parentElement.tagName !== 'BLOCKQUOTE') {
+                                       $mpReplaceToParagraph.call(this, clone);
+                               }
+                       }).bind(this);
+               },
+               
                /**
                 * Partially overwrites the 'link' module.
                 *