diff --git a/common/src/app/common/logic/duplicate.cljc b/common/src/app/common/logic/duplicate.cljc index b9acaf490a..d5ede27e37 100644 --- a/common/src/app/common/logic/duplicate.cljc +++ b/common/src/app/common/logic/duplicate.cljc @@ -469,6 +469,34 @@ (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 + 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 %)))))))))) detach-copy (fn [objects shape] (reduce #(update %1 %2 ctk/detach-shape) objects @@ -487,11 +515,12 @@ (detach-copy objects shape)) :else - (let [unchanged-ids (cli/swapped-descendant-ids objects (:id shape))] - (reduce (partial advance-shape level-delta) - objects - (->> (cfh/get-children-with-self objects (:id shape)) - (remove #(contains? unchanged-ids (:id %)))))))) + (or (content-reheaded objects shape level-delta) + (let [unchanged-ids (cli/swapped-descendant-ids objects (:id shape))] + (reduce (partial advance-shape level-delta) + objects + (->> (cfh/get-children-with-self objects (:id shape)) + (remove #(contains? unchanged-ids (:id %))))))))) objects))] (reduce advance-copy objects diff --git a/common/test/common_tests/logic/swap_group_test.cljc b/common/test/common_tests/logic/swap_group_test.cljc index 960eddffcb..d16020756a 100644 --- a/common/test/common_tests/logic/swap_group_test.cljc +++ b/common/test/common_tests/logic/swap_group_test.cljc @@ -384,6 +384,38 @@ (t/is (ctk/swapped? head)) (t/is (= (thi/id :badge-dot) (:shape-ref (get objects dot-id)))))) +(t/deftest paste-out-of-a-copy-follows-a-swap-in-its-main + ;; {:page-main} [:name Frame1] # [Component :page] + ;; :inner-copy [:name Frame1] @--> [Component :card] :card-main + ;; :inner-star [:name Frame1] @--> [Component :badge] :card-star (swapped) + ;; :outer-copy [:name Frame1] #--> [Component :page] :page-main + ;; :outer-inner [:name Frame1] @--> [Component :card] :inner-copy + ;; :outer-star [:name Frame1] @--> [Component :badge] :inner-star + ;; pasted out of its copy, :outer-star shows the badge the main swapped in: + ;; it becomes a plain copy of :badge, as do its children + (let [file (-> (setup) + (tho/add-simple-component :dot :dot-main :dot-rect) + (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]) + (tho/swap-component-in-shape :inner-star :badge) + (thc/make-component :page :page-main) + (thc/instantiate-component :page :outer-copy + :children-labels [:outer-inner :outer-star]) + (thf/validate-file!)) + star (ths/get-shape file :outer-star) + objects (copy-objects file :outer-star) + head (get objects (thi/id :outer-star)) + dot (get objects (first (:shapes head)))] + (t/is (= (:id (thc/get-component file :badge)) (:component-id star))) + (t/is (= (thi/id :badge-main) (:shape-ref head))) + (t/is (not (ctk/swapped? head))) + (t/is (= (thi/id :badge-dot) (:shape-ref dot))))) + (t/deftest duplicate-keeps-the-swap (let [file (-> (setup) (tho/duplicate-shape :copy)