From 624172350ccbf95797d30bb1d262b649ef2ed62c Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 11 Sep 2017 19:00:30 +0200 Subject: [PATCH] Improved backspace behavior for images in Firefox --- .../redactor2/plugins/WoltLabKeydown.js | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabKeydown.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabKeydown.js index 3b5ff3740e..ebd24db79b 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabKeydown.js +++ b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabKeydown.js @@ -5,8 +5,46 @@ $.Redactor.prototype.WoltLabKeydown = function() { return { init: function () { + var selection = window.getSelection(); + var mpInit = this.keydown.init; this.keydown.init = (function (e) { + var node; + + // remove empty whitespaces in front of an when backspacing in Firefox + if (this.detect.isFirefox() && selection.isCollapsed && e.which === this.keyCode.BACKSPACE) { + node = selection.anchorNode; + if (node.nodeType === Node.ELEMENT_NODE && selection.anchorOffset > 0) { + node = node.childNodes[selection.anchorOffset]; + } + + if (node.nodeType === Node.TEXT_NODE && node.textContent === '\u200B') { + var emptyNodes = []; + var sibling = node; + while (sibling = sibling.previousSibling) { + if (sibling.nodeType === Node.ELEMENT_NODE) { + if (sibling.nodeName !== 'IMG') emptyNodes = []; + + break; + } + else if (sibling.nodeType === Node.TEXT_NODE) { + var text = sibling.textContent; + if (text === '' || text === '\u200B') { + emptyNodes.push(sibling); + } + else { + emptyNodes = []; + break; + } + } + } + + if (emptyNodes.length) { + emptyNodes.forEach(elRemove); + } + } + } + var returnValue = mpInit.call(this, e); if (returnValue !== false && !e.originalEvent.defaultPrevented) { @@ -14,7 +52,6 @@ $.Redactor.prototype.WoltLabKeydown = function() { // 39 == right if (e.which === 39 && !e.ctrlKey && !e.shiftKey && !e.metaKey && !e.altKey) { - var selection = window.getSelection(); if (!selection.isCollapsed) { return; } @@ -33,7 +70,7 @@ $.Redactor.prototype.WoltLabKeydown = function() { // check if there is absolutely nothing afterwards var isAtTheVeryEnd = true; - var node = parent; + node = parent; while (node && node !== this.core.editor()[0]) { if (node.nextSibling !== null) { // strip empty text nodes -- 2.20.1