🐛 Keep every style of a typography a switch carries as an override

Applying a typography to a copy text overrides all six styles it sets,
equal values too, so the typography stays the copy's own. A switch that
carried that text set its overrides from what differs from the new main
only: a family equal to the new main's lost its override while the text
kept the typography, a reference whose styles were split between the
copy and its main, which a later sync could not describe.

When the text a switch carries holds a typography that differs from
the new main's, it overrides all six styles, as applying it does. The
component model predicts the same, and also when the typography came
from a copy level the switched copy refers to.

Benchmark (scripts/bench-components, ms, lower 2.5% quantile):

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  20.947 |  0.59 |
| edit-through-a-chain-of-dependents |   68.931 |  40.747 |  0.59 |
| library-update-into-copies         |   83.083 |  47.063 |  0.57 |
| main-edit-among-many-shapes        |    4.837 |   2.363 |  0.49 |
| main-edit-into-10-copies           |    1.790 |   0.964 |  0.54 |
| main-edit-into-100-copies          |   15.415 |   9.223 |  0.60 |
| main-edit-into-1000-copies         |  153.086 |  95.751 |  0.63 |
| variant-switch-into-copies         |   32.052 |  21.325 |  0.67 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-07 22:37:44 +01:00
parent 564caf7cf2
commit 69835e1187
5 changed files with 58 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -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`

View File

@ -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)]

View File

@ -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)))))