♻️ Remove redundant flag on text overrides (#6933)

This commit is contained in:
Pablo Alba
2025-07-22 12:32:24 +02:00
committed by GitHub
parent f768ffbdad
commit ea5521485a
4 changed files with 72 additions and 74 deletions

View File

@@ -1665,27 +1665,62 @@
:shapes all-parents})])))) :shapes all-parents})]))))
(defn- text-partial-change-value (defn- text-change-value
[touched-content untouched-content touched] [touched-content ;; The :content of the copy text before updating
(cond untouched-content ;; The :content of the main component
(touched :text-content-structure-same-attrs) touched]
(if (touched :text-content-attribute)
;; Both structure and attrs has been touched, keep the (let [main-comps-diff (cttx/get-diff-type touched-content untouched-content)
diff-structure? (contains? main-comps-diff :text-content-structure)
touched-attrs (cttx/get-first-paragraph-text-attrs touched-content)
;; Have touched content an uniform style?
thed-unif-style? (cttx/equal-attrs? touched-content touched-attrs)
untouched-attrs (cttx/get-first-paragraph-text-attrs untouched-content)
;; Have untouched content an uniform style?
untched-unif-style? (cttx/equal-attrs? untouched-content untouched-attrs)]
(cond
;; Both text and attrs has been touched, keep the
;; touched-content ;; touched-content
(and (touched :text-content-text) (touched :text-content-attribute))
touched-content touched-content
;; Keep the touched-content structure and texts, update
;; its attrs to make them like the untouched-content
(cttx/copy-attrs-keys touched-content (cttx/get-first-paragraph-text-attrs untouched-content)))
(touched :text-content-text) (touched :text-content-structure)
;; Keep the texts touched in touched-content, so copy the ;; Special case for adding or removing paragraphs:
;; texts from touched-content into untouched-content ;; If the structure has been touched, but the attrs don't,
(cttx/copy-text-keys touched-content untouched-content) ;; and both have uniform attributes, we keep the touched-content structure and
;; texts, updating its attrs to make them like the untouched-content
(if (and (not (touched :text-content-attribute)) thed-unif-style? untched-unif-style?)
(cttx/copy-attrs-keys touched-content untouched-attrs)
;; In other case, we keep the touched content
touched-content)
(touched :text-content-text)
;; Keep the texts touched in touched-content, so copy the
;; texts from touched-content into untouched-content
(cttx/copy-text-keys touched-content untouched-content)
(touched :text-content-attribute)
;; The untouched content has a different structure, but the touched content had't
;; touched the structure
(if diff-structure?
;; If both have uniform attributes, we keep the untouched-content structure and
;; texts, updating its attrs to make them like the touched-content
(if (and thed-unif-style? untched-unif-style?)
(cttx/copy-attrs-keys untouched-content touched-attrs)
;; In other case, we keep the touched content
touched-content)
;; Keep the attrs touched in touched-content, so copy the
;; texts from untouched-content into touched-content
(cttx/copy-text-keys untouched-content touched-content))
;; Nothing is touched
:else
untouched-content)))
(touched :text-content-attribute)
;; Keep the attrs touched in touched-content, so copy the
;; texts from untouched-content into touched-content
(cttx/copy-text-keys untouched-content touched-content)))
(defn- add-update-attr-operations (defn- add-update-attr-operations
[attr dest-shape roperations uoperations attr-val] [attr dest-shape roperations uoperations attr-val]
@@ -1700,34 +1735,6 @@
[(conj roperations roperation) [(conj roperations roperation)
(conj uoperations uoperation)])) (conj uoperations uoperation)]))
(defn- is-text-partial-change?
"Check if the attr update is a text partial change"
[untouched-shape touched-shape]
(let [touched (get touched-shape :touched #{})
partial-text-keys [:text-content-attribute :text-content-text]
active-keys (filter touched partial-text-keys)
untouched-content (:content untouched-shape)
untouched-attrs (cttx/get-first-paragraph-text-attrs untouched-content)
eq-untouched-attrs? (cttx/equal-attrs? untouched-content untouched-attrs)]
(and
(or
;; One and only one of the keys is pressent
(= 1 (count active-keys))
(and
(not (touched :text-content-attribute))
(touched :text-content-structure-same-attrs)))
(or
;; Both has the same structure
(cttx/equal-structure? untouched-content (:content touched-shape))
;; The origin and destiny have different structures, but each have the same attrs
;; for all the items on its content tree
(and
eq-untouched-attrs?
(touched :text-content-structure-same-attrs))))))
(defn- update-attrs (defn- update-attrs
"The main function that implements the attribute sync algorithm. Copy "The main function that implements the attribute sync algorithm. Copy
attributes that have changed in the origin shape to the dest shape. attributes that have changed in the origin shape to the dest shape.
@@ -1784,13 +1791,13 @@
;; and attrs (bold, font, etc) are in the same attr :content. ;; and attrs (bold, font, etc) are in the same attr :content.
;; If only one of them is touched, we want to adress this case and ;; If only one of them is touched, we want to adress this case and
;; only update the untouched one ;; only update the untouched one
text-partial-change? text-content-change?
(when (and (and
omit-touched? omit-touched?
(cfh/text-shape? origin-shape) (cfh/text-shape? origin-shape)
(= :content attr) (= :content attr)
(touched attr-group)) (touched attr-group))
(is-text-partial-change? origin-shape dest-shape))
skip-operations? skip-operations?
(or (= (get origin-shape attr) (get dest-shape attr)) (or (= (get origin-shape attr) (get dest-shape attr))
@@ -1799,7 +1806,7 @@
;; When it is a text-partial-change, we should generate operations ;; When it is a text-partial-change, we should generate operations
;; even when omit-touched? is true, but updating only the text or ;; even when omit-touched? is true, but updating only the text or
;; the attributes, omiting the other part ;; the attributes, omiting the other part
(not text-partial-change?))) (not text-content-change?)))
attr-val (when-not skip-operations? attr-val (when-not skip-operations?
(cond (cond
@@ -1808,10 +1815,10 @@
reset-pos-data? reset-pos-data?
nil nil
text-partial-change? text-content-change?
(text-partial-change-value (:content dest-shape) (text-change-value (:content dest-shape)
(:content origin-shape) (:content origin-shape)
touched) touched)
:else :else
(get origin-shape attr))) (get origin-shape attr)))
@@ -1824,7 +1831,7 @@
;; On a text-partial-change, we want to force a position-data reset ;; On a text-partial-change, we want to force a position-data reset
;; so it's calculated again ;; so it's calculated again
[roperations uoperations] [roperations uoperations]
(if (and text-partial-change? (not skip-operations?)) (if (and text-content-change? (not skip-operations?))
(add-update-attr-operations :position-data dest-shape roperations uoperations nil) (add-update-attr-operations :position-data dest-shape roperations uoperations nil)
[roperations uoperations]) [roperations uoperations])

View File

@@ -83,21 +83,12 @@
compare them, and returns a set with the type of differences. compare them, and returns a set with the type of differences.
The possibilities are The possibilities are
:text-content-text :text-content-text
:text-content-attribute, :text-content-attribute
:text-content-structure :text-content-structure"
:text-content-structure-same-attrs."
[a b] [a b]
(let [diff-type (compare-text-content a b (compare-text-content a b
{:text-cb (fn [acc] (conj acc :text-content-text)) {:text-cb (fn [acc] (conj acc :text-content-text))
:attribute-cb (fn [acc _] (conj acc :text-content-attribute))})] :attribute-cb (fn [acc _] (conj acc :text-content-attribute))}))
(if-not (contains? diff-type :text-content-structure)
diff-type
(let [;; get attrs of the first paragraph of the first paragraph-set
attrs (get-first-paragraph-text-attrs a)]
(if (and (equal-attrs? a attrs)
(equal-attrs? b attrs))
#{:text-content-structure :text-content-structure-same-attrs}
diff-type)))))
(defn get-diff-attrs (defn get-diff-attrs
"Given two content text structures, conformed by maps and vectors, "Given two content text structures, conformed by maps and vectors,

View File

@@ -103,7 +103,7 @@
file' (thf/apply-changes file changes) file' (thf/apply-changes file changes)
copy-child' (ths/get-shape file' :copy-child)] copy-child' (ths/get-shape file' :copy-child)]
(t/is (= #{:content-group :text-content-structure :text-content-structure-same-attrs} (:touched copy-child'))))) (t/is (= #{:content-group :text-content-structure} (:touched copy-child')))))
(t/deftest test-text-copy-changed-structure-diff-attrs (t/deftest test-text-copy-changed-structure-diff-attrs
(let [;; ==== Setup (let [;; ==== Setup

View File

@@ -39,7 +39,7 @@
(t/is (= #{:text-content-attribute} diff-attr)) (t/is (= #{:text-content-attribute} diff-attr))
(t/is (= #{:text-content-text :text-content-attribute} diff-both)) (t/is (= #{:text-content-text :text-content-attribute} diff-both))
(t/is (= #{:text-content-structure} diff-structure)) (t/is (= #{:text-content-structure} diff-structure))
(t/is (= #{:text-content-structure :text-content-structure-same-attrs} diff-structure-same-attrs)))) (t/is (= #{:text-content-structure} diff-structure-same-attrs))))
(t/deftest test-get-diff-attrs (t/deftest test-get-diff-attrs