Internally storing the implicit range instead of altering it
authorAlexander Ebert <ebert@woltlab.com>
Sat, 17 Jan 2015 11:47:40 +0000 (12:47 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 17 Jan 2015 11:47:40 +0000 (12:47 +0100)
The previous approach provided the actual range in effect which was great if you want to work with the real one. The only downside is that the editor itself relies on the flawed range and changing it into the real one does break stuff.

wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js

index c35169bac63b6be8ca8dc57dc5c57b38af162c9a..e6b04678e71154e7864a1123c868a6390917ebe7 100644 (file)
@@ -1450,9 +1450,10 @@ RedactorPlugins.wbbcode = function() {
                                                        }
                                                }
                                                else {
-                                                       var $scope = current;
+                                                       var $range = (this.selection.implicitRange === null) ? this.range : this.selection.implicitRange;
+                                                       var $scope = $range.startContainer;
                                                        if ($scope.nodeType === Node.TEXT_NODE) {
-                                                               if (this.range.startOffset === 0 || (this.range.startOffset === 1 && $scope.textContent === '\u200b')) {
+                                                               if ($range.startOffset === 0 || ($range.startOffset === 1 && $scope.textContent === '\u200b')) {
                                                                        if (!$scope.previousSibling) {
                                                                                $scope = $scope.parentElement;
                                                                        }
index 51007c899f4ac5bf42a7eefabbbd4011719cfef8..c8737b9d7787f925a72c9529f3c4a48e3fea0696 100644 (file)
@@ -17,6 +17,8 @@ if (!RedactorPlugins) var RedactorPlugins = {};
 RedactorPlugins.wmonkeypatch = function() {
        "use strict";
        
+       var $rangeBeforeRemoveMarker = null;
+       
        return {
                /**
                 * Initializes the RedactorPlugins.wmonkeypatch plugin.
@@ -868,6 +870,8 @@ RedactorPlugins.wmonkeypatch = function() {
                 *  - remove superflous empty text nodes caused by the selection markers (#2083)
                 */
                selection: function() {
+                       this.selection.implicitRange = null;
+                       
                        var $removeEmptyTextNodes = (function(index, marker) {
                                var $nextSibling = marker.nextSibling;
                                if ($nextSibling !== null && $nextSibling.nodeType === Node.TEXT_NODE && $nextSibling.length === 0) {
@@ -882,7 +886,12 @@ RedactorPlugins.wmonkeypatch = function() {
                                $(marker).remove();
                                
                                if ($node !== null) {
-                                       this.caret.set($node, $node.length, $node, $node.length);
+                                       this.selection.implicitRange = document.createRange();
+                                       this.selection.implicitRange.setStart($node, $node.length);
+                                       this.selection.implicitRange.setEnd($node, $node.length);
+                               }
+                               else {
+                                       this.selection.implicitRange = null;
                                }
                        }).bind(this);
                        
index 4e8fb64fa691740a5ef8eea6d21df8ee95ae45e2..1aa841b9ac05b234bf5adcc06c2a311a0ffc88bd 100644 (file)
@@ -60,13 +60,18 @@ RedactorPlugins.wutil = function() {
                
                /**
                 * Saves current caret position.
+                * 
+                * @param       boolean         discardSavedIfEmpty
                 */
-               saveSelection: function() {
+               saveSelection: function(discardSavedIfEmpty) {
                        var $selection = getSelection();
                        
                        if ($selection.rangeCount) {
                                this.wutil._range = $selection.getRangeAt(0);
                        }
+                       else if (discardSavedIfEmpty) {
+                               this.wutil._range = null;
+                       }
                },
                
                /**
@@ -90,7 +95,16 @@ RedactorPlugins.wutil = function() {
                 * Clears the current selection.
                 */
                clearSelection: function() {
-                       this._wutil.range = null;
+                       this.wutil._range = null;
+               },
+               
+               /**
+                * Returns stored selection or null.
+                * 
+                * @return      Range
+                */
+               getSelection: function() {
+                       return this.wutil._range;
                },
                
                /**