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

  • 添加对齐插件如下

从'@ckeditor/ckeditor5-alignment/src/alignment'导入对齐;ClassicEditor .create(this.element.nativeElement, { plugins: [ Alignment ], toolbar: [ '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;
  }

}
4

3 回答 3

3

除了我在第二个答案中写的内容之外,您的代码中还有另一个问题。它还没有表现出来,但如果编辑器已经启动它就会出现。

问题在这里:

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

您不能像这样将插件添加到现有构建中。这将导致可怕的代码重复和运行时错误。原因是构建本身已经捆绑了许多插件,所以整个核心包也包含在其中。对齐功能也取决于核心包,所以如果你像这样构建它,核心包将被包含两次。

有一个单独的指南如何安装插件,我强烈建议阅读它。

于 2018-05-29T09:14:49.297 回答
3

通过单击下面的链接,根据您的要求选择自定义构建

CKEditor 5 在线生成器

1.选择插件。添加对齐插件。 选择插件

2.在工具栏中拖放您要使用的插件。 在此处输入图像描述

3.下载捆绑包。 下载捆绑包

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

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

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() {
  }
}

输出: 在此处输入图像描述

于 2020-06-11T10:59:23.630 回答
1

你没有正确配置 webpack。如果您从源代码构建编辑器(而不是使用现有自定义构建),您需要确保 webpack 配置为处理 CKEditor 5 资产。这包括处理 CSS 和 SVG 文件,如Webpack 配置部分所述。

示例设置可能如下所示:

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
                        } )
                    },
                ]
            }
        ]
    }
};

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

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

于 2018-05-29T09:09:30.243 回答