diff --git a/src/re_frame/undo.cljs b/src/re_frame/undo.cljs index a02e708..163bfab 100644 --- a/src/re_frame/undo.cljs +++ b/src/re_frame/undo.cljs @@ -30,21 +30,29 @@ (def ^:private undo-explain-list (reagent/atom [])) ;; mirrors undo-list (def ^:private redo-explain-list (reagent/atom [])) ;; mirrors redo-list +(defn- clear-undos! + [] + (reset! undo-list []) + (reset! undo-explain-list [])) + + +(defn- clear-redos! + [] + (reset! redo-list []) + (reset! redo-explain-list [])) + (defn clear-history! [] - (reset! undo-list []) - (reset! redo-list []) - (reset! undo-explain-list []) - (reset! redo-explain-list []) + (clear-undos!) + (clear-redos!) (reset! app-explain "")) (defn store-now! "stores the value currently in app-db, so the user can later undo" [explanation] - (reset! redo-list []) ;; clear and redo state created by previous undos - (reset! redo-explain-list []) ;; clear and redo state created by previous undos + (clear-redos!) (reset! undo-list (vec (take @max-undos (conj @undo-list @app-db)))) @@ -53,6 +61,7 @@ (conj @undo-explain-list @app-explain)))) (reset! app-explain explanation)) + (defn undos? [] (pos? (count @undo-list))) @@ -121,10 +130,13 @@ (handlers/register-base ;; not a pure handler :undo ;; usage: (dispatch [:undo n]) (fn handler - [_ [_ n]] ;; if n absent, defaults to 1 + [_ [_ n clear-redos?]] + ;; if n absent, defaults to 1. If clear-redos? absent, defaults to false. (if-not (undos?) (warn "re-frame: you did a (dispatch [:undo]), but there is nothing to undo.") - (undo-n (or n 1))))) + (do + (undo-n (or n 1)) + (when clear-redos? (clear-redos!)))))) (defn- redo