如何在ckeditor中使用多个插件?

时间:2011-11-14 12:39:46

标签: javascript ckeditor

我需要将h1,h2,h3标签添加到ckeditor的键绑定中,我发现了这个简单的功能。
这个函数正常工作并且符合预期,但我只能使用它一次,如果我将相同的函数复制到另一个目录并尝试包含它,它就不起作用。我究竟做错了什么?

位置:插件/ button-h1 / plugin.js

 var a= {
    exec:function(editor){
    var format = {
    element : "h1"
    };
    var style = new CKEDITOR.style(format);
        style.apply(editor.document);
    }
},

// Add the plugin
b="button-h1";
CKEDITOR.plugins.add(b,{
    init:function(editor){
    editor.addCommand(b,a);
    editor.ui.addButton("button-h1",{
    label:"Button H1",
    icon: this.path + "button-h1.png",
    command:b
    });
}
});

但是当我在另一个名为'button-h2'的文件夹中创建另一个插件时,使用相同的代码但名称和标签不同,它不起作用。

位置:插件/ button-h2 / plugin.js

// Exactly the same as above, but with "h2" tags.
var a= {
    exec:function(editor){
    var format = {
    element : "h2"
    };
    var style = new CKEDITOR.style(format);
        style.apply(editor.document);
    }
},

// Add the plugin
b="button-h2";
CKEDITOR.plugins.add(b,{
    init:function(editor){
    editor.addCommand(b,a);
    editor.ui.addButton("button-h2",{
    label:"Button H2",
    icon: this.path + "button-h2.png",
    command:b
    });
}
});

基本上,我需要用户能够使用“CTRL + 1”在所选文本周围添加标题标签。
此方法有效,但我只能将其用于其中一个标题,H1或H2,而不是两个或更多。

在我的config.js中,我有以下内容来设置所有内容。

config.extraPlugins = "button-h1,button-h2";
config.keystrokes =
[
    [ CKEDITOR.CTRL + 49 /*1*/, 'button-h1' ],
    [ CKEDITOR.CTRL + 50 /*2*/, 'button-h2' ]

];

所以,
  - 插件工作,但我只能在H1或H2上使用它,不能同时使用它?
我是否需要将它放在一个函数或其他东西中,以便它可以在同一时间执行多次?

1 个答案:

答案 0 :(得分:0)

我找到了答案。我需要将它包装在一个匿名函数中。

(function(){
var a= 
{
    exec:function(editor){
        var format = {
        element : "h1"
        };
    var style = new CKEDITOR.style(format);
    style.apply(editor.document);
    }
},

b="tags-h1";
CKEDITOR.plugins.add(b,{
    init:function(editor){
    editor.addCommand(b,a);
    editor.ui.addButton(b,{
    label:"Heading 1",
    icon: this.path + "heading-1.png",
    command:b
    });
    }
});
})();