0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-12 18:18:24 -05:00

♻️ Components refactor: generator for restore-component

This commit is contained in:
Pablo Alba 2024-04-22 13:17:42 +02:00 committed by Andrés Moya
parent f91d60eeb6
commit 7d44eef4ab
3 changed files with 27 additions and 20 deletions

View file

@ -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)))

View file

@ -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

View file

@ -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]