🔧 Normalize text nodes comparison, to be used in tokens detach

This commit is contained in:
Andrés Moya 2026-06-19 14:58:20 +02:00
parent 1c8d26faaf
commit 5f8d9740d6
3 changed files with 34 additions and 8 deletions

View File

@ -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]

View File

@ -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))))
#{}

View File

@ -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)
{})