Avoid timeout issue in Chrome on Android when pasting
authorAlexander Ebert <ebert@woltlab.com>
Thu, 26 Oct 2017 21:22:59 +0000 (23:22 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 26 Oct 2017 21:22:59 +0000 (23:22 +0200)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabPaste.js

index ccd68afde9470ea07931032c4d2b8de651ea420e..b4a12353e8d43f8b2abb726740a6581bee93ce79 100644 (file)
@@ -1,6 +1,8 @@
 $.Redactor.prototype.WoltLabPaste = function() {
        "use strict";
        
+       var _environment = null;
+       
        return {
                init: function () {
                        var clipboardData = null;
@@ -11,6 +13,41 @@ $.Redactor.prototype.WoltLabPaste = function() {
                        
                        var pastedHtml = null, pastedPlainText = null;
                        
+                       // special `init()` implementation for Chrome on Android which seems to have
+                       // some serious issues with `setTimeout()` during a `paste` event
+                       var mpInitChromeOnAndroid = (function (e) {
+                               this.rtePaste = true;
+                               var pre = !!(this.opts.type === 'pre' || this.utils.isCurrentOrParent('pre'));
+                               
+                               this.utils.saveScroll();
+                               this.selection.save();
+                               this.paste.createPasteBox(pre);
+                               
+                               var html = this.paste.getPasteBoxCode(pre);
+                               
+                               // buffer
+                               this.buffer.set();
+                               this.selection.restore();
+                               
+                               this.utils.restoreScroll();
+                               
+                               // paste info
+                               var data = this.clean.getCurrentType(html);
+                               
+                               // clean
+                               html = this.clean.onPaste(html, data);
+                               
+                               // callback
+                               var returned = this.core.callback('paste', html);
+                               html = (typeof returned === 'undefined') ? html : returned;
+                               
+                               this.paste.insert(html, data);
+                               this.rtePaste = false;
+                               
+                               // clean pre breaklines
+                               if (pre) this.clean.cleanPre();
+                       }).bind(this);
+                       
                        var mpInit = this.paste.init;
                        this.paste.init = (function (e) {
                                pastedPlainText = pastedHtml = null;
@@ -71,20 +108,18 @@ $.Redactor.prototype.WoltLabPaste = function() {
                                        e.preventDefault();
                                }
                                
-                               mpInit.call(this, e);
-                       }).bind(this);
-                       
-                       var mpCreatePasteBox = this.paste.createPasteBox;
-                       this.paste.createPasteBox = (function (pre) {
-                               if (pastedHtml === null && pastedPlainText === null) {
-                                       mpCreatePasteBox.call(this, pre);
+                               if (_environment.platform() === 'android' && _environment.browser() === 'chrome') {
+                                       mpInitChromeOnAndroid(e);
+                               }
+                               else {
+                                       mpInit.call(this, e);
                                }
-                               
-                               // do nothing
                        }).bind(this);
                        
                        require(['Environment'], (function(Environment) {
-                               if (Environment.platform() === 'ios' && Environment.browser() === 'safari') {
+                               _environment = Environment;
+                               
+                               if (_environment.platform() === 'ios') {
                                        var mpAppendPasteBox = this.paste.appendPasteBox;
                                        this.paste.appendPasteBox = (function() {
                                                // iOS doesn't like `position: fixed` and font-sizes below 16px that much
@@ -103,6 +138,15 @@ $.Redactor.prototype.WoltLabPaste = function() {
                                }
                        }).bind(this));
                        
+                       var mpCreatePasteBox = this.paste.createPasteBox;
+                       this.paste.createPasteBox = (function (pre) {
+                               if (pastedHtml === null && pastedPlainText === null) {
+                                       mpCreatePasteBox.call(this, pre);
+                               }
+                               
+                               // do nothing
+                       }).bind(this);
+                       
                        var mpGetPasteBoxCode = this.paste.getPasteBoxCode;
                        this.paste.getPasteBoxCode = (function (pre) {
                                if (pastedHtml !== null || pastedPlainText !== null) {