From b313aa47ce7962be4e091e0898c5c6d765c7182a Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 10 Mar 2017 10:36:40 +0100 Subject: [PATCH] Properly remove empty gropups after relocation. Related to #72. --- frontend/src/uxbox/main/data/shapes_impl.cljs | 13 +++++- .../tests/test_main_data_shapes_impl.cljs | 46 +++++++++++++++++-- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/frontend/src/uxbox/main/data/shapes_impl.cljs b/frontend/src/uxbox/main/data/shapes_impl.cljs index e15be75d37..74fbf60109 100644 --- a/frontend/src/uxbox/main/data/shapes_impl.cljs +++ b/frontend/src/uxbox/main/data/shapes_impl.cljs @@ -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)] diff --git a/frontend/test/uxbox/tests/test_main_data_shapes_impl.cljs b/frontend/test/uxbox/tests/test_main_data_shapes_impl.cljs index f62a789394..546aea586f 100644 --- a/frontend/test/uxbox/tests/test_main_data_shapes_impl.cljs +++ b/frontend/test/uxbox/tests/test_main_data_shapes_impl.cljs @@ -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)] - ;; (pprint expected) - ;; (pprint result) - (t/is (= result expected))))) + 3 {:id 3 :page 1 :type :group :items [2]}}} + result (impl/degroup-shapes initial [1] 1)] + ;; (pprint expected) + ;; (pprint result) + (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