Hello,

In a given node form including a textarea with a ckeditor, and a file upload widget (or any other widget using AJAX calls), triggering the AJAX call (e.g. by uploading the image, or clicking "Add another item" on a multi-delta field) will remove and re-add again ckeditor.
In a node form with many widgets using AJAX, it can get quit annoying and slow down the user (on slower computers).

This behavior is triggered due to the following bit of code in misc/ajax.js which detach behaviors:

Drupal.ajax.prototype.beforeSerialize = function (element, options) {
  // Allow detaching behaviors to update field values before collecting them.
  // This is only needed when field values are added to the POST data, so only
  // when there is a form such that this.form.ajaxSubmit() is used instead of
  // $.ajax(). When there is no form and $.ajax() is used, beforeSerialize()
  // isn't called, but don't rely on that: explicitly check this.form.
  if (this.form) {
    var settings = this.settings || Drupal.settings;
    Drupal.detachBehaviors(this.form, settings, 'serialize');
  }

Which will call the ckeditor behavior detach function, which remove the ckeditor. And then it's added back just after.

Since the main job of the ckeditor detach function is to update the textarea input for form submission, I'm not sure why the ckeditor is removed in top of that. It could stay.
Attaching a patch just below.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ndeschildre’s picture

Status: Active » Needs review
FileSize
565 bytes

Patch attached. The textarea is still updated.
But I'm pretty sure I'm missing why the actual ckeditor is removed on detach.

diff --git a/includes/ckeditor.utils.js b/includes/ckeditor.utils.js
index 0943525..e7ae9f2 100644
--- a/includes/ckeditor.utils.js
+++ b/includes/ckeditor.utils.js
@@ -275,8 +275,7 @@ window.CKEDITOR_BASEPATH = Drupal.settings.ckeditor.editor_path;
                 var ta_id=$(this).attr("id");
                 if (CKEDITOR.instances[ta_id])
                     $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
-                Drupal.ckeditorOff(ta_id);
-            }).removeClass('ckeditor-processed');
+            });
         }
     };
 })(jQuery);
mkesicki’s picture

@ndeschildre thank you for patch.
We will check this as soon as possible.

ndeschildre’s picture

Ok I updated the patch to be more 'smart', as I discovered the detach 'trigger' parameter. So now it will only unload ckeditor on detach with the 'unload'/'move' event, but no longer on the 'serialize' event (which is called in the use case I'm trying to solve).

diff --git a/includes/ckeditor.utils.js b/includes/ckeditor.utils.js
index 0943525..114e04b 100644
--- a/includes/ckeditor.utils.js
+++ b/includes/ckeditor.utils.js
@@ -270,13 +270,16 @@ window.CKEDITOR_BASEPATH = Drupal.settings.ckeditor.editor_path;
             });
         },
         detach:
-        function(context){
+        function(context, settings, trigger){
             $(context).find("textarea.ckeditor-mod.ckeditor-processed").each(function () {
                 var ta_id=$(this).attr("id");
                 if (CKEDITOR.instances[ta_id])
                     $('#'+ta_id).val(CKEDITOR.instances[ta_id].getData());
-                Drupal.ckeditorOff(ta_id);
-            }).removeClass('ckeditor-processed');
+                if(trigger != 'serialize') {
+                  Drupal.ckeditorOff(ta_id);
+                  $(this).removeClass('ckeditor-processed');
+                }
+            });
         }
     };
 })(jQuery);
mkesicki’s picture

Title: Ckeditor gets disabled/enabled on all AJAX calls » CKEditor gets disabled/enabled on all AJAX calls
FileSize
1.42 KB

I add patch that can be added to repository.
Thank you @ndeschildre , well done.
Maybe some other will test this ?

dczepierga’s picture

Status: Needs review » Fixed

Patch commited to GIT (diff)

Greetings

dczepierga’s picture

Title: CKEditor gets disabled/enabled on all AJAX calls » [D7] CKEditor gets disabled/enabled on all AJAX calls
ndeschildre’s picture

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.