diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index a6ee7eaf99..88ca6360eb 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1515,17 +1515,19 @@ (defn attr-operations "The redo and undo `:set` operations, as `[roperations uoperations]`, that - copy attributes into `dest-shape`. For each of `updatable-attrs`, - `attr-ops` gives the `[attr value]` pairs to set, in order: none to - leave it alone, and possibly some derived attributes before it." - [dest-shape attr-ops] - (reduce (fn [operations attr] - (reduce (fn [[roperations uoperations] [attr value]] - (add-update-attr-operations attr dest-shape roperations uoperations value)) - operations - (attr-ops attr))) - [[] '()] - updatable-attrs)) + copy attributes into `dest-shape`. For each of `attrs`, by default + `updatable-attrs`, `attr-ops` gives the `[attr value]` pairs to set, in + order: none to leave it alone, and possibly some derived attributes + before it." + ([dest-shape attr-ops] (attr-operations dest-shape attr-ops updatable-attrs)) + ([dest-shape attr-ops attrs] + (reduce (fn [operations attr] + (reduce (fn [[roperations uoperations] [attr value]] + (add-update-attr-operations attr dest-shape roperations uoperations value)) + operations + (attr-ops attr))) + [[] '()] + attrs))) (defn- automatic-name "The name the editors give a text after its `content`, or nil when it has diff --git a/common/src/app/common/logic/swap.cljc b/common/src/app/common/logic/swap.cljc index 31857ef8d5..c780360611 100644 --- a/common/src/app/common/logic/swap.cljc +++ b/common/src/app/common/logic/swap.cljc @@ -309,11 +309,16 @@ touched (get previous-shape :touched #{}) text-auto? (and (cfh/text-shape? current-shape) (contains? #{:auto-height :auto-width} (:grow-type current-shape))) + ;; a child carries its layout item overrides too; the head keeps its own placement + attrs (cond-> cll/updatable-attrs + (not= (:id current-shape) (:id current-root)) + (concat (disj ctk/swap-keep-attrs :interactions))) [roperations uoperations] (cll/attr-operations current-shape #(switch-attr-operations current-shape previous-shape origin-ref-shape - touched text-auto? %)) + touched text-auto? %) + attrs) updated-attrs (into #{} (map :attr) roperations) updated-sync-groups (into #{} diff --git a/common/src/app/common/types/shape.cljc b/common/src/app/common/types/shape.cljc index cc8ce31674..1772247ed2 100644 --- a/common/src/app/common/types/shape.cljc +++ b/common/src/app/common/types/shape.cljc @@ -596,7 +596,10 @@ :hidden :masked-group :fills :proportion :proportion-lock :constraints-h :constraints-v :fixed-scroll :r1 :r2 :r3 :r4 :rotation :opacity :grids :exports :strokes :blend-mode :interactions :shadow :blur :background-blur :grow-type :applied-tokens - :plugin-data :flip-x :flip-y}) + :plugin-data :flip-x :flip-y + :layout-item-margin :layout-item-margin-type :layout-item-h-sizing :layout-item-v-sizing + :layout-item-max-h :layout-item-min-h :layout-item-max-w :layout-item-min-w + :layout-item-absolute :layout-item-z-index :layout-item-align-self}) (def ^:private allowed-shape-geom-attrs #{:x :y :width :height}) (def ^:private allowed-shape-base-attrs #{:id :name :type :selrect :points :transform diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index a91ebda9ac..858751b5c4 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1749,6 +1749,13 @@ :grid-empty false :layout-copies true :text true}] :ops [[:swap 0 0 0 0] [:library-add-variant 0 0 0 0] [:library-sync] [:switch 0]]})))) +(t/deftest switching-a-copy-keeps-the-layout-item-overrides-of-its-children + (t/is (map? (cg/run {:world [3 2 {:variants true :text-styles true :layout :grid :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children true :grid-empty true :layout-copies true :text true}] + :ops [[:make-variant 2 0 0 0] [:make-variant 14 0 0 0] [:make-variant 14 0 0 0] + [:style 13 27 1] [:make-variant 0] [:switch 18]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index 15da52f3ee..e8f8181113 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -2079,6 +2079,46 @@ ;; The later sync must not overwrite the protected overrides (check-layout-item-attrs-kept-partial copy01'' "after main sync"))) +(def ^:private child-layout-item-overrides + ;; Overrides on a copy child that leave its geometry alone. + {:layout-item-margin {:m1 10 :m2 10 :m3 10 :m4 10} + :layout-item-margin-type :multiple + :layout-item-max-h 500 + :layout-item-absolute true + :layout-item-z-index 5 + :layout-item-align-self :start}) + +(t/deftest test-switch-keeps-the-layout-item-overrides-of-a-copy-child + ;; The children of both variants agree on their layout item values, so + ;; the copy child's overrides carry with their touched groups. + (let [;; ==== Setup + child-params (select-keys layout-item-main-values (keys child-layout-item-overrides)) + file (-> (thf/sample-file :file1) + (thv/add-variant-with-child :v01 :c01 :m01 :c02 :m02 :r01 :r02 + {:child1-params child-params + :child2-params child-params}) + (tho/change-shape :m01 #(assoc % :layout :flex :layout-flex-dir :row)) + (tho/change-shape :m02 #(assoc % :layout :flex :layout-flex-dir :row)) + (thc/instantiate-component :c01 :copy01 :children-labels [:copy-r01])) + page (thf/current-page file) + changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) + #{(thi/id :copy-r01)} + #(merge % child-layout-item-overrides) + (:objects page) + {}) + file (thf/apply-changes file changes) + + ;; ==== Action + file' (tho/swap-component-in-shape file :copy01 :c02 {:new-shape-label :copy02 :keep-touched? true}) + copy02' (ths/get-shape file' :copy02) + child' (get-in (thf/current-page file') [:objects (first (:shapes copy02'))])] + + (t/is (= (thi/id :r02) (:shape-ref child'))) + (t/is (= child-layout-item-overrides (select-keys child' (keys child-layout-item-overrides)))) + (t/is (= #{:layout-item-margin :layout-item-max-h :layout-item-absolute + :layout-item-z-index :layout-item-align-self} + (:touched child'))))) + ;; ============================================================ ;; LAYOUT ITEM SIZING - VERTICAL (fix, auto, fill, none) ;; ============================================================