From 03aa0817f7ca753d8a4f6f33a92f6ab8f574a03d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Tue, 11 Jun 2024 17:14:28 +0200 Subject: [PATCH] :bug: Fix swap slots when detaching a copy with subcopies --- backend/src/app/srepl/fixes.clj | 5 +--- common/src/app/common/files/repair.cljc | 2 +- common/src/app/common/logic/libraries.cljc | 31 +++++++++++++--------- common/src/app/common/types/component.cljc | 16 +++++++++++ 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/backend/src/app/srepl/fixes.clj b/backend/src/app/srepl/fixes.clj index ee40421df..0db429aa3 100644 --- a/backend/src/app/srepl/fixes.clj +++ b/backend/src/app/srepl/fixes.clj @@ -184,10 +184,7 @@ (ctk/instance-head? child)) (let [slot (guess-swap-slot component-child component-container)] (l/dbg :hint "child" :id (:id child) :name (:name child) :slot slot) - (ctn/update-shape container (:id child) - #(update % :touched - cfh/set-touched-group - (ctk/build-swap-slot-group slot)))) + (ctn/update-shape container (:id child) #(ctk/set-swap-slot % slot))) container)] (recur (process-copy-head container child) (rest children) diff --git a/common/src/app/common/files/repair.cljc b/common/src/app/common/files/repair.cljc index cd2a656d2..c40b60227 100644 --- a/common/src/app/common/files/repair.cljc +++ b/common/src/app/common/files/repair.cljc @@ -481,7 +481,7 @@ (let [slot (:swap-slot args)] (when (some? slot) (log/debug :hint (str " -> set swap-slot to " slot)) - (update shape :touched cfh/set-touched-group (ctk/build-swap-slot-group slot)))))] + (ctk/set-swap-slot shape slot))))] (log/dbg :hint "repairing shape :missing-slot" :id (:id shape) :name (:name shape) :page-id page-id) (-> (pcb/empty-changes nil page-id) diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 595ed5726..ec40277e0 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -284,9 +284,17 @@ (let [children (cfh/get-children-with-self (:objects container) shape-id) skip-near (fn [changes shape] (let [ref-shape (ctf/find-ref-shape file container libraries shape {:include-deleted? true})] - (if (some? (:shape-ref ref-shape)) - (pcb/update-shapes changes [(:id shape)] #(assoc % :shape-ref (:shape-ref ref-shape))) - changes)))] + (cond-> changes + (some? (:shape-ref ref-shape)) + (pcb/update-shapes [(:id shape)] #(assoc % :shape-ref (:shape-ref ref-shape))) + + ;; When advancing level, if the referenced shape has a swap slot, it must be + ;; copied to the current shape, because the shape-ref now will not be pointing + ;; to a near main (except for first level subcopies). + (and (some? (ctk/get-swap-slot ref-shape)) + (nil? (ctk/get-swap-slot shape)) + (not= (:id shape) shape-id)) + (pcb/update-shapes [(:id shape)] #(ctk/set-swap-slot % (ctk/get-swap-slot ref-shape))))))] (reduce skip-near changes children))) (defn prepare-restore-component @@ -1194,7 +1202,7 @@ :shapes all-parents})) changes' (reduce del-obj-change changes' new-shapes)] - (if (and (cfh/touched-group? parent-shape :shapes-group) omit-touched?) + (if (and (ctk/touched-group? parent-shape :shapes-group) omit-touched?) changes changes'))) @@ -1349,7 +1357,7 @@ changes' ids)] - (if (and (cfh/touched-group? parent :shapes-group) omit-touched?) + (if (and (ctk/touched-group? parent :shapes-group) omit-touched?) changes changes'))) @@ -1385,7 +1393,7 @@ :ignore-touched true :syncing true})))] - (if (and (cfh/touched-group? parent :shapes-group) omit-touched?) + (if (and (ctk/touched-group? parent :shapes-group) omit-touched?) changes changes'))) @@ -1846,12 +1854,11 @@ ;; if the shape isn't inside a main component, it shouldn't have a swap slot (and (nil? (ctk/get-swap-slot new-shape)) inside-comp?) - (update :touched cfh/set-touched-group (-> (ctf/find-swap-slot shape - page - {:id (:id file) - :data file} - libraries) - (ctk/build-swap-slot-group))))] + (ctk/set-swap-slot (ctf/find-swap-slot shape + page + {:id (:id file) + :data file} + libraries)))] [new-shape (-> changes ;; Restore the properties diff --git a/common/src/app/common/types/component.cljc b/common/src/app/common/types/component.cljc index bcd6ef3b3..b4060abed 100644 --- a/common/src/app/common/types/component.cljc +++ b/common/src/app/common/types/component.cljc @@ -183,6 +183,15 @@ (and (= shape-id (:main-instance-id component)) (= page-id (:main-instance-page component)))) +(defn set-touched-group + [touched group] + (when group + (conj (or touched #{}) group))) + +(defn touched-group? + [shape group] + ((or (:touched shape) #{}) group)) + (defn build-swap-slot-group "Convert a swap-slot into a :touched group" [swap-slot] @@ -204,6 +213,13 @@ (when group (group->swap-slot group)))) +(defn set-swap-slot + "Add a touched group with a form :swap-slot-." + [shape swap-slot] + (cond-> shape + (some? swap-slot) + (update :touched set-touched-group (build-swap-slot-group swap-slot)))) + (defn match-swap-slot? [shape-main shape-inst] (let [slot-main (get-swap-slot shape-main)