From a591e4539c2a5253fe2178dc92624fa7dd89bccc Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 15 Apr 2020 14:47:06 +0200 Subject: [PATCH] :recycle: Refactor `add-shape` event. And remove `add-frame` because `add-shape` is now generic. --- frontend/src/uxbox/main/data/workspace.cljs | 90 +++++++------------ .../src/uxbox/main/ui/workspace/drawarea.cljs | 5 +- 2 files changed, 32 insertions(+), 63 deletions(-) diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index 752d4c8b5..a91837cd1 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -953,21 +953,6 @@ (recur (inc counter)) candidate))))) -(defn impl-assoc-shape - "Add a shape to the current workspace page, inside a given frame. - Give it a name that is unique in the page" - [state {:keys [id frame-id] :as data}] - (let [page-id (::page-id state) - objects (get-in state [:workspace-data page-id :objects]) - name (impl-generate-unique-name objects (:name data)) - shape (assoc data :name name) - page-id (::page-id state)] - (-> state - (update-in [:workspace-data page-id :objects frame-id :shapes] conj id) - (update-in [:workspace-data page-id :objects] assoc id shape)))) - -(declare select-shape) - (defn- calculate-frame-overlap [objects shape] (let [rshp (geom/shape->rect-shape shape) @@ -987,53 +972,40 @@ (defn add-shape [attrs] (us/verify ::shape-attrs attrs) - (let [id (uuid/next)] - (ptk/reify ::add-shape - ptk/UpdateEvent - (update [_ state] - (let [page-id (::page-id state) - objects (get-in state [:workspace-data page-id :objects]) - shape (-> (geom/setup-proportions attrs) - (assoc :id id)) - frame-id (calculate-frame-overlap objects shape) - shape (merge cp/default-shape-attrs shape {:frame-id frame-id})] - (-> state - (impl-assoc-shape shape) - (assoc-in [:workspace-local :selected] #{id})))) + (ptk/reify ::add-shape + ptk/WatchEvent + (watch [_ state stream] + (let [page-id (::page-id state) + objects (get-in state [:workspace-data page-id :objects]) - ptk/WatchEvent - (watch [_ state stream] - (let [page-id (::page-id state) - obj (get-in state [:workspace-data page-id :objects id])] - (rx/of (commit-changes [{:type :add-obj - :id id - :frame-id (:frame-id obj) - :obj obj}] - [{:type :del-obj - :id id}]))))))) + id (uuid/next) + shape (geom/setup-proportions attrs) -(defn add-frame - [data] - (us/verify ::shape-attrs data) - (let [id (uuid/next)] - (ptk/reify ::add-frame - ptk/UpdateEvent - (update [_ state] - (let [shape (-> (geom/setup-proportions data) - (assoc :id id)) - shape (merge cp/default-frame-attrs shape)] - (impl-assoc-shape state shape))) + unames (retrieve-used-names objects) + name (generate-unique-name unames (:name shape)) - ptk/WatchEvent - (watch [_ state stream] - (let [page-id (::page-id state) - obj (get-in state [:workspace-data page-id :objects id])] - (rx/of (commit-changes [{:type :add-obj - :id id - :frame-id (:frame-id obj) - :obj obj}] - [{:type :del-obj - :id id}]))))))) + frame-id (if (= :frame (:type shape)) + uuid/zero + (calculate-frame-overlap objects shape)) + + shape (merge + (if (= :frame (:type shape)) + cp/default-frame-attrs + cp/default-shape-attrs) + (assoc shape + :id id + :name name + :frame-id frame-id)) + + rchange {:type :add-obj + :id id + :frame-id frame-id + :obj shape} + uchange {:type :del-obj + :id id}] + + (rx/of (commit-changes [rchange] [uchange] {:commit-local? true}) + (select-shapes #{id})))))) ;; --- Duplicate Shapes diff --git a/frontend/src/uxbox/main/ui/workspace/drawarea.cljs b/frontend/src/uxbox/main/ui/workspace/drawarea.cljs index 81243043f..300c39ed7 100644 --- a/frontend/src/uxbox/main/ui/workspace/drawarea.cljs +++ b/frontend/src/uxbox/main/ui/workspace/drawarea.cljs @@ -275,10 +275,7 @@ shape) shape (dissoc shape ::initialized? :resize-modifier)] ;; Add & select the created shape to the workspace - (rx/of dw/deselect-all - (if (= :frame (:type shape)) - (dw/add-frame shape) - (dw/add-shape shape)))))))))) + (rx/of dw/deselect-all (dw/add-shape shape))))))))) (def close-drawing-path (ptk/reify ::close-drawing-path