From 28d83f708dd62b477a443f73dbfd59311b2674cc Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 20 Feb 2015 23:44:50 +0100 Subject: [PATCH] Fixed inserting smileys breaking out of current formatting --- .../js/3rdParty/redactor/plugins/wbbcode.js | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index f91083bca7..1335db8c4a 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -199,7 +199,45 @@ RedactorPlugins.wbbcode = function() { } if (this.opts.visual) { - this.insert.html(' ' + smileyCode + ' ', false); + var $parentBefore = null; + if (window.getSelection().rangeCount && window.getSelection().getRangeAt(0).collapsed) { + $parentBefore = window.getSelection().getRangeAt(0).startContainer; + if ($parentBefore.nodeType === Node.TEXT_NODE) { + $parentBefore = $parentBefore.parentElement; + } + + if (!this.utils.isRedactorParent($parentBefore)) { + $parentBefore = null; + } + } + + this.insert.html('' + smileyCode + '', false); + + var $smiley = document.getElementById('redactorSmiley'); + if ($parentBefore !== null) { + var $currentParent = window.getSelection().getRangeAt(0).startContainer; + if ($currentParent.nodeType === Node.TEXT_NODE) { + $currentParent = $currentParent.parentElement; + } + + // smiley has been inserted outside the original caret parent, move + if ($parentBefore !== $currentParent) { + $parentBefore.appendChild($smiley); + } + } + + // add spaces as paddings + var $parent = $smiley.parentElement; + var $node = document.createTextNode('\u00A0'); + $parent.insertBefore($node, $smiley); + + var $node = document.createTextNode('\u00A0'); + if ($parent.lastChild === $smiley) { + $parent.appendChild($node); + } + else { + $parent.insertBefore($node, $smiley.nextSibling); + } } else { this.wutil.insertAtCaret(' ' + smileyCode + ' '); -- 2.20.1