Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CKeditor support #62

Open
ghost opened this issue Sep 17, 2014 · 10 comments
Open

CKeditor support #62

ghost opened this issue Sep 17, 2014 · 10 comments

Comments

@ghost
Copy link

ghost commented Sep 17, 2014

Hi,

You can add CKeditor support. Just simply add following code into your solution.

checkForm()

(...)
// ckeditor
if (typeof (evt.editor) !== 'undefined') {
if (CKEDITOR.instances[evt.editor.name].checkDirty()) {
setDirtyStatus($("#" + evt.editor.name).parents('form'), true);
return;
}
}

(...)

initForm()

(...)
//ckeditor
if (typeof(CKEDITOR) !== 'undefined') {
CKEDITOR.on('instanceReady', function () {
for (var instanceName in CKEDITOR.instances) {
CKEDITOR.instances[instanceName].removeListener("change", checkForm);
CKEDITOR.instances[instanceName].on("change", checkForm);
CKEDITOR.instances[instanceName].removeListener("keyup", checkForm);
CKEDITOR.instances[instanceName].document.on("keyup", checkForm);
}
});
}
(...)

@paulofreitas
Copy link

+1, that would be awesome! 👍

@lagseeing
Copy link

You should make a pull request.

@codedance
Copy link
Owner

That's good information. Would you be willing to submit a demo page to the repo? I'm thinking an example HTML file located at:

~demo/integration/ckeditor-ays-example.html

Just a simple page that demonstrates this integration I'm sure would be a great help to others.

@codedance codedance added this to the 1.10 release milestone Oct 23, 2014
tyama added a commit to tyama/jquery.AreYouSure that referenced this issue Oct 9, 2015
@robme
Copy link

robme commented Oct 22, 2015

I tried this and it doesn't work for changes made to the text, only changes made via the toolbar.

I've narrowed down the problem to a race condition. The event for the form fires, and sets dirty to false. And then the event for CKEditor fires and sets dirty to true. If it happens the other way round then it works (such as while debugging), but without a breakpoint set, I found it was setting dirty and then immediately unsetting it.

@robme
Copy link

robme commented Oct 22, 2015

I got it working. I was using tyama's commit above, which didn't work. You need to put the initForm code at the end, not above, and tweak it slightly. Here is my initForm:

var initForm = function($form) {
  var fields = $form.find(settings.fieldSelector);
  $(fields).each(function() { storeOrigValue($(this)); });
  $(fields).unbind(settings.fieldEvents, checkForm);
  $(fields).bind(settings.fieldEvents, checkForm);
  $form.data("ays-orig-field-count", $(fields).length);
  setDirtyStatus($form, false);

  //CKEditor
  if (typeof(CKEDITOR) !== 'undefined') {
    CKEDITOR.on('instanceReady', function () {
      for (var instanceName in CKEDITOR.instances) {
        $('#' + instanceName).unbind(settings.fieldEvents, checkForm);
        CKEDITOR.instances[instanceName].on("change", checkForm);
        CKEDITOR.instances[instanceName].document.on("keyup", checkForm);
      }
    });
  }
};

Explanation: The CKEditor part has to come afterwards because we want to unbind the events added. And in order to unbind them, I just use the instanceName as a selector and call it in the same way they were bound above. This means the change/keyup events are not firing on the hidden textarea (the contents of which does not change), and instead, CKEditor's events will fire.

@jfcartier
Copy link

This worked for me :

  function beforeUnload(evt) {
    for (var name in CKEDITOR.instances) {
      if (CKEDITOR.instances[name].checkDirty())
        return evt.returnValue = "messageUnsaved";
    }
  }

  if (window.addEventListener)
    window.addEventListener('beforeunload', beforeUnload, false);
  else
    window.attachEvent('onbeforeunload', beforeUnload);

@cmanley
Copy link

cmanley commented Dec 31, 2016

I'm using v4 of CKEditor so ymmv... but this is how I do it without modifying jquery.are-you-sure.js:

$('#my_happy_little_form')
.areYouSure({'message': "Your changes haven't been saved."} )
.on('dirty.areYouSure', function() {
	$(this).find('input[type="reset"], input[type="submit"]').prop('disabled', false);
})
.on('clean.areYouSure', function() {
	$(this).find('input[type="reset"], input[type="submit"]').prop('disabled', true);
});

CKEDITOR.on('instanceCreated', function(e) {
	e.editor.on('change', function(event) {
		var textarea = event.editor.element.$;
		$(textarea).val(event.editor.getData().trim());
		$(textarea.form).trigger('checkform.areYouSure');
	});
})
CKEDITOR.replace('body_xhtml', { 
	// place options here
});

@bryanjamesmiller
Copy link

@cmanley Thanks for getting me started on this. I'm wondering why not just this:

        CKEDITOR.on("instanceReady", function (event) {
                $('form').areYouSure();
                event.editor.on('change', function(event) {
                    $('form').trigger('checkform.areYouSure');
                });
            });

@cmanley
Copy link

cmanley commented Sep 3, 2017

@bryanjamesmiller It won't work like that because you need to copy the CKEDITOR data into the textarea element before triggering checkform.areYouSure.
However, you can use either instanceCreated or instanceReady as both seem to work, and you can initialize areYouSure either inside the instanceCreated or instanceReady or outside as I did. The latter makes more sense to me as areYouSure depends on the textarea existing and not a CKEditor instance.

@LiamDawe
Copy link

LiamDawe commented Sep 20, 2017

Just wanted to pop along to say thank you @cmanley

Slotting in:

CKEDITOR.on('instanceCreated', function(e) {
	e.editor.on('change', function(event) {
		var textarea = event.editor.element.$;
		$(textarea).val(event.editor.getData().trim());
		$(textarea.form).trigger('checkform.areYouSure');
	});
})

Makes it work perfectly as far as I can tell 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants