🐛 Index an empty paragraph's run before adding it to the runs

The letters of a text keep, for a paragraph with no letters, the index of
its run among the runs. It was counted after the run was added to the
transient list, so it pointed one past the run: at the next paragraph's
run, or past the end when the empty paragraph came last. Comparing the
styles of such a copy with its main failed, and importing the Penpot
Design System template stopped in the migration that recomputes old
text overrides.

Benchmark (lower quantile, ms):

| world                              | baseline |     now |    mean | ratio |
|------------------------------------+----------+---------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  21.783 |  24.854 |  0.62 |
| edit-through-a-chain-of-dependents |   68.931 |  47.467 |  53.332 |  0.69 |
| library-update-into-copies         |   83.083 |  70.859 | 102.070 |  0.85 |
| main-edit-among-many-shapes        |    4.837 |   3.322 |   3.654 |  0.69 |
| main-edit-into-10-copies           |    1.79  |   1.200 |   1.572 |  0.67 |
| main-edit-into-100-copies          |   15.415 |  11.457 |  18.418 |  0.74 |
| main-edit-into-1000-copies         |  153.086 | 109.634 | 125.910 |  0.72 |
| variant-switch-into-copies         |   32.052 |  30.110 |  35.890 |  0.94 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-14 08:37:57 +01:00
parent 29a1b18f08
commit 30acd12f98
2 changed files with 14 additions and 3 deletions

View File

@ -690,11 +690,13 @@
no-letters? (not (some #(seq (:text % "")) (:children paragraph)))
node (dissoc paragraph :children)]
(if no-letters?
(let [run (-> paragraph :children first (dissoc :text))]
(let [run (-> paragraph :children first (dissoc :text))
;; its index in `runs`, taken before the run is added to them
ri (count runs)]
(recur (inc i)
(conj! runs {:paragraph i :node run :empty? true})
letters
(conj! paragraphs' (assoc node :empty-run run :empty-ri (count runs)))))
(conj! paragraphs' (assoc node :empty-run run :empty-ri ri))))
(recur (inc i) runs letters (conj! paragraphs' node))))))))
(defn letters->content

View File

@ -63,7 +63,16 @@
(map #(get-in % [:run :font-weight]) (:letters letters))))
(t/testing "a paragraph with no letters keeps its run"
(t/is (= [false true false] (map #(contains? % :empty-run) (:paragraphs letters))))
(t/is (nil? (some :children (:paragraphs letters)))))))
(t/is (nil? (some :children (:paragraphs letters)))))
(t/testing "and its index among the runs"
(t/is (= 2 (get-in letters [:paragraphs 1 :empty-ri])))
(t/is (:empty? (get-in letters [:runs 2]))))))
(t/deftest a-copy-ending-in-an-empty-paragraph-is-compared-with-its-main
;; the empty paragraph's run is the last of the copy's runs
(let [main (thtx/content [[["Label"]]])
copy (thtx/content [[["Copy"]] [[""]]])]
(t/is (contains? (cttx/override-groups main copy) :text-content-text))))
(t/deftest letters-build-the-content-back
(doseq [text [[[["Hello world"]]]