From 14cb15e844c85842046b246653e284f886b7460e Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Tue, 17 Nov 2026 10:35:36 +0100 Subject: [PATCH] :bug: Re-measure overridden auto-grow texts on page open On the SVG renderer, a text is measured only when its text properties change or when it has no position-data. A component copy with overridden text content keeps its own position-data. If its main changes while the page is closed, the copy takes the main's size, and nothing measures it when the page opens. It keeps the main's height, so its flex siblings overlap the extra lines. When a page opens, each auto-width or auto-height text with overridden content on a copy starts in a :check phase and gets one hidden HTML measurement. In :check, the result is written (size and position-data) only if the size differs by more than 1px on the axis the text grows on. Otherwise nothing is written, though the text modifier is still cleaned. The phase is then dropped, so later measurements work as before. A copy with no position-data skips the check and is measured and committed as usual. The new dwt/text-size-drifted? holds the 1px rule. It ignores an axis that fills its layout parent, because the layout sets that size and the check would otherwise find drift on every open. The 1px tolerance matches the WebGL page-load check. SVG stores whole pixels, rounded up, so smaller gaps are only rounding. A finer tolerance would rewrite the same texts each time a page opened. Each content-overridden auto-grow copy costs one extra measurement per page open. Tests: frontend-tests.data.workspace-texts-test covers text-size-drifted? for sub-pixel and exact 1px gaps, the growing axis only, and fill axes. frontend-tests.ui.viewport-texts-html-test covers which copies start in :check, the drifted, matching and unmeasurable cases, and the 1px commit when no phase is set. Known limit: the heal commits like any SVG measurement, so it adds to the user's last undo entry. Relates to #12721 Relates to #13065 --- .../src/app/main/data/workspace/texts.cljs | 13 ++ .../shapes/text/viewport_texts_html.cljs | 132 ++++++++++++++---- .../data/workspace_texts_test.cljs | 32 +++++ .../ui/viewport_texts_html_test.cljs | 92 +++++++++++- 4 files changed, 236 insertions(+), 33 deletions(-) diff --git a/frontend/src/app/main/data/workspace/texts.cljs b/frontend/src/app/main/data/workspace/texts.cljs index 8bf9a875ea..1e219992b3 100644 --- a/frontend/src/app/main/data/workspace/texts.cljs +++ b/frontend/src/app/main/data/workspace/texts.cljs @@ -813,6 +813,19 @@ (defn not-changed? [old-dim new-dim] (> (mth/abs (- old-dim new-dim)) 0.1)) +(defn text-size-drifted? + "True when a measured auto-grow size is more than 1px off the stored one on + the axis that grows (width for :auto-width, height for both). Gaps up to + 1px come from rounding differences between renderers and browsers. An + axis that fills its layout parent takes its size from the layout, so it + never drifts." + [{:keys [grow-type width height] :as shape} new-width new-height] + (or (and (= :auto-width grow-type) + (not (ctl/fill-width? shape)) + (> (mth/abs (- width new-width)) 1)) + (and (not (ctl/fill-height? shape)) + (> (mth/abs (- height new-height)) 1)))) + (defn commit-resize-text [] (ptk/reify ::commit-resize-text diff --git a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs index 87b65864a3..b190719e65 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs @@ -74,29 +74,53 @@ (update-shape-with-content shape content editor-content))) +(defn- measured-auto-size + "Measured [width height] of an auto-grow text, or nil when there is nothing + to resize." + [{:keys [grow-type migrate]} node] + (when (contains? #{:auto-height :auto-width} grow-type) + (let [{:keys [width height]} + (-> (dom/query node ".paragraph-set") + (dom/get-bounding-rect)) + + width (mth/ceil width) + height (mth/ceil height)] + (when (and (not (mth/almost-zero? width)) + (not (mth/almost-zero? height)) + (not migrate)) + [width height])))) + +(defn- text-heal + "How to commit a measured auto-grow text in heal `phase`: nil commits + nothing, otherwise a map with the `:next` heal phase, if any. + The `:check` phase runs when the page opens: it commits only when the size + drifted, so rounding differences between renderers or browsers are not + written back on every open. Without a phase the text is committed as + usual." + [phase shape size] + (case phase + :check (when (and (some? size) (apply dwt/text-size-drifted? shape size)) + {}) + {})) + (defn- update-text-shape - [{:keys [grow-type id migrate] :as shape} node] - ;; Check if we need to update the size because it's auto-width or auto-height - ;; Update the position-data of every text fragment + "Measure the text and commit its position-data and auto-grow size as + `text-heal` decides for `phase`. Resolves to the next heal phase, if any." + [{:keys [id] :as shape} node phase] (->> (tsp/calc-position-data id) (p/fmap (fn [position-data] - ;; At least one paragraph needs to be inside the bounding box - (when (gsht/overlaps-position-data? shape position-data) - (st/emit! (dwt/update-position-data id position-data))) + (let [size (measured-auto-size shape node) + heal (text-heal phase shape size)] + (when (some? heal) + ;; At least one paragraph needs to be inside the bounding box + (when (gsht/overlaps-position-data? shape position-data) + (st/emit! (dwt/update-position-data id position-data))) - (when (contains? #{:auto-height :auto-width} grow-type) - (let [{:keys [width height]} - (-> (dom/query node ".paragraph-set") - (dom/get-bounding-rect)) + (when-let [[width height] size] + (st/emit! (dwt/resize-text id width height)))) - width (mth/ceil width) - height (mth/ceil height)] - (when (and (not (mth/almost-zero? width)) - (not (mth/almost-zero? height)) - (not migrate)) - (st/emit! (dwt/resize-text id width height))))) - - (st/emit! (dwt/clean-text-modifier id)))) + (st/emit! (dwt/clean-text-modifier id)) + (:next heal)))) ;; Log measurement errors and resolve to nil. The returned promise ;; stays pending until the measurement settles, so callers keep the ;; text mounted while it is measured. @@ -132,13 +156,19 @@ [props] (let [shape (obj/get props "shape") on-update (obj/get props "on-update") + phase (obj/get props "phase") + ;; `phase` is left out of the deps on purpose: a shape change already + ;; rebuilds the callback with the current value, and a phase change + ;; after a measurement must not measure the text again. handle-update (mf/use-callback (mf/deps shape on-update) (fn [node] (when (some? node) - (on-update shape node))))] + (if (some? phase) + (on-update shape node phase) + (on-update shape node)))))] [:> html/text-shape* {:key (str "shape-" (:id shape)) :ref handle-update @@ -157,6 +187,37 @@ (mth/close? (dm/get-prop shape :width) (dm/get-prop other :width)) (mth/close? (dm/get-prop shape :height) (dm/get-prop other :height))))) +(defn text-phase + "Heal phase `shape` is measured in, from `phases`. A text without + position-data gets none: it is measured and committed as usual, so it + always gets a layout." + [phases shape] + (when (some? (:position-data shape)) + (get phases (:id shape)))) + +(defn text-changed? + "True when `new-shape` must be measured, given its previous version and its + heal `phase`." + [old-shape new-shape phase] + (let [remote? (some? (-> new-shape meta :session-id))] + (or (and (not remote?) ;; changes caused by a remote peer are not re-calculated + (not (text-properties-equal? old-shape new-shape))) + ;; When the position data is nil we force to recalculate + (nil? (:position-data new-shape)) + (= :check phase)))) + +(defn override-copy-heal-phases + "Auto-grow texts whose content is overridden on a copy, each in the `:check` + heal phase: a component sync can give them the main's size, so they are + measured again when the page opens." + [text-shapes] + (into {} + (comp (map val) + (filter (comp #{:auto-width :auto-height} :grow-type)) + (filter #(contains? (:touched %) :content-group)) + (map (fn [{:keys [id]}] [id :check]))) + text-shapes)) + (mf/defc text-changes-renderer {::mf/wrap-props false} [props] @@ -169,19 +230,21 @@ pending-update* (mf/use-state {}) pending-update (deref pending-update*) + ;; Heal phase per text id. Copies whose text is overridden start in + ;; `:check` and are measured on page open: a sync while their page was + ;; closed can give them the main's size. + phases* (mf/use-state #(override-copy-heal-phases text-shapes)) + phases (deref phases*) + text-change? (fn [id] - (let [new-shape (get text-shapes id) - old-shape (get prev-text-shapes id) - remote? (some? (-> new-shape meta :session-id))] - - (or (and (not remote?) ;; changes caused by a remote peer are not re-calculated - (not (text-properties-equal? old-shape new-shape))) - ;; When the position data is nil we force to recalculate - (nil? (:position-data new-shape))))) + (let [new-shape (get text-shapes id)] + (text-changed? (get prev-text-shapes id) + new-shape + (text-phase phases new-shape)))) changed-texts - (mf/with-memo [text-shapes pending-update] + (mf/with-memo [text-shapes pending-update phases] (let [pending-shapes (into #{} (vals pending-update))] (->> (keys text-shapes) (filter (fn [id] @@ -191,7 +254,7 @@ handle-update-shape (mf/use-fn - (fn [shape node] + (fn [shape node phase] ;; Unique to indentify the pending state (let [uid (uuid/next) id (:id shape)] @@ -202,13 +265,20 @@ (wrf/run-pending! :text-measure [id] - #(-> (update-text-shape shape node) - (p/finally (fn [] (swap! pending-update* dissoc uid))))))))] + #(-> (update-text-shape shape node phase) + (p/then (fn [next-phase] + (swap! phases* (fn [phases] + (if next-phase + (assoc phases id next-phase) + (dissoc phases id)))))) + (p/finally (fn [] + (swap! pending-update* dissoc uid))))))))] [:.text-changes-renderer (for [{:keys [id] :as shape} changed-texts] [:& text-container {:key (dm/str "text-container-" id) :shape shape + :phase (text-phase phases shape) :on-update handle-update-shape}])])) (mf/defc text-modifiers-renderer diff --git a/frontend/test/frontend_tests/data/workspace_texts_test.cljs b/frontend/test/frontend_tests/data/workspace_texts_test.cljs index 57838bcd45..bf87c5162c 100644 --- a/frontend/test/frontend_tests/data/workspace_texts_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_texts_test.cljs @@ -769,3 +769,35 @@ (t/deftest same-styled-text-sees-a-text-change (t/is (not (dwt/same-styled-text? (text-content {:text "Hello" :font-weight "400"}) (text-content {:text "Hello!" :font-weight "400"}))))) + +(t/deftest text-size-drifted-ignores-sub-pixel-gaps + (t/is (not (dwt/text-size-drifted? {:grow-type :auto-width :width 100 :height 17} 100.5 17.5))) + (t/is (not (dwt/text-size-drifted? {:grow-type :auto-height :width 100 :height 17} 100 17.5)))) + +(t/deftest text-size-drifted-checks-only-the-growing-axis + (t/is (not (dwt/text-size-drifted? {:grow-type :auto-height :width 100 :height 17} 140 17)) + "auto-height width comes from the box, not the text") + (t/is (dwt/text-size-drifted? {:grow-type :auto-height :width 100 :height 17} 100 34)) + (t/is (dwt/text-size-drifted? {:grow-type :auto-width :width 44 :height 17} 67 17)) + (t/is (dwt/text-size-drifted? {:grow-type :auto-width :width 44 :height 17} 44 34) + "auto-width grows in height too")) + +(t/deftest text-size-drifted-treats-exactly-one-pixel-as-rounding + (t/is (not (dwt/text-size-drifted? {:grow-type :auto-height :width 100 :height 17} 100 18)) + "a 1px height gap is rounding, as in the WebGL check") + (t/is (not (dwt/text-size-drifted? {:grow-type :auto-width :width 44 :height 17} 45 17)) + "a 1px width gap is rounding, as in the WebGL check")) + +(t/deftest text-size-drifted-ignores-an-axis-that-fills-its-parent + (t/is (not (dwt/text-size-drifted? {:grow-type :auto-height :width 100 :height 40 + :layout-item-v-sizing :fill} + 100 17)) + "a height set by the layout is not drift") + (t/is (not (dwt/text-size-drifted? {:grow-type :auto-width :width 200 :height 17 + :layout-item-h-sizing :fill} + 44 17)) + "a width set by the layout is not drift") + (t/is (dwt/text-size-drifted? {:grow-type :auto-width :width 200 :height 17 + :layout-item-h-sizing :fill} + 44 34) + "the axis that does not fill is still checked")) diff --git a/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs b/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs index d6369e5882..0225ceafbd 100644 --- a/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs +++ b/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs @@ -7,10 +7,12 @@ (ns frontend-tests.ui.viewport-texts-html-test "How the SVG renderer measures text shapes and commits the result." (:require + [app.common.geom.point :as gpt] [app.common.uuid :as uuid] [app.main.data.workspace.texts :as dwt] [app.main.store :as st] [app.main.ui.workspace.shapes.text.viewport-texts-html :as vth] + [app.util.dom :as dom] [app.util.text-svg-position :as tsp] [cljs.test :as t :include-macros true] [frontend-tests.helpers.async :as async] @@ -25,8 +27,10 @@ "Measure `shape` as the SVG renderer does and return the measurement promise. Kept out of the `^:async` test bodies: there, a call through the var compiles to an awaited wrapper that would wait for the measurement." - [shape] - (#'vth/update-text-shape shape nil)) + ([shape] + (measure-text shape nil nil)) + ([shape node phase] + (#'vth/update-text-shape shape node phase))) (defn- with-measurement "Run `f` with the text measurement mocked to `measurement`, a promise the @@ -69,3 +73,87 @@ (p/reject measurement (ex-info "measure failed" {})) (t/is (nil? (await result)) "a failed measurement resolves to nil"))))))) + +(def ^:private fragment + "A position-data fragment inside the box of `text`." + {:x 0 :y 17 :width 100 :height 17 :text "label"}) + +(defn- text + [grow-type touched] + {:id (uuid/next) :type :text :grow-type grow-type :touched touched + :x 0 :y 0 :width 200 :height 17 + :points [(gpt/point 0 0) (gpt/point 200 0) (gpt/point 200 17) (gpt/point 0 17)] + :position-data [fragment]}) + +(defn- by-id + [& shapes] + (into {} (map (juxt :id identity)) shapes)) + +(t/deftest override-copy-heal-phases-checks-overridden-auto-grow-copies + (let [fixed (text :fixed #{:content-group}) + untouched (text :auto-height nil) + geometry (text :auto-height #{:geometry-group}) + unknown (text nil #{:content-group}) + override (text :auto-width #{:content-group})] + (t/is (= {(:id override) :check} + (vth/override-copy-heal-phases (by-id fixed untouched geometry unknown override))) + "only auto-grow texts with overridden content are checked"))) + +(t/deftest unchanged-copy-is-measured-in-the-check-phase + (let [copy (text :auto-height #{:content-group})] + (t/is (vth/text-changed? copy copy (vth/text-phase {(:id copy) :check} copy)) + "an unchanged copy waiting for its check is measured") + (t/is (not (vth/text-changed? copy copy (vth/text-phase {} copy))) + "once checked, an unchanged copy is left alone"))) + +(t/deftest text-without-position-data-gets-no-phase + (let [copy (assoc (text :auto-height #{:content-group}) :position-data nil)] + (t/is (nil? (vth/text-phase {(:id copy) :check} copy)) + "a text without position-data is committed as usual"))) + +(defn- measure-with-box + "Measure `shape` in heal `phase` with its layout box measured at `width` x + `height`. Resolves to the events emitted to the store." + [shape phase width height] + (let [emitted (atom [])] + (->> (mock/with-mocks* + {tsp/calc-position-data (mock/stub (constantly (p/resolved [fragment]))) + dom/query (mock/stub (constantly #js {})) + dom/get-bounding-rect (mock/stub (constantly {:width width :height height})) + st/emit! (mock/stub (fn [& events] (swap! emitted into events) nil))} + (await (measure-text shape #js {} phase))) + (p/fmap (fn [_] @emitted))))) + +(defn- emitted? + [type events] + (some #(= type (ptk/type %)) events)) + +(t/deftest ^:async check-phase-commits-a-drifted-copy + (let [events (await (measure-with-box (text :auto-height #{:content-group}) :check 200 34))] + (t/is (emitted? ::dwt/resize-text events) + "a copy measured taller than its stored box is resized") + (t/is (emitted? ::dwt/update-position-data events) + "its position-data is written with the new layout"))) + +(t/deftest ^:async check-phase-leaves-a-matching-copy-alone + (let [events (await (measure-with-box (text :auto-height #{:content-group}) :check 200 17.4))] + (t/is (not (emitted? ::dwt/resize-text events)) + "a copy that matches its stored box is not resized") + (t/is (not (emitted? ::dwt/update-position-data events)) + "nor is its position-data written back") + (t/is (emitted? ::dwt/clean-text-modifier events) + "the measurement still completes"))) + +(t/deftest ^:async check-phase-commits-nothing-when-it-cannot-measure + (let [events (await (measure-with-box (text :auto-height #{:content-group}) :check 0 0))] + (t/is (not (emitted? ::dwt/resize-text events)) + "an empty layout box is not a size") + (t/is (not (emitted? ::dwt/update-position-data events)) + "nothing is written for a text that could not be measured"))) + +(t/deftest ^:async no-phase-commits-as-usual + (let [events (await (measure-with-box (text :auto-height nil) nil 200 17.4))] + (t/is (emitted? ::dwt/resize-text events) + "outside a heal, a size within 1px is still committed") + (t/is (emitted? ::dwt/update-position-data events) + "and so is the position-data")))