CKEditor 4:如何从插件添加CSS样式表?

Seb*_*bin 5 javascript css ckeditor

试过这个,但没有运气

  editor.addCss(this.path + 'tabber.css');
  editor.document.appendStyleSheet(this.path + 'tabber.css');
Run Code Online (Sandbox Code Playgroud)

完整代码

(function () {
  CKEDITOR.plugins.add('tabber', {
    init: function (editor) {
      editor.ui.addButton('addTab', {
        command: 'addTab',
        icon: this.path + 'icons/tabber.png',
        label: Drupal.t('Insert tabs')
      });
      editor.addCss(this.path + 'tabber.css');
      editor.document.appendStyleSheet(this.path + 'tabber.css');
      editor.addCommand('addTab', {
        exec: function (editor) {
          editor.insertHtml('<dl>' +
            '<dt>Tab title 1</dt>' +
            '<dd><p>Tab content 1.</p></dd>' +
            '<dt>Tab title 2</dt>' +
            '<dd><p>Tab content 2.</p></dd>' +
            '</dl>');
        }
      });
    }
  });
})();
Run Code Online (Sandbox Code Playgroud)

在init中解决方案(感谢指出正确方向的答案)

  var cssPath = this.path + 'tabber.css';
  editor.on('instanceReady', function () {
    this.document.appendStyleSheet(cssPath);
  });
Run Code Online (Sandbox Code Playgroud)

ole*_*leq 10

CKEDITOR.addCss接受字符串作为参数,因此无法在那里传递任何路径.CKEDITOR.document.appendStyleSheet虽然是正确的(小提琴):

CKEDITOR.replace( 'editor', {
    on: {
        instanceReady: function() {
            this.document.appendStyleSheet( 'http://path.to.your.css' );
        }
    }
} );
Run Code Online (Sandbox Code Playgroud)

请确保:

  1. 你的CSS存在.
  2. 它具有正确的读取权限.
  3. 您的HTML 在编辑器中允许的(也可以阅读自4.1以来的集成指南,您需要根据allowedContent命令进行调整).

我想你也可能会发现CKEDITOR.getUrl很有用.