diff --git a/common/src/app/common/logic/swap.cljc b/common/src/app/common/logic/swap.cljc index e1ca9731ad..32d87649b0 100644 --- a/common/src/app/common/logic/swap.cljc +++ b/common/src/app/common/logic/swap.cljc @@ -154,6 +154,22 @@ (mth/close? (:width selrect-a) (:width selrect-b)) (mth/close? (:height selrect-a) (:height selrect-b))))))))) +(defn- carried-text-overrides + "The text overrides of `touched` a switch carries from the copy text + `previous-shape` into `current-shape`, its copy of the new variant: + those both variants, `origin-ref-shape` and the new one, agree on and + where the copy differs from the new variant. A carried typography takes + every unit it sets along." + [touched previous-shape current-shape origin-ref-shape] + (let [;; the overrides both variants agree on + agreed (set/difference (txt/text-overrides touched) + (txt/override-groups (:content origin-ref-shape) (:content current-shape))) + carried (set/intersection agreed + (txt/override-groups (:content previous-shape) (:content current-shape)))] + (cond-> carried + (contains? carried :text-content-typography) + (into (filter agreed) txt/typography-units)))) + (defn- switch-attr-operations "The `[attr value]` pairs that carry `attr` over a variant switch, from `previous-shape`, the copy shape before the switch placed like @@ -248,14 +264,13 @@ reset-pos-data? nil - ;; Each text override that the old and the new main agree on is - ;; carried over the new main's content + ;; Each text override the switch carries goes over the new + ;; main's content text-change? (txt/merge-overrides (:content previous-shape) (:content current-shape) - (set/difference (txt/text-overrides touched) - (txt/override-groups (:content origin-ref-shape) - (:content current-shape)))) + (carried-text-overrides touched previous-shape current-shape + origin-ref-shape)) path-change? (switch-path-change-value previous-shape @@ -329,10 +344,8 @@ updated-attrs) new-content (some #(when (= (:attr %) :content) (:val %)) roperations) text-groups (when (and (cfh/text-shape? current-shape) (some? new-content)) - (let [;; the text overrides the switch carries: those both variants agree on - carried (set/difference (txt/text-overrides touched) - (txt/override-groups (:content origin-ref-shape) - (:content current-shape)))] + (let [carried (carried-text-overrides touched previous-shape current-shape + origin-ref-shape)] (cond-> (txt/override-groups (:content current-shape) new-content) ;; the copy's own styles drop a typography it does not apply (txt/drops-typography? carried) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 1acd546750..6cfb9de89e 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1871,6 +1871,26 @@ :ops [[:delete 0] [:restore 0] [:style 1 8 1] [:add-variant 0] [:switch 0] [:add-variant 0 0 0 0]]})))) +(t/deftest switching-a-copy-whose-text-equals-the-new-variant-holds-no-override + (t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :library-variants true :stale-libraries true + :layout nil :layout-item-margins false :grid-square false :library-depth 1 + :library true :rich-variants true :layout-children false :library-text true + :grid-empty false :layout-copies true :text true}] + :ops [[:library-text-style 0 8 1] [:text 10 0 1] [:library-add-variant 0 0 0 0] + [:library-make-variant 0] [:library-sync] [:switch 0] [:library-text 0 0 1] + [:library-text 5 0 1] [:library-variant-property 0 0 0 0] [:library-sync] + [:switch 6] [:swap 7 0 0 0] [:library-make-variant 0] [:library-sync] + [:switch 12]]})))) + +(t/deftest switching-a-copy-sized-like-the-new-variant-keeps-its-typography + (t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :library-variants true :stale-libraries true + :layout nil :layout-item-margins false :grid-square false :library-depth 1 + :library true :rich-variants true :layout-children false :library-text true + :grid-empty false :layout-copies true :text true}] + :ops [[:nest 0 0 0 0] [:text-style 20 4 1] [:library-text-style 0 4 15 0] + [:library-add-variant 0] [:library-sync] [:switch 0] [:library-text-style 6 8 1] + [:library-variant-property 0 0 0 0] [:library-sync] [:switch 19]]})))) + (t/deftest restoring-a-variant-an-undo-removed-keeps-its-swap (t/is (map? (cg/run {:world [3 2 {:variants true :text-styles true :layout nil :variant-diff false :layout-item-margins false :grid-square false :rich-variants true diff --git a/common/test/common_tests/logic/text_override_switch_test.cljc b/common/test/common_tests/logic/text_override_switch_test.cljc index 1598a5891b..1fd2b667f7 100644 --- a/common/test/common_tests/logic/text_override_switch_test.cljc +++ b/common/test/common_tests/logic/text_override_switch_test.cljc @@ -18,7 +18,8 @@ [app.common.test-helpers.shapes :as ths] [app.common.test-helpers.texts :as thtx] [app.common.test-helpers.variants :as thv] - [clojure.test :as t])) + [clojure.test :as t] + [clojure.walk :as walk])) (t/use-fixtures :each thi/test-fixture) @@ -170,3 +171,44 @@ copy-t (ths/get-shape-by-id file (first (:shapes (ths/get-shape file :copy-2))))] (t/is (not-any? :typography-ref-id (tree-seq map? :children (:content copy-t)))) (t/is (= #{:content-group :text-content-size} (:touched copy-t))))) + +(t/deftest copy-whose-letters-equal-the-new-variant-holds-no-override + ;; the new variant's paragraph names a typography the copy's does not; + ;; no override group differs, so the copy keeps the new variant's text + (let [file (-> (thf/sample-file :file1) + (ths/add-sample-typography :heading)) + styled (walk/postwalk #(cond-> % + (and (map? %) (= "paragraph" (:type %))) + (assoc :typography-ref-id (thi/id :heading) + :typography-ref-file (:id file))) + (thtx/content small)) + file (-> file + (thv/add-variant-with-text :v01 :c01 :m01 :c02 :m02 :t01 :t02 "" "" + :text1-params {:content (thtx/content small)} + :text2-params {:content styled}) + (thc/instantiate-component :c01 :copy :children-labels [:copy-t]) + (ths/update-shape :copy-t :touched #{:content-group :text-content-text}) + (tho/swap-component-in-shape :copy :c02 {:new-shape-label :copy-2 + :keep-touched? true})) + copy-t (ths/get-shape-by-id file (first (:shapes (ths/get-shape file :copy-2))))] + (t/is (= styled (:content copy-t))) + (t/is (empty? (:touched copy-t))))) + +(t/deftest copy-sized-like-the-new-variant-keeps-its-typography + ;; the copy's size override is the size the new variant's typography + ;; sets, so there is nothing to carry and the typography stays + (let [large [[["Add to cart" {:size 24}]]] + heading (thtx/apply-typography small :heading {:size 24 :weight 700}) + file (-> (thf/sample-file :file1) + (ths/add-sample-typography :heading) + (thv/add-variant-with-text :v01 :c01 :m01 :c02 :m02 :t01 :t02 "" "" + :text1-params {:content (thtx/content large)} + :text2-params {:content (thtx/content heading)}) + (thc/instantiate-component :c01 :copy :children-labels [:copy-t]) + (ths/update-shape :copy-t :touched #{:content-group :text-content-size})) + main-t (:content (ths/get-shape file :t02)) + file (tho/swap-component-in-shape file :copy :c02 {:new-shape-label :copy-2 + :keep-touched? true}) + copy-t (ths/get-shape-by-id file (first (:shapes (ths/get-shape file :copy-2))))] + (t/is (= main-t (:content copy-t))) + (t/is (empty? (:touched copy-t)))))