diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index a8dce1eca8..68eaad19e6 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -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) diff --git a/common/test/common_tests/logic/comp_detach_with_nested_test.cljc b/common/test/common_tests/logic/comp_detach_with_nested_test.cljc index 3e06167e35..ec2ee8235d 100644 --- a/common/test/common_tests/logic/comp_detach_with_nested_test.cljc +++ b/common/test/common_tests/logic/comp_detach_with_nested_test.cljc @@ -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)