From e673035817808d2b8476211372c02675310cec8c Mon Sep 17 00:00:00 2001 From: Elena Torro Date: Tue, 4 Nov 2025 09:31:28 +0100 Subject: [PATCH] :wrench: Filter out empty paragraph content --- .../ui/workspace/shapes/text/v2_editor.scss | 2 +- .../src/app/util/text/content/to_dom.cljs | 27 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/frontend/src/app/main/ui/workspace/shapes/text/v2_editor.scss b/frontend/src/app/main/ui/workspace/shapes/text/v2_editor.scss index f1ba49ac36..bf89d6093f 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/v2_editor.scss +++ b/frontend/src/app/main/ui/workspace/shapes/text/v2_editor.scss @@ -35,7 +35,7 @@ } [data-itype="inline"] { - display: inline-block; + display: inline; line-break: auto; line-height: inherit; caret-color: var(--text-editor-caret-color); diff --git a/frontend/src/app/util/text/content/to_dom.cljs b/frontend/src/app/util/text/content/to_dom.cljs index 9b38939aa9..7c7b8af6b7 100644 --- a/frontend/src/app/util/text/content/to_dom.cljs +++ b/frontend/src/app/util/text/content/to_dom.cljs @@ -99,8 +99,9 @@ (dissoc styles :line-height))) (defn get-inline-children - [inline] - [(if (= "" (:text inline)) + [inline paragraph] + [(if (and (= "" (:text inline)) + (= 1 (count (:children paragraph)))) (dom/create-element "br") (dom/create-text (:text inline)))]) @@ -108,6 +109,17 @@ [] (.toString (.floor js/Math (* (.random js/Math) (.-MAX_SAFE_INTEGER js/Number))) 36)) +(defn has-content? + [paragraph] + (some #(not= "" (:text % "")) (:children paragraph))) + +(defn should-filter-empty-paragraph? + [paragraphs index] + (and (not (has-content? (nth paragraphs index))) + (< index (count paragraphs)) + (some has-content? (drop (inc index) paragraphs)) + (every? #(not (has-content? %)) (take (inc index) paragraphs)))) + (defn create-inline [inline paragraph] (create-element @@ -115,7 +127,7 @@ {:id (or (:key inline) (create-random-key)) :data {:itype "inline"} :style (get-inline-styles inline paragraph)} - (get-inline-children inline))) + (get-inline-children inline paragraph))) (defn create-paragraph [paragraph] @@ -128,10 +140,15 @@ (defn create-root [root] - (let [root-styles (get-root-styles root)] + (let [root-styles (get-root-styles root) + paragraphs (get-in root [:children 0 :children]) + filtered-paragraphs (->> paragraphs + (map-indexed vector) + (remove (fn [[index _]] (should-filter-empty-paragraph? paragraphs index))) + (mapv second))] (create-element "div" {:id (or (:key root) (create-random-key)) :data {:itype "root"} :style root-styles} - (mapv create-paragraph (get-in root [:children 0 :children]))))) + (mapv create-paragraph filtered-paragraphs))))