Added CKEditor support in autosave.js file, tested and it worked fine for me. Maybe someone else will find it useful.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

CB’s picture

Awesome, thank you.

jthinaka’s picture

It didn't work for me as it said that Drupal.settings.wysiwyg.triggers doesn't exist. Any ideas?

cedewey’s picture

Status: Active » Needs review
nmosesgo’s picture

Not certain how to properly apply this patch. I'm in the autosave.js file. Do i simply replace the tinymce portion, or do I append it to the end? I tried replacing the tinymce section, and it doesn't work for me.
Here's how my code looks.
Thanks!

(function ($) {

var showingRestoreCommand;

Drupal.behaviors.autosave = {

  attach: function (context, settings) {
    var autosaveSettings;

    if ($('#autosave-status').size() == 0) {
     // Add a div for us to put messages in.
      $('body').append('<div id="autosave-status"><span id="status"></span></div>');
    }

    autosaveSettings = settings.autosave;

    $('#' + autosaveSettings.formid).not('.autosave-processed').addClass('autosave-processed').autosave({
      interval: autosaveSettings.period * 1000, // Time in ms
      url: autosaveSettings.url,
      setup: function (e, o) {
        var ignoreLink, restoreLink, callbackPath;

        // If there is a saved form for this user, let him know so he can reload it
        // if desired.
        if (autosaveSettings.savedTimestamp) {
          showingRestoreCommand = true;

          ignoreLink = $('<a>').attr('href', '#').attr('title', Drupal.t('Ignore/Delete saved form')).html(Drupal.t('Ignore')).click(function (e) {
            Drupal.behaviors.autosave.hideMessage();
            return false;
          });

          callbackPath = Drupal.settings.basePath + 'autosave/restore/' + autosaveSettings.formid + '/' + autosaveSettings.savedTimestamp + '/' + autosaveSettings.formToken;
          restoreLink = $('<a>').attr('href', callbackPath).addClass('use-ajax').attr('title', Drupal.t('Restore saved form')).html(Drupal.t('Restore')).click(function (e) {
            Drupal.behaviors.autosave.hideMessage();
          });

          Drupal.behaviors.autosave.displayMessage(Drupal.t('This form was autosaved on ' + autosaveSettings.savedDate), {
            // Show the message for 30 seconds, or hide it when the user starts
            // editing the form.
            timeout: 30000,
            extra: $('<span id="operations">').append(ignoreLink).append(' - ').append(restoreLink)
          });
        }

        // Wire up Ckeditor to autosave.
        if (typeof(Ckeditor) !== 'undefined') {
          setInterval(function() {
            var triggers = Drupal.settings.wysiwyg.triggers;
            var id;
            var field;
            for (id in triggers) {
              field = triggers[id].field;
             $('#' + field).val( CKEDITOR.instances[field].getData() );
            }
          }, autosaveSettings.period * 1000);
        }

      },
	  
      save: function (e, o) {
        Drupal.behaviors.autosave.displayMessage(Drupal.t('Form autosaved.'));
      },
      dirty: function (e, o) {
        if (showingRestoreCommand) {
          Drupal.behaviors.autosave.hideMessage();
        }
      }
    });
  },

  hideMessage: function() {
    $('#autosave-status').fadeOut('slow');
  },

  displayMessage: function(message, settings) {
    settings = settings || {};
    settings.timeout = settings.timeout || 3000;
    settings.extra = settings.extra || '';
    //settings = $.extend({}, {timeout: 3000, extra: ''}, settings);
    var status = $('#autosave-status');
    status.empty().append('<span id="status">' + message + '</span>');
    if (settings.extra) {
      status.append(settings.extra);
    }
    Drupal.attachBehaviors(status);

    $('#autosave-status').slideDown();
    setTimeout(Drupal.behaviors.autosave.hideMessage, settings.timeout);
  }
}

})(jQuery);
Nikita Petrov’s picture

I'm appended block with "if (typeof(CKEDITOR) !== "undefined") {}" after section with " if (typeof(tinymce) !== 'undefined') {}" and all works fine for me. Thanks!

haydeniv’s picture

@jthinaka If you are like me and not using the WYSIWYG API and just have CKEditor module installed directly the patch will not work instead you need to use this for the patch:

      	// Wire up Ckeditor to autosave.
      	if (typeof(CKEDITOR) !== "undefined") {
          setInterval(function() {
            var triggers = Drupal.settings.ckeditor.elements;
            var id;

            for (id in triggers) {
               $('#' + id).val( CKEDITOR.instances[id].getData() );
            }
          }, autosaveSettings.period * 1000);
      	}
eugene.ilyin’s picture

FileSize
786 bytes

Little fix in your patch to avoid js errors with undefined keys.

Patribus’s picture

Hello. Is there a similar patch for TinyMCE?

Thanks
Pat

haydeniv’s picture

@osmanedosbatuque you should probably open a separate issue as this issue deals specifically with CKEditor.

Patribus’s picture

ok, sorry, i just thought, the approach would be similar...

thnaks , cheers

Deciphered’s picture

So the patch at #7 doesn't apply, but then there's also the issue of how it uses CKEDITOR, as it appears that it's for CKEDITOR's native module not for CKEDITOR via Wysiwyg.

I don't want to get into that argument, I'll leave that for someone else, but my client needs support for CKEDITOR via Wysiwyg, so find patch attached.

haydeniv’s picture

Status: Needs review » Needs work

I would think that there should be some way we could detect whether this was loaded using WYSIWYG or CKEditor module.

xamount’s picture

I tried the patch at the very top and I also tried the patch at #11. Both did not work.

I'm using the CKEDITOR via Wysiwyg.

Drupal 7.18
CKEditor 3.5.0.6260 (via Wysiwyg)

@Deciphered: how did you get this to work exactly? I applied your patch and the ckeditor field still was not saved via autosave.

The steps I did:

1. Applied your patch.

2. Opened a node that has autosave enabled. Made 2 changes to 2 different fields. One ckeditor field and the node title. (I have the autosave timer set to 10s, so I made sure to make these changes withing 10s).

3. Went to a different page without saving the node.

4. Went back to the same node and hit restore from the autosave prompt.

5. Only the node title field was restored.

Any suggestions welcomed. Thanks

Deciphered’s picture

@xamount,

This was one of three patches required for me, I can't remember the specifics now as I was reviewing the functionality on behalf of a client and it was determined that while once three patches where applied it did somewhat work, there where too many issues with the functionality and it would be best not to go ahead with it for the time being.

pjcdawkins’s picture

Assigned: maris.abols » Unassigned
FileSize
704 bytes

I opened #1894354: Support CKEditor and added a patch before I found this issue. So you might want to look at my patch which works for me, re-attached here.

pjcdawkins’s picture

Status: Needs work » Needs review
freblasty’s picture

Patch in #15 works for me but changed editorElement.val(editor.getData()); to editor.updateElement();

freblasty’s picture

Patch based upon #15 with change mentioned in #17.

Deciphered’s picture

@freblasty

Please include an interdiff.txt with your patch if your patch is a re-roll of another patch, it helps others review the patch alot easier.

Dave Reid’s picture

Status: Needs review » Fixed

Tested and confirmed #18 works at a basic level with CKeditor 3 and WYSIWYG module. Committed to 7.x-2.x. Let's use follow-ups for anything else remaining like #1965880: CKEditor v4 not autosaving.

http://drupalcode.org/project/autosave.git/commit/72bc7d3

Status: Fixed » Closed (fixed)

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