且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

定制数据处理器中的CKEditor5使用模型

更新时间:2023-12-05 22:32:22

最近,在CKE5 GitHub上提出了一个类似的问题。问题是关于将JSON数据作为编辑器输出,但是您提出的主题也被部分覆盖。

Recently, a similar question was raised on CKE5 GitHub. The question is about getting JSON data as editor output, but topic raised by you is also partially covered.


(...)从数据处理器访问模型

(...) how to access the model from a data processor

直接在模型上运行存在某些问题和风险。不建议这样做。

There are certain problems and risks connected with operating straight on the model. This is not something that is recommended. It is explained in the linked post.


(...)我的问题是如何将其转换为 model / DocumentFragment

与直接在模型上进行操作相比,这是一种更好(风险较小)的方法。但是,我不得不问-为什么要从模型转换?也许有更好的解决方案?

This is a better (less risky) approach than operating straight on the model. However, I have to ask - why do you want to convert from the model? Maybe there's a better solution to your problem?

要在视图和模型之间进行转换,必须使用 DataController#toView DataController#toModel DataController 实例可从 Editor#data 获得。要在数据处理器中使用它,数据处理器将需要访问编辑器实例。

To convert between view and model, one would have to use DataController#toView and DataController#toModel. DataController instance is available at Editor#data. To use it in a data processor, the data processor would need access to the editor instance.

我建议创建自己的编辑器类,扩展一个CKE5编辑器类。 。然后,在新的编辑器类构造函数中,覆盖数据处理器并传递编辑器实例。像这样的东西:

I'd suggest creating your own editor class, extending one of CKE5 editor classes. Then, in the new editor class constructor, overwrite data processor and also pass editor instance. Something like:

class MyEditor extends ClassicEditor {
  constructor() {
    this.data.processor = new MyDataProcessor( this );
  }
}

class MyDataProcessor() {
  constructor( editor ) {
    this._editor = editor;
  }

  toData( viewDocumentFragment ) {
    const modelDocumentFragment = this._editor.data.toModel( viewDocumentFragment );
    // ...
  }

  toView( modelData ) {
    // ...
    this._editor.data.toView( ... );
    // ...
  }
}

这些是

仍然,我想知道为什么您坚持使用模型而不是视图来生成编辑器输出

Still, I'd like to know why you insist on using the model rather than the view to generate editor output.

BTW。如果继续这样实施,整个过程将很愚蠢:)。首先,您将获得模型数据,然后将其转换为视图(在数据处理器中),然后编辑器将获取视图数据并将其转换回模型:)。因此,也许您也会对覆盖 Editor#setData 方法感兴趣,这样就不会发生不必要的转换。

BTW. If you go on and implement it like this, the whole process will be a bit silly :). First, you will get a model data, then convert it to view (in data processor), then the editor will take view data and convert it back to the model :). So maybe you will be also interested in overwriting Editor#setData method so unnecessary conversions won't take place.