From 9c1f83dc723d6da0c6e57a9d46f77b51ee015156 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 24 Nov 2026 19:52:02 +0100 Subject: [PATCH] :bug: Apply text range alignment and direction to paragraphs Setting `align` on a text range through the plugin API stored the value on the range's spans, while the renderers read it from the paragraph, so it was saved but never drawn, in horizontal and vertical text alike. The range `direction` setter had the same fault and wrote a `:direction` key that nothing reads at all. Both now go to the whole paragraphs the range touches, as the language already did, and their getters read those paragraphs: spans of a new text carry the default values, which used to hide the paragraph ones. AI-assisted-by: claude-opus-5-5 --- .../src/app/main/data/workspace/texts.cljs | 31 ++++++++++++----- frontend/src/app/plugins/text.cljs | 19 +++++++---- .../data/workspace_texts_test.cljs | 21 ++++++++++++ .../frontend_tests/plugins/text_test.cljs | 33 +++++++++++++++++++ plugins/CHANGELOG.md | 1 + plugins/libs/plugin-types/index.d.ts | 8 +++-- 6 files changed, 96 insertions(+), 17 deletions(-) diff --git a/frontend/src/app/main/data/workspace/texts.cljs b/frontend/src/app/main/data/workspace/texts.cljs index 0884e96952..a74e39193a 100644 --- a/frontend/src/app/main/data/workspace/texts.cljs +++ b/frontend/src/app/main/data/workspace/texts.cljs @@ -628,27 +628,42 @@ [start end] (annotated-span-bounds content start end)] (<= (count (overlapping-text-nodes content start end)) 1)))) -;; Paragraph attrs a text range sets on the whole paragraphs it touches. +;; Paragraph attrs a text range sets on the whole paragraphs it touches: +;; the renderers read them from the paragraph, never from a span. (def ^:private range-paragraph-attrs - [:text-language]) + (d/concat-vec txt/text-align-attrs + txt/text-direction-attrs + [:text-language])) + +(defn- paragraph-in-range? + "True when `node` of a range-decorated content is a paragraph with + characters in [start, end)." + [node start end] + (and (txt/is-paragraph-node? node) + (let [{node-start :start node-end :end} (meta node)] + (and (< node-start end) (< start node-end))))) + +(defn range-paragraphs + "Paragraphs of `content` with characters in [start, end)." + [content start end] + (->> (txt/node-seq txt/is-paragraph-node? (decorate-range-info content)) + (filter #(paragraph-in-range? % start end)))) (defn- update-paragraph-range "Applies `attrs` to the paragraphs of a range-decorated content with characters in [start, end); a nil value removes the attr." [content start end attrs] (txt/transform-nodes - (fn [node] - (and (txt/is-paragraph-node? node) - (let [{node-start :start node-end :end} (meta node)] - (and (< node-start end) (< start node-end))))) + #(paragraph-in-range? % start end) #(d/patch-object % attrs) content)) (defn update-text-range-attrs "Applies `attrs` to the characters [start, end) of a text shape. A range that cuts a ruby or warichu span covers the whole span. Spans left with - the same style as their neighbour are joined back. Paragraph attrs such - as the language go to the whole paragraphs the range touches." + the same style as their neighbour are joined back. Paragraph attrs (the + alignment, the direction and the language) go to the whole paragraphs + the range touches." [shape start end attrs] (let [content (decorate-range-info (:content shape)) [start end] (annotated-span-bounds content start end) diff --git a/frontend/src/app/plugins/text.cljs b/frontend/src/app/plugins/text.cljs index 1794def569..1a2f504786 100644 --- a/frontend/src/app/plugins/text.cljs +++ b/frontend/src/app/plugins/text.cljs @@ -120,6 +120,15 @@ (map #(get % attr default)) (u/mixed-value)))) +(defn- range-paragraph-value + "Value of a paragraph `attr` over the paragraphs a text range proxy + touches, with unset paragraphs at their default." + [range-proxy start end attr] + (let [default (get txt/default-paragraph-attrs attr)] + (->> (-> range-proxy u/proxy->shape :content (dwt/range-paragraphs start end)) + (map #(get % attr default)) + (u/mixed-value)))) + (defn- enum-value? "Validator accepting the values of the CJK layout enum `attr`." [attr] @@ -560,9 +569,7 @@ {:this true :get (fn [self] - (let [range-data - (-> self u/proxy->shape :content (content-range->text+styles start end))] - (->> range-data (map :direction) u/mixed-value))) + (range-paragraph-value self start end :text-direction)) :set (fn [_ value] (cond @@ -576,15 +583,13 @@ (u/not-valid plugin-id :direction "Cannot modify a page that is not currently active") :else - (st/emit! (dwt/update-text-range id start end {:direction value}))))} + (st/emit! (dwt/update-text-range id start end {:text-direction value}))))} :align {:this true :get (fn [self] - (let [range-data - (-> self u/proxy->shape :content (content-range->text+styles start end))] - (->> range-data (map :text-align) u/mixed-value))) + (range-paragraph-value self start end :text-align)) :set (fn [_ value] (cond diff --git a/frontend/test/frontend_tests/data/workspace_texts_test.cljs b/frontend/test/frontend_tests/data/workspace_texts_test.cljs index 8c8955a3f3..dc5118a48b 100644 --- a/frontend/test/frontend_tests/data/workspace_texts_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_texts_test.cljs @@ -1102,6 +1102,27 @@ (t/is (= [{:text "一盅兩件"}] (spans-of shape))) (t/is (dwt/single-span-range? (:content shape) 0 4))))) +(t/deftest update-text-range-sets-alignment-and-direction-on-the-paragraphs-it-touches + (let [content {:type "root" + :children [{:type "paragraph-set" + :children [{:type "paragraph" :text-align "left" :text-direction "ltr" + :children [{:text "ab" :font-size "20"}]} + {:type "paragraph" :text-align "left" :text-direction "ltr" + :children [{:text "cd" :font-size "20"}]}]}]} + paragraphs (fn [shape] + (->> (:content shape) + (txt/node-seq txt/is-paragraph-node?) + (mapv #(select-keys % [:text-align :text-direction])))) + shape (-> {:content content} + (dwt/update-text-range-attrs 3 5 {:text-align "right"}) + (dwt/update-text-range-attrs 3 4 {:text-direction "rtl"}))] + (t/is (= [{:text-align "left" :text-direction "ltr"} + {:text-align "right" :text-direction "rtl"}] + (paragraphs shape))) + (t/is (= [{:text "ab" :font-size "20"} {:text "cd" :font-size "20"}] + (->> (:content shape) (txt/node-seq txt/is-text-node?) (mapv #(into {} %)))) + "spans are neither split nor given the paragraph attrs"))) + (t/deftest update-text-range-sets-the-language-on-the-paragraphs-it-touches (let [content {:type "root" :children [{:type "paragraph-set" diff --git a/frontend/test/frontend_tests/plugins/text_test.cljs b/frontend/test/frontend_tests/plugins/text_test.cljs index 47cdac8467..7c2e3f9e61 100644 --- a/frontend/test/frontend_tests/plugins/text_test.cljs +++ b/frontend/test/frontend_tests/plugins/text_test.cljs @@ -177,6 +177,39 @@ @captured))))) +(t/deftest text-range-direction-sets-the-paragraph-direction + (let [shape-id (random-uuid) + range (plugins.text/text-range-proxy plugin-id (random-uuid) (random-uuid) shape-id 1 4) + captured (atom [])] + (with-redefs [r/check-permission (constantly true) + u/page-active? (constantly true) + dwt/update-text-range + (fn [id start end attrs] + (swap! captured conj [id start end attrs]) + :update-text-range) + st/emit! mock/noop] + (set! (.-direction range) "rtl") + (t/is (= [[shape-id 1 4 {:text-direction "rtl"}]] @captured))))) + +(t/deftest text-range-align-and-direction-read-the-paragraphs + ;; Spans of a new text carry the default alignment and direction; the + ;; paragraph holds the values that are drawn. + (let [span {:text "ab" :text-align "left" :text-direction "ltr"} + content {:type "root" + :children [{:type "paragraph-set" + :children [{:type "paragraph" :children [span]} + {:type "paragraph" :text-align "right" :text-direction "rtl" + :children [(assoc span :text "cd")]}]}]} + range (fn [start end] + (plugins.text/text-range-proxy + plugin-id (random-uuid) (random-uuid) (random-uuid) start end))] + (with-redefs [u/proxy->shape (constantly {:content content})] + (t/is (= "right" (.-align (range 3 5)))) + (t/is (= "rtl" (.-direction (range 3 5)))) + (t/is (= "left" (.-align (range 0 2))) + "a paragraph without an alignment reads as the default") + (t/is (= "mixed" (.-align (range 1 4))))))) + (t/deftest font-apply-to-text-uses-font-id-not-shape-id (let [file-id (random-uuid) page-id (random-uuid) diff --git a/plugins/CHANGELOG.md b/plugins/CHANGELOG.md index c79129df52..6749be23e8 100644 --- a/plugins/CHANGELOG.md +++ b/plugins/CHANGELOG.md @@ -28,6 +28,7 @@ ### 🩹 Fixes +- **plugins-runtime:** Text range `align` and `direction` apply to the whole paragraphs the range touches and read their values from them. They used to set the range's spans, which nothing draws, and `direction` set a property no renderer reads. - **plugins-runtime:** `flatten()` outlines a text whose font is still loading once the font is in, instead of tracing a fallback font. Until then the returned shape is still a text; `penpot.waitForLayoutUpdate()` waits for the conversion. - **plugins-runtime:** Methods that rejected an invalid value with a plain string (`connectLibrary`, file `restore`, `remove`, `pin`, `export`, `findVersions`, `saveVersion`) now reject with an `Error` carrying the same message. - **plugin-runtime:** `LibraryComponent.instance()`, `createShapeFromSvg()` and `createShapeFromSvgWithImages()` now create shapes at the page root instead of inside a board at the page origin. Closes #12086. diff --git a/plugins/libs/plugin-types/index.d.ts b/plugins/libs/plugin-types/index.d.ts index ec65222781..9aeed435c6 100644 --- a/plugins/libs/plugin-types/index.d.ts +++ b/plugins/libs/plugin-types/index.d.ts @@ -5279,7 +5279,9 @@ export interface TextRange { lang: string | 'mixed' | null; /** - * The text direction for the text range. It can be a specific direction or 'mixed' if multiple directions are used. + * The text direction for the text range. It belongs to paragraphs: + * setting it applies to every paragraph the range touches. Returns + * 'mixed' if those paragraphs use different directions. */ direction: 'ltr' | 'rtl' | 'mixed' | null; @@ -5289,7 +5291,9 @@ export interface TextRange { fills: Fill[] | 'mixed'; /** - * The horizontal alignment of the text range. It can be a specific alignment or 'mixed' if multiple alignments are used. + * The alignment of the text range. It belongs to paragraphs: setting it + * applies to every paragraph the range touches. Returns 'mixed' if those + * paragraphs use different alignments. */ align: 'left' | 'center' | 'right' | 'justify' | 'distribute' | 'mixed' | null;