diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 4d2e934e79..88844528e0 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -2298,6 +2298,30 @@ (ctsl/create-cells shape [column row 1 1]))) shape added))) +(defn- default-cell? + "Whether `cell` has nothing of its own: automatic, single and aligned by + default." + [cell] + (and (= :auto (:position cell)) + (= 1 (:row-span cell) (:column-span cell)) + (contains? #{nil :auto} (:align-self cell)) + (contains? #{nil :auto} (:justify-self cell)))) + +(defn- without-default-cells-beyond-tracks + "`shape` without its default cells past its tracks: their children take a + free slot again, or a track the grid grows for them, as when they first + came; the cells with something of their own there are fitted instead." + [shape] + (let [rows (count (:layout-grid-rows shape)) + cols (count (:layout-grid-columns shape))] + (update shape :layout-grid-cells + (fn [cells] + (into {} + (remove (fn [[_ cell]] + (and (default-cell? cell) + (or (> (:row cell) rows) (> (:column cell) cols))))) + cells))))) + (defn- assign-inherited-grid-cells [shape before objects] (if-not (or (< (count (:layout-grid-rows shape)) (count (:layout-grid-rows before))) @@ -2313,14 +2337,11 @@ (and active? (empty? (:layout-grid-columns shape))) (assoc :layout-grid-columns [ctsl/default-track-value]))] (-> target + (without-default-cells-beyond-tracks) (ctsl/check-deassigned-cells objects) (ctsl/fit-cells-to-tracks) - (ctsl/assign-cells objects) - ;; New children can exhaust the remaining slots. Fit any overflow - ;; back into inherited tracks instead of deriving extra tracks. - (assoc :layout-grid-rows (:layout-grid-rows target) - :layout-grid-columns (:layout-grid-columns target)) - (ctsl/fit-cells-to-tracks))))) + ;; a child without a free slot grows a default track to hold it + (ctsl/assign-cells objects))))) (defn- keep-cell-ids "`shape` with the cells its tracks add, those its main `shape-main` lacks, diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 65626fec29..635116c404 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2573,6 +2573,25 @@ :layout-copies true :layout-children false}] :ops [[:style 8 29 1] [:nest 6] [:switch 9]]})))) +(t/deftest a-grid-copy-with-its-own-cells-grows-a-column-for-a-new-child-for-good + (t/is (map? (cg/run {:world [2 2 {:variants true :layout :grid :layout-item-margins false :grid-square false + :rich-variants true :layout-children false :grid-empty true + :layout-copies true :text true}] + :ops [[:detach 0] [:grid-cell 0 0 0 1] [:grid-track 4 4 0 9] [:swap 7] [:grid-track 0 3] + [:grid-track 7] [:nest 18]]})))) + +(t/deftest a-grid-copy-keeps-an-aligned-cell-its-main-takes-a-column-from + (t/is (map? (cg/run {:world [1 1 {:variants false :layout :grid :layout-item-margins false :grid-square false + :rich-variants false :layout-children false :grid-empty true + :layout-copies true :text false}] + :ops [[:grid-cell 4 25] [:grid-track 3] [:grid-track 0 3]]})))) + +(t/deftest a-grid-copy-keeps-its-aligned-cell-when-its-main-adds-a-row + (t/is (map? (cg/run {:world [1 0 {:variants false :layout :grid :layout-item-margins false :grid-square true + :rich-variants false :layout-children true :grid-empty false + :layout-copies false :text false}] + :ops [[:grid-cell 0 15 0 1] [: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}]