CKEditor 4+;增强图像,默认为居中。供参考

CKEditor 4+;增强图像,默认为居中。供参考,ckeditor,alignment,config,center,Ckeditor,Alignment,Config,Center,这不是一个问题,而是一个解决方案(用一个肮脏的小把戏) 我需要在CKEditor中插入一个默认为“中心”对齐的图像。我找不到有效的例子,所以我花了很多时间,得出了以下答案 在编辑器的config.js CKEDITOR.on( 'dialogDefinition', function( ev ) { // Take the dialog name and its definition from the event data. var dialogName = ev.data.na

这不是一个问题,而是一个解决方案(用一个肮脏的小把戏)


我需要在CKEditor中插入一个默认为“中心”对齐的图像。我找不到有效的例子,所以我花了很多时间,得出了以下答案

在编辑器的
config.js

CKEDITOR.on( 'dialogDefinition', function( ev ) {
    // Take the dialog name and its definition from the event data.
    var dialogName = ev.data.name;
    var dialogDefinition = ev.data.definition;

    if ( dialogName == 'image2' ) {

        ev.data.definition.dialog.on('show', function() {
            //debugger;
            var widget = ev.data.definition.dialog.widget;
            // To prevent overwriting saved alignment
            if (widget.data['src'].length == 0)
                widget.data['align'] = 'center';

        });

    }
});

享受吧

我已经成功地使用了上述解决方案好几年了——尽管现在(CKEditor版本4.14.0)它抛出了一个错误,不再有效。经过一系列的故障排除和来自这里公认的旧文档的帮助: ... 以下几点似乎在这里起作用:

在编辑器config.js文件中:

CKEDITOR.on('dialogDefinition', function(ev) {
  let dialogName       = ev.data.name;
  let dialogDefinition = ev.data.definition;
  console.log(ev);
  if (dialogName == 'image2') {
    dialogDefinition.onFocus = function() {
      /**
       * 'none' is no good for us - if is none - reset to 'center'
       * if it's already 'left','center', or 'right' - leave alone.
       */
      if (this.getContentElement('info', 'align')
          .getValue() === 'none') {
        this.getContentElement('info', 'align')
            .setValue('center');
      }
    };
  }
});