diff --git a/common/src/app/common/logic/bool.cljc b/common/src/app/common/logic/bool.cljc index 3637af2885..a5f3d0b484 100644 --- a/common/src/app/common/logic/bool.cljc +++ b/common/src/app/common/logic/bool.cljc @@ -99,10 +99,30 @@ (create-bool-shape force-shape-id type name (reverse shapes) objects) shape-id - (get shape :id)] + (get shape :id) + + ;; the bool takes the grid cell of the shape whose place it takes + head-id + (:id (if (= type :difference) (last shapes) (first shapes))) + + parent-id + (:parent-id shape) + + target-cell + (when (ctl/grid-layout? objects parent-id) + (ctl/get-cell-by-shape-id (get objects parent-id) head-id)) + + grid-parents + (into #{} (comp (map :parent-id) (filter (partial ctl/grid-layout? objects))) shapes)] [shape (-> (pcb/empty-changes it page-id) (pcb/with-objects objects) (pcb/add-object shape {:index (inc index)}) (pcb/update-shapes (map :id shapes) ctl/remove-layout-item-data) - (pcb/change-parent shape-id shapes))])))) + (pcb/change-parent shape-id shapes) + (cond-> target-cell + (pcb/update-shapes + [parent-id] + (fn [parent] + (assoc-in parent [:layout-grid-cells (:id target-cell) :shapes] [shape-id])))) + (pcb/update-shapes grid-parents ctl/assign-cells {:with-objects? true}))])))) diff --git a/common/test/common_tests/logic/groups_test.cljc b/common/test/common_tests/logic/groups_test.cljc index c8d18bd6b6..ee211dabf7 100644 --- a/common/test/common_tests/logic/groups_test.cljc +++ b/common/test/common_tests/logic/groups_test.cljc @@ -13,6 +13,7 @@ [app.common.test-helpers.files :as thf] [app.common.test-helpers.ids-map :as thi] [app.common.test-helpers.shapes :as ths] + [app.common.types.shape.layout :as ctl] [app.common.uuid :as uuid] [clojure.test :as t])) @@ -79,3 +80,23 @@ (t/is (= :bool (:type (get (objects file) (:id shape))))) (t/is (= #{(thi/id :rect-a) (thi/id :rect-b)} (set (:shapes (get (objects file) (:id shape)))))) (t/is (nil? (clb/generate-create-bool nil (:id page) (:objects page) [(thi/id :board)] :union nil))))) + +(t/deftest a-bool-in-a-grid-takes-the-cell-of-its-last-shape + (let [file (-> (thf/sample-file :file1) + (tho/add-frame :grid :layout :grid + :layout-grid-rows [{:type :flex :value 1}] + :layout-grid-columns [{:type :flex :value 1} {:type :flex :value 1}]) + (ths/add-sample-shape :rect-a :parent-label :grid) + (ths/add-sample-shape :rect-b :parent-label :grid) + (ths/update-shape :grid :layout-grid-cells {}) + (as-> file (let [page (thf/current-page file)] + (ths/update-shape file :grid :layout-grid-cells + (:layout-grid-cells + (ctl/assign-cells (get (:objects page) (thi/id :grid)) + (:objects page))))))) + page (thf/current-page file) + ids (map thi/id [:rect-a :rect-b]) + [shape changes] (clb/generate-create-bool nil (:id page) (:objects page) ids :union nil) + file (thf/apply-changes file changes) + grid (get (objects file) (thi/id :grid))] + (t/is (some #(= [(:id shape)] (:shapes %)) (vals (:layout-grid-cells grid))))))