diff --git a/common/src/app/common/logic/groups.cljc b/common/src/app/common/logic/groups.cljc index 0a6792b4b6..539aa673e6 100644 --- a/common/src/app/common/logic/groups.cljc +++ b/common/src/app/common/logic/groups.cljc @@ -235,16 +235,21 @@ "`grid` with the cells of the children an ungroup brings into it assigned. A copy with its own cells first takes the tracks of `ref-grid`, the grid it refers to as the ungroup leaves it, on the axes it does not override; - then it gives its free cells to them in the layer order of its main, as - sync does, which is the reverse of the order the main's grid flows them - in." + then it gives its free cells to them in the layer order of their + references there, as sync adds them, and to the others in the reverse of + its own layer order." [grid objects ref-grid] (if (ctk/touched-group? grid :layout-grid-cells) - (-> grid - (cond-> (some? ref-grid) (with-ref-tracks ref-grid)) - (update :shapes (comp vec reverse)) - (ctl/assign-cells objects) - (assoc :shapes (:shapes grid))) + (let [;; where the reference of each child sits among the children of `ref-grid` + rank (into {} (map-indexed (fn [i id] [id i])) (:shapes ref-grid))] + (-> grid + (cond-> (some? ref-grid) (with-ref-tracks ref-grid)) + (assoc :shapes (->> (:shapes grid) + reverse + (sort-by #(get rank (:shape-ref (get objects %)) ##Inf)) + vec)) + (ctl/assign-cells objects) + (assoc :shapes (:shapes grid)))) (ctl/assign-cells grid objects))) (defn- ungroup-shape-changes @@ -298,7 +303,10 @@ (get-in pages-index [target-page-id :objects])) changes (ungroup-shape-changes it target-page-id target-objects id options ref-grids) - grid (ungrouped-grid changes target-objects id)] + grid (ungrouped-grid changes target-objects id) + ;; the layout update after the ungroup orders the + ;; children of a main's grid by their cells + grid (cond-> grid (and grid (nil? options)) (ctl/reorder-grid-children))] [(cond-> changes-list changes (conj changes)) (cond-> ref-grids grid (assoc (:id grid) grid))])) [[] {}]) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 324d9c3fca..ca1569a95f 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2495,6 +2495,18 @@ :ops [[:variant-property 0 9 3 0] [:switch 4] [:combine 2] [:switch 8] [:style 11 7 1] [:update-main 2 0 0 0]]})))) +(t/deftest an-ungroup-fills-a-grid-copy-of-a-copy-in-its-references-layer-order + ;; a bool ungrouped in a grid main whose copy of a copy holds its own + ;; cells: the copy's new shapes take its free cells in the layer order of + ;; their references, which keep the order the ungroup gives them + (t/is (map? (cg/run {:world [3 2 {:variants true :rich-variants true :text true :layout :grid :layout-copies true + :layout-children true}] + :ops [[:add-child 0] [:structure 2 0 0] [:add-child 0] [:structure 0 0 0] [:delete 0] + [:restore 0] [:add-child 0] [:structure 3 4 15] [:grid-direction 15 0 0 1] + [:grid-cell 15 0 0 1] [:add-child 0] [:structure 12 0 0] [:add-child 0] + [:structure 13 0 0] [:detach 9 0 0 0] [:add-child 0] [:structure 0 0 0] + [:add-child 0] [:structure 9 2 0]]})))) + (t/deftest a-synced-masked-group-fits-the-mask-its-copy-turned ;; a copy turns the mask of a masked group; a sync gives the group its ;; main's geometry and then fits it to the turned mask again