🐛 Carry no text override a switch's new variant already shows

A switch carried every text override both variants agree on, also those
whose value the new variant already shows. A copy text whose letters
equal the new variant's kept a content override, because the new
variant's paragraph names a typography the copy's does not; a copy sized
like the new variant's typography kept the size and so dropped that
typography. The switch now carries only the overrides both variants agree
on and where the copy differs from the new variant, and a carried
typography takes every unit it sets along.

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

|                             :world | :baseline |   :now |  :mean | :ratio |
|------------------------------------+-----------+--------+--------+--------|
|  deepest-edit-through-three-levels |    35.215 | 19.068 | 20.738 |   0.54 |
| edit-through-a-chain-of-dependents |    68.931 | 42.028 | 42.803 |   0.61 |
|         library-update-into-copies |    83.083 | 46.312 | 49.715 |   0.56 |
|        main-edit-among-many-shapes |     4.837 |  2.149 |  2.298 |   0.44 |
|           main-edit-into-10-copies |      1.79 |  1.002 |  1.023 |   0.56 |
|          main-edit-into-100-copies |    15.415 |  9.234 |  9.643 |    0.6 |
|         main-edit-into-1000-copies |   153.086 | 92.417 | 97.463 |    0.6 |
|         variant-switch-into-copies |    32.052 | 21.472 | 21.958 |   0.67 |

Common suite: 2367 tests, 0 failures.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-09 05:57:12 +01:00
parent 04b78e706f
commit 2e03a9f44d
3 changed files with 85 additions and 10 deletions

View File

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

View File

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

View File

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