🐛 Keep refs under a swapped nested copy when detaching

Detaching a copy moved the shape-ref of every shape under its first-level
nested copies up one level. Below a nested copy swapped in the detached
copy itself, the swap already points it into the swapped-in main, so its
children ended up one level too far and the detach failed to save with
:missing-slot.

Skip those swapped nested heads, and their descendants, when advancing
the nesting level.

Closes #13176

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Pablo Alba 2026-11-19 13:03:13 +01:00 committed by Pablo Alba
parent ad4359efec
commit 51c04cd686
2 changed files with 63 additions and 4 deletions

View File

@ -408,10 +408,18 @@
[changes file container libraries shape-id]
(log/trace :msg " -> advance-nesting-level")
(let [detached-ids (atom #{})
children (cfh/get-children-with-self (:objects container) shape-id) ;; TODO: this function should be refactored to be a recursive tree traversal.
skip-near (fn [changes shape] ;; this way we could shake the tree more easily when detaching shapes
(shape-log :trace (:id shape) container ;; and perhaps even allow to recover nested instances that have been
:msg " * advancing" :shape-id (:id shape)) ;; swapped and so we can access the main instance again.
;; Nested heads swapped in this copy, and their descendants, already
;; point to the swapped-in main, so they must not be advanced
children (let [objects (:objects container)
swapped-head? #(let [shape (get objects %)]
(and (ctk/instance-head? shape)
(some? (ctk/get-swap-slot shape))))]
(->> (cfh/get-children-ids objects shape-id {:ignore-children-fn swapped-head?})
(cons shape-id)
(map (d/getf objects))))
skip-near (fn [changes shape]
(shape-log :trace (:id shape) container
:msg " * advancing" :shape-id (:id shape))
(if (contains? @detached-ids (:id shape))
(do (log/trace :msg " (detached)")
changes)

View File

@ -226,6 +226,57 @@
(t/is (= (:shape-ref copy-nested-rectangle) (thi/id :nested-rectangle)))
(t/is (nil? (ctk/get-swap-slot copy-nested-rectangle)))))
(t/deftest test-dont-advance-under-swapped-nested-copy
(let [;; ==== Setup
;; :copy-big-board [:name Big Board] #--> :big-board
;; :copy-h-board-with-ellipse [:name Board with ellipse] @--> :h-board-with-ellipse
;; :copy-board-with-rectangle [:name Board with rectangle, :swap-slot-label :nested-h-ellipse] @--> :board-with-rectangle
;; :copy-nested-h-rectangle [:name Rectangle] @--> :nested-h-rectangle
;; :copy-nested-rectangle [:name rectangle] ---> :nested-rectangle
file (-> (setup-file)
(thc/instantiate-component :c-big-board
:copy-big-board
:children-labels [:copy-h-board-with-ellipse
:copy-nested-h-ellipse
:copy-nested-ellipse])
(thc/component-swap :copy-nested-h-ellipse
:c-board-with-rectangle
:copy-board-with-rectangle
:children-labels [:copy-nested-h-rectangle
:copy-nested-rectangle]))
page (thf/current-page file)
;; ==== Action
changes (cll/generate-detach-instance (-> (pcb/empty-changes nil)
(pcb/with-page page)
(pcb/with-objects (:objects page)))
page
{(:id file) file}
(thi/id :copy-big-board))
file' (thf/apply-changes file changes)
;; ==== Get
copy-h-board-with-ellipse (ths/get-shape file' :copy-h-board-with-ellipse)
copy-board-with-rectangle (ths/get-shape file' :copy-board-with-rectangle)
copy-nested-h-rectangle (ths/get-shape file' :copy-nested-h-rectangle)
copy-nested-rectangle (ths/get-shape file' :copy-nested-rectangle)]
;; ==== Check
;; The first level copy becomes a root copy and advances one level.
(t/is (ctk/instance-root? copy-h-board-with-ellipse))
(t/is (= (:shape-ref copy-h-board-with-ellipse) (thi/id :board-with-ellipse)))
(t/is (nil? (ctk/get-swap-slot copy-h-board-with-ellipse)))
;; The nested copy swapped in the detached copy already points to the
;; main of board-with-rectangle, so it and its children must not advance.
(t/is (= (:shape-ref copy-board-with-rectangle) (thi/id :board-with-rectangle)))
(t/is (= (ctk/get-swap-slot copy-board-with-rectangle) (thi/id :nested-h-ellipse)))
(t/is (= (:shape-ref copy-nested-h-rectangle) (thi/id :nested-h-rectangle)))
(t/is (= (:shape-ref copy-nested-rectangle) (thi/id :nested-rectangle)))))
(t/deftest test-propagate-slot-when-swapped-main
(let [;; ==== Setup
file (-> (setup-file)