Moved wmonkeypatch.*Selection -> wutil, fixed selection saving
authorAlexander Ebert <ebert@woltlab.com>
Thu, 4 Dec 2014 17:41:25 +0000 (18:41 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 4 Dec 2014 17:41:25 +0000 (18:41 +0100)
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 66d8d8f0a8b625d259b22587ae6a1fb5207af377..69f92204c6416c752c0f6663a2a2a39cbee83a1a 100644 (file)
@@ -30,7 +30,7 @@ RedactorPlugins.wbbcode = function() {
                                delete this.opts.woltlab.originalValue;
                                
                                $(document).trigger('resize');
-                               this.wmonkeypatch.saveSelection();
+                               this.wutil.saveSelection();
                        }).bind(this);
                        
                        this.opts.pasteBeforeCallback = $.proxy(this.wbbcode._pasteBeforeCallback, this);
@@ -101,13 +101,13 @@ 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();
+                                       this.wutil.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();
+                               this.wutil.saveSelection();
                                
                                if (event.target === this.$editor[0]) {
                                        if (this.$editor[0].lastElementChild && this.$editor[0].lastElementChild.tagName === 'BLOCKQUOTE') {
@@ -1508,7 +1508,7 @@ RedactorPlugins.wbbcode = function() {
                                this.wutil.insertAtCaret($openTag + plainText + $closingTag);
                        }
                        
-                       this.wmonkeypatch.saveSelection();
+                       this.wutil.saveSelection();
                        
                        return $quote;
                },
index b553b4ad2e9f6508c59b56b25c343dd8f8d5ad9e..ce8704d823de8e2f9670b87b85badceeb2ed5f5e 100644 (file)
@@ -18,12 +18,6 @@ RedactorPlugins.wmonkeypatch = function() {
        "use strict";
        
        return {
-               /**
-                * saved selection range
-                * @var range
-                */
-               _range: null,
-               
                /**
                 * Initializes the RedactorPlugins.wmonkeypatch plugin.
                 */
@@ -54,34 +48,6 @@ RedactorPlugins.wmonkeypatch = function() {
                        this.wmonkeypatch.fixWebKit();
                },
                
-               /**
-                * Saves current caret position.
-                */
-               saveSelection: function() {
-                       var $selection = getSelection();
-                       
-                       if ($selection.rangeCount) {
-                               this.wmonkeypatch._range = $selection.getRangeAt(0);
-                       }
-               },
-               
-               /**
-                * Restores saved selection.
-                */
-               restoreSelection: function() {
-                       if (document.activeElement !== this.$editor[0]) {
-                               this.$editor.focus();
-                       }
-                       
-                       if (this.wmonkeypatch._range !== null) {
-                               var $selection = window.getSelection();
-                               $selection.removeAllRanges();
-                               $selection.addRange(this.wmonkeypatch._range);
-                               
-                               this.wmonkeypatch._range = null;
-                       }
-               },
-               
                /**
                 * Setups event listeners and callbacks.
                 */
@@ -102,7 +68,7 @@ RedactorPlugins.wmonkeypatch = function() {
                        
                        // keyup
                        this.wutil.setOption('keyupCallback', (function(event) {
-                               this.wmonkeypatch.saveSelection();
+                               this.wutil.saveSelection();
                                
                                var $data = {
                                        cancel: false,
@@ -121,14 +87,6 @@ RedactorPlugins.wmonkeypatch = function() {
                                this.$editor.on('mouseup.redactor keyup.redactor focus.redactor', $.proxy(this.observe.buttons, this));
                                this.$editor.on('keyup.redactor', $.proxy(this.keyup.init, this));
                        }
-                       
-                       // 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));*/
                },
                
                /**
@@ -393,7 +351,7 @@ RedactorPlugins.wmonkeypatch = function() {
                                $fixDropdown($dropdown);
                                
                                if ($.browser.iOS) {
-                                       this.wmonkeypatch.saveSelection();
+                                       this.wutil.saveSelection();
                                }
                                
                                $mpShow.call(this, e, key);
@@ -534,15 +492,7 @@ RedactorPlugins.wmonkeypatch = function() {
                                }
                                else {
                                        if (document.activeElement !== this.$editor[0]) {
-                                               this.$editor.focus();
-                                               
-                                               if (this.wmonkeypatch._range) {
-                                                       var $selection = window.getSelection();
-                                                       $selection.removeAllRanges();
-                                                       $selection.addRange(this.wmonkeypatch._range);
-                                                       
-                                                       this.wmonkeypatch._range = null;
-                                               }
+                                               this.wutil.restoreSelection();
                                        }
                                }
                        }).bind(this);
@@ -568,7 +518,7 @@ RedactorPlugins.wmonkeypatch = function() {
                                });
                                
                                if ($removedSpan) {
-                                       this.wmonkeypatch.saveSelection();
+                                       this.wutil.saveSelection();
                                }
                        }).bind(this);
                        
@@ -579,7 +529,7 @@ RedactorPlugins.wmonkeypatch = function() {
                                
                                $mpHtml.call(this, html, clean);
                                
-                               this.wmonkeypatch.saveSelection();
+                               this.wutil.saveSelection();
                                
                                if ($isWebKit) {
                                        setTimeout(function() {
index 2baf5d6712fcd9c53b74837176d1c03aab877d0d..89905b823d3a7252ee06633339db63a8f237fb37 100644 (file)
@@ -20,6 +20,12 @@ RedactorPlugins.wutil = function() {
                 */
                _autosaveWorker: null,
                
+               /**
+                * saved selection range
+                * @var range
+                */
+               _range: null,
+               
                /**
                 * Initializes the RedactorPlugins.wutil plugin.
                 */
@@ -50,6 +56,34 @@ RedactorPlugins.wutil = function() {
                        }).bind(this);
                },
                
+               /**
+                * Saves current caret position.
+                */
+               saveSelection: function() {
+                       var $selection = getSelection();
+                       
+                       if ($selection.rangeCount) {
+                               this.wutil._range = $selection.getRangeAt(0);
+                       }
+               },
+               
+               /**
+                * Restores saved selection.
+                */
+               restoreSelection: function() {
+                       if (document.activeElement !== this.$editor[0]) {
+                               this.$editor.focus();
+                       }
+                       
+                       if (this.wutil._range !== null) {
+                               var $selection = window.getSelection();
+                               $selection.removeAllRanges();
+                               $selection.addRange(this.wutil._range);
+                               
+                               this.wutil._range = null;
+                       }
+               },
+               
                /**
                 * Allows inserting of text contents in Redactor's source area.
                 * 
@@ -206,7 +240,7 @@ RedactorPlugins.wutil = function() {
                reset: function() {
                        if (this.opts.visual) {
                                this.$editor.html('<p>' + this.opts.invisibleSpace + '</p>');
-                               this.wmonkeypatch.saveSelection();
+                               this.wutil.saveSelection();
                        }
                        
                        this.$textarea.val('');
@@ -549,6 +583,8 @@ RedactorPlugins.wutil = function() {
                        else {
                                this.wutil.setCaretAfter($lastChild);
                        }
+                       
+                       this.wutil.saveSelection();
                },
                
                /**