checkDirty always returning true

对着背影说爱祢 提交于 2019-12-08 13:42:47

问题


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:


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;

};


来源:https://stackoverflow.com/questions/29016487/checkdirty-always-returning-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!