我如何使用多个插件's在编辑器

How do I use multiple plugin's in ckeditor?

本文关键字:编辑器 插件 何使用      更新时间:2023-09-26

我需要将h1, h2, h3标签添加到ckeditor的键绑定中,我发现这个简单的函数可以做到这一点。
这个函数工作良好,如预期的,但我只能使用它一次,如果我复制相同的函数到另一个目录,并试图包括它,它不工作。我做错了什么?

位置:plugins/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'的文件夹中创建另一个插件时,使用相同的代码但不同的名称和标签,它不起作用。

位置:plugins/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上使用它,而不是两者,为什么?
我需要把它放在一个函数或其他东西,所以它可以执行不止一次在同一时间?

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

(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
    });
    }
});
})();