diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index fba12644d5..a0b24bef3c 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1920,6 +1920,54 @@ :layout-grid-columns (:layout-grid-columns target)) (ctsl/fit-cells-to-tracks))))) +(defn copy-grid-cells + "The grid copy `shape-copy` with the cells of its main `shape-main`, its + own children in place of the main's, laid out in its own direction: the + cells of a copy without cell overrides." + [shape-copy shape-main objects] + (let [ids-map + (into {} + (comp + (map #(get objects %)) + (keep + (fn [copy-shape] + ;; Paired children refer to this near main, which can + ;; belong to another component when resetting nested copies. + (when-let [ref (:shape-ref copy-shape)] + [ref (:id copy-shape)])))) + (:shapes shape-copy))] + (-> shape-copy + (assoc :layout-grid-cells (-> shape-main (ctsl/remap-grid-cells ids-map) :layout-grid-cells)) + (ctsl/assign-cells objects)))) + +(defn grid-direction-cells + "The update that gives a grid shape of `page` the cells it has once its + direction changes to `dir`: its single-span auto cells reflow in the new + order. In a copy without cell overrides, a reflow that moves items makes + the cells an override, and one that moves none leaves the cells its + main gives it, laid out as sync lays them out in the new direction, so a + copy that changes its direction back has its main's cells again." + [file page libraries dir] + (let [container (ctn/make-container page :page) + objects (:objects page)] + (fn [shape] + (let [from-dir (ctsl/grid-direction shape)] + (if (or (not= :grid (:layout shape)) (= from-dir dir)) + shape + (let [reflowed (ctsl/reflow-grid-auto-items-for-direction shape from-dir dir) + main (when (and (:shape-ref shape) + (not (ctk/touched-group? shape :layout-grid-cells))) + (ctf/find-ref-shape file container libraries shape :include-deleted? true))] + (cond + (not= :grid (:layout main)) + reflowed + + (not= (:layout-grid-cells reflowed) (:layout-grid-cells shape)) + (update reflowed :touched ctk/set-touched-group :layout-grid-cells) + + :else + (copy-grid-cells (assoc shape :layout-grid-dir dir) main objects)))))))) + (defn- update-grid-copy-attrs "Synchronizes the `layout-grid-cells` property from the main shape to the copies" [changes shape-before shape-main omit-touched?] @@ -1928,35 +1976,22 @@ (pcb/update-shapes [(:id shape-before)] (fn [shape-copy objects] - (let [ids-map - (into {} - (comp - (map #(get objects %)) - (keep - (fn [copy-shape] - ;; Paired children refer to this near main, which can - ;; belong to another component when resetting nested copies. - (when-let [ref (:shape-ref copy-shape)] - [ref (:id copy-shape)])))) - (:shapes shape-copy)) - - remove-orphan-cells + (let [remove-orphan-cells (fn [cells {:keys [shapes]}] (let [child? (set shapes)] (-> cells (update-vals (fn [cell] (update cell :shapes #(filterv child? %))))))) - ;; Take cells from main and remap the shapes to assign it to the copy copy-cells (-> shape-copy :layout-grid-cells (remove-orphan-cells shape-copy)) - main-cells (-> shape-main (ctsl/remap-grid-cells ids-map) :layout-grid-cells) keep-cells? (and omit-touched? (contains? (:touched shape-copy) :layout-grid-cells))] - (let [shape-copy (assoc shape-copy :layout-grid-cells (if keep-cells? copy-cells main-cells))] - (if keep-cells? - (-> shape-copy - (add-inherited-grid-cells shape-before) - (assign-inherited-grid-cells shape-before objects)) - (ctsl/assign-cells shape-copy objects))))) + (if keep-cells? + (-> shape-copy + (assoc :layout-grid-cells copy-cells) + (add-inherited-grid-cells shape-before) + (assign-inherited-grid-cells shape-before objects)) + ;; 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}))) (defn- update-grid-main-attrs diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 2e55f5d5a1..014c9e9658 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1240,3 +1240,18 @@ :grid-empty false :layout-copies true :text true}] :ops [[:library-transform 9 3 4 0] [:delete 5] [:restore 5] [:library-transform 9 4 0 0] [:library-transform 4 3 0 0] [:library-sync 0 0 0 0] [:switch 13 0 0 0]]})))) + +(t/deftest generated-direction-change-in-a-copy-lays-out-its-mains-cells + ;; a copy without cell overrides takes its main's cells in the direction + ;; it changes to, pins included; changing the direction back gives the + ;; main's grid again + (t/is (map? (cg/run {:world [3 0 {:text true :text-styles true :layout :grid :layout-children false + :layout-item-margins false :grid-square true :grid-empty false}] + :ops [[:grid-direction 1 0 1 0] [:add-child 5 0 0 0] [:grid-direction 0 0 0 1] + [:grid-cell 0 0 0 1]]}))) + (t/is (map? (cg/run {:world [2 0 {:text true :text-styles true :layout :grid :layout-children true + :layout-item-margins false :grid-square false :grid-empty false}] + :ops [[:delete 0 0 0 0] [:restore 0 0 0 0] [:delete 0 0 0 0] [:grid-direction 0 0 0 1] + [:grid-cell 0 0 0 1] [:restore 0 0 0 0] [:delete 0 0 0 0] [:grid-direction 5 0 3 0] + [:add-child 1 0 0 0] [:update-main 0 0 0 0] [:grid-direction 0 0 0 0] + [:grid-direction 0 0 3 0]]})))) diff --git a/frontend/src/app/main/data/workspace/shape_layout.cljs b/frontend/src/app/main/data/workspace/shape_layout.cljs index b66493e085..d06e31f98b 100644 --- a/frontend/src/app/main/data/workspace/shape_layout.cljs +++ b/frontend/src/app/main/data/workspace/shape_layout.cljs @@ -13,6 +13,7 @@ [app.common.files.shapes-helpers :as cfsh] [app.common.geom.point :as gpt] [app.common.logic.duplicate :as cld] + [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] [app.common.types.color :as clr] [app.common.types.component :as ctc] @@ -292,34 +293,30 @@ (rx/of (with-meta event (meta it))))))))) (defn change-grid-direction - "Set grid direction and reflow single-span auto items in one undo step." + "Set grid direction and lay out the cells for it in one undo step. A copy + without cell overrides takes its main's cells laid out in the new + direction; only the direction becomes its override." [ids dir] (ptk/reify ::change-grid-direction ptk/WatchEvent - (watch [_ _ _] - (let [undo-id (js/Symbol)] - (rx/of (dwu/start-undo-transaction undo-id) - (dwsh/update-shapes - ids - (fn [shape] - (let [from-dir (ctl/grid-direction shape)] - (cond - (not= :grid (:layout shape)) - shape - - (not (contains? #{:row :column} dir)) - shape - - (= from-dir dir) - (assoc shape :layout-grid-dir dir) - - :else - (ctl/reflow-grid-auto-items-for-direction shape from-dir dir)))) - ;; Auto cells are already reflowed, so skip generic reassignment - ;; and child reordering. - {:skip-grid-reassignment? true}) - (ptk/data-event :layout/update {:ids ids}) - (dwu/commit-undo-transaction undo-id)))))) + (watch [_ state _] + (if (contains? #{:row :column} dir) + (let [undo-id (js/Symbol) + cells (cll/grid-direction-cells (dsh/lookup-file state) (dsh/lookup-page state) + (dsh/lookup-libraries state) dir)] + (rx/of (dwu/start-undo-transaction undo-id) + ;; Auto cells are already laid out, so skip generic reassignment + ;; and child reordering. + (dwsh/update-shapes ids cells {:attrs #{:layout-grid-cells :touched} + :ignore-touched true + :skip-grid-reassignment? true}) + (dwsh/update-shapes ids + #(cond-> % (= :grid (:layout %)) (assoc :layout-grid-dir dir)) + {:attrs #{:layout-grid-dir} + :skip-grid-reassignment? true}) + (ptk/data-event :layout/update {:ids ids}) + (dwu/commit-undo-transaction undo-id))) + (rx/empty))))) (defn update-layout ([ids changes] (update-layout ids changes nil))