react-undo-redo icon indicating copy to clipboard operation
react-undo-redo copied to clipboard

Add support to group actions?

Open mrcoles opened this issue 2 years ago • 2 comments
trafficstars

Hi, thanks for this repo! Feature request—it’d be really nice if I could group actions in the history, so one or more get grouped in a single undo action.

An example use-case would be when an element’s position is stored in state along with other attributes and I might want a sequence of position changes to group together while other attribute updates would be individual states in the history.

In the meantime, my workarounds are either trying to manage additional state in a separate context (which solves a lot of the scenarios, but eventually gets dodgy) or I could switch to redux and use redux-undo, which allows you set a custom grouping function.

mrcoles avatar Sep 18 '23 18:09 mrcoles

Hey @mrcoles, thanks for the idea. I have to think about this some more :) Currently, react-undo-redo does not make any assumptions about how your actions look like. I.e., also not that they have a type property. I somewhat like that. I could keep it that decoupled. Maybe I (we) can come up with an API that would keep this logic outside of the main lib but would still allow for grouping (because I like the sound of that).

frontendphil avatar Sep 26 '23 19:09 frontendphil

Agree, a generic function that assumes nothing about actions sounds best!

The redux-undo one takes an optional function of this type:

type GroupByFunction<S = any, A extends Action = AnyAction> = (action: A, currentState: S, previousHistory: StateWithHistory<S>) => any;

and it uses the return value as a groupBy key, unless the value is null, then it assumes it’s not grouping (IDK if undefined should be treated the same too?).

mrcoles avatar Oct 02 '23 19:10 mrcoles