diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 88844528e0..7e67d7d5ef 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -2322,11 +2322,22 @@ (or (> (:row cell) rows) (> (:column cell) cols))))) cells))))) +(defn- assign-in-main-order + "`shape` with its children without a cell assigned free cells in the order + of the main shapes they refer to in `shape-main`, as sync adds them." + [shape objects shape-main] + (let [rank (into {} (map-indexed (fn [index id] [id index])) (:shapes shape-main)) + main-order (vec (sort-by #(get rank (:shape-ref (get objects %)) ##Inf) (:shapes shape)))] + (-> shape + (assoc :shapes main-order) + (ctsl/assign-cells objects) + (assoc :shapes (:shapes shape))))) + (defn- assign-inherited-grid-cells - [shape before objects] + [shape before objects shape-main] (if-not (or (< (count (:layout-grid-rows shape)) (count (:layout-grid-rows before))) (< (count (:layout-grid-columns shape)) (count (:layout-grid-columns before)))) - (ctsl/assign-cells shape objects) + (assign-in-main-order shape objects shape-main) (let [active? (some #(not (ctsl/position-absolute? objects %)) (:shapes shape)) ;; Like assign-cells, a grid with participating children needs at ;; least one track per axis, including an initially empty grid. @@ -2341,7 +2352,7 @@ (ctsl/check-deassigned-cells objects) (ctsl/fit-cells-to-tracks) ;; a child without a free slot grows a default track to hold it - (ctsl/assign-cells objects))))) + (assign-in-main-order objects shape-main))))) (defn- keep-cell-ids "`shape` with the cells its tracks add, those its main `shape-main` lacks, @@ -2439,7 +2450,7 @@ (-> shape-copy (assoc :layout-grid-cells copy-cells) (add-inherited-grid-cells shape-before) - (assign-inherited-grid-cells shape-before objects)) + (assign-inherited-grid-cells shape-before objects shape-main)) ;; Take cells from main and remap the shapes to assign it to the copy (copy-grid-cells shape-copy shape-main objects)))) {:ignore-touched true :with-objects? true}))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 635116c404..780590be1e 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2592,6 +2592,12 @@ :layout-copies false :text false}] :ops [[:grid-cell 0 15 0 1] [:grid-track 0 2]]})))) +(t/deftest a-grid-copy-gives-free-cells-to-children-in-their-main-order + (t/is (map? (cg/run {:world [3 1 {:variants false :layout :grid :layout-item-margins false :grid-square false + :rich-variants false :layout-children true :grid-empty false + :layout-copies false :text true}] + :ops [[:grid-cell 13] [:grid-track 0 0 11] [:grid-track 0 3] [:add-child 8] [:grid-track 0 2]]})))) + (t/deftest settling-a-bool-in-a-grid-copy-twice-keeps-its-shapes-in-place (t/is (map? (cg/run {:world [3 0 {:variants true :rich-variants true :text true :layout :grid :layout-copies true :layout-children true}]