CKEditor中RichCombo框的动态菜单

Dynamic menu for a RichCombo box in CKEditor

本文关键字:动态 菜单 RichCombo CKEditor      更新时间:2023-09-26

我写了一个插件,它为我的CKEditor添加了一个RichCombo框。我希望能够在这个RichCombo 中更新ListBox中的内容

这是我的密码。

var merge_fields = [];
CKEDITOR.plugins.add('mergefields',
{
    requires: ['richcombo'], //, 'styles' ],
    init: function (editor) {
        var config = editor.config,
           lang = editor.lang.format;
        // Gets the list of tags from the settings.
        var tags = merge_fields; //new Array();
        // Create style objects for all defined styles.
        editor.ui.addRichCombo('tokens',
        {
            label: "Merge",
            title: "title",
            voiceLabel: "voiceLabel",
            className: 'cke_format',
            multiSelect: false,
            panel:
            {
                css: [config.contentsCss, CKEDITOR.getUrl(CKEDITOR.skin.getPath('editor') + 'editor.css')],
                voiceLabel: lang.panelVoiceLabel
            },
            init: function () {
                // this.startGroup("mergefields");
                for (var this_tag in tags) {
                    this.add(tags[this_tag], tags[this_tag], tags[this_tag]);
                }
            },
            onClick: function (value) {
                editor.focus();
                editor.fire('saveSnapshot');
                editor.insertText(value);
                editor.fire('saveSnapshot');
            }
        });
    }
});

不幸的是,当merge_fields发生更改时,此列表不会更新。有没有办法重新初始化插件,或者删除插件并重新添加更新的内容?

注意我不想删除整个编辑器并替换它,因为这对用户来说非常不愉快

更新

根据要求,这里有一个jsfiddle来帮助

http://jsfiddle.net/q8r0dkc4/

在这个JSFiddle中,您将看到菜单是在第一次访问时动态创建的。它应该是选中的复选框。但是,以后每次访问它时,它都会保持相同的值,并且不会更新。更新它的唯一方法是使用我提供的reinit按钮重新初始化编辑器,但这会导致编辑器消失和重新出现,所以我不想这样做。

200点奖励,奖励那些每次调用下拉列表时都能动态更新的人。

使用这样的CKEditors自定义事件怎么样?

首先获取对CKEditors实例的引用

var myinstance = CKEDITOR.instances.editor1;

由于复选框不在CKEditor的范围内,请为复选框添加一个更改处理程序

$(':checkbox').change(function () {
    myinstance.fire('updateList'); // here is where you will fire the custom event
});

在插件定义中,在编辑器上添加一个事件侦听器,如下

editor.on("updateList", function () { // this is the checkbox change listener
  self.buildList(); // the entire build is created again here
});

我使用CKEditor的自定义事件,而不是直接在插件内的复选框(不在CKEditor范围内)上附加事件。现在,编辑器实例和复选框已解耦。

这是DEMO

希望这能帮助

更新

选项2

插件的方法可以像这样直接调用

$(':checkbox').change(function () {
    CKEDITOR.instances.editor1.ui.instances.Merge.buildList(); //this method should build the entire list again
});

尽管这看起来很简单,但我不认为它是完全脱钩的。(但仍然有效)

以下是选项2的演示

我想我已经帮你解决了。

richComboinit函数中添加以下行:$('input').on('click', rebuildList);

非常简单的JQuery修复,但每次我单击这些输入框时,它都会重新生成列表。

Fiddle证明:https://jsfiddle.net/q8r0dkc4/7/

相关文章: