用于关闭编辑器的 CKEditor 插件

发布于 2024-10-10 06:30:48 字数 770 浏览 0 评论 0原文

从插件内销毁编辑器的问题是,某些代码在破坏性插件代码之后尝试使用编辑器,而实际上编辑器已不存在,从而导致错误和不稳定。

我为一个插件编写了以下代码,它使用 async:true 和 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);
  }
 };

我看到的问题是它使用了一个狡猾的 setTimout 调用,根据计算机的执行速度,可能会产生混合结果 - 100ms到可以销毁编辑器的时候可能还没有过去。

有没有正确的方法从插件中销毁编辑器?即使使用 async: true;并且不会产生 setTimeout 错误。

如果可能的话,也许一个可能的解决方案是停止与编辑器相关的任何现有/任何更多代码的运行?


我尝试过使用事件,例如 on('afterCommandExec', function(){ editor.destroy(); }) 和其他一些事件,但这并没有多大帮助......似乎没有一个事件当编辑器跳出堆栈调用来处理按钮时。

有没有办法通过更正确地处理 CKEditor 实例来停止执行?

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 editor 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-10-17 06:30:48

如果我是正确的,您希望关闭 CKEditor,但可能会或可能不会运行一些需要首先完成的进程。

你应该做的是,当你尝试使用 CKEditor 时,首先检查它是否仍然存在。如果没有,只需停止执行该函数即可。您还可以延迟销毁编辑器,例如将布尔值设置为 false,然后稍后销毁它们。

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.

公布 2024-10-17 06:30:48

显然 setTimeout 是可以接受的,因为在 JS 中没有办法像 PHP 的 die() 那样停止代码执行。

Apparently setTimeout is acceptable, as there is no way to stop code execution in JS like with PHP's die().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文