From a27fa8b3178e154b6956b467072ae620149eb96a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Mon, 3 Apr 2023 11:52:26 +0200 Subject: [PATCH] :tada: Detach component now only affects top instance, not subinstances --- common/src/app/common/types/container.cljc | 4 +++ .../data/workspace/libraries_helpers.cljs | 34 +++++++++---------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/common/src/app/common/types/container.cljc b/common/src/app/common/types/container.cljc index 3c230e15c..9b06dd45c 100644 --- a/common/src/app/common/types/container.cljc +++ b/common/src/app/common/types/container.cljc @@ -63,6 +63,10 @@ [container shape-id f] (update-in container [:objects shape-id] f)) +(defn get-direct-children + [container shape] + (map #(get-shape container %) (:shapes shape))) + (defn get-component-shape "Get the parent shape linked to a component for this shape, if any" [objects shape] diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index 88272e990..e4362797c 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -171,26 +171,26 @@ [new-shape changes]))) +(declare generate-detach-recursive) + (defn generate-detach-instance "Generate changes to remove the links between a shape and all its children with a component." [changes container shape-id] (log/debug :msg "Detach instance" :shape-id shape-id :container (:id container)) - (let [shapes (->> (cph/get-children-with-self (:objects container) shape-id) - (map :id)) - - update-fn - (fn [shape] - (assoc shape - :component-id nil - :component-file nil - :component-root? nil - :remote-synced? nil - :shape-ref nil - :touched nil))] - - (pcb/update-shapes changes shapes update-fn))) + (generate-detach-recursive changes container shape-id true)) +(defn- generate-detach-recursive + [changes container shape-id first] + (let [shape (ctn/get-shape container shape-id)] + (if (and (ctk/instance-root? shape) (not first)) + ;; Subinstances are not detached, but converted in top instances + (pcb/update-shapes changes [(:id shape)] #(assoc % :component-root? true)) + ;; Otherwise, detach the shape and all children + (let [children-ids (:shapes shape)] + (reduce #(generate-detach-recursive %1 container %2 false) + (pcb/update-shapes changes [(:id shape)] ctk/detach-shape) + children-ids))))) (defn prepare-restore-component ([library-data component-id it] @@ -579,10 +579,8 @@ component-container (ctf/get-component-container library component) - children-inst (mapv #(ctn/get-shape container %) - (:shapes shape-inst)) - children-main (mapv #(ctn/get-shape component-container %) - (:shapes shape-main)) + children-inst (vec (ctn/get-direct-children container shape-inst)) + children-main (vec (ctn/get-direct-children component-container shape-main)) only-inst (fn [changes child-inst] (if-not (and omit-touched?