From e32188f59322ad54c03289c1f5f8b975fe11d2e8 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sun, 3 Apr 2016 10:07:26 +0300 Subject: [PATCH] Improved history state layout. --- src/uxbox/data/history.cljs | 42 +++++++++++++++++---- src/uxbox/ui/workspace/sidebar/history.cljs | 12 ++---- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/uxbox/data/history.cljs b/src/uxbox/data/history.cljs index a44c5b003..6fe878d4e 100644 --- a/src/uxbox/data/history.cljs +++ b/src/uxbox/data/history.cljs @@ -19,14 +19,20 @@ [uxbox.state.project :as stpr] [uxbox.ui.messages :as uum] [uxbox.util.datetime :as dt] - [uxbox.util.data :refer (without-keys replace-by-id)])) + [uxbox.util.data :refer (without-keys + replace-by-id + index-by)])) ;; --- Pinned Page History Fetched +(declare update-history-index) + (defrecord PinnedPageHistoryFetched [history] rs/UpdateEvent (-apply-update [_ state] - (assoc-in state [:workspace :history :pinned-items] history))) + (-> state + (assoc-in [:workspace :history :pinned-items] (mapv :version history)) + (update-history-index history true)))) ;; --- Fetch Pinned Page History @@ -52,12 +58,22 @@ (defrecord PageHistoryFetched [history append?] rs/UpdateEvent (-apply-update [_ state] - (let [items (into [] history) - minv (apply min (map :version history)) - state (assoc-in state [:workspace :history :min-version] minv)] - (if-not append? - (assoc-in state [:workspace :history :items] items) - (update-in state [:workspace :history :items] #(reduce conj % items)))))) + (letfn [(update-counters [state items] + (-> (assoc state :min-version (apply min items)) + (assoc :max-version (apply max items)))) + + (update-lists [state items] + (if append? + (update state :items #(reduce conj %1 items)) + (assoc state :items items)))] + + (let [items (mapv :version history) + hstate (-> (get-in state [:workspace :history] {}) + (update-counters items) + (update-lists items))] + (-> state + (assoc-in [:workspace :history] hstate) + (update-history-index history append?)))))) ;; --- Fetch Page History @@ -217,3 +233,13 @@ (defn update-history-item [item] (UpdateHistoryItem. item)) + +;; --- Helpers + +(defn- update-history-index + [state history append?] + (let [index (index-by history :version)] + (if append? + (update-in state [:workspace :history :by-version] merge index) + (assoc-in state [:workspace :history :by-version] index)))) + diff --git a/src/uxbox/ui/workspace/sidebar/history.cljs b/src/uxbox/ui/workspace/sidebar/history.cljs index 703a958b6..2e533ea5e 100644 --- a/src/uxbox/ui/workspace/sidebar/history.cljs +++ b/src/uxbox/ui/workspace/sidebar/history.cljs @@ -81,7 +81,8 @@ :on-click on-select} [:div.pin-icon i/pin] [:span (str "Version " (:version page) " (current)")]] - (for [item (:items history)] + (for [version (:items history) + :let [item (get-in history [:by-version version])]] (-> (history-item item selected) (rum/with-key (str (:id item))))) (if show-more? @@ -113,16 +114,11 @@ [own history] (html [:ul.history-content - (for [item (:pinned-items history)] + (for [version (:pinned-items history) + :let [item (get-in history [:by-version version])]] (-> (history-item item (:selected history)) (rum/with-key (str (:id item)))))])) - ;; [:li - ;; [:span "Version 02/02/2016 12:33h"] - ;; [:div.page-actions - ;; [:a i/pencil] - ;; [:a i/trash]]]])) - (def history-pinned-list (mx/component {:render history-pinned-list-render