Force a whitespace before and after inserted smileys
authorAlexander Ebert <ebert@woltlab.com>
Mon, 7 Dec 2020 16:27:54 +0000 (17:27 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 7 Dec 2020 16:27:54 +0000 (17:27 +0100)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSmiley.js

index 62a6815fb7d781d3d0e30080270763cebe776490..08d505c26563491af1b8aa37d60a800a7fcf36f9 100644 (file)
@@ -29,11 +29,22 @@ $.Redactor.prototype.WoltLabSmiley = function() {
                        smiley = elById(id);
                        smiley.removeAttribute('id');
                        
-                       // Safari does not properly update the caret position on insert
-                       this.caret.after(smiley);
+                       smiley.parentNode.insertBefore(document.createTextNode(" "), smiley);
                        
+                       const whitespace = document.createTextNode(" ");
+                       smiley.parentNode.insertBefore(whitespace, smiley.nextSibling);
+                       
+                       // Replace the image with itself to forcefully invalidate any references.
                        //noinspection SillyAssignmentJS
                        smiley.outerHTML = smiley.outerHTML;
+
+                       const selection = window.getSelection();
+                       const range = document.createRange();
+                       range.selectNode(whitespace);
+                       range.collapse(false);
+
+                       selection.removeAllRanges();
+                       selection.addRange(range);
                        
                        // force-save the caret position
                        this.WoltLabCaret.forceSelectionSave();