From de7b9eeaa9e461df3a7e3cb8df410cee93d7d9bd Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 11 Jan 2015 01:37:55 +0100 Subject: [PATCH] Improved keydown/keyup handling --- .../js/3rdParty/redactor/plugins/wbbcode.js | 56 +++++++++++-------- .../3rdParty/redactor/plugins/wmonkeypatch.js | 16 ++++++ 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index c45679054e..58b10c5a5f 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -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(); diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js index 8aa23a4ed0..91a447ac5c 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js @@ -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. * -- 2.20.1