diff --git a/common/src/app/common/logic/swap.cljc b/common/src/app/common/logic/swap.cljc index e8f803a5bf..31857ef8d5 100644 --- a/common/src/app/common/logic/swap.cljc +++ b/common/src/app/common/logic/swap.cljc @@ -320,10 +320,15 @@ (keep #(ctk/resolve-sync-group (:type previous-shape) %)) updated-attrs) new-content (some #(when (= (:attr %) :content) (:val %)) roperations) + text-groups (when (and (cfh/text-shape? current-shape) (some? new-content)) + (txt/override-groups (:content current-shape) new-content)) new-touched (set/union (or (:touched current-shape) #{}) updated-sync-groups - (when (and (cfh/text-shape? current-shape) (some? new-content)) - (txt/override-groups (:content current-shape) new-content))) + text-groups + ;; a carried typography owns every unit it sets, equal ones too + (when (and (contains? text-groups :text-content-typography) + (txt/typography-owner? new-content)) + txt/typography-units)) roperations (into [{:type :set-touched :touched new-touched}] roperations) uoperations (into (list {:type :set-touched :touched (:touched current-shape)}) uoperations)] (cond-> changes diff --git a/common/src/app/common/test_helpers/composable/shadow.cljc b/common/src/app/common/test_helpers/composable/shadow.cljc index 500800e62e..7dbb6a26ef 100644 --- a/common/src/app/common/test_helpers/composable/shadow.cljc +++ b/common/src/app/common/test_helpers/composable/shadow.cljc @@ -663,19 +663,27 @@ (not (contains? text-differences unit))) unit))) text-groups) + typography-units #{:family :weight :size :line-height :spacing :case} + old-reference? (some #(or (:typography-ref-id %) (:typography-ref-file %)) + (tree-seq map? :children (:content old))) ;; a typography the copy applied carries only with all its styles - mixed-typography? (and (some #(or (:typography-ref-id %) (:typography-ref-file %)) - (tree-seq map? :children (:content old))) + mixed-typography? (and old-reference? (some #(not= (contains? carried-text-units :typography) (contains? carried-text-units %)) - [:family :weight :size :line-height :spacing :case]))] + typography-units)) + ;; and then owns all of them, equal ones too + owned-typography? (and old-reference? (not mixed-typography?) + (contains? carried-text-units :typography) + (not (same-value? main :text-content-typography + {:content (:content old)})))] (reduce-kv (fn [result group _] (if-let [unit (get text-groups group)] (cond-> result (and (not (and (= unit :typography) mixed-typography?)) (not (contains? text-differences unit)) - (not (same-value? main group {:content (:content old)}))) + (or (and owned-typography? (contains? typography-units unit)) + (not (same-value? main group {:content (:content old)})))) (-> (assoc-in [(:id shape) group] {:content (:content old)}) (assoc-in [(:id shape) :content-group] {}))) (let [attrs (get attributes group) diff --git a/common/src/app/common/types/text.cljc b/common/src/app/common/types/text.cljc index b96afa80f3..a05c836fa8 100644 --- a/common/src/app/common/types/text.cljc +++ b/common/src/app/common/types/text.cljc @@ -756,11 +756,16 @@ [attr value]))) (select-keys style (or compare attrs))))) -(def ^:private typography-units +(def typography-units "The units a typography sets." #{:text-content-family :text-content-weight :text-content-size :text-content-line-height :text-content-spacing :text-content-case}) +(defn typography-owner? + "Whether `content` applies a typography, which owns every unit it sets." + [content] + (some #(some? (:typography-ref-id %)) (tree-seq map? :children content))) + (defn text-overrides "The text override groups in the touched groups `touched`. Files older than the feature components/text-overrides hold `:text-content-attribute` diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index ae62a505f9..bd0f077303 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1717,6 +1717,15 @@ [:variant-property 0 0 0 0] [:switch 0] [:variant-property 0 0 0 0] [:switch 0] [:add-variant 0] [:switch 0] [:add-variant 0] [:switch 0]]})))) +(t/deftest switching-a-copy-text-that-holds-a-typography-keeps-all-its-styles + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:text-style 13 0 1] [:add-variant 11 0 0 0] [:nest 7 0 0 0] + [:variant-property 0 0 0 0] [:switch 4] [:text-style 20 20 1 0] + [:add-variant 0] [:switch 0] [:variant-property 0 0 0 0] [:switch 14] + [:sync 0 0 0 0]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] 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 9a808ad431..35e84a4475 100644 --- a/common/test/common_tests/logic/text_override_switch_test.cljc +++ b/common/test/common_tests/logic/text_override_switch_test.cljc @@ -106,3 +106,27 @@ new [[["Hello world" {:size 24}]]]] (t/is (= [[["Hello" {:weight 700 :size 24}] [" world" {:size 24}]]] (switch-text old [[["Hello" {:weight 700}] [" world"]]] new))))) + +(t/deftest copy-that-applies-a-typography-keeps-it-and-all-its-styles-across-a-switch + ;; the typography's family is the variants' own, so only applying it makes + ;; the family an override + (let [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 small)} + :text2-params {:content (thtx/content small)}) + (thc/instantiate-component :c01 :copy :children-labels [:copy-t]) + (thtx/set-text :copy-t heading) + (ths/update-shape :copy-t :touched + #{:content-group :text-content-typography :text-content-family + :text-content-weight :text-content-size :text-content-line-height + :text-content-spacing :text-content-case}) + (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 (= (thtx/normalize heading) (thtx/view (:content copy-t)))) + (t/is (= #{:content-group :text-content-typography :text-content-family + :text-content-weight :text-content-size :text-content-line-height + :text-content-spacing :text-content-case} + (:touched copy-t)))))