🐛 Ignore default constraints when syncing library components

Dragging a shape into a board sets its constraints to the default for
that place (left/top, or scale inside a group) without marking them
touched. A component main on the canvas has no constraints, so the
sync saw nil against left/top and wrote nil over the copy. With nested
components that sync writes into the middle library, so any change to
the inner component, even a 1px move, showed an update banner in every
file that uses it.

Treat a missing constraint as the default for the placement of the
shape being written when comparing during sync.

Closes #12719

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Pablo Alba 2026-11-24 16:46:36 +01:00 committed by Pablo Alba
parent 6337810676
commit 7f53bf76b6
2 changed files with 103 additions and 0 deletions

View File

@ -63,6 +63,12 @@
(def ^:private drifting-geometry-attrs
#{:x :y :width :height :selrect :points})
;; Constraints where nil means "the default for the shape's placement",
;; so nil and that default are the same value.
(def ^:private default-constraints-fns
{:constraints-h gsh/default-constraints-h
:constraints-v gsh/default-constraints-v})
(defn- position-data-close?
"Compares text position-data entry by entry: numbers with tolerance,
every other field (fills, fonts, text) exactly."
@ -93,6 +99,10 @@
(= :position-data attr)
(position-data-close? origin-val dest-val)
(contains? default-constraints-fns attr)
(let [default ((get default-constraints-fns attr) dest-shape)]
(= (or origin-val default) (or dest-val default)))
:else
false))))

View File

@ -1184,3 +1184,96 @@
(t/is (= "Group / Subgroup" main-root-name))
(t/is (= "Group / Subgroup" component-name))
(t/is (nil? (cfv/validate-file file' {}))))))
(defn- relocate-into
"Drags the shape `label` into the board `parent-label`, as the UI does."
[file label parent-label]
(let [page (thf/current-page file)]
(thf/apply-changes file
(cls/generate-relocate (-> (pcb/empty-changes nil)
(pcb/with-page-id (:id page))
(pcb/with-objects (:objects page)))
(thi/id parent-label)
0
#{(thi/id label)}))))
(defn- constraints-attrs
[changes]
(into #{} (filter #{:constraints-h :constraints-v}) (changes-attrs changes)))
(defn- file-with-relocated-copy
"A component with a copy dragged into a board; the relocation writes
explicit default constraints on the copy while the main keeps nil."
[]
(-> (thf/sample-file :file1)
(tho/add-simple-component :component1 :main-root :main-child
:child-params {:fills (ths/sample-fills-color :fill-color "#abcdef")})
(tho/add-frame :board :x 300 :y 300 :width 200 :height 200)
(thc/instantiate-component :component1 :copy-root)
(relocate-into :copy-root :board)))
(t/deftest test-sync-keeps-default-constraints-of-relocated-copy
(let [;; ==== Setup
file (file-with-relocated-copy)
;; ==== Action
updated-file (update-fill file :main-child "#fabada")
sync-changes (sync-file-changes updated-file)]
;; ==== Check
(t/is (= :left (:constraints-h (ths/get-shape file :copy-root))))
(t/is (nil? (:constraints-h (ths/get-shape file :main-root))))
(t/is (contains? (changes-attrs sync-changes) :fills))
(t/is (= #{} (constraints-attrs sync-changes)))))
(t/deftest test-no-sync-changes-for-nested-copy-with-default-constraints
(let [;; ==== Setup
file (-> (file-with-relocated-copy)
(tho/add-frame :main2-root :x 600 :y 0 :width 300 :height 300)
(relocate-into :copy-root :main2-root)
(thc/make-component :component2 :main2-root)
(thc/instantiate-component :component2 :copy2-root))
;; ==== Action
updated-file (move-shapes file [:main-root :main-child] (gpt/point 1 0))
changes1 (sync-file-changes updated-file)
file' (thf/apply-changes updated-file changes1)
changes2 (cll/generate-sync-file-changes (pcb/empty-changes)
nil
:components
(:id file')
(thi/id :component2)
(:id file')
{(:id file') file'}
(:id file'))]
;; ==== Check
(t/is (= #{} (constraints-attrs changes1)))
(t/is (empty? (:redo-changes changes2)))))
(t/deftest test-sync-constraints-changed-inside-main
;; An explicit constraint on the main is a real change and must sync.
(let [;; ==== Setup
file (-> (thf/sample-file :file1)
(tho/add-frame :main-root :x 0 :y 0 :width 100 :height 100)
(tho/add-rect :main-child :parent-label :main-root :x 10 :y 10
:width 10 :height 10)
(thc/make-component :component1 :main-root)
(thc/instantiate-component :component1 :copy-root
:children-labels [:copy-child]))
page (thf/current-page file)
;; ==== Action
updated-file (thf/apply-changes
file
(cls/generate-update-shapes (pcb/empty-changes nil (:id page))
#{(thi/id :main-child)}
#(assoc % :constraints-h :right)
(:objects page)
{}))
sync-changes (sync-file-changes updated-file)
file' (thf/apply-changes updated-file sync-changes)]
;; ==== Check
(t/is (contains? (constraints-attrs sync-changes) :constraints-h))
(t/is (= :right (:constraints-h (ths/get-shape file' :copy-child))))))