automerge-classic icon indicating copy to clipboard operation
automerge-classic copied to clipboard

Allow a document to be reverted back to a previous version

Open ept opened this issue 3 years ago • 0 comments

Automerge.getHistory allows you to inspect past versions of a document, but it does not provide a mechanism for reverting a document to a past version without discarding the change history since that version. It would be good to have some way of generating an "inverse change" which can be applied to a document in order to revert those changes. This is also related to undo (#318). Here are some ideas on how to do that:

  • The simplest way of doing this is probably to generate the patch corresponding to the changes that should be undone, and translating this into a new change that reverses the patch. This is probably simpler than doing it at the level of individual operations.
  • It might be desirable to introduce optimisations: for example, say the patch includes a bunch of list items being inserted and then removed again. The inverse patch could avoid re-inserting and re-deleting all those items since the operations cancel out. However, detecting this situation would complicate the logic and so maybe it's best to leave out this optimisation in the first version.
  • Another difficulty arises if the user performs more than one revert. Say you have an editing history containing versions A, B, C. The user first reverts to B by inverting C. Let's say C^ is the inverse of C, so the history becomes A B C C^. Then the user wants to revert to A. Ideally we should detect that we only have to invert B, so the history should become A B C C^ B^. However, if this is not done carefully, we would revert all the changes between A and the head, i.e. we would revert B C C^, so the history would become A B C C^ C C^ B^. This could get messy quickly…
  • Another question is whether to implement the revert logic in the frontend or backend. In the backend would have the advantage that it would only have to be implemented twice (JS and Rust), rather than in every programming language we want to support.

ept avatar May 07 '21 10:05 ept