diff --git a/common/src/app/common/files/libraries_helpers.cljc b/common/src/app/common/files/libraries_helpers.cljc index 0bb7d0e3a..bfe78afb0 100644 --- a/common/src/app/common/files/libraries_helpers.cljc +++ b/common/src/app/common/files/libraries_helpers.cljc @@ -286,15 +286,15 @@ (reduce skip-near changes children))) (defn prepare-restore-component - ([library-data component-id current-page it] + ([changes library-data component-id current-page] (let [component (ctkl/get-deleted-component library-data component-id) page (or (ctf/get-component-page library-data component) (when (some #(= (:id current-page) %) (:pages library-data)) ;; If the page doesn't belong to the library, it's not valid current-page) (ctpl/get-last-page library-data))] - (prepare-restore-component nil library-data component-id it page (gpt/point 0 0) nil nil nil))) + (prepare-restore-component changes library-data component-id page (gpt/point 0 0) nil nil nil))) - ([changes library-data component-id it page delta old-id parent-id frame-id] + ([changes library-data component-id 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)]) @@ -316,7 +316,7 @@ (not inside-component?) (assoc :component-root true)) - changes (-> (or changes (pcb/empty-changes it)) + changes (-> changes (pcb/with-page page) (pcb/with-objects (:objects page)) (pcb/with-library-data library-data)) @@ -1790,4 +1790,22 @@ [root (:id root-shape) changes])) +(defn generate-restore-component + "Restore a deleted component, with the given id, in the given file library." + [changes library-data component-id library-id current-page objects] + (let [{:keys [changes shape]} (prepare-restore-component changes library-data component-id current-page) + parent-id (:parent-id shape) + objects (cond-> (assoc objects (:id shape) shape) + (not (nil? parent-id)) + (update-in [parent-id :shapes] + #(conj % (:id shape)))) + + ;; Adds a resize-parents operation so the groups are updated. We add all the new objects + new-objects-ids (->> changes :redo-changes (filter #(= (:type %) :add-obj)) (mapv :id)) + changes (-> changes + (pcb/with-objects objects) + (pcb/resize-parents new-objects-ids))] + + (assoc changes :file-id library-id))) + diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index fcee4d366..184d55933 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -488,22 +488,11 @@ (watch [it state _] (let [page-id (:current-page-id state) current-page (dm/get-in state [:workspace-data :pages-index page-id]) - objects (wsh/lookup-page-objects state page-id) library-data (wsh/get-file state library-id) - {:keys [changes shape]} (cflh/prepare-restore-component library-data component-id current-page it) - parent-id (:parent-id shape) - objects (cond-> (assoc objects (:id shape) shape) - (not (nil? parent-id)) - (update-in [parent-id :shapes] - #(conj % (:id shape)))) - - ;; Adds a resize-parents operation so the groups are updated. We add all the new objects - new-objects-ids (->> changes :redo-changes (filter #(= (:type %) :add-obj)) (mapv :id)) - changes (-> changes - (pcb/with-objects objects) - (pcb/resize-parents new-objects-ids))] - - (rx/of (dch/commit-changes (assoc changes :file-id library-id))))))) + objects (wsh/lookup-page-objects state page-id) + changes (-> (pcb/empty-changes it) + (cflh/generate-restore-component library-data component-id library-id current-page objects))] + (rx/of (dch/commit-changes changes)))))) (defn restore-components diff --git a/frontend/src/app/main/data/workspace/selection.cljs b/frontend/src/app/main/data/workspace/selection.cljs index 1fcf1ecae..8461da54e 100644 --- a/frontend/src/app/main/data/workspace/selection.cljs +++ b/frontend/src/app/main/data/workspace/selection.cljs @@ -447,7 +447,7 @@ {}) restore-component - #(let [restore (cflh/prepare-restore-component changes library-data (:component-id component-root) it page delta (:id component-root) parent-id frame-id)] + #(let [restore (cflh/prepare-restore-component changes library-data (:component-id component-root) page delta (:id component-root) parent-id frame-id)] [(:shape restore) (:changes restore)]) [_shape changes]