Attempt to fix formatting on iOS
authorAlexander Ebert <ebert@woltlab.com>
Tue, 2 Dec 2014 13:55:08 +0000 (14:55 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 2 Dec 2014 13:55:08 +0000 (14:55 +0100)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js

index 2d5495971f15a9f9fd3ef5f7b8c740ea0df4cb1c..b553b4ad2e9f6508c59b56b25c343dd8f8d5ad9e 100644 (file)
@@ -35,6 +35,7 @@ RedactorPlugins.wmonkeypatch = function() {
                        this.wmonkeypatch.code();
                        this.wmonkeypatch.dropdown();
                        this.wmonkeypatch.image();
+                       this.wmonkeypatch.inline();
                        this.wmonkeypatch.insert();
                        this.wmonkeypatch.keydown();
                        this.wmonkeypatch.link();
@@ -64,6 +65,23 @@ RedactorPlugins.wmonkeypatch = function() {
                        }
                },
                
+               /**
+                * 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.
                 */
@@ -331,6 +349,7 @@ RedactorPlugins.wmonkeypatch = function() {
                 * Partially overwrites the 'dropdown' module.
                 * 
                 *  - emulate WCF-like dropdowns.
+                *  - save text selection on iOS (#2003)
                 */
                dropdown: function() {
                        // dropdown.build
@@ -373,6 +392,10 @@ RedactorPlugins.wmonkeypatch = function() {
                                var $dropdown = this.button.get(key).data('dropdown');
                                $fixDropdown($dropdown);
                                
+                               if ($.browser.iOS) {
+                                       this.wmonkeypatch.saveSelection();
+                               }
+                               
                                $mpShow.call(this, e, key);
                                
                                $dropdown.off('mouseover mouseout');
@@ -458,6 +481,31 @@ RedactorPlugins.wmonkeypatch = function() {
                        }).bind(this);
                },
                
+               /**
+                * Partially overwrites the 'inline' module.
+                * 
+                *  - restore the text selection on iOS (#2003)
+                */
+               inline: function() {
+                       var $mpFormat = this.inline.format;
+                       this.inline.format = (function(tag, type, value) {
+                               if ($.browser.iOS) {
+                                       this.wmonkeypatch.restoreSelection();
+                               }
+                               
+                               $mpFormat.call(this, tag, type, value);
+                       }).bind(this);
+                       
+                       var $mpRemoveStyleRule = this.inline.removeStyleRule;
+                       this.inline.removeStyleRule = (function(name) {
+                               if ($.browser.iOS) {
+                                       this.wmonkeypatch.restoreSelection();
+                               }
+                               
+                               $mpRemoveStyleRule.call(this, name);
+                       }).bind(this);
+               },
+               
                /**
                 * Partially overwrites the 'insert' module.
                 *