From 08062e8ce8ffbd090391f666dce7b84eb0e41d2e Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 27 Jul 2021 12:01:46 +0200 Subject: [PATCH] :books: Add better docstring to group creation internal function. --- .../src/app/main/data/workspace/groups.cljs | 87 ++++++++++--------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/frontend/src/app/main/data/workspace/groups.cljs b/frontend/src/app/main/data/workspace/groups.cljs index fc88141b3..a49b8c0b0 100644 --- a/frontend/src/app/main/data/workspace/groups.cljs +++ b/frontend/src/app/main/data/workspace/groups.cljs @@ -36,8 +36,14 @@ (gsh/setup selrect) (assoc :shapes (mapv :id shapes))))) -(defn get-empty-groups - "Retrieve emtpy groups after group creation" +(defn- get-empty-groups-after-group-creation + "An auxiliar function that finds and returns a set of ids that + corresponds to groups that should be deleted after a group creation. + + The corner case happens when you selects two (or more) shapes that + belongs each one to different groups, and after creating the new + group, one (or many) groups can become empty because they have had a + single shape which is moved to the created group." [objects parent-id shapes] (let [ids (cp/clean-loops objects (into #{} (map :id) shapes)) parents (->> ids @@ -72,55 +78,56 @@ (defn prepare-create-group [objects page-id shapes prefix keep-name] - (let [group (make-group shapes prefix keep-name) - frame-id (:frame-id (first shapes)) + (let [group (make-group shapes prefix keep-name) + frame-id (:frame-id (first shapes)) parent-id (:parent-id (first shapes)) - rchanges [{:type :add-obj - :id (:id group) - :page-id page-id - :frame-id frame-id - :parent-id parent-id - :obj group - :index (::index (first shapes))} - {:type :mov-objects - :page-id page-id - :parent-id (:id group) - :shapes (mapv :id shapes)}] + rchanges [{:type :add-obj + :id (:id group) + :page-id page-id + :frame-id frame-id + :parent-id parent-id + :obj group + :index (::index (first shapes))} - uchanges (-> (mapv - (fn [obj] - {:type :mov-objects - :page-id page-id - :parent-id (:parent-id obj) - :index (::index obj) - :shapes [(:id obj)]}) shapes) - (conj - {:type :del-obj - :id (:id group) - :page-id page-id})) + {:type :mov-objects + :page-id page-id + :parent-id (:id group) + :shapes (mapv :id shapes)}] - ids-to-delete (get-empty-groups objects parent-id shapes) + uchanges (-> (mapv (fn [obj] + {:type :mov-objects + :page-id page-id + :parent-id (:parent-id obj) + :index (::index obj) + :shapes [(:id obj)]}) + shapes) + (conj {:type :del-obj + :id (:id group) + :page-id page-id})) + + ;; Look at the `get-empty-groups-after-group-creation` + ;; doctring to understand the real purpuse of this code + ids-to-delete (get-empty-groups-after-group-creation objects parent-id shapes) delete-group (fn [changes id] - (-> changes - (conj {:type :del-obj - :id id - :page-id page-id}))) + (conj changes {:type :del-obj + :id id + :page-id page-id})) add-deleted-group (fn [changes id] (let [obj (-> (get objects id) - (d/without-keys [:shapes]))] - - (d/concat [{:type :add-obj - :id id - :page-id page-id - :frame-id (:frame-id obj) - :parent-id (:parent-id obj) - :obj obj - :index (::index obj)}] changes))) + (dissoc :shapes))] + (into [{:type :add-obj + :id id + :page-id page-id + :frame-id (:frame-id obj) + :parent-id (:parent-id obj) + :obj obj + :index (::index obj)}] + changes))) rchanges (->> ids-to-delete (reduce delete-group rchanges))