🐛 Keep a copy text's own family's variant of the main's weight

When sync or a switch takes a text's family from one side and its weight
from the other, a run that shows the same weight and style takes the
variant id of the family's side, which names a variant of that family.
A copy that changed a bold text to Roboto kept the main's "bold" after a
sync instead of Roboto's "700", so a second sync was no longer a no-op.
Runs with another weight keep the weight's variant for the frontend to
fix.

The golden master is recorded again: its Roboto copy texts now hold
Roboto's "700", and update-main carries it to the main.

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

| world                              | baseline |   now | ratio |
|------------------------------------+----------+-------+-------|
| deepest-edit-through-three-levels  |   35.215 | 19.955 |  0.57 |
| edit-through-a-chain-of-dependents |   68.931 | 39.875 |  0.58 |
| library-update-into-copies         |   83.083 | 45.419 |  0.55 |
| main-edit-among-many-shapes        |    4.837 |  2.323 |  0.48 |
| main-edit-into-10-copies           |     1.79 |  1.037 |  0.58 |
| main-edit-into-100-copies          |   15.415 |  9.335 |  0.61 |
| main-edit-into-1000-copies         |  153.086 | 97.201 |  0.63 |
| variant-switch-into-copies         |   32.052 | 24.822 |  0.77 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-10-31 22:28:49 +01:00
parent 72fb4f8d41
commit 3e934593d1
3 changed files with 48 additions and 14 deletions

View File

@ -907,15 +907,27 @@
(contains? node :typography-ref-id) (assoc :typography-ref-id nil)
(contains? node :typography-ref-file) (assoc :typography-ref-file nil)))
(defn- write-variant
"`node` with the variant id of the weight `style` when it shows the same
weight and style."
[node style]
(if (and (some? (:font-variant-id style))
(= (style-key :text-content-weight node) (style-key :text-content-weight style)))
(assoc node :font-variant-id (:font-variant-id style))
node))
(defn- write-units
"`dst` (as `content->letters` gives it) with the placed styles of `units`
(unit -> what `place-unit` gives) written on its runs, paragraphs and
root; without typography references when `drop-typo?`. Run styles go on
the runs, and on the paragraphs that hold run styles (as the DOM and v1
editors write them); a line height goes on a run only when it has one,
and on a paragraph when it has one or no run of it has."
[dst placed drop-typo?]
(let [run-units (filterv #(= :run (get-in style-units [% :level])) (keys placed))
and on a paragraph when it has one or no run of it has. `variants`, the
placed weights of the side the family comes from, give their variant id
to the runs that show the same weight and style."
[dst placed drop-typo? variants]
(let [placed (cond-> placed (some? variants) (assoc ::variant variants))
run-units (filterv #(or (= % ::variant) (= :run (get-in style-units [% :level]))) (keys placed))
;; a run that already shows the placed style keeps its own values;
;; but its variant id names a font of its own family, so with a new
;; family it takes the placed one
@ -923,9 +935,15 @@
write-run (fn [node styles]
(cond-> (reduce (fn [node [unit style]]
(let [attrs (get-in style-units [unit :attrs])]
(if (and (= (style-key unit (select-keys node attrs)) (style-key unit style))
(not (and family? (= unit :text-content-weight))))
(cond
(= unit ::variant)
(write-variant node style)
(and (= (style-key unit (select-keys node attrs)) (style-key unit style))
(not (and family? (= unit :text-content-weight))))
node
:else
(write-style node style attrs (= unit :text-content-line-height)))))
node
(map vector run-units styles))
@ -963,9 +981,12 @@
(fn [i paragraph]
(let [styles (mapv #(first-style i %) run-units)
paragraph (reduce (fn [paragraph [unit style]]
(write-style paragraph style (get-in style-units [unit :attrs])
(or (not= unit :text-content-line-height)
(contains? lh-on-runs? i))))
(if (= unit ::variant)
(cond-> paragraph
(contains? paragraph :font-variant-id) (write-variant style))
(write-style paragraph style (get-in style-units [unit :attrs])
(or (not= unit :text-content-line-height)
(contains? lh-on-runs? i)))))
paragraph
(map vector run-units styles))
paragraph (reduce (fn [paragraph [unit values]]
@ -1026,7 +1047,12 @@
units)]
(if (and (empty? placed) (not drop-typo?))
base
(letters->content (write-units dst placed drop-typo?)))))))
;; the family's side names the variants of its family
(let [variants (when (and (contains? placed :text-content-family)
(not-any? #{:text-content-weight} units))
(place-unit src dst @lmap :text-content-weight
(unit-values src :text-content-weight) false))]
(letters->content (write-units dst placed drop-typo? variants))))))))
(defn- unit-differs?
"Whether the styles of `unit` of `b` differ from those of `a` placed on

View File

@ -68,9 +68,9 @@
{:transcript "edit-text {:target :copy-instance, :edit :fills, :value [{:fill-color \"#FF0000\", :fill-opacity 1}]}\n edit-text {:target :main-instance, :edit :font-family, :value \"Inter\"}\n one-of -> recorded-choice\n reset-overrides {:target :copy-root, :reset copy-root}", :states ["68abaaef59a6d2ca" "44314da0d19445fd" "c352f47573457d52" "e78d1424d71e9cea"]}
{:transcript "edit-text {:target :copy-instance, :edit :fills, :value [{:fill-color \"#FF0000\", :fill-opacity 1}]}\n edit-text {:target :main-instance, :edit :font-family, :value \"Inter\"}\n one-of -> recorded-choice\n update-main {:target :copy-root, :updated copy-root}", :states ["68abaaef59a6d2ca" "44314da0d19445fd" "ca2a2b2037e4e291" "8103e4a9c41bc5dc"]}
{:transcript "edit-text {:target :copy-instance, :edit :fills, :value [{:fill-color \"#FF0000\", :fill-opacity 1}]}\n edit-text {:target :main-instance, :edit :font-family, :value \"Inter\"}\n one-of -> skip", :states ["68abaaef59a6d2ca" "44314da0d19445fd" "64f2927e9a353a05"]}
{:transcript "edit-text {:target :copy-instance, :edit :font-family, :value \"Roboto\"}\n edit-text {:target :main-instance, :edit :fills, :value [{:fill-color \"#0000FF\", :fill-opacity 1}]}\n one-of -> recorded-choice\n reset-overrides {:target :copy-root, :reset copy-root}", :states ["6ee24a0ab7d40d4e" "26063961adf48e87" "6fdf08d7be36c783" "4070583c39b2ea8d"]}
{:transcript "edit-text {:target :copy-instance, :edit :font-family, :value \"Roboto\"}\n edit-text {:target :main-instance, :edit :fills, :value [{:fill-color \"#0000FF\", :fill-opacity 1}]}\n one-of -> recorded-choice\n update-main {:target :copy-root, :updated copy-root}", :states ["6ee24a0ab7d40d4e" "26063961adf48e87" "7fe95a2dfd32e79b" "c1e54744d1d3f83b"]}
{:transcript "edit-text {:target :copy-instance, :edit :font-family, :value \"Roboto\"}\n edit-text {:target :main-instance, :edit :fills, :value [{:fill-color \"#0000FF\", :fill-opacity 1}]}\n one-of -> skip", :states ["6ee24a0ab7d40d4e" "26063961adf48e87" "4944dbbc5cba9e76"]}
{:transcript "edit-text {:target :copy-instance, :edit :font-family, :value \"Roboto\"}\n edit-text {:target :main-instance, :edit :fills, :value [{:fill-color \"#0000FF\", :fill-opacity 1}]}\n one-of -> recorded-choice\n reset-overrides {:target :copy-root, :reset copy-root}", :states ["6ee24a0ab7d40d4e" "4ec37c96f0cf258a" "6fdf08d7be36c783" "4070583c39b2ea8d"]}
{:transcript "edit-text {:target :copy-instance, :edit :font-family, :value \"Roboto\"}\n edit-text {:target :main-instance, :edit :fills, :value [{:fill-color \"#0000FF\", :fill-opacity 1}]}\n one-of -> recorded-choice\n update-main {:target :copy-root, :updated copy-root}", :states ["6ee24a0ab7d40d4e" "4ec37c96f0cf258a" "c0afbee200044890" "f74597d51906dbd4"]}
{:transcript "edit-text {:target :copy-instance, :edit :font-family, :value \"Roboto\"}\n edit-text {:target :main-instance, :edit :fills, :value [{:fill-color \"#0000FF\", :fill-opacity 1}]}\n one-of -> skip", :states ["6ee24a0ab7d40d4e" "4ec37c96f0cf258a" "311e90a6fff90b4d"]}
{:transcript "edit-text {:target :copy-instance, :edit :font-family, :value \"Roboto\"}\n edit-text {:target :main-instance, :edit :font-family, :value \"Inter\"}\n one-of -> recorded-choice\n reset-overrides {:target :copy-root, :reset copy-root}", :states ["6ee24a0ab7d40d4e" "91928fc2e449bd94" "c352f47573457d52" "e78d1424d71e9cea"]}
{:transcript "edit-text {:target :copy-instance, :edit :font-family, :value \"Roboto\"}\n edit-text {:target :main-instance, :edit :font-family, :value \"Inter\"}\n one-of -> recorded-choice\n update-main {:target :copy-root, :updated copy-root}", :states ["6ee24a0ab7d40d4e" "91928fc2e449bd94" "1b53cfa51c185e11" "8957f0d3a3d4bcf6"]}
{:transcript "edit-text {:target :copy-instance, :edit :font-family, :value \"Roboto\"}\n edit-text {:target :main-instance, :edit :font-family, :value \"Inter\"}\n one-of -> skip", :states ["6ee24a0ab7d40d4e" "91928fc2e449bd94" "18f354097caba14c"]}]

View File

@ -176,6 +176,13 @@
(t/is (= [[["Hello" {:family "Roboto" :weight 700 :font-variant-id "bold"}] [" world" {:family "Roboto"}]]]
(sync-text [[["Hello world"]]] hello-bold [[["Hello world" {:family "Roboto"}]]]))))
(t/deftest copy-with-its-own-family-keeps-its-familys-variant-of-the-main-weight
;; the copy's Roboto names its bold "700", the main's family "bold"
(t/is (= [[["Hello world" {:family "Roboto" :weight 700 :color :blue}]]]
(sync-text [[["Hello world" {:weight 700}]]]
[[["Hello world" {:family "Roboto" :weight 700}]]]
[[["Hello world" {:weight 700 :color :blue}]]]))))
(t/deftest copy-that-bolds-a-word-the-main-replaces-loses-the-bold
;; "Goodbye" shares no stretch of 3 letters with "Hello", so it takes the
;; style of the first shared letter after it, the regular space
@ -380,9 +387,10 @@
["applies a typography"
(thtx/apply-typography [[["Hello world"]]] :heading heading)
;; the weight is the copy's, so the typography goes
;; the weight is the copy's, so the typography goes; the bold takes
;; the variant the typography's Inter bold names
[[{:line-height 1.4}
["Hello" {:family "Inter" :weight 700 :font-variant-id "bold" :size 24 :spacing 1 :case "uppercase"}]
["Hello" {:family "Inter" :weight 700 :size 24 :spacing 1 :case "uppercase"}]
[" world!!!" {:family "Inter" :size 24 :spacing 1 :case "uppercase"}]]]]])
(t/deftest copy-that-bolds-a-word-and-adds-letters-against-main-edits