CKEditor:如何删除已添加的插件?

发布于 2024-12-25 11:23:04 字数 1114 浏览 4 评论 0原文

我刚刚开始使用 CKEditor,但很难理解插件系统。

我可以添加一个简单的按钮,当您点击它时显示“测试”:

  var myplugin_function = function () {
    alert('Test');
  }
  var plugin_name='myplugin';
  CKEDITOR.plugins.add(plugin_name,   
  {    
    init:function(c) {
      c.addCommand(plugin_name,myplugin_function);
      c.ui.addButton(plugin_name, 
      {
       label:'This is my plugin',
       command:plugin_name,
       icon:this.path+'myplugin.png'
      });
    }
  });

我知道这段代码应该只执行一次,例如在 plugin.js 中,但这不是我的方式使用它。每次加载 Ajax 页面时都会执行 CKEditor 实例,包括我的插件代码。

这就是为什么我使用它来删除实例(如果存在):

  if (CKEDITOR.instances['mytextarea']) {
    CKEDITOR.remove(CKEDITOR.instances['mytextarea']);
  }

然后我使用 jquery 方式从文本区域创建 ckeditor:

$('#mytextarea').ckeditor();

但是第二次加载 ajax 页面时,我收到有关插件已注册的错误。所以我需要一种方法来删除插件并能够再次添加它。

这可能吗?

更新

这似乎有效:

我现在检查插件是否已经

if (!CKEDITOR.plugins.registered[plugin_name]) {

}

CKEDITOR.plugins.add(b, .. 。 部分

I'm just beginning to use CKEditor, but have a hard time understanding the plugins system.

I was able to add a simple button that says 'Test' when you click on it with :

  var myplugin_function = function () {
    alert('Test');
  }
  var plugin_name='myplugin';
  CKEDITOR.plugins.add(plugin_name,   
  {    
    init:function(c) {
      c.addCommand(plugin_name,myplugin_function);
      c.ui.addButton(plugin_name, 
      {
       label:'This is my plugin',
       command:plugin_name,
       icon:this.path+'myplugin.png'
      });
    }
  });

I know this code should be executed only once, for example in a plugin.js, but that's not how I use it. The CKEditor instance, including my plugin code is executed each time the Ajax-page is loaded.

That's why I use this to remove the instance, if it exists :

  if (CKEDITOR.instances['mytextarea']) {
    CKEDITOR.remove(CKEDITOR.instances['mytextarea']);
  }

Then I use the jquery way to create the ckeditor from a textarea:

$('#mytextarea').ckeditor();

But the 2nd time the ajax-page loads, I get an error about the plugin already being registered. So I need a way to remove the plugin and be able to add it again.

Is this even possible?

UPDATE

This seems to work :

I now check if the plugin is already registered with :

if (!CKEDITOR.plugins.registered[plugin_name]) {

}

around the CKEDITOR.plugins.add(b, ... part

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

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

发布评论

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

评论(1

忆离笙 2025-01-01 11:23:04

您没有显示如何添加插件,因此很难判断您的确切问题是什么;但根据您提供的代码,我可以建议您使用比“a”、“b”和“c”更好的变量名称。以这种方式理解代码相当困难。

另外, CKEDITOR.remove 只是从实例数组中删除实例,但它并没有真正清除已使用的资源,您应该使用 CKEDITOR.instances['mytextarea'].destroy( true ) 代替

You are not showing how you are adding the plugin, so it's hard to tell what's your exact problem; but from the code that you have provided I can suggest that you use variable names better than "a", "b" and "c". It's quite harder to understand the code this way.

Also, CKEDITOR.remove just removes the instance from the instances array, but it doesn't really clear the used resources, you should use CKEDITOR.instances['mytextarea'].destroy( true ) instead

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