ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Store operations in DB and then replay operations from database.

Open dustinormond opened this issue 5 years ago • 4 comments

I know that the operationreplayer exists, but I have a use case where I would like to store all the operations in a database and then replay all the operations to show the history of a document over time. Is it possible to store operations in a MySQL database and then load those operations in to the operation replayer on request?

I know that I can get the batch of operations using 'change:data'; however, they lose information when stored so when I reload these operations, they aren't the same.

For example, here is what I see on the change event: image

...but this is what is reloaded: image

Am I missing something?

dustinormond avatar Jan 25 '21 03:01 dustinormond

OperationReplayer was created as a debug tool to be used when working on some specific issues, so it may not have some expected functionalities. You can use it (or get inspired by it) but we do not encourage using dev utils in production features. We use them for testing purposes.

As far as operations go, you are right that the information about the batch is gone. When an operation is casted to plain object (and/or JSON) we only store the information required to describe the change introduced by the operation. Batch is not needed for that, and, for example, it has no use when the operation is broadcasted from one user to another. As of now, batch has meaning only for a local user and only during current editing session. It may change in the future, but this is the current state. If you'd like to use information about batch in your feature, you have to implement this part by yourself.

Keep in mind that if you are storing operations - there are three risks that may happen over time when you go with this approach:

  1. Operation class may change (API/internals) and the data format may start missing something. This is less likely but still can happen as we develop the editor.
  2. The editor model for existing feature may change and your operations will become outdated. So, your operations will create an incorrect model. It could lead to an editor crash or unexpected data loss.
  3. You may want to change your editor configuration (for example, remove a feature). This is a similar problem as described above.

This is why we encourage to save the editor data (HTML or Markdown) instead of the model or operations. Even as the editor changes over time, the saved data will be supported. You may experience data loss (in some obvious cases, for example if you turn off a plugin) but the editor data will be always "correct".

scofalik avatar Jan 25 '21 09:01 scofalik

Thank you for the in-depth response. I will stick with what you suggest.

One follow up question, how would I apply the changes from an operation casted to JSON to an existing model? The method applyOperation() doesn't seem to work and as the documentation suggests shouldn't be used for anything except maybe undo. I know that writer is suggested instead, but is there an easy way to recognize the type of operation and make that change to an existing model.

dustinormond avatar Jan 25 '21 14:01 dustinormond

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

CKEditorBot avatar Sep 06 '23 05:09 CKEditorBot

We've closed your issue due to inactivity over the last year. 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 13 '23 05:10 CKEditorBot