The problem with destroying the editor from within a plugin, is that certain code tries to use the editor after the destructive plugin code, when in fact the editor is no longer there, causing errors and instability.
I have come up with the following code for a plugin which closes the editor using both async:true and setTimeout:
var cancelAddCmd =
{
modes : { wysiwyg:1, source:1 },
async: true,
exec : function( editor )
{
if(confirm('Are you sure you want to cancel editing and discard all content?')) setTimeout(function() { editor.destroy(); }, 100);
}
};
The problem I see is that it uses a dodgy setTimout call that would likely have mixed results depending on the computer's speed of execution - 100ms might not have passed by the time it would be OK to destroy the editor.
Is there a proper way to destroy the 开发者_如何学Ceditor from within a plugin? Even with async: true; and no setTimeout errors are created.
Maybe a possible solution would be to stop any existing/any more code related to the editor from running afterwards, if possible?
I have tried using events, like on('afterCommandExec', function(){ editor.destroy(); }) and some other events, but that hasn't helped much... doesn't seem like there is an event for when the editor has jumped out of its stack call for handling the button.
And there is no way to stop execution by disposing of the CKEditor instance more properly?
If im correct, you want your CKEditor to be closed, but may or may not have some process running that needs to be finished first.
What you should do is when you attempt to use the CKEditor, first check if it still exists. Simply stop executing the function if it doesnt. You could also delay destroying the editor, by for example setting a boolean to false, and destroying them later.
Apparently setTimeout is acceptable, as there is no way to stop code execution in JS like with PHP's die().
精彩评论