简体   繁体   中英

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.

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.

a) Is there a way to get the Mapper from the DataController (eg an event)?

b) If I have to build my own Mapper then what is the best way to do that?

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.

Update (with more context) : Basically this question is a follow up on using the model in a DataProcessor in the toData method.

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). 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. And now, we arrive to this question.

My plan for doing the conversion is as follows:

a) Convert the viewFragment to a 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.

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.

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. 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.

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. 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. 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 . Keep that in mind. There was never any need for us to use Mapper when loading data.

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 . Create your own instance of Mapper and don't clear bindings in your code.

Alternatively, you could create your own MyMapper and MyDataController classes that would extend CKE5 classes. MyDataController could overwrite #mapper property -> this.mapper = new MyMapper() . And MyMapper could send some kind of event when it is cleared. On that event, you could copy the mapper data for your use.

The last option would be to create an issue on CKE5 Github. I could see that in the DataController#toView the clearing could be moved to the beginning of the method. There should be no detrimental results of that change and the mapper could be used until the next toView call. If you create the issue we might discuss this change internally and maybe implement it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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