简体   繁体   English

CKEditor5视图模型位置和范围转换

[英]CKEditor5 view-model position and range conversion

The engine/conversion/mapper class implements the functionality that I need, however, I have a few questions on how to get/build this object the most efficiently. engine/conversion/mapper类实现了我需要的功能,但是,我对如何最有效地获取/构建该对象有一些疑问。

I would like to get the Mapper object in a state when both ways (model to view and view to model) conversions are possible at the same time. 我想在同时进行两种方式(从模型到视图以及从视图到模型)转换的状态下,使Mapper对象处于一种状态。

a) Is there a way to get the Mapper from the DataController (eg an event)? a)有没有办法从DataController获取Mapper (例如事件)?

b) If I have to build my own Mapper then what is the best way to do that? b)如果我必须构建自己的Mapper那么最好的方法是什么?

I really would like to avoid having to modify the DataController , however, I would prefer a solution that is the most likely to be forward compatible with future CKEditor5 releases. 我真的很想避免不得不修改DataController ,但是,我更喜欢一个最可能与将来的CKEditor5版本向前兼容的解决方案。

Update (with more context) : Basically this question is a follow up on using the model in a DataProcessor in the toData method. 更新(具有更多上下文) :基本上,这个问题是在toData方法的DataProcessor中使用模型toData

I start with the task at hand, and after I explain what exactly I'm trying to do with the positions which is more related to the original question. 我从手头的任务开始,然后在解释完与原始问题更相关的职位后,我到底要做什么。

So, I need to convert editor data to BBCode (so far so good), however I don't know what the HTML is for anything (another plugin would set up the schema for bold and italic etc). 因此,我需要将编辑器数据转换为BBCode(到目前为止非常好),但是我不知道HTML适用于什么(另一个插件会将模式设置为bolditalic等)。 Thus, I'd like to use the model for some conversions, as for example, converting the text nodes from the model seems easier this way even with the fact that I have to convert the attributes to tags myself. 因此,我想将模型用于某些转换,例如,即使我必须自己将属性转换为标签,从这种方式转换文本节点似乎也很容易。

However, despite the warnings on my other question that it might get too complex and I'd be better off converting eg from the DOM (which I still disagree with), I decided to make it even a bit more complex and convert from both the view and the model. 但是,尽管警告了我另一个问题,即它可能太复杂了,我最好还是从DOM(我仍然不同意)进行转换,但我还是决定使其变得更加复杂,并从两者进行转换。视图和模型。 And now, we arrive to this question. 现在,我们解决了这个问题。

My plan for doing the conversion is as follows: 我进行转换的计划如下:

a) Convert the viewFragment to a modelFragment . a)将viewFragment转换为modelFragment And here I would like to have all the position conversion available between them in both ways. 在这里,我想以两种方式在它们之间提供所有位置转换。

b) Pass down both on the conversion pipeline, where there are some general framework for processing elements and text nodes, with an extensible set of "conversion rule" objects, to handle paragraph and the bold ˛attribute. b)在转换管道上向下传递,转换管道上有一些用于处理元素和文本节点的通用框架,带有一组可扩展的“转换规则”对象,以处理paragraphbold属性。

In b), each "conversion rule" could decide from what they want to convert from (view, model, convert to view to DOM or Markdown or whatever) and then return the conversion result in text, a model position pointing before the next element to be processed, and the same position in the view. 在b)中,每个“转换规则”都可以根据其要转换的内容(视图,模型,转换为视图到DOM或Markdown等)来决定,然后以文本形式返回转换结果,模型位置指向下一个元素之前要处理,并且在视图中处于相同位置。

So for example, <p><b>text</b></p><p>...</p> being the text the data processor would get a position before the first p in both the model and the view plus the document fragment. 因此,例如, <p><b>text</b></p><p>...</p>是文本,数据处理器将在模型和视图中的第一个p之前获得位置加上文档片段。 And when all the magic happened, it would return an object, that has {result:'[b]text[/b]\\n\\n', modelPos:'[par]...[/par]ˇ[par]...[/par]', viewPos: '<p><b>text</b></p>ˇ<p>...</p>'} , where ˇ would be where the appropriate position object points. 并且当所有魔术发生时,它将返回一个对象,该对象具有{result:'[b]text[/b]\\n\\n', modelPos:'[par]...[/par]ˇ[par]...[/par]', viewPos: '<p><b>text</b></p>ˇ<p>...</p>'} ,其中ˇ其中的适当位置对象点。

I hope it is somewhat clear, what I'm trying to do, it got a bit long, as I tried to describe the problem, so you can better judge my solution I try to apply to it. 我希望它很清楚,我正在尝试做的事情,花了很长时间,因为我试图描述问题,所以您可以更好地判断我尝试应用的解决方案。

Unfortunately, the problem you described is quite complicated and probably too broad for SO answer. 不幸的是,您描述的问题非常复杂,可能对于SO答案而言太宽泛了。 This is why I'll keep the answer short. 这就是为什么我将答案简短的原因。

Unfortunately, I don't see an easy and reasonable way to use DataController s Mapper in your case. 不幸的是,在您的情况下,我没有看到一种简单合理的方法来使用DataControllerMapper It means that you will need to hack it one way or the other. 这意味着您将需要以一种或另一种方式来破解它。

Also, normally, Mapper is being used only in downcast conversion, so only when you do DataController#toView . 同样,通常, Mapper仅用于向下转换,因此仅当您执行DataController#toView Keep that in mind. 记住这一点。 There was never any need for us to use Mapper when loading data. 加载数据时,我们从不需要使用Mapper

I am thinking of the easiest way to hack it, but to be honest, I think that you will have to modify DataController or just prepare your bit of a code that does similar stuff as DataController#toView . 我正在考虑一种最简单的方法来破解它,但是老实说,我认为您将不得不修改DataController或只是准备一段与DataController#toView相似的代码。 Create your own instance of Mapper and don't clear bindings in your code. 创建您自己的Mapper实例,并且不要清除代码中的绑定。

Alternatively, you could create your own MyMapper and MyDataController classes that would extend CKE5 classes. 另外,您可以创建自己的MyMapperMyDataController类,以扩展CKE5类。 MyDataController could overwrite #mapper property -> this.mapper = new MyMapper() . MyDataController可以覆盖#mapper属性-> this.mapper = new MyMapper() And MyMapper could send some kind of event when it is cleared. MyMapper被清除时,它可以发送某种事件。 On that event, you could copy the mapper data for your use. 在那种情况下,您可以复制映射器数据供您使用。

The last option would be to create an issue on CKE5 Github. 最后一个选择是在CKE5 Github上创建问题。 I could see that in the DataController#toView the clearing could be moved to the beginning of the method. 我可以看到在DataController#toView ,清除可以移动到方法的开头。 There should be no detrimental results of that change and the mapper could be used until the next toView call. 该更改不应有任何有害结果,并且可以在下一次toView调用之前使用该映射器。 If you create the issue we might discuss this change internally and maybe implement it. 如果您创建问题,我们可能会在内部讨论此更改并实施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM