0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-11 23:31:21 -05:00

Properly remove empty gropups after relocation.

Related to #72.
This commit is contained in:
Andrey Antukh 2017-03-10 10:36:40 +01:00
parent 13e02283d8
commit b313aa47ce
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
2 changed files with 53 additions and 6 deletions

View file

@ -419,6 +419,16 @@
(assert (not (nil? parent-id)) "parent-id should never be nil here")
(update-in state [:shapes parent-id :items] #(into [] (remove #{id}) %)))
(strip-empty-groups [state parent-id]
(if (nil? parent-id)
state
(let [group (get-in state [:shapes parent-id])]
(if (empty? (:items group))
(-> state
(remove-group group)
(strip-empty-groups (:group group)))
state))))
(selective-degroup [state [shape & rest :as shapes]]
(let [group (get-in state [:shapes (:group shape)])
position (get-relocation-position state group)
@ -429,7 +439,8 @@
(-> state
(relocate-shape shape-id parent-id position)
(remove-from-parent shape-id (:id group))))
$ shapes))))]
$ (reverse shapes))
(strip-empty-groups $ (:id group)))))]
(let [shapes (into #{} (map #(get-in state [:shapes %])) shapes)
groups (into #{} (filter #(= (:type %) :group)) shapes)
parents (into #{} (map :group) shapes)]

View file

@ -455,11 +455,47 @@
:pages {1 {:id 1 :shapes [1 3]}}
:shapes {1 {:id 1 :page 1}
2 {:id 2 :page 1 :group 3}
3 {:id 3 :page 1 :type :group :items [2]}}}]
(let [result (impl/degroup-shapes initial [1] 1)]
3 {:id 3 :page 1 :type :group :items [2]}}}
result (impl/degroup-shapes initial [1] 1)]
;; (pprint expected)
;; (pprint result)
(t/is (= result expected)))))
(t/is (= result expected))))
;; degroup all shapes from group
(t/deftest degroup-shapes-1-2
(let [initial {:pages {1 {:id 1 :shapes [3]}}
:shapes {1 {:id 1 :page 1 :group 3}
2 {:id 2 :page 1 :group 3}
3 {:id 3 :page 1 :type :group :items [1 2]}}}
expected {:workspace {:selected #{1 2}}
:pages {1 {:id 1 :shapes [1 2]}}
:shapes {1 {:id 1 :page 1}
2 {:id 2 :page 1}}}
result (impl/degroup-shapes initial [1 2] 1)]
;; (pprint expected)
;; (pprint result)
(t/is (= result expected))))
;; degroup all shapes from neested group
(t/deftest degroup-shapes-1-3
(let [initial {:pages {1 {:id 1 :shapes [4]}}
:shapes {1 {:id 1 :page 1 :group 3}
2 {:id 2 :page 1 :group 3}
3 {:id 3 :page 1 :group 4 :type :group :items [1 2]}
4 {:id 4 :page 1 :type :group :items [3]}}}
expected {:workspace {:selected #{1 2}}
:pages {1 {:id 1 :shapes [4]}}
:shapes {1 {:id 1 :page 1 :group 4}
2 {:id 2 :page 1 :group 4}
4 {:id 4 :page 1 :type :group :items [1 2]}}}
result (impl/degroup-shapes initial [1 2] 1)]
;; (pprint expected)
;; (pprint result)
(t/is (= result expected))))
;; degroup group inside a group