【问题标题】:CKeditor custom plugin allowedContent not workingCKeditor 自定义插件 allowedContent 不起作用
【发布时间】:2017-10-27 11:34:17
【问题描述】:

我正在创建一个自定义插件以允许编辑 itemprop 属性。 它工作得很好,只是在重新加载时属性会被剥离。 知道我做错了什么吗?

我阅读了可以在文档中找到的所有内容,包括以下示例:https://github.com/ckeditor/ckeditor-docs-samples/blob/master/tutorial-abbr-acf/abbr/plugin.js#L24

如果我禁用 ACF,它会起作用。

export default CKEDITOR => {
    CKEDITOR.plugins.add('itemprop', {
        init(editor) {
            editor.addCommand('itemprop', new CKEDITOR.dialogCommand('itempropDialog', {
                allowedContent: '*[itemprop]'
            }));

            CKEDITOR.dialog.add('itempropDialog', editor => {
                return {
                    title: 'Itemprop',
                    contents: [
                        {
                            id: 'tab-main',
                            label: 'Itemprop',
                            elements: [
                                {
                                    type: 'text',
                                    id: 'itemprop',
                                    label: 'Itemprop',
                                    setup(element) {
                                        this.setValue(element.getAttribute('itemprop'));
                                    },
                                    commit(element) {
                                        element.setAttribute('itemprop', this.getValue());
                                    }
                                }
                            ]
                        }
                    ],
                    onShow() {
                        const selection = editor.getSelection();
                        const element = selection.getStartElement();
                        this.element = element;
                        this.setupContent(this.element);
                    },
                    onOk() {
                        this.commitContent(this.element);
                    }
                };
            });
        }
    });
};

【问题讨论】:

  • 现在我可以通过extraAllowedContent('*[itemprop]') 让它工作,但我更愿意让自动化解决方案工作。

标签: ckeditor


【解决方案1】:

我怀疑它可能与 CKEditor 中的这个错误有关:https://github.com/ckeditor/ckeditor-dev/issues/678

我做了一个简单的测试,当我在工具栏上添加一个按钮时,你的代码就开始工作了。还允许带有itemprop 条目的内容开始显示在 CKEditor 允许的过滤器中,而无需在编辑器配置中定义它。这就是为什么最简单的解决方案是在插件中添加一个按钮:

editor.ui.addButton( 'itemprop', {
  label: 'Item Prop',
  command: 'itemprop'
});

整个插件定义如下:

CKEDITOR.plugins.add('itemprop', {
  init(editor) {

    editor.ui.addButton( 'itemprop', {
      label: 'Item Prop',
      command: 'itemprop'
    });

    editor.addCommand('itemprop', new CKEDITOR.dialogCommand('itempropDialog', {
      allowedContent: '*[itemprop]'
    }));

    CKEDITOR.dialog.add('itempropDialog', editor => {
      return {
        title: 'Itemprop',
        contents: [
          {
            id: 'tab-main',
            label: 'Itemprop',
            elements: [
              {
                type: 'text',
                id: 'itemprop',
                label: 'Itemprop',
                setup(element) {
                  this.setValue(element.getAttribute('itemprop'));
                },
                commit(element) {
                  element.setAttribute('itemprop', this.getValue());
                }
              }
            ]
          }
        ],
        onShow() {
          const selection = editor.getSelection();
          const element = selection.getStartElement();
          this.element = element;
          this.setupContent(this.element);
        },
        onOk() {
          this.commitContent(this.element);
        }
      };
    });
  }
});

这里的工作示例:https://codepen.io/msamsel/pen/RjbOpP?editors=1010

【讨论】:

  • 我认为这与 CKEditor 错误无关,但简化了向 ACF 报告的工作方式。当插件有按钮时,您可以在命令定义中向 ACF 报告内容:github.com/ckeditor/ckeditor-dev/blob/major/plugins/image/…。当插件为组合时,需要在下拉定义中上报内容:github.com/ckeditor/ckeditor-dev/blob/major/plugins/font/…
  • 如果插件没有,除了extraAllowedContent 之外,似乎没有办法向ACF 添加一些东西。如果您尝试console.log(JSON.stringify(CKEDITOR.instances.editor1.filter.allowedContent, null, '\t'));,您将在输出中看到没有itemprop。在这种情况下,您的解决方案是添加一个按钮或使用extraAllowedContent
  • 谢谢!但是我正在添加一个命令,它不应该像我在第 9 行那样工作吗?
  • 稍后我通过execCommand执行这些命令:github.com/neos/neos-ui/blob/…
  • 好的,我想我明白了,没有原生按钮没有 ACF :)
猜你喜欢
  • 2013-11-26
  • 2019-08-07
  • 1970-01-01
  • 2017-11-12
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多