Using a more reliable way to save the caret position
authorAlexander Ebert <ebert@woltlab.com>
Mon, 24 Nov 2014 00:54:24 +0000 (01:54 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 24 Nov 2014 00:54:24 +0000 (01:54 +0100)
blur() isn't that great with Firefox because getSelection() already contains the new range and focusout is not supported.

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 e647f2bbe0ae9112868c5e2a0a564f899886fb96..a71a31884d84f93839d2e11b020519f4c788155d 100644 (file)
@@ -17,7 +17,7 @@ RedactorPlugins.wbbcode = function() {
                init: function() {
                        var $identifier = this.$textarea.wcfIdentify();
                        
-                       this.opts.initCallback = $.proxy(function() {
+                       this.opts.initCallback = (function() {
                                // use stored editor contents
                                var $content = $.trim(this.wutil.getOption('woltlab.originalValue'));
                                if ($content.length) {
@@ -30,7 +30,8 @@ RedactorPlugins.wbbcode = function() {
                                delete this.opts.woltlab.originalValue;
                                
                                $(document).trigger('resize');
-                       }, this);
+                               this.wmonkeypatch.saveSelection();
+                       }).bind(this);
                        
                        this.opts.pasteBeforeCallback = $.proxy(this.wbbcode._pasteBeforeCallback, this);
                        this.opts.pasteCallback = $.proxy(this.wbbcode._pasteCallback, this);
@@ -99,11 +100,15 @@ RedactorPlugins.wbbcode = function() {
                                        
                                        this.button.get('html').children('i').removeClass('fa-square').addClass('fa-square-o');
                                        $tooltip.text(WCF.Language.get('wcf.bbcode.button.toggleBBCode'));
+                                       
+                                       this.wmonkeypatch.saveSelection();
                                }
                        }).bind(this);
                        
                        // insert a new line if user clicked into the editor and the last children is a quote (same behavior as arrow down)
                        this.wutil.setOption('clickCallback', (function(event) {
+                               this.wmonkeypatch.saveSelection();
+                               
                                if (event.target === this.$editor[0]) {
                                        if (this.$editor[0].lastElementChild && this.$editor[0].lastElementChild.tagName === 'BLOCKQUOTE') {
                                                this.wutil.setCaretAfter($(this.$editor[0].lastElementChild));
index d3ee1b876840d9fa4256174bfec838ebf7092a09..f871ac72b2cddf2b5930fe7946f17571e462573a 100644 (file)
@@ -53,6 +53,13 @@ RedactorPlugins.wmonkeypatch = function() {
                        this.wmonkeypatch.fixWebKit();
                },
                
+               /**
+                * Saves current caret position.
+                */
+               saveSelection: function() {
+                       this.wmonkeypatch._range = getSelection().getRangeAt(0);
+               },
+               
                /**
                 * Setups event listeners and callbacks.
                 */
@@ -72,7 +79,9 @@ RedactorPlugins.wmonkeypatch = function() {
                        });
                        
                        // keyup
-                       this.wutil.setOption('keyupCallback', function(event) {
+                       this.wutil.setOption('keyupCallback', (function(event) {
+                               this.wmonkeypatch.saveSelection();
+                               
                                var $data = {
                                        cancel: false,
                                        event: event
@@ -81,7 +90,7 @@ RedactorPlugins.wmonkeypatch = function() {
                                WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'keyup_' + $identifier, $data);
                                
                                return ($data.cancel ? false : true);
-                       });
+                       }).bind(this));
                        
                        // buttons response
                        if (this.opts.activeButtons) {
@@ -91,12 +100,13 @@ RedactorPlugins.wmonkeypatch = function() {
                                this.$editor.on('keyup.redactor', $.proxy(this.keyup.init, this));
                        }
                        
-                       this.$editor.on('blur.wredactor', (function() {
+                       // blur is unreliable in Firefox, especially since 'focusout' is not available
+                       /*this.$editor.on('blur.wredactor', (function() {
                                var $selection = window.getSelection();
                                if ($selection.rangeCount) {
                                        this.wmonkeypatch._range = $selection.getRangeAt(0);
                                }
-                       }).bind(this));
+                       }).bind(this));*/
                },
                
                /**
index bbf47f3e1b69c67eaf84a05265b4626cccdd7a62..24bc91d231d6dbcdd777cca2a31754807cad26db 100644 (file)
@@ -206,6 +206,7 @@ RedactorPlugins.wutil = function() {
                reset: function() {
                        if (this.opts.visual) {
                                this.$editor.html('<p>' + this.opts.invisibleSpace + '</p>');
+                               this.wmonkeypatch.saveSelection();
                        }
                        
                        this.$textarea.val('');