diff --git a/common/src/app/common/logic/duplicate.cljc b/common/src/app/common/logic/duplicate.cljc index d5ede27e37..61ba9f5b0a 100644 --- a/common/src/app/common/logic/duplicate.cljc +++ b/common/src/app/common/logic/duplicate.cljc @@ -469,34 +469,43 @@ (assoc % :touched touched) (dissoc % :touched))))) objects))) - ;; a copy whose references skip a swapped head shows that head's - ;; component: each shape refers to its counterpart in the main of the - ;; component, the first of its content references there, and keeps - ;; the overrides of the references it skips + ;; a copy out of its copy levels shows its component: each shape refers + ;; to its counterpart in the main of the component, the first of its + ;; content references there, and keeps the overrides of the references + ;; it skips. Counting levels instead would miss when a swapped head, its + ;; own or one around it, makes its references skip another number. content-reheaded (fn [objects shape level-delta] - (let [chain (->> (ctf/get-ref-chain-until-target-ref container libraries shape nil) - rest - (take level-delta))] - (when (some ctk/swapped? chain) - (when-let [[file-data component] (ctf/head-component file libraries shape :include-deleted? true)] - (let [main-ids (into #{} (map :id) (ctf/get-component-shapes file-data component)) - unchanged-ids (cli/swapped-descendant-ids objects (:id shape))] - (reduce - (fn [objects child] - (let [chain (rest (ctf/get-ref-chain-until-target-ref container libraries child nil)) - [skipped [target]] (split-with #(not (contains? main-ids (:id %))) chain)] - (if (some? target) - (let [touched (-> (into (or (:touched child) #{}) (mapcat :touched) skipped) - (disj :swap-group))] - (update objects (:id child) - #(cond-> (assoc % :shape-ref (:id target)) - (seq touched) (assoc :touched touched) - (empty? touched) (dissoc :touched)))) - objects))) - objects - (->> (cfh/get-children-with-self objects (:id shape)) - (remove #(contains? unchanged-ids (:id %)))))))))) + (when-let [[file-data component] (ctf/head-component file libraries shape :include-deleted? true)] + (let [main-ids (into #{} (map :id) (ctf/get-component-shapes file-data component)) + unchanged-ids (cli/swapped-descendant-ids objects (:id shape)) + ;; a swapped head below follows its slot, not its own component + positional (fn [child] + (->> (ctf/find-ref-shape file container libraries child + :include-deleted? true :with-context? true) + (iterate #(when (:shape-ref %) + (ctf/find-ref-shape file (:container (meta %)) libraries % + :include-deleted? true :with-context? true))) + (take-while some?) + (take 16)))] + (reduce + (fn [objects child] + (let [chain (if (and (ctk/swapped? child) (not= (:id child) (:id shape))) + (positional child) + (rest (ctf/get-ref-chain-until-target-ref container libraries child nil))) + [skipped [target]] (split-with #(not (contains? main-ids (:id %))) chain)] + (if (some? target) + (let [touched (-> (into (or (:touched child) #{}) + (cli/inherited-touched child (into #{} (mapcat :touched) skipped))) + (cond-> (= (:id child) (:id shape)) (disj :swap-group)))] + (update objects (:id child) + #(cond-> (assoc % :shape-ref (:id target)) + (seq touched) (assoc :touched touched) + (empty? touched) (dissoc :touched)))) + (advance-shape level-delta objects child)))) + objects + (->> (cfh/get-children-with-self objects (:id shape)) + (remove #(contains? unchanged-ids (:id %)))))))) detach-copy (fn [objects shape] (reduce #(update %1 %2 ctk/detach-shape) objects diff --git a/common/test/common_tests/logic/swap_group_test.cljc b/common/test/common_tests/logic/swap_group_test.cljc index d16020756a..9a910781b9 100644 --- a/common/test/common_tests/logic/swap_group_test.cljc +++ b/common/test/common_tests/logic/swap_group_test.cljc @@ -416,6 +416,49 @@ (t/is (not (ctk/swapped? head))) (t/is (= (thi/id :badge-dot) (:shape-ref dot))))) +(t/deftest paste-out-of-a-swapped-head-reaches-the-nested-copies-of-its-component + ;; {:dot-main} [:name Frame1] # [Component :dot] + ;; :dot-heart [:name Frame1] @--> [Component :heart] :heart-main + ;; {:badge-main} [:name Frame1] # [Component :badge] + ;; :badge-dot [:name Frame1] @--> [Component :dot] :dot-main + ;; {:page-main} [:name Frame1] # [Component :page] + ;; :inner-copy [:name Frame1] @--> [Component :card] :card-main + ;; :inner-star [:name Frame1] @--> [Component :star] :card-star + ;; :outer-copy [:name Frame1] #--> [Component :page] :page-main + ;; :outer-inner [:name Frame1] @--> [Component :card] :inner-copy + ;; :outer-star [:name Frame1] @--> [Component :badge] :inner-star (swapped) + ;; @--> [Component :dot] :badge-dot + ;; @--> [Component :heart] (dot-heart, via badge-dot) + ;; the dot pasted out of its copy leaves three copy levels, but the swap makes + ;; its references reach the main of :dot in two: its heart refers to the + ;; heart of that main, not further up to the main of :heart + (let [file (-> (setup) + (tho/add-frame :dot-main) + (thc/instantiate-component :heart :dot-heart :parent-label :dot-main) + (thc/make-component :dot :dot-main) + (tho/add-frame :badge-main) + (thc/instantiate-component :dot :badge-dot :parent-label :badge-main) + (thc/make-component :badge :badge-main) + (tho/add-frame :page-main) + (thc/instantiate-component :card :inner-copy + :parent-label :page-main + :children-labels [:inner-star]) + (thc/make-component :page :page-main) + (thc/instantiate-component :page :outer-copy + :children-labels [:outer-inner :outer-star]) + (tho/swap-component-in-shape :outer-star :badge) + (thf/validate-file!)) + dot (first-child file :outer-star) + heart (ths/get-shape-by-id file (first (:shapes dot))) + page (thf/current-page file) + objects (cld/advance-copied-objects file page {(:id file) file} + (select-keys (:objects page) + (cfh/get-children-ids-with-self (:objects page) (:id dot))) + [(:id dot)])] + (t/is (= (thi/id :dot-main) (:shape-ref (get objects (:id dot))))) + (t/is (= (thi/id :dot-heart) (:shape-ref (get objects (:id heart))))))) + + (t/deftest duplicate-keeps-the-swap (let [file (-> (setup) (tho/duplicate-shape :copy)