ckeditor 插件 strinsert:如何在初始化后设置自定义字符串

ckeditor plugin strinsert: how to set custom strings after initialization

我正在尝试使用插件 placeholder and strinsert as described here.

为 ckeditor 实现固定占位符

不幸的是,我无法动态更改 strings 插件数组 strinsert

config.js 中的这几行应该有效,但无效:

CKEDITOR.editorConfig = function( config ) {
    config.strinsert_strings = [
        ['[[foo]]']
    ];
};

也许这个插件不支持在初始化后改变字符串数组?

如何在不克隆 strinsert 插件 x 次的情况下在不同的 ckeditor 实例中向用户呈现不同的占位符?

注意:我使用的是所有这些工具的最新版本。

像建议的那样改用占位符 rplugin。只需编辑文件 ckedit/plugins/placeholder/dialogs/placeholder.js。将元素类型更改为 select 并将所需值添加到 items,如以下示例所示:

contents: [
            {
                id: 'info',
                label: generalLabel,
                title: generalLabel,
                elements: [
                    // Dialog window UI elements.
                    {
                        id: 'name',
                        type: 'select',
                        style: 'width: 100%;',
                        label: lang.name,
                        items:[
                            ['ONE'],
                            ['TWO'],
                            ['THREE']
                        ],
                        // SNIP...

您可以使用 dialogDefinition 事件动态设置自定义占位符。 我是这样做的(html 属性 data-placeholders"placeholder1,different text to show=placeholder2,another text=placeholder3"

的形式指定有效的占位符
  CKEDITOR.on ('dialogDefinition', function (e) {

    // Check if the definition is from the dialog window you are interested in (the "Link" dialog window).
    if (e.data.name == 'placeholder') {
      var $textarea = $(e.editor.element.$),
        placeholders = $textarea.attr("data-placeholders"),
        tab, ff, i, a;

      //placeholders = [['placeholder1'],['Text to show', 'placholdervalue']];
      if (placeholders && placeholders.length) {
        // convert placeholders from desc1=val1,desc2=val2,val3,... format to array(array(desc,val))
        placeholders = placeholders.split(",");
        // ensure placeholders is array of arrays with exact 2 members
        for (i = 0; i < placeholders.length; i++) {
          a = placeholders[i].split("=");
          if (a.length < 1) a[1] = a[0];
          placeholders[i] = a;
        }
        tab = e.data.definition.getContents ('info');

        // Set the default value for the URL field.
        ff = tab.get ('name');
        ff['type'] = 'select';
        ff['items'] = placeholders;
      }
    }
  });

对于 strinsert,我通过编辑它的 plugin.js 让它工作,去掉字符串声明并替换为:

var strings = editor.config.strinsert_strings;

然后在你的 html 创建 CKEDITOR 之前:

CKEDITOR.config.strinsert_strings =  [];
CKEDITOR.config.strinsert_strings.push(['myvalue1', 'myname1', 'mylabel1']);
CKEDITOR.config.strinsert_strings.push(['myvalue2', 'myname2', 'mylabel2']);
CKEDITOR.replace('mytextarea');