Added a work-around for incorrect selection restore
authorAlexander Ebert <ebert@woltlab.com>
Fri, 6 Oct 2017 16:34:37 +0000 (18:34 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 6 Oct 2017 16:34:37 +0000 (18:34 +0200)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js

index b77aebc6aadbb3a934d7fdd7b2c3e19dba244538..6d50f4d6be55f0c904cc64b6e17db0fb31af289a 100644 (file)
@@ -65,6 +65,68 @@ $.Redactor.prototype.WoltLabCaret = function() {
                        this.$editor[0].addEventListener(WCF_CLICK_EVENT, this.WoltLabCaret._handleEditorClick.bind(this));
                        
                        this.WoltLabCaret._initInternalRange();
+                       
+                       var mpSaveInstant = this.selection.saveInstant;
+                       this.selection.saveInstant = (function() {
+                               mpSaveInstant.call(this);
+                               
+                               this.saved.isAtNodeStart = false;
+                               
+                               var selection = window.getSelection();
+                               if (selection.rangeCount && !selection.isCollapsed) {
+                                       var range = selection.getRangeAt(0);
+                                       if (range.startContainer.nodeType === Node.TEXT_NODE && range.startOffset === 0) {
+                                               this.saved.isAtNodeStart = true;
+                                       }
+                               }
+                       }).bind(this);
+                       
+                       var mpRestoreInstant = this.selection.restoreInstant;
+                       this.selection.restoreInstant = (function(saved) {
+                               if (typeof saved === 'undefined' && !this.saved) {
+                                       return;
+                               }
+                               
+                               var localSaved = (typeof saved !== 'undefined') ? saved : this.saved;
+                               
+                               mpRestoreInstant.call(this, saved);
+                               
+                               if (localSaved.isAtNodeStart === true) {
+                                       var selection = window.getSelection();
+                                       if (selection.rangeCount && !selection.isCollapsed) {
+                                               var range = selection.getRangeAt(0);
+                                               var start = range.startContainer;
+                                               
+                                               if (localSaved.node === start) {
+                                                       // selection is valid
+                                                       return;
+                                               }
+                                               
+                                               // find the parent <p>
+                                               while (start !== null && start.nodeName !== 'P') start = start.parentNode;
+                                               
+                                               if (start !== null) {
+                                                       // check if the next element sibling is an empty <p>
+                                                       start = start.nextElementSibling;
+                                                       if (start !== null && start.nodeName === 'P' && start.textContent.replace(/\u200B/g, '').length === 0) {
+                                                               start = start.nextElementSibling;
+                                                               
+                                                               var parent = localSaved.node;
+                                                               while (parent !== null && parent !== start) parent = parent.parentNode;
+                                                               
+                                                               if (parent === start) {
+                                                                       // force selection to start with the original start node
+                                                                       range = range.cloneRange();
+                                                                       range.setStart(localSaved.node, 0);
+                                                                       
+                                                                       selection.removeAllRanges();
+                                                                       selection.addRange(range);
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                       }).bind(this);
                },
                
                paragraphAfterBlock: function (block) {