Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CKEditor: how to remove a plugin that has been added?

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

like image 313
Dylan Avatar asked Nov 14 '22 12:11

Dylan


1 Answers

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

like image 104
AlfonsoML Avatar answered Jan 21 '23 11:01

AlfonsoML