"The present is the least important time we live in" --Alan Kay
A drop-in atom-state-management (UNDOs!!) for your clojurescript projects.
Also supports clojure in case you would want to make similar applications, or simply for testing.
Table of contents
Add the following dependency in your
And require historian in your namespace:
(ns my-ns (:require [historian.core :as hist]))
Suppose you have your state in an atom
(def my-state (atom "ABC"))
To keep an history of all changes, simply add your atom to historian:
(hist/record! my-state :my-state) ;; then change the state of your atom (reset! my-state "DEF") @my-state => "DEF" (hist/undo!) @my-state => "ABC" (hist/redo!) @my-state => "DEF" ;; tada!
Of course, sometimes we want to do some things without anyone noticing...
;; our current state is "ABC" (hist/off-the-record (reset! my-state "GHI")) ;; <--- this change won't be added to the undo history (reset! my-state "ZZZ") (hist/undo!) @my-state => "ABC"
If you have a bunch of operations initiated by a single user action:
(hist/with-single-record (doseq [i (range 200)] (reset! my-state i))) ;; We've just done 200 operations on the atom, but only the last state is recorded. (hist/undo!) @my-state => "ABC"
To check if any undo/redo history is available, use
When loading an app with multiple atoms, you should use
trigger-record! to start with a clean slate.
Keyboard Shortcuts (cljs)
You can bind
ctrl-y to undo and redo by using
bind-keys in the
(ns some-ns (:require [reagent.core :refer [atom]] [historian.core :as hist])) ;; for undos: (hist/replace-library! (atom )) ; <----- the new atom must be a vector. ;; for redos: (hist/replace-prophecy! (atom )) ; <----- the new atom must be a vector.