Fixed caret management when inserting images
authorAlexander Ebert <ebert@woltlab.com>
Mon, 4 Sep 2017 12:47:38 +0000 (14:47 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 4 Sep 2017 12:47:38 +0000 (14:47 +0200)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabInsert.js

index 1206b2eb1af2308ef89359b0ec7d29e3b984ef6c..55a948cd61c63b4e06c1891abbfe510671f84e3b 100644 (file)
@@ -9,17 +9,22 @@ $.Redactor.prototype.WoltLabInsert = function() {
                        this.insert.html = (function (html, data) {
                                if (callback) callback = callback();
                                
+                               var selection = window.getSelection();
+                               if (selection.rangeCount && selection.anchorNode.nodeName === 'IMG') {
+                                       this.caret.after(selection.anchorNode);
+                               }
+                               
                                this.placeholder.hide();
                                this.core.editor().focus();
                                
                                // Firefox may have an incorrect selection if pasting into the editor using the contextual menu
                                if (this.detect.isFirefox()) {
-                                       var selection = this.selection.get();
-                                       if (selection.anchorNode.closest('.redactor-layer') === null) {
+                                       var anchorNode = (selection.anchorNode.nodeType === Node.TEXT_NODE) ? selection.anchorNode.parentNode : selection.anchorNode;
+                                       if (anchorNode.closest('.redactor-layer') === null) {
                                                this.selection.restore();
                                                
-                                               selection = this.selection.get();
-                                               if (selection.anchorNode.closest('.redactor-layer') === null) {
+                                               anchorNode = (selection.anchorNode.nodeType === Node.TEXT_NODE) ? selection.anchorNode.parentNode : selection.anchorNode;
+                                               if (anchorNode.closest('.redactor-layer') === null) {
                                                        this.WoltLabCaret.endOfEditor();
                                                        this.selection.save();
                                                }
@@ -52,6 +57,10 @@ $.Redactor.prototype.WoltLabInsert = function() {
                                                elRemove(block);
                                        }
                                }
+                               
+                               if (selection.rangeCount && selection.anchorNode.nodeName === 'IMG') {
+                                       this.caret.after(selection.anchorNode);
+                               }
                        }).bind(this);
                        
                        var mpText = this.insert.text;