CKEditor: call a plugin function without toolbar button

纵饮孤独 提交于 2019-12-23 01:36:32

问题


How can I call a plugin function without a toolbar button?

I have an external floating toolbar integrated in my cms. I insert images, videos and other pieces of static code with the InsertHTML() API of CKEditor.

Now I need to insert also video from URL, and there is the fantastic oembed plugin. How can I fire that plugin using a button in my cms without the toolbar button?

I load the plugin in my config, and I try to create this function:

function oembed() {
// Get the editor instance that we want to interact with.
var editor = CKEDITOR.instances.editor1;
var url = 'http://www.youtube.com/watch?v=AQmbsmT12SE'
var wrapperHtml = jQuery('<div />').append(editor.config.oembed_WrapperClass != null ? '<div class="' + editor.config.oembed_WrapperClass + '" />' : '<div />');

// Check the active editing mode.
if ( editor.mode == 'wysiwyg' )
{
    // Insert HTML code.
    // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-insertHtml
    editor.embedCode(url, editor, false, wrapperHtml, 650, 400, false);
}
else
    alert( 'You must be in WYSIWYG mode!' );

}

The result is this:

Uncaught TypeError: Object [object Object] has no method 'embedCode'

Is there any way to create a new API like "InsertHTML" to call plugin functions without toolsbar buttons?

EDIT

Maybe I can use the createFakeElement API.

http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-createFakeElement

I add the class fakegallery to my doc.

I use this code but nothing happens:

      function Fake()
   {
      var editor = CKEDITOR.instances.editor1;
      var element = CKEDITOR.dom.element.createFromHtml( '<div class="bold"><b>Example</b></div>' );
      alert( element.getOuterHtml() ); 
      editor.createFakeElement( element, 'fakegallery', 'div', false );

   }

回答1:


I found this post looking for the answer...

Checked out the link provided in the answers here [ http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-fire ], scrolled down to execCommand

This worked for me and only requires that you know the name of your plugin, so it'll always work. Obviously, you may need to change "editable" to your editor instance.

CKEDITOR.instances['editable'].execCommand('YOUR_PLUGIN_NAME_HERE');

If above fails, HACK:

This would work (first line of code below), but requires you to look at the source to find the correct #. If you have 1 custom plugin, no big deal. But if you have a dozen or more, like me, this is annoying, and could change as more plugins are added.

CKEDITOR.tools.callFunction(145,this);

Hope this helps!




回答2:


Read this: http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-fire

editor.fire( 'MediaEmbed', data);

I think that this is the structure that your data needs to have:

var data = {title : 'Embed Media',
                 minWidth : 550,
                 minHeight : 200,
                 contents :
                       [
                          {
                             id : 'iframe',
                             expand : true,
                             elements :[{
                                id : 'embedArea',
                                type : 'textarea',
                                label : 'Paste Embed Code Here',
                                'autofocus':'autofocus',
                                setup: function(element){
                                },
                                commit: function(element){
                                }
                              }]
                          }
                       ]}

I'm not secure but this will help you on the way. Look at the Source code of the plugin to find the available commands. The command name that i specified above is the only one that your plugin haves.

EDIT - Manual inserting

                var div = editor.document.createElement('div');
                div.setHtml("<embed src=" + url +" width=" + width +" height=" + height + ">");
                editor.insertElement(div);

You can add every attribute you like, Type?? maby? Autofocus??



来源:https://stackoverflow.com/questions/16585428/ckeditor-call-a-plugin-function-without-toolbar-button

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