diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 3f74b75daa..475795f809 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -2474,6 +2474,22 @@ (ctsl/assign-cells objects) (assoc :shapes (:shapes shape))))) +(defn- grow-for-children + "`shape` with the default tracks its grid adds along its flow until each + child it lays out has a slot, as `ctsl/assign-cells` grows it. Only the + track lists grow: the cells the grid already holds keep their places." + [shape objects] + (let [children (count (remove #(ctsl/position-absolute? objects %) (:shapes shape))) + row-dir? (= :row (ctsl/grid-direction shape))] + (loop [shape shape] + (let [rows (count (:layout-grid-rows shape)) + cols (count (:layout-grid-columns shape))] + (if (or (zero? rows) (zero? cols) (>= (* rows cols) children)) + shape + (recur (if row-dir? + (update shape :layout-grid-columns (fnil conj []) ctsl/default-track-value) + (update shape :layout-grid-rows (fnil conj []) ctsl/default-track-value)))))))) + (defn- assign-inherited-grid-cells [shape before objects shape-main] (if-not (or (< (count (:layout-grid-rows shape)) (count (:layout-grid-rows before))) @@ -2489,6 +2505,9 @@ (and active? (empty? (:layout-grid-columns shape))) (assoc :layout-grid-columns [ctsl/default-track-value]))] (-> target + ;; the tracks the children need come first, so a grid that grows + ;; back the tracks it lost keeps its own cells where they were + (grow-for-children objects) (without-default-cells-beyond-tracks) (ctsl/check-deassigned-cells objects) (ctsl/fit-cells-to-tracks) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 550935de91..60c975270f 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -4547,3 +4547,8 @@ ;; a few thousandths of a pixel from their new mains' places: no override (t/is (map? (cg/run {:world [3 2 {:variants true :layout :grid :rich-variants true :grid-empty true :layout-copies true :text true}] :ops [[:nest 7 2 4 2] [:rotate 6 2 4 1] [:duplicate 1 3 1 0] [:make-variant 2] [:duplicate 8 2 8 6] [:style 2 29 1] [:detach 8 6 8 2]]})))) +(t/deftest generated-a-copy-grid-keeps-an-authored-empty-cell-where-it-was-when-it-grows-back + ;; a copy grid with its own cells loses a row its main removes and grows it + ;; back for its children: its authored empty cell keeps its place (user + ;; decision) + (t/is (map? (cg/run {:world [2 1 {:variants true :layout :grid :layout-item-margins false :grid-square true :rich-variants true :layout-children true :grid-empty false :layout-copies true :text true}] :ops [[:grid-track 9 5 0 0] [:grid-direction 0 0 1 1] [:grid-cell 0 15 0 1] [:grid-track 0 2 0 0]]})))) diff --git a/frontend/test/frontend_tests/composable_tests/generated_test.cljs b/frontend/test/frontend_tests/composable_tests/generated_test.cljs index f19ccbf2cb..f7ddf2cfbc 100644 --- a/frontend/test/frontend_tests/composable_tests/generated_test.cljs +++ b/frontend/test/frontend_tests/composable_tests/generated_test.cljs @@ -1230,3 +1230,9 @@ ;; main holds, which takes it with its sizing fixed as its own override (await (replay {:world [3 2 {:text true :layout :flex}] :ops [[:nest 24 11 9 6] [:delete 1] [:restore 1] [:add-child 15 16 4 29] [:move 23 24 12 20] [:edit 25 25 27 25] [:undo 23 17 15 25] [:style 23 29 25 20] [:transform 29 11 7 27] [:update-main 2 19 2 29]]}))) +(t/deftest ^:async generated-a-copy-grid-keeps-an-authored-empty-cell-where-it-was-when-it-grows-back + ;; a copy grid with its own cells loses a row its main removes and grows it + ;; back for its children: its authored empty cell keeps its place (user + ;; decision) + (await (replay {:world [2 1 {:variants true :layout :grid :layout-item-margins false :grid-square true :rich-variants true :layout-children true :grid-empty false :layout-copies true :text true}] :ops [[:grid-track 9 5 0 0] [:grid-direction 0 0 1 1] [:grid-cell 0 15 0 1] [:grid-track 0 2 0 0]]}))) +