0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-25 07:58:49 -05:00

📚 Add better docstring to group creation internal function.

This commit is contained in:
Andrey Antukh 2021-07-27 12:01:46 +02:00 committed by Andrés Moya
parent bff35de39f
commit 08062e8ce8

View file

@ -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))