From e84a462ddecd648d6fc4cdf12ec10a62d8596f4a Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 30 Apr 2020 14:53:36 +0200 Subject: [PATCH] :bug: Refactor shape attrs updating. --- frontend/src/uxbox/main/data/workspace.cljs | 43 +++++++++---------- .../src/uxbox/main/data/workspace/texts.cljs | 20 ++++++--- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index 255b0487f7..633c0a140d 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -607,25 +607,26 @@ [id attrs] (us/verify ::us/uuid id) (us/verify ::shape-attrs attrs) - (ptk/reify ::update-shape - dwc/IBatchedChange - dwc/IUpdateGroup - (get-ids [_] [id]) + (letfn [(update-shape [shape] + (cond-> (merge shape attrs) + (and (= :text (:type shape)) + (string? (:fill-color attrs))) + (dwtxt/impl-update-shape-attrs {:fill (:fill-color attrs)})))] + (ptk/reify ::update-shape + dwc/IBatchedChange + dwc/IUpdateGroup + (get-ids [_] [id]) - ptk/UpdateEvent - (update [_ state] - (let [page-id (:current-page-id state) - grouped #{:frame :group}] - (update-in state [:workspace-data page-id :objects] - (fn [objects] - (let [childs (d/concat [id] (helpers/get-children id objects))] - (reduce (fn [objects id] - (let [obj (get objects id)] - (if (contains? grouped (:type obj)) - objects - (update objects id merge attrs)))) - objects - childs)))))))) + ptk/UpdateEvent + (update [_ state] + (let [page-id (:current-page-id state) + grouped #{:frame :group}] + (update-in state [:workspace-data page-id :objects] + (fn [objects] + (->> (d/concat [id] (helpers/get-children id objects)) + (map #(get objects %)) + (remove #(grouped (:type %))) + (reduce #(update %1 (:id %2) update-shape) objects))))))))) ;; --- Update Page Options @@ -660,11 +661,7 @@ page-id (get-in state [:workspace-page :id])] (->> (rx/from selected) (rx/map (fn [id] - (let [shape (get-in state [:workspace-data page-id :objects id])] - (if (and (string? fill-color) - (= :text (:type shape))) - (dwtxt/update-text-attrs {:id id :attrs {:fill fill-color}}) - (update-shape-recursive id attrs)))))))))) + (update-shape-recursive id attrs)))))))) ;; --- Shape Movement (using keyboard shorcuts) diff --git a/frontend/src/uxbox/main/data/workspace/texts.cljs b/frontend/src/uxbox/main/data/workspace/texts.cljs index 3563709850..52a91d3b93 100644 --- a/frontend/src/uxbox/main/data/workspace/texts.cljs +++ b/frontend/src/uxbox/main/data/workspace/texts.cljs @@ -141,8 +141,20 @@ node attrs)) -(defn- update-attrs - [{:keys [id editor attrs pred split]}] +(defn impl-update-shape-attrs + ([shape attrs] + ;; NOTE: this arity is used in workspace for properly update the + ;; fill color using colorpalette, then the predicate should be + ;; defined. + (impl-update-shape-attrs shape attrs is-text-node?)) + ([{:keys [type content] :as shape} attrs pred] + (assert (= :text type) "should be shape type") + (let [merge-attrs #(merge-attrs % attrs)] + (update shape :content #(transform-nodes pred merge-attrs %))))) + +(defn update-attrs + [{:keys [id editor attrs pred split] + :or {pred is-text-node?}}] (if editor (ptk/reify ::update-attrs ptk/EffectEvent @@ -156,9 +168,7 @@ (let [page-id (get-in state [:workspace-page :id]) merge-attrs #(merge-attrs % attrs)] (update-in state [:workspace-data page-id :objects id] - (fn [{:keys [type content] :as shape}] - (assert (= :text type) "should be shape type") - (update shape :content #(transform-nodes pred merge-attrs %))))))))) + #(impl-update-shape-attrs % attrs pred))))))) (defn update-text-attrs [options]