ckeditor 在没有聚焦的实例中默认设置 justifyblock

ckeditor set justifyblock at default in an instance without focusing

我在一页上有 4 个 ckeditor 实例,我想在最后一个 (justifybloc) 上设置样式。

现在我使用这个代码:

  CKEDITOR.on('instanceReady', function(evt) {

    if (evt.editor.name != undefined) {
      if (evt.editor.name.indexOf('edit-field-body-1') > -1) {
         evt.editor.execCommand( 'justifyblock' );
      }
    }
 });

execCommand 聚焦我的文本区域,但我不想要。

我不知道还有其他方法可以做到这一点。

谢谢。

你提供的代码按你说的工作,重点是编辑器。此外,它将 justify/align 只有第一个块元素(例如段落),因为焦点设置在编辑器的开头。没有焦点编辑器就无法使用此命令。

但是,要实现 justifying/aligning 只有第一个块元素,您可以使用一些 hackish 解决方案并手动设置元素样式(以某种方式被 justify 插件正确识别):

CKEDITOR.on('instanceReady', function(evt) {

    if (evt.editor.name != undefined) {
        if (evt.editor.name.indexOf('editor1') > -1) {
            evt.editor.editable().getChild( 0 ).setStyle( 'text-align', 'justify' );
        }
    }
} );

参见 working codepen

这样的解决方案有一些缺点(例如,它假设编辑器中的第一个元素是块元素 - 在大多数情况下都是如此,等等),但对于大多数情况应该足够了。


顺便说一句,如果编辑器内容被选中这一事实不会打扰您,并且您想 justify/align 必须首先选择整个内容(需要 selectall 插件),例如:

CKEDITOR.on('instanceReady', function(evt) {

    if ( evt.editor.name != undefined ) {
        if ( evt.editor.name.indexOf( 'editor1' ) > -1 ) {
            evt.editor.execCommand( 'selectAll' );
            evt.editor.execCommand( 'justifyblock' );
        }
    }
} );

当然,您以后可以随时将 selection/focus 移到其他地方。

我用这个解决方案解决了我的问题

CKEDITOR.on('instanceReady', function(evt) {
  if (evt.editor.name != undefined) {
    if (evt.editor.name.indexOf('edit-field-body-1') > -1) {
       evt.editor.on('focus', function() { 
         this.execCommand( 'justifyblock' ); 
       });
     }
   }
 }

Code pen exemple