ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Make `model.Model#_beforeChanges` and `_afterChanges` event public

Open scofalik opened this issue 2 years ago • 3 comments

📝 Provide a description of the improvement

Currently, there's no nice way to provide a callback that would be fired before a change is being applied on the model.

We can listen to model.Document#change event but that's after all changes are applied.

We can listen to applyOperation on highest priority but that's quite low level, and hacky, and a bit "too late" as well, as you might want to do something before any action is done and before any operation is generated.

We have an internal _beforeChanges event, and also _afterChanges event, in model. I see no reason why they have to be internal and I'd like to suggest making them public.

The event is fired before the first model.change() or model.enqueueChange() callback is called. Even if you have multiple nested enqueueChange() blocks, it will be fired just one, before the first one. Same for _afterChanges (just after all is done, it is also fired after post fixers and document change event).

AFAIU, it should be also possible to use model.change() inside such callback and it will be fired before first original changes. This should be check as it may be important to have it working this way.

It will be useful for any features that need to do some cleanup or setup before some changes are done.

scofalik avatar Aug 25 '23 13:08 scofalik

Maybe a very basic question, but what would be some scenarios where I would want to listen to beforeChanges and act upon them? Is there anything outside of the core editing engine, a consumer would be interested in doing before the changes happen?

urbanspr1nter avatar Aug 26 '23 00:08 urbanspr1nter

This is a more advanced place to hook into, so only very specific features would want to use it. As I mentioned, in case if you want to check, setup, make a snapshot, or cleanup something, it may be useful. Right now, there's no place that you could plug into, if you have such a need. It's difficult to capture the start of an action that will lead to changes in the document and capture the end of it.

You can use model document change event but:

  1. it is fired after changes;
  2. you can't be sure if that's the end of changes (you may got more change events if there were enqueueChanges used, it may happen if in case of some complicated features).

BTW. _beforeChanges and _afterChanges are quite interchangeable and possibly one of them is enough.

Features that would want to use it probably need to integrate with other features without knowing about them. Or, you might want to customize a feature that does some changes in the model but you can't find a good place to hook into.

One such problem we have in track changes right now, where we want to be sure that all changes done as a result of one user action are grouped together.

scofalik avatar Aug 28 '23 12:08 scofalik

There has been no activity on this issue for the past year. We've marked it as stale and will close it in 30 days. We understand it may still be relevant, so if you're interested in the solution, leave a comment or reaction under this issue.

CKEditorBot avatar Aug 28 '24 01:08 CKEditorBot

We've closed your issue due to inactivity. We understand that the issue may still be relevant. If so, feel free to open a new one (and link this issue to it).

CKEditorBot avatar Oct 01 '24 23:10 CKEditorBot