ckeditor5文本对齐插件无法正常工作

时间:2018-05-29 00:04:22

标签: angular ckeditor ckeditor5

  • 我按照ckeditor5的docs中提到的文本对齐插件的安装步骤进行了操作。

  • 添加了对齐插件,如下所示

  

从'@ ckeditor / ckeditor5-alignment / src / alignment'导入对齐方式;       ClassicEditor         .create(this.element.nativeElement,{           插件:[对齐],           工具栏:['alignment']       })

我收到以下错误:

TypeError: Cannot read property 'getAttribute' of null
    at IconView._updateXMLContent (iconview.js:100)
    at IconView.render (iconview.js:76)
    at IconView.on (observablemixin.js:241)
    at IconView.fire (emittermixin.js:196)
    at IconView.(anonymous function) [as render] (webpack-internal:///./node_modules/@ckeditor/ckeditor5-utils/src/observablemixin.js:249:16)
    at ViewCollection.on (viewcollection.js:68)
    at ViewCollection.fire (emittermixin.js:196)
    at ViewCollection.add (collection.js:182)
    at ButtonView.render (buttonview.js:160)
    at ButtonView.on (observablemixin.js:241)

有人可以帮我解决这个问题吗?按照文档中提到的步骤进行操作,但仍然遇到此问题。

以下是ckeditor的完整angular5组件代码:

import { Component, OnInit, OnDestroy, NgZone, ElementRef, ChangeDetectionStrategy, forwardRef } from '@angular/core';
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

@Component({
  selector: 'ck-editor',
  template: '<textarea></textarea>',
  styleUrls: ['./ck-editor.component.scss'],
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => CkEditorComponent),
    multi: true
  }],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CkEditorComponent implements ControlValueAccessor, OnInit, OnDestroy {

  onChange: Function;
  onTouched: Function;
  model: string;
  editor;

  constructor(private ngZone: NgZone,
    private element: ElementRef) {
  }

  ngOnInit() {
    ClassicEditor
      .create(this.element.nativeElement,  {
      plugins: [Alignment],
      toolbar: [
          'heading', '|', 'bulletedList', 'numberedList', 'alignment', 'undo', 'redo'
      ]
    })
      .then(editor => {
        this.editor = editor;
        editor.model.document.on('change', () => {
          if (editor.model.document.differ.getChanges().length > 0) {
            this.ngZone.run(() => this.onChange(editor.getData()));
          }
        });
        editor.model.document.on('blur', () => {
          this.ngZone.run(() => this.onTouched());
        });
        this.editor.setData(this.model ? this.model : '');
      })
      .catch(error => {
        console.error(error);
      });
  }

  ngOnDestroy() {
    if (this.editor) {
      return this.editor.destroy();
    }
  }

  writeValue(value) {
    this.model = value;
  }

  registerOnChange(fn) {
    this.onChange = fn;
  }

  registerOnTouched(fn) {
    this.onTouched = fn;
  }

}

3 个答案:

答案 0 :(得分:2)

除了我在第二个回答中写的内容,您的代码中还有另一个问题。它还没有表现出来,但如果编辑开始的话就会出现。

问题在于:

import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment';

您无法将插件添加到此类现有版本中。这将导致糟糕的代码重复和运行时错误。原因是构建本身已经捆绑了很多插件,所以整个核心包都包含在那里。对齐功能也取决于核心包,所以如果您像这样构建它,核心包将被包含两次。

有一个单独的指南如何install plugins,我强烈建议您阅读它。

答案 1 :(得分:1)

您没有正确配置webpack。如果您build the editor from source(而不是使用existingcustom build),则需要确保将webpack配置为处理CKEditor 5资产。这包括处理CSS和SVG文件,如Webpack configuration部分所述。

示例设置可能如下所示:

const { styles } = require( '@ckeditor/ckeditor5-dev-utils' );

module.exports = {
    module: {
        rules: [
            {
                // Or /ckeditor5-[^/]+\/theme\/icons\/[^/]+\.svg$/ if you want to limit this loader
                // to CKEditor 5 icons only.
                test: /\.svg$/,

                use: [ 'raw-loader' ]
            },
            {
                // Or /ckeditor5-[^/]+\/theme\/[\w-/]+\.css$/ if you want to limit this loader
                // to CKEditor 5 theme only.
                test: /\.css$/,
                use: [
                    {
                        loader: 'style-loader',
                        options: {
                            singleton: true
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: styles.getPostCssConfig( {
                            themeImporter: {
                                themePath: require.resolve( '@ckeditor/ckeditor5-theme-lark' )
                            },
                            minify: true
                        } )
                    },
                ]
            }
        ]
    }
};

如果没有raw-loader处理SVG文件,它们将作为外部资源加载,因此编辑器会获取它们的路径,而不是它们的XML源会破坏编辑器。

PS。如果您使用Angular,则可能需要从处理SVG和CSS的加载器中排除CKEditor 5文件。

答案 2 :(得分:1)

点击下面的链接,根据您的要求选择自定义版本

CKEditor 5 Online Builder

1。选择插件。添加Alignment插件。 Choose plugins

2。拖放要在工具栏中使用的插件。 enter image description here

3。下载捆绑软件。 Download the bundle

4。解压缩zip ,然后从build文件夹复制ckeditor.js文件,并将其粘贴到node_modules / @ ckeditor / ckeditor5-build-classic / build中。然后,它将替换为您选择的任何功能。**

5。安装带有版本(无论package.json中@ ckeditor / ckeditor5-build-classic的版本)的ckeditor5-alignment软件包

npm i --save-dev @ckeditor/ckeditor5-alignment@18.0.0

6.editor.component.html

<ckeditor [editor]="Editor" [config]="config"></ckeditor>

7.editor.component.ts

import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
@Component({
  selector: 'app-editor',
  templateUrl: './editor.component.html',
  styleUrls: ['./editor.component.scss']
})


export class EditorComponent implements OnInit {
  public Editor = ClassicEditor;
  public config = {
    toolbar: {
      items: [
        'heading',
        '|',
        'bold',
        'italic',
        'link',
        'bulletedList',
        'numberedList',
        'alignment',
        'imageUpload',
        'blockQuote',
        'undo',
        'redo'
      ]
    },
    alignment: {
      options: ['left', 'right', 'center', 'justify']
    },
    removePlugins: ['MediaEmbed', 'Link'], mediaEmbed: {} //if you want to remove any plugin
  };

  ngOnInit() {
  }
}

输出: enter image description here