checkDirty always returning true

2019-06-10 19:52发布

Whenever I call checkDirty() I am always getting true. Run the example below and just click the destroy button.

jsfiddle

$(document).ready(function() {

    CKEDITOR.config.autoUpdateElement = false; 
    CKEDITOR.on('contentDom', function (event) {

                        event.editor.resetDirty();
                    });
    CKEDITOR.on('instanceReady', function (event) {

         event.editor.on('contentDomUnload', function (destroyevent) {
             alert('isdirty: ' + destroyevent.editor.checkDirty());
         });

    });
    $("#item_ckeditor").ckeditor();
    $("#item_docompare").on("click", function (event) {

        var $textarea = $("#item_ckeditor"),
            editor = $textarea.ckeditorGet();


        editor.destroy();

    });
});

1条回答
看我几分像从前
2楼-- · 2019-06-10 20:15

UPDATE

I have a new isdirty function here that will return an accurate value for if the user has changed the content in the CKEditor. This will not always return true and will still work with the element attributes being automatically sorted.

    var isdirty = function(ckeditor) {
            return ckeditor.initialdata !== ckeditor.getData();

        };
CKEDITOR.on('instanceReady', function (event) {
        event.editor.initialdata = event.editor.getData();
    });

Original

Looks like there was a space added in the content automatically by the CKEditor so checkDirty() always returned true. I wrote my own function that ignores case and trims the spacing that was added automatically here.

This works perfectly fine, UNLESS you set allowedContent to true and disable the ACF. The new problem is that CKEditor sorts the attributes on elements which causes check for modified content to not work properly.

I logged a separate question here to find out how to disable sorting of element attributes.

 var isdirty = function(ckeditor) {
     var originalcontent = $.trim(ckeditor._.previousValue.toLowerCase());
         newcontent = $.trim(ckeditor.getSnapshot().toLowerCase());
     return originalcontent !== newcontent;

};
查看更多
登录 后发表回答