From 5f8d9740d6d171e38c775e9c59ea65dd80e7403b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Fri, 19 Jun 2026 14:58:20 +0200 Subject: [PATCH] :wrench: Normalize text nodes comparison, to be used in tokens detach --- common/src/app/common/data.cljc | 16 +++++++++---- common/src/app/common/types/text.cljc | 24 +++++++++++++++++-- .../common_tests/logic/token_apply_test.cljc | 2 +- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/common/src/app/common/data.cljc b/common/src/app/common/data.cljc index c7d709483d..7cbfdcc4f5 100644 --- a/common/src/app/common/data.cljc +++ b/common/src/app/common/data.cljc @@ -9,7 +9,7 @@ data resources." (:refer-clojure :exclude [read-string hash-map merge name update-vals parse-double group-by iteration concat mapcat - parse-uuid max min regexp? array?]) + parse-uuid max min regexp? array? empty?]) #?(:cljs (:require-macros [app.common.data])) @@ -175,11 +175,17 @@ (.isArray (class o)) false))) +(defn empty? + [val] + (if (or (coll? val) (string? val)) + (clojure.core/empty? val) + (nil? val))) + (defn not-empty? - [coll] - (if (coll? coll) - (boolean (seq coll)) - (not (nil? coll)))) + [val] + (if (or (coll? val) (string? val)) + (boolean (seq val)) + (some? val))) (defn editable-collection? [m] diff --git a/common/src/app/common/types/text.cljc b/common/src/app/common/types/text.cljc index f928321f77..c2fc8f1ab3 100644 --- a/common/src/app/common/types/text.cljc +++ b/common/src/app/common/types/text.cljc @@ -201,6 +201,27 @@ [text] (subs text 0 (min 280 (count text)))) +(defn- compare-text-attr + "Compare two attribute values and return true if they are different. + Take into account the following: + - Only process keys that belong to text node attrs (ignore deprecated + attributes or other things that may be attached). + - Consider nil values, empty strings or empty lists all equal. + - Normalize numeric values (legacy) into strings. + - No value is equal than the default value." + [key value1 value2] + (when (d/index-of text-node-attrs key) + (let [default-value (get default-text-attrs key) + normalize-value (fn [value] + (as-> value $ + (if (number? $) (str $) $) + (if (or (d/empty? $) (= $ default-value)) + nil + $))) + value1' (normalize-value value1) + value2' (normalize-value value2)] + (not= value1' value2')))) + (defn- compare-text-content "Given two content text structures, conformed by maps and vectors, compare them, and returns a set with the differences info. @@ -245,8 +266,7 @@ ;; If the key is not :text, and they are different, it is an attribute difference. ;; Take into account that some processes remove empty attributes, so in some ;; cases we will compare [] with nil, and this is not a difference. - (if (and (not= v1 v2) - (or (d/not-empty? v1) (d/not-empty? v2))) + (if (compare-text-attr k v1 v2) (attribute-cb acc k) acc)))) #{} diff --git a/common/test/common_tests/logic/token_apply_test.cljc b/common/test/common_tests/logic/token_apply_test.cljc index 69e07e0510..93829cf0f5 100644 --- a/common/test/common_tests/logic/token_apply_test.cljc +++ b/common/test/common_tests/logic/token_apply_test.cljc @@ -331,7 +331,7 @@ d/txt-merge {:fills (ths/sample-fills-color :fill-color "#fabada") :font-size "1" - :letter-spacing "0" + :letter-spacing "2" :font-family "Arial"})) (:objects page) {})