evenorog / undo

An undo-redo library.
https://docs.rs/undo
Apache License 2.0
49 stars 6 forks source link

Question: How to store data for undo? #15

Closed porky11 closed 1 year ago

porky11 commented 2 years ago

In the examples, undo always just removes the last symbol from the string.

When I also have a Remove action, I would need to store, which letter has been removed in order to undo that action.

I didn't find anything about that in the documentation. It has to be possible already. Else this crate wouldn't be very useful.

evenorog commented 2 years ago

In the example you can see the removed character being stored in the command.

This line: self.0 = s.pop().ok_or("s is empty")?; stores the removed character in self.0.

TedDriggs commented 2 years ago

What if someone has an action enum with two variants: The Add from the example, and one that deletes n characters starting at a given offset? Should the action store the removed characters when apply is called so it can put them back if undo is later invoked?

evenorog commented 2 years ago

@TedDriggs Yes that is what I would do