diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index e6373c31b..c87e43368 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -205,6 +205,7 @@ [:map {:title "DelComponentChange"} [:type [:= :del-component]] [:id ::sm/uuid] + [:main-instance {:optional true} :any] [:skip-undelete? {:optional true} :boolean]]] [:restore-component @@ -642,8 +643,8 @@ (ctkl/mod-component data params)) (defmethod process-change :del-component - [data {:keys [id skip-undelete?]}] - (ctf/delete-component data id skip-undelete?)) + [data {:keys [id skip-undelete? main-instance]}] + (ctf/delete-component data id skip-undelete? main-instance)) (defmethod process-change :restore-component [data {:keys [id page-id]}] diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index c2c905be9..a11dc13f1 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -752,17 +752,15 @@ :page-id page-id}))) (defn restore-component - ([changes id] - (restore-component changes id nil)) - ([changes id page-id] - (assert-library! changes) - (-> changes - (update :redo-changes conj {:type :restore-component - :id id - :page-id page-id}) - (update :undo-changes conj {:type :del-component - :id id})))) - + [changes id page-id main-instance] + (assert-library! changes) + (-> changes + (update :redo-changes conj {:type :restore-component + :id id + :page-id page-id}) + (update :undo-changes conj {:type :del-component + :id id + :main-instance main-instance}))) (defn ignore-remote [changes] (letfn [(add-ignore-remote diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index dc77af127..fc8281386 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -258,16 +258,18 @@ (defn delete-component "Mark a component as deleted and store the main instance shapes iside it, to be able to be recovered later." - ([file-data component-id] - (delete-component file-data component-id false)) - - ([file-data component-id skip-undelete?] - (let [components-v2 (dm/get-in file-data [:options :components-v2])] - (if (or (not components-v2) skip-undelete?) - (ctkl/delete-component file-data component-id) - (-> file-data - (ctkl/update-component component-id (partial load-component-objects file-data)) - (ctkl/mark-component-deleted component-id)))))) + [file-data component-id skip-undelete? main-instance] + (let [components-v2 (dm/get-in file-data [:options :components-v2])] + (if (or (not components-v2) skip-undelete?) + (ctkl/delete-component file-data component-id) + (let [set-main-instance ;; If there is a saved main-instance, restore it. This happens on the restore-component action + #(if main-instance + (assoc-in % [:objects (:main-instance-id %)] main-instance) + %)] + (-> file-data + (ctkl/update-component component-id (partial load-component-objects file-data)) + (ctkl/update-component component-id set-main-instance) + (ctkl/mark-component-deleted component-id)))))) (defn restore-component "Recover a deleted component and all its shapes and put all this again in place." diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index 2ce22441e..a60301109 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -254,6 +254,7 @@ ([changes library-data component-id it page delta old-id parent-id frame-id] (let [component (ctkl/get-deleted-component library-data component-id) parent (get-in page [:objects parent-id]) + main-inst (get-in component [:objects (:main-instance-id component)]) inside-component? (some? (ctn/get-instance-root (:objects page) parent)) shapes (cfh/get-children-with-self (:objects component) (:main-instance-id component)) @@ -281,7 +282,7 @@ changes (reduce #(pcb/add-object %1 %2 {:ignore-touched true}) changes (rest shapes))] - {:changes (pcb/restore-component changes component-id (:id page)) + {:changes (pcb/restore-component changes component-id (:id page) main-inst) :shape (first shapes)}))) ;; ---- General library synchronization functions ----